Clean up TreeNode API
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug8307Test.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.stmt;
9
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;
17
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.parser.rfc7950.reactor.RFC7950Reactors;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
27
28 class Bug8307Test {
29     private static final StatementStreamSource FOO_MODULE = sourceForResource("/bugs/bug8307/foo.yang");
30     private static final StatementStreamSource BAR_MODULE = sourceForResource("/bugs/bug8307/bar.yang");
31     private static final StatementStreamSource BAZ_MODULE = sourceForResource("/bugs/bug8307/baz.yang");
32     private static final StatementStreamSource FOOBAR_MODULE = sourceForResource("/bugs/bug8307/foobar.yang");
33     private static final StatementStreamSource FOO_INVALID_MODULE = sourceForResource("/bugs/bug8307/foo-invalid.yang");
34     private static final StatementStreamSource BAR_INVALID_MODULE = sourceForResource("/bugs/bug8307/bar-invalid.yang");
35     private static final StatementStreamSource BAZ_INVALID_MODULE = sourceForResource("/bugs/bug8307/baz-invalid.yang");
36
37     private static final QNameModule FOO = QNameModule.of("foo-ns", "2017-05-16");
38     private static final QName MY_FOO_CONT_A = QName.create(FOO, "my-foo-cont-a");
39     private static final QName MY_FOO_CONT_B = QName.create(FOO, "my-foo-cont-b");
40     private static final QName MY_FOO_CONT_C = QName.create(FOO, "my-foo-cont-c");
41     private static final QNameModule BAR = QNameModule.of("bar-ns", "2017-05-16");
42     private static final QName MY_BAR_CONT_A = QName.create(BAR, "my-bar-cont-a");
43     private static final QName MY_BAR_CONT_B = QName.create(BAR, "my-bar-cont-b");
44     private static final QNameModule BAZ = QNameModule.of("baz-ns", "2017-05-16");
45     private static final QName MY_BAZ_CONT = QName.create(BAZ, "my-baz-cont");
46
47     @Test
48     void testDeviationsSupportedInSomeModules() throws Exception {
49         final var schemaContext = RFC7950Reactors.defaultReactor().newBuild()
50             .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
51             .setModulesWithSupportedDeviations(ImmutableSetMultimap.<QNameModule, QNameModule>builder()
52                 .put(FOO, BAR)
53                 .put(FOO, BAZ)
54                 .put(BAR, BAZ)
55                 .build())
56             .buildEffective();
57         assertNotNull(schemaContext);
58
59         assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_FOO_CONT_A));
60         assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_FOO_CONT_B));
61         assertNotNull(schemaContext.findDataTreeChild(MY_FOO_CONT_C).orElse(null));
62         assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_BAR_CONT_A));
63         assertNotNull(schemaContext.findDataTreeChild(MY_BAR_CONT_B).orElse(null));
64     }
65
66     @Test
67     void testDeviationsSupportedInAllModules() throws Exception {
68         final var schemaContext = RFC7950Reactors.defaultReactor().newBuild()
69             .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
70             .buildEffective();
71         assertNotNull(schemaContext);
72
73         assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_FOO_CONT_A));
74         assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_FOO_CONT_B));
75         assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_FOO_CONT_C));
76         assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_BAR_CONT_A));
77         assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_BAR_CONT_B));
78     }
79
80     @Test
81     void testDeviationsSupportedInNoModule() throws Exception {
82         final var schemaContext = RFC7950Reactors.defaultReactor().newBuild()
83             .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
84             .setModulesWithSupportedDeviations(ImmutableSetMultimap.of())
85             .buildEffective();
86         assertNotNull(schemaContext);
87
88         assertNotNull(schemaContext.findDataTreeChild(MY_FOO_CONT_A).orElse(null));
89         assertNotNull(schemaContext.findDataTreeChild(MY_FOO_CONT_B).orElse(null));
90         assertNotNull(schemaContext.findDataTreeChild(MY_FOO_CONT_C).orElse(null));
91         assertNotNull(schemaContext.findDataTreeChild(MY_BAR_CONT_A).orElse(null));
92         assertNotNull(schemaContext.findDataTreeChild(MY_BAR_CONT_B).orElse(null));
93     }
94
95     @Test
96     void shouldFailOnAttemptToDeviateTheSameModule() {
97         final var reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(FOO_INVALID_MODULE);
98
99         final var cause = assertInstanceOf(InferenceException.class,
100             assertThrows(ReactorException.class, reactor::buildEffective).getCause());
101         assertThat(cause.getMessage(),
102             startsWith("Deviation must not target the same module as the one it is defined in"));
103     }
104
105     @Test
106     void shouldFailOnAttemptToDeviateTheSameModule2() {
107         final var reactor = RFC7950Reactors.defaultReactor().newBuild()
108             .addSources(BAR_INVALID_MODULE, BAZ_INVALID_MODULE);
109
110         final var cause = assertInstanceOf(InferenceException.class,
111             assertThrows(ReactorException.class, reactor::buildEffective).getCause());
112         assertThat(cause.getMessage(),
113             startsWith("Deviation must not target the same module as the one it is defined in"));
114     }
115 }