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