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.ImmutableMap;
17 import com.google.common.collect.ImmutableSet;
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.impl.DefaultReactors;
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;
35 public class Bug8307Test {
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");
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");
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;
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");
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));
78 final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
79 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
80 .setModulesWithSupportedDeviations(modulesWithSupportedDeviations)
82 assertNotNull(schemaContext);
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)));
92 public void testDeviationsSupportedInAllModules() throws Exception {
93 final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
94 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
96 assertNotNull(schemaContext);
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)));
106 public void testDeviationsSupportedInNoModule() throws Exception {
107 final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
108 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
109 .setModulesWithSupportedDeviations(ImmutableMap.of())
111 assertNotNull(schemaContext);
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)));
121 public void shouldFailOnAttemptToDeviateTheSameModule() {
122 final BuildAction reactor = DefaultReactors.defaultReactor().newBuild().addSources(FOO_INVALID_MODULE);
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"));
136 public void shouldFailOnAttemptToDeviateTheSameModule2() {
137 final BuildAction reactor = DefaultReactors.defaultReactor().newBuild()
138 .addSources(BAR_INVALID_MODULE, BAZ_INVALID_MODULE);
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"));