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;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.common.Revision;
22 import org.opendaylight.yangtools.yang.common.XMLNamespace;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
30 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
32 public class Bug8307Test {
34 private static final StatementStreamSource FOO_MODULE = sourceForResource("/bugs/bug8307/foo.yang");
35 private static final StatementStreamSource BAR_MODULE = sourceForResource("/bugs/bug8307/bar.yang");
36 private static final StatementStreamSource BAZ_MODULE = sourceForResource("/bugs/bug8307/baz.yang");
37 private static final StatementStreamSource FOOBAR_MODULE = sourceForResource("/bugs/bug8307/foobar.yang");
38 private static final StatementStreamSource FOO_INVALID_MODULE = sourceForResource("/bugs/bug8307/foo-invalid.yang");
39 private static final StatementStreamSource BAR_INVALID_MODULE = sourceForResource("/bugs/bug8307/bar-invalid.yang");
40 private static final StatementStreamSource BAZ_INVALID_MODULE = sourceForResource("/bugs/bug8307/baz-invalid.yang");
42 private static final XMLNamespace FOO_NS = XMLNamespace.of("foo-ns");
43 private static final XMLNamespace BAR_NS = XMLNamespace.of("bar-ns");
44 private static final XMLNamespace BAZ_NS = XMLNamespace.of("baz-ns");
46 private static final Revision REVISION = Revision.of("2017-05-16");
47 private static final QNameModule FOO = QNameModule.create(FOO_NS, REVISION);
48 private static final QName MY_FOO_CONT_A = QName.create(FOO, "my-foo-cont-a");
49 private static final QName MY_FOO_CONT_B = QName.create(FOO, "my-foo-cont-b");
50 private static final QName MY_FOO_CONT_C = QName.create(FOO, "my-foo-cont-c");
51 private static final QNameModule BAR = QNameModule.create(BAR_NS, REVISION);
52 private static final QName MY_BAR_CONT_A = QName.create(BAR, "my-bar-cont-a");
53 private static final QName MY_BAR_CONT_B = QName.create(BAR, "my-bar-cont-b");
54 private static final QNameModule BAZ = QNameModule.create(BAZ_NS, REVISION);
55 private static final QName MY_BAZ_CONT = QName.create(BAZ, "my-baz-cont");
58 public void testDeviationsSupportedInSomeModules() throws Exception {
59 final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations =
60 ImmutableSetMultimap.<QNameModule, QNameModule>builder()
66 final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
67 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
68 .setModulesWithSupportedDeviations(modulesWithSupportedDeviations)
70 assertNotNull(schemaContext);
72 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_A)));
73 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_B)));
74 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_C)));
75 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_BAR_CONT_A)));
76 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_BAR_CONT_B)));
80 public void testDeviationsSupportedInAllModules() throws Exception {
81 final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
82 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
84 assertNotNull(schemaContext);
86 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_A)));
87 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_B)));
88 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_C)));
89 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_BAR_CONT_A)));
90 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_BAR_CONT_B)));
94 public void testDeviationsSupportedInNoModule() throws Exception {
95 final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
96 .addSources(FOO_MODULE, BAR_MODULE, BAZ_MODULE, FOOBAR_MODULE)
97 .setModulesWithSupportedDeviations(ImmutableSetMultimap.of())
99 assertNotNull(schemaContext);
101 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_A)));
102 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_B)));
103 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_C)));
104 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_BAR_CONT_A)));
105 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_BAR_CONT_B)));
109 public void shouldFailOnAttemptToDeviateTheSameModule() {
110 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(FOO_INVALID_MODULE);
113 reactor.buildEffective();
114 fail("Deviation that targets the same module as the one it is defined is forbidden.");
115 } catch (final ReactorException ex) {
116 final Throwable cause = ex.getCause();
117 assertTrue(cause instanceof InferenceException);
118 assertTrue(cause.getMessage().startsWith(
119 "Deviation must not target the same module as the one it is defined in"));
124 public void shouldFailOnAttemptToDeviateTheSameModule2() {
125 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild()
126 .addSources(BAR_INVALID_MODULE, BAZ_INVALID_MODULE);
129 reactor.buildEffective();
130 fail("Deviation that targets the same module as the one it is defined is forbidden.");
131 } catch (final ReactorException ex) {
132 final Throwable cause = ex.getCause();
133 assertTrue(cause instanceof InferenceException);
134 assertTrue(cause.getMessage().startsWith(
135 "Deviation must not target the same module as the one it is defined in"));