Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-impl / 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.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
15
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.collect.ImmutableSet;
18 import java.net.URI;
19 import java.util.Map;
20 import java.util.Set;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.common.Revision;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
29 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
33 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
34
35 public class Bug8307Test {
36
37     private static final StatementStreamSource FOO_MODULE = sourceForResource("/bugs/bug8307/foo.yang");
38     private static final StatementStreamSource BAR_MODULE = sourceForResource("/bugs/bug8307/bar.yang");
39     private static final StatementStreamSource BAZ_MODULE = sourceForResource("/bugs/bug8307/baz.yang");
40     private static final StatementStreamSource FOOBAR_MODULE = sourceForResource("/bugs/bug8307/foobar.yang");
41     private static final StatementStreamSource FOO_INVALID_MODULE = sourceForResource("/bugs/bug8307/foo-invalid.yang");
42     private static final StatementStreamSource BAR_INVALID_MODULE = sourceForResource("/bugs/bug8307/bar-invalid.yang");
43     private static final StatementStreamSource BAZ_INVALID_MODULE = sourceForResource("/bugs/bug8307/baz-invalid.yang");
44
45     private static final URI FOO_NS = URI.create("foo-ns");
46     private static final URI BAR_NS = URI.create("bar-ns");
47     private static final URI BAZ_NS = URI.create("baz-ns");
48
49     private static final Revision REVISION = Revision.of("2017-05-16");
50     private static QNameModule foo;
51     private static QName myFooContA;
52     private static QName myFooContB;
53     private static QName myFooContC;
54     private static QNameModule bar;
55     private static QName myBarContA;
56     private static QName myBarContB;
57     private static QNameModule baz;
58     private static QName myBazCont;
59
60     @BeforeClass
61     public static void setup() {
62         foo = QNameModule.create(FOO_NS, REVISION);
63         myFooContA = QName.create(foo, "my-foo-cont-a");
64         myFooContB = QName.create(foo, "my-foo-cont-b");
65         myFooContC = QName.create(foo, "my-foo-cont-c");
66         bar = QNameModule.create(BAR_NS, REVISION);
67         myBarContA = QName.create(bar, "my-bar-cont-a");
68         myBarContB = QName.create(bar, "my-bar-cont-b");
69         baz = QNameModule.create(BAZ_NS, REVISION);
70         myBazCont = QName.create(baz, "my-baz-cont");
71     }
72
73     @Test
74     public void testDeviationsSupportedInSomeModules() throws Exception {
75         final Map<QNameModule, Set<QNameModule>> modulesWithSupportedDeviations = ImmutableMap.of(
76                 foo, ImmutableSet.of(bar, baz), bar, ImmutableSet.of(baz));
77
78         final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
79                 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
80                 .setModulesWithSupportedDeviations(modulesWithSupportedDeviations)
81                 .buildEffective();
82         assertNotNull(schemaContext);
83
84         assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContA)));
85         assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContB)));
86         assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContC)));
87         assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myBarContA)));
88         assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myBarContB)));
89     }
90
91     @Test
92     public void testDeviationsSupportedInAllModules() throws Exception {
93         final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
94                 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
95                 .buildEffective();
96         assertNotNull(schemaContext);
97
98         assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContA)));
99         assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContB)));
100         assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContC)));
101         assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myBarContA)));
102         assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myBarContB)));
103     }
104
105     @Test
106     public void testDeviationsSupportedInNoModule() throws Exception {
107         final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
108                 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
109                 .setModulesWithSupportedDeviations(ImmutableMap.of())
110                 .buildEffective();
111         assertNotNull(schemaContext);
112
113         assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContA)));
114         assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContB)));
115         assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContC)));
116         assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myBarContA)));
117         assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myBarContB)));
118     }
119
120     @Test
121     public void shouldFailOnAttemptToDeviateTheSameModule() {
122         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(FOO_INVALID_MODULE);
123
124         try {
125             reactor.buildEffective();
126             fail("Deviation that targets the same module as the one it is defined is forbidden.");
127         } catch (final ReactorException ex) {
128             final Throwable cause = ex.getCause();
129             assertTrue(cause instanceof InferenceException);
130             assertTrue(cause.getMessage().startsWith(
131                     "Deviation must not target the same module as the one it is defined in"));
132         }
133     }
134
135     @Test
136     public void shouldFailOnAttemptToDeviateTheSameModule2() {
137         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild()
138                 .addSources(BAR_INVALID_MODULE, BAZ_INVALID_MODULE);
139
140         try {
141             reactor.buildEffective();
142             fail("Deviation that targets the same module as the one it is defined is forbidden.");
143         } catch (final ReactorException ex) {
144             final Throwable cause = ex.getCause();
145             assertTrue(cause instanceof InferenceException);
146             assertTrue(cause.getMessage().startsWith(
147                     "Deviation must not target the same module as the one it is defined in"));
148         }
149     }
150 }