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