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