Remove RevisionSourceIdentifier
[yangtools.git] / parser / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / SchemaContextFactoryDeviationsTest.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. 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 package org.opendaylight.yangtools.yang.parser.repo;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotEquals;
15
16 import com.google.common.base.Throwables;
17 import com.google.common.collect.ImmutableSetMultimap;
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.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
26
27 public class SchemaContextFactoryDeviationsTest extends AbstractSchemaRepositoryTest {
28     private static final String FOO = "/bug9195/foo.yang";
29     private static final String BAR = "/bug9195/bar.yang";
30     private static final String BAZ = "/bug9195/baz.yang";
31     private static final String FOOBAR = "/bug9195/foobar.yang";
32     private static final String BAR_INVALID = "/bug9195/bar-invalid.yang";
33     private static final String BAZ_INVALID = "/bug9195/baz-invalid.yang";
34     private static final XMLNamespace FOO_NS = XMLNamespace.of("foo-ns");
35     private static final XMLNamespace BAR_NS = XMLNamespace.of("bar-ns");
36     private static final XMLNamespace BAZ_NS = XMLNamespace.of("baz-ns");
37     private static final Revision REVISION = Revision.of("2017-05-16");
38     private static final QNameModule FOO_MODULE = QNameModule.create(FOO_NS, REVISION);
39     private static final QName MY_FOO_CONT_A = QName.create(FOO_MODULE, "my-foo-cont-a");
40     private static final QName MY_FOO_CONT_B = QName.create(FOO_MODULE, "my-foo-cont-b");
41     private static final QName MY_FOO_CONT_C = QName.create(FOO_MODULE, "my-foo-cont-c");
42     private static final QNameModule BAR_MODULE = QNameModule.create(BAR_NS, REVISION);
43     private static final QName MY_BAR_CONT_A = QName.create(BAR_MODULE, "my-bar-cont-a");
44     private static final QName MY_BAR_CONT_B = QName.create(BAR_MODULE, "my-bar-cont-b");
45     private static final QNameModule BAZ_MODULE = QNameModule.create(BAZ_NS, REVISION);
46
47     @Test
48     public void testDeviationsSupportedInSomeModules() {
49         final var context = assertModelContext(ImmutableSetMultimap.<QNameModule, QNameModule>builder()
50             .put(FOO_MODULE, BAR_MODULE)
51             .put(FOO_MODULE, BAZ_MODULE)
52             .put(BAR_MODULE, BAZ_MODULE)
53             .build(),
54             FOO, BAR, BAZ, FOOBAR);
55
56         assertAbsent(context, MY_FOO_CONT_A);
57         assertAbsent(context, MY_FOO_CONT_B);
58         assertPresent(context, MY_FOO_CONT_C);
59         assertAbsent(context, MY_BAR_CONT_A);
60         assertPresent(context, MY_BAR_CONT_B);
61     }
62
63     @Test
64     public void testDeviationsSupportedInAllModules() {
65         final var context = assertModelContext(null, FOO, BAR, BAZ, FOOBAR);
66
67         assertAbsent(context, MY_FOO_CONT_A);
68         assertAbsent(context, MY_FOO_CONT_B);
69         assertAbsent(context, MY_FOO_CONT_C);
70         assertAbsent(context, MY_BAR_CONT_A);
71         assertAbsent(context, MY_BAR_CONT_B);
72     }
73
74     @Test
75     public void testDeviationsSupportedInNoModule() {
76         final var context = assertModelContext(ImmutableSetMultimap.of(), FOO, BAR, BAZ, FOOBAR);
77
78         assertPresent(context, MY_FOO_CONT_A);
79         assertPresent(context, MY_FOO_CONT_B);
80         assertPresent(context, MY_FOO_CONT_C);
81         assertPresent(context, MY_BAR_CONT_A);
82         assertPresent(context, MY_BAR_CONT_B);
83     }
84
85     @Test
86     public void shouldFailOnAttemptToDeviateTheSameModule2() {
87         final var cause = Throwables.getRootCause(assertExecutionException(null, BAR_INVALID, BAZ_INVALID));
88
89         assertThat(cause, instanceOf(InferenceException.class));
90         assertThat(cause.getMessage(),
91             startsWith("Deviation must not target the same module as the one it is defined in"));
92     }
93
94     private static void assertAbsent(final EffectiveModelContext schemaContext, final QName qname) {
95         assertEquals(Optional.empty(), schemaContext.findDataTreeChild(qname));
96     }
97
98     private static void assertPresent(final EffectiveModelContext schemaContext, final QName qname) {
99         assertNotEquals(Optional.empty(), schemaContext.findDataTreeChild(qname));
100     }
101 }