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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
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;
18 import java.util.Optional;
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.common.XMLNamespace;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
31 public class Bug8307Test {
33 private static final StatementStreamSource FOO_MODULE = sourceForResource("/bugs/bug8307/foo.yang");
34 private static final StatementStreamSource BAR_MODULE = sourceForResource("/bugs/bug8307/bar.yang");
35 private static final StatementStreamSource BAZ_MODULE = sourceForResource("/bugs/bug8307/baz.yang");
36 private static final StatementStreamSource FOOBAR_MODULE = sourceForResource("/bugs/bug8307/foobar.yang");
37 private static final StatementStreamSource FOO_INVALID_MODULE = sourceForResource("/bugs/bug8307/foo-invalid.yang");
38 private static final StatementStreamSource BAR_INVALID_MODULE = sourceForResource("/bugs/bug8307/bar-invalid.yang");
39 private static final StatementStreamSource BAZ_INVALID_MODULE = sourceForResource("/bugs/bug8307/baz-invalid.yang");
41 private static final XMLNamespace FOO_NS = XMLNamespace.of("foo-ns");
42 private static final XMLNamespace BAR_NS = XMLNamespace.of("bar-ns");
43 private static final XMLNamespace BAZ_NS = XMLNamespace.of("baz-ns");
45 private static final Revision REVISION = Revision.of("2017-05-16");
46 private static final QNameModule FOO = QNameModule.create(FOO_NS, REVISION);
47 private static final QName MY_FOO_CONT_A = QName.create(FOO, "my-foo-cont-a");
48 private static final QName MY_FOO_CONT_B = QName.create(FOO, "my-foo-cont-b");
49 private static final QName MY_FOO_CONT_C = QName.create(FOO, "my-foo-cont-c");
50 private static final QNameModule BAR = QNameModule.create(BAR_NS, REVISION);
51 private static final QName MY_BAR_CONT_A = QName.create(BAR, "my-bar-cont-a");
52 private static final QName MY_BAR_CONT_B = QName.create(BAR, "my-bar-cont-b");
53 private static final QNameModule BAZ = QNameModule.create(BAZ_NS, REVISION);
54 private static final QName MY_BAZ_CONT = QName.create(BAZ, "my-baz-cont");
57 public void testDeviationsSupportedInSomeModules() throws Exception {
58 final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations =
59 ImmutableSetMultimap.<QNameModule, QNameModule>builder()
65 final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
66 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
67 .setModulesWithSupportedDeviations(modulesWithSupportedDeviations)
69 assertNotNull(schemaContext);
71 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_FOO_CONT_A));
72 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_FOO_CONT_B));
73 assertNotNull(schemaContext.findDataTreeChild(MY_FOO_CONT_C).orElse(null));
74 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_BAR_CONT_A));
75 assertNotNull(schemaContext.findDataTreeChild(MY_BAR_CONT_B).orElse(null));
79 public void testDeviationsSupportedInAllModules() throws Exception {
80 final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
81 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
83 assertNotNull(schemaContext);
85 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_FOO_CONT_A));
86 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_FOO_CONT_B));
87 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_FOO_CONT_C));
88 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_BAR_CONT_A));
89 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(MY_BAR_CONT_B));
93 public void testDeviationsSupportedInNoModule() throws Exception {
94 final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
95 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
96 .setModulesWithSupportedDeviations(ImmutableSetMultimap.of())
98 assertNotNull(schemaContext);
100 assertNotNull(schemaContext.findDataTreeChild(MY_FOO_CONT_A).orElse(null));
101 assertNotNull(schemaContext.findDataTreeChild(MY_FOO_CONT_B).orElse(null));
102 assertNotNull(schemaContext.findDataTreeChild(MY_FOO_CONT_C).orElse(null));
103 assertNotNull(schemaContext.findDataTreeChild(MY_BAR_CONT_A).orElse(null));
104 assertNotNull(schemaContext.findDataTreeChild(MY_BAR_CONT_B).orElse(null));
108 public void shouldFailOnAttemptToDeviateTheSameModule() {
109 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(FOO_INVALID_MODULE);
112 reactor.buildEffective();
113 fail("Deviation that targets the same module as the one it is defined is forbidden.");
114 } catch (final ReactorException ex) {
115 final Throwable cause = ex.getCause();
116 assertTrue(cause instanceof InferenceException);
117 assertTrue(cause.getMessage().startsWith(
118 "Deviation must not target the same module as the one it is defined in"));
123 public void shouldFailOnAttemptToDeviateTheSameModule2() {
124 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild()
125 .addSources(BAR_INVALID_MODULE, BAZ_INVALID_MODULE);
128 reactor.buildEffective();
129 fail("Deviation that targets the same module as the one it is defined is forbidden.");
130 } catch (final ReactorException ex) {
131 final Throwable cause = ex.getCause();
132 assertTrue(cause instanceof InferenceException);
133 assertTrue(cause.getMessage().startsWith(
134 "Deviation must not target the same module as the one it is defined in"));