2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.stmt;
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;
16 import com.google.common.collect.ImmutableSetMultimap;
17 import com.google.common.collect.SetMultimap;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.common.Revision;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
31 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
33 public class Bug8307Test {
35 private static final StatementStreamSource FOO_MODULE = sourceForResource("/bugs/bug8307/foo.yang");
36 private static final StatementStreamSource BAR_MODULE = sourceForResource("/bugs/bug8307/bar.yang");
37 private static final StatementStreamSource BAZ_MODULE = sourceForResource("/bugs/bug8307/baz.yang");
38 private static final StatementStreamSource FOOBAR_MODULE = sourceForResource("/bugs/bug8307/foobar.yang");
39 private static final StatementStreamSource FOO_INVALID_MODULE = sourceForResource("/bugs/bug8307/foo-invalid.yang");
40 private static final StatementStreamSource BAR_INVALID_MODULE = sourceForResource("/bugs/bug8307/bar-invalid.yang");
41 private static final StatementStreamSource BAZ_INVALID_MODULE = sourceForResource("/bugs/bug8307/baz-invalid.yang");
43 private static final URI FOO_NS = URI.create("foo-ns");
44 private static final URI BAR_NS = URI.create("bar-ns");
45 private static final URI BAZ_NS = URI.create("baz-ns");
47 private static final Revision REVISION = Revision.of("2017-05-16");
48 private static QNameModule foo;
49 private static QName myFooContA;
50 private static QName myFooContB;
51 private static QName myFooContC;
52 private static QNameModule bar;
53 private static QName myBarContA;
54 private static QName myBarContB;
55 private static QNameModule baz;
56 private static QName myBazCont;
59 public static void setup() {
60 foo = QNameModule.create(FOO_NS, REVISION);
61 myFooContA = QName.create(foo, "my-foo-cont-a");
62 myFooContB = QName.create(foo, "my-foo-cont-b");
63 myFooContC = QName.create(foo, "my-foo-cont-c");
64 bar = QNameModule.create(BAR_NS, REVISION);
65 myBarContA = QName.create(bar, "my-bar-cont-a");
66 myBarContB = QName.create(bar, "my-bar-cont-b");
67 baz = QNameModule.create(BAZ_NS, REVISION);
68 myBazCont = QName.create(baz, "my-baz-cont");
72 public void testDeviationsSupportedInSomeModules() throws Exception {
73 final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations =
74 ImmutableSetMultimap.<QNameModule, QNameModule>builder()
80 final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
81 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
82 .setModulesWithSupportedDeviations(modulesWithSupportedDeviations)
84 assertNotNull(schemaContext);
86 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContA)));
87 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContB)));
88 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContC)));
89 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myBarContA)));
90 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myBarContB)));
94 public void testDeviationsSupportedInAllModules() throws Exception {
95 final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
96 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
98 assertNotNull(schemaContext);
100 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContA)));
101 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContB)));
102 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContC)));
103 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myBarContA)));
104 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myBarContB)));
108 public void testDeviationsSupportedInNoModule() throws Exception {
109 final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
110 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
111 .setModulesWithSupportedDeviations(ImmutableSetMultimap.of())
113 assertNotNull(schemaContext);
115 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContA)));
116 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContB)));
117 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myFooContC)));
118 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myBarContA)));
119 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, myBarContB)));
123 public void shouldFailOnAttemptToDeviateTheSameModule() {
124 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(FOO_INVALID_MODULE);
127 reactor.buildEffective();
128 fail("Deviation that targets the same module as the one it is defined is forbidden.");
129 } catch (final ReactorException ex) {
130 final Throwable cause = ex.getCause();
131 assertTrue(cause instanceof InferenceException);
132 assertTrue(cause.getMessage().startsWith(
133 "Deviation must not target the same module as the one it is defined in"));
138 public void shouldFailOnAttemptToDeviateTheSameModule2() {
139 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild()
140 .addSources(BAR_INVALID_MODULE, BAZ_INVALID_MODULE);
143 reactor.buildEffective();
144 fail("Deviation that targets the same module as the one it is defined is forbidden.");
145 } catch (final ReactorException ex) {
146 final Throwable cause = ex.getCause();
147 assertTrue(cause instanceof InferenceException);
148 assertTrue(cause.getMessage().startsWith(
149 "Deviation must not target the same module as the one it is defined in"));