2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.stmt;
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.jupiter.api.Assertions.assertEquals;
13 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
14 import static org.junit.jupiter.api.Assertions.assertNotNull;
15 import static org.junit.jupiter.api.Assertions.assertThrows;
16 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
18 import com.google.common.collect.ImmutableSetMultimap;
19 import java.util.Optional;
20 import org.junit.jupiter.api.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.common.Revision;
24 import org.opendaylight.yangtools.yang.common.XMLNamespace;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
31 private static final StatementStreamSource FOO_MODULE = sourceForResource("/bugs/bug8307/foo.yang");
32 private static final StatementStreamSource BAR_MODULE = sourceForResource("/bugs/bug8307/bar.yang");
33 private static final StatementStreamSource BAZ_MODULE = sourceForResource("/bugs/bug8307/baz.yang");
34 private static final StatementStreamSource FOOBAR_MODULE = sourceForResource("/bugs/bug8307/foobar.yang");
35 private static final StatementStreamSource FOO_INVALID_MODULE = sourceForResource("/bugs/bug8307/foo-invalid.yang");
36 private static final StatementStreamSource BAR_INVALID_MODULE = sourceForResource("/bugs/bug8307/bar-invalid.yang");
37 private static final StatementStreamSource BAZ_INVALID_MODULE = sourceForResource("/bugs/bug8307/baz-invalid.yang");
39 private static final XMLNamespace FOO_NS = XMLNamespace.of("foo-ns");
40 private static final XMLNamespace BAR_NS = XMLNamespace.of("bar-ns");
41 private static final XMLNamespace BAZ_NS = XMLNamespace.of("baz-ns");
43 private static final Revision REVISION = Revision.of("2017-05-16");
44 private static final QNameModule FOO = QNameModule.create(FOO_NS, REVISION);
45 private static final QName MY_FOO_CONT_A = QName.create(FOO, "my-foo-cont-a");
46 private static final QName MY_FOO_CONT_B = QName.create(FOO, "my-foo-cont-b");
47 private static final QName MY_FOO_CONT_C = QName.create(FOO, "my-foo-cont-c");
48 private static final QNameModule BAR = QNameModule.create(BAR_NS, REVISION);
49 private static final QName MY_BAR_CONT_A = QName.create(BAR, "my-bar-cont-a");
50 private static final QName MY_BAR_CONT_B = QName.create(BAR, "my-bar-cont-b");
51 private static final QNameModule BAZ = QNameModule.create(BAZ_NS, REVISION);
52 private static final QName MY_BAZ_CONT = QName.create(BAZ, "my-baz-cont");
55 void testDeviationsSupportedInSomeModules() throws Exception {
56 final var schemaContext = RFC7950Reactors.defaultReactor().newBuild()
57 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
58 .setModulesWithSupportedDeviations(ImmutableSetMultimap.<QNameModule, QNameModule>builder()
64 assertNotNull(schemaContext);
66 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_FOO_CONT_A));
67 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_FOO_CONT_B));
68 assertNotNull(schemaContext.findDataTreeChild(MY_FOO_CONT_C).orElse(null));
69 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_BAR_CONT_A));
70 assertNotNull(schemaContext.findDataTreeChild(MY_BAR_CONT_B).orElse(null));
74 void testDeviationsSupportedInAllModules() throws Exception {
75 final var schemaContext = RFC7950Reactors.defaultReactor().newBuild()
76 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
78 assertNotNull(schemaContext);
80 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_FOO_CONT_A));
81 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_FOO_CONT_B));
82 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_FOO_CONT_C));
83 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_BAR_CONT_A));
84 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_BAR_CONT_B));
88 void testDeviationsSupportedInNoModule() throws Exception {
89 final var schemaContext = RFC7950Reactors.defaultReactor().newBuild()
90 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
91 .setModulesWithSupportedDeviations(ImmutableSetMultimap.of())
93 assertNotNull(schemaContext);
95 assertNotNull(schemaContext.findDataTreeChild(MY_FOO_CONT_A).orElse(null));
96 assertNotNull(schemaContext.findDataTreeChild(MY_FOO_CONT_B).orElse(null));
97 assertNotNull(schemaContext.findDataTreeChild(MY_FOO_CONT_C).orElse(null));
98 assertNotNull(schemaContext.findDataTreeChild(MY_BAR_CONT_A).orElse(null));
99 assertNotNull(schemaContext.findDataTreeChild(MY_BAR_CONT_B).orElse(null));
103 void shouldFailOnAttemptToDeviateTheSameModule() {
104 final var reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(FOO_INVALID_MODULE);
106 final var cause = assertInstanceOf(InferenceException.class,
107 assertThrows(ReactorException.class, reactor::buildEffective).getCause());
108 assertThat(cause.getMessage(),
109 startsWith("Deviation must not target the same module as the one it is defined in"));
113 void shouldFailOnAttemptToDeviateTheSameModule2() {
114 final var reactor = RFC7950Reactors.defaultReactor().newBuild()
115 .addSources(BAR_INVALID_MODULE, BAZ_INVALID_MODULE);
117 final var cause = assertInstanceOf(InferenceException.class,
118 assertThrows(ReactorException.class, reactor::buildEffective).getCause());
119 assertThat(cause.getMessage(),
120 startsWith("Deviation must not target the same module as the one it is defined in"));