Fix failure source not being reported
[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 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertThrows;
17 import static org.junit.Assert.assertTrue;
18
19 import com.google.common.base.Throwables;
20 import com.google.common.collect.ImmutableSetMultimap;
21 import com.google.common.collect.SetMultimap;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import java.util.Optional;
24 import java.util.concurrent.ExecutionException;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
28 import org.opendaylight.yangtools.yang.common.Revision;
29 import org.opendaylight.yangtools.yang.common.XMLNamespace;
30 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
32
33 public class SchemaContextFactoryDeviationsTest extends AbstractSchemaRepositoryTest {
34     private static final String FOO = "/bug9195/foo.yang";
35     private static final String BAR = "/bug9195/bar.yang";
36     private static final String BAZ = "/bug9195/baz.yang";
37     private static final String FOOBAR = "/bug9195/foobar.yang";
38     private static final String BAR_INVALID = "/bug9195/bar-invalid.yang";
39     private static final String BAZ_INVALID = "/bug9195/baz-invalid.yang";
40     private static final XMLNamespace FOO_NS = XMLNamespace.of("foo-ns");
41     private static final XMLNamespace BAR_NS = XMLNamespace.of("bar-ns");
42     private static final XMLNamespace BAZ_NS = XMLNamespace.of("baz-ns");
43     private static final Revision REVISION = Revision.of("2017-05-16");
44     private static final QNameModule FOO_MODULE = QNameModule.create(FOO_NS, REVISION);
45     private static final QName MY_FOO_CONT_A = QName.create(FOO_MODULE, "my-foo-cont-a");
46     private static final QName MY_FOO_CONT_B = QName.create(FOO_MODULE, "my-foo-cont-b");
47     private static final QName MY_FOO_CONT_C = QName.create(FOO_MODULE, "my-foo-cont-c");
48     private static final QNameModule BAR_MODULE = QNameModule.create(BAR_NS, REVISION);
49     private static final QName MY_BAR_CONT_A = QName.create(BAR_MODULE, "my-bar-cont-a");
50     private static final QName MY_BAR_CONT_B = QName.create(BAR_MODULE, "my-bar-cont-b");
51     private static final QNameModule BAZ_MODULE = QNameModule.create(BAZ_NS, REVISION);
52
53     @Test
54     public void testDeviationsSupportedInSomeModules() throws Exception {
55         final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations =
56                 ImmutableSetMultimap.<QNameModule, QNameModule>builder()
57                 .put(FOO_MODULE, BAR_MODULE)
58                 .put(FOO_MODULE, BAZ_MODULE)
59                 .put(BAR_MODULE, BAZ_MODULE)
60                 .build();
61
62         final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(modulesWithSupportedDeviations, FOO, BAR,
63             BAZ, FOOBAR);
64         assertTrue(lf.isDone());
65         final EffectiveModelContext schemaContext = lf.get();
66         assertNotNull(schemaContext);
67
68         assertAbsent(schemaContext, MY_FOO_CONT_A);
69         assertAbsent(schemaContext, MY_FOO_CONT_B);
70         assertPresent(schemaContext, MY_FOO_CONT_C);
71         assertAbsent(schemaContext, MY_BAR_CONT_A);
72         assertPresent(schemaContext, MY_BAR_CONT_B);
73     }
74
75     @Test
76     public void testDeviationsSupportedInAllModules() throws Exception {
77         final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(null, FOO, BAR, BAZ, FOOBAR);
78         assertTrue(lf.isDone());
79         final EffectiveModelContext schemaContext = lf.get();
80         assertNotNull(schemaContext);
81
82         assertAbsent(schemaContext, MY_FOO_CONT_A);
83         assertAbsent(schemaContext, MY_FOO_CONT_B);
84         assertAbsent(schemaContext, MY_FOO_CONT_C);
85         assertAbsent(schemaContext, MY_BAR_CONT_A);
86         assertAbsent(schemaContext, MY_BAR_CONT_B);
87     }
88
89     @Test
90     public void testDeviationsSupportedInNoModule() throws Exception {
91         final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(ImmutableSetMultimap.of(), FOO, BAR, BAZ,
92             FOOBAR);
93         assertTrue(lf.isDone());
94         final EffectiveModelContext schemaContext = lf.get();
95         assertNotNull(schemaContext);
96
97         assertPresent(schemaContext, MY_FOO_CONT_A);
98         assertPresent(schemaContext, MY_FOO_CONT_B);
99         assertPresent(schemaContext, MY_FOO_CONT_C);
100         assertPresent(schemaContext, MY_BAR_CONT_A);
101         assertPresent(schemaContext, MY_BAR_CONT_B);
102     }
103
104     @Test
105     public void shouldFailOnAttemptToDeviateTheSameModule2() throws Exception {
106         final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(null, BAR_INVALID, BAZ_INVALID);
107         assertTrue(lf.isDone());
108
109         final ExecutionException ex = assertThrows(ExecutionException.class, lf::get);
110         final Throwable cause = Throwables.getRootCause(ex);
111         assertThat(cause, instanceOf(InferenceException.class));
112         assertThat(cause.getMessage(),
113             startsWith("Deviation must not target the same module as the one it is defined in"));
114     }
115
116     private static void assertAbsent(final EffectiveModelContext schemaContext, final QName qname) {
117         assertEquals(Optional.empty(), schemaContext.findDataTreeChild(qname));
118     }
119
120     private static void assertPresent(final EffectiveModelContext schemaContext, final QName qname) {
121         assertNotEquals(Optional.empty(), schemaContext.findDataTreeChild(qname));
122     }
123 }