82a85c0d04fcfa60c5713bff1790c614cab7c318
[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.ArrayList;
24 import java.util.Collection;
25 import java.util.Optional;
26 import java.util.concurrent.ExecutionException;
27 import org.junit.Test;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.QNameModule;
30 import org.opendaylight.yangtools.yang.common.Revision;
31 import org.opendaylight.yangtools.yang.common.XMLNamespace;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
34 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
35 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
36 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
39
40 public class SchemaContextFactoryDeviationsTest {
41     private static final String FOO = "/bug9195/foo.yang";
42     private static final String BAR = "/bug9195/bar.yang";
43     private static final String BAZ = "/bug9195/baz.yang";
44     private static final String FOOBAR = "/bug9195/foobar.yang";
45     private static final String BAR_INVALID = "/bug9195/bar-invalid.yang";
46     private static final String BAZ_INVALID = "/bug9195/baz-invalid.yang";
47     private static final XMLNamespace FOO_NS = XMLNamespace.of("foo-ns");
48     private static final XMLNamespace BAR_NS = XMLNamespace.of("bar-ns");
49     private static final XMLNamespace BAZ_NS = XMLNamespace.of("baz-ns");
50     private static final Revision REVISION = Revision.of("2017-05-16");
51     private static final QNameModule FOO_MODULE = QNameModule.create(FOO_NS, REVISION);
52     private static final QName MY_FOO_CONT_A = QName.create(FOO_MODULE, "my-foo-cont-a");
53     private static final QName MY_FOO_CONT_B = QName.create(FOO_MODULE, "my-foo-cont-b");
54     private static final QName MY_FOO_CONT_C = QName.create(FOO_MODULE, "my-foo-cont-c");
55     private static final QNameModule BAR_MODULE = QNameModule.create(BAR_NS, REVISION);
56     private static final QName MY_BAR_CONT_A = QName.create(BAR_MODULE, "my-bar-cont-a");
57     private static final QName MY_BAR_CONT_B = QName.create(BAR_MODULE, "my-bar-cont-b");
58     private static final QNameModule BAZ_MODULE = QNameModule.create(BAZ_NS, REVISION);
59
60     @Test
61     public void testDeviationsSupportedInSomeModules() throws Exception {
62         final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations =
63                 ImmutableSetMultimap.<QNameModule, QNameModule>builder()
64                 .put(FOO_MODULE, BAR_MODULE)
65                 .put(FOO_MODULE, BAZ_MODULE)
66                 .put(BAR_MODULE, BAZ_MODULE)
67                 .build();
68
69         final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(modulesWithSupportedDeviations, FOO, BAR,
70             BAZ, FOOBAR);
71         assertTrue(lf.isDone());
72         final EffectiveModelContext schemaContext = lf.get();
73         assertNotNull(schemaContext);
74
75         assertAbsent(schemaContext, MY_FOO_CONT_A);
76         assertAbsent(schemaContext, MY_FOO_CONT_B);
77         assertPresent(schemaContext, MY_FOO_CONT_C);
78         assertAbsent(schemaContext, MY_BAR_CONT_A);
79         assertPresent(schemaContext, MY_BAR_CONT_B);
80     }
81
82     @Test
83     public void testDeviationsSupportedInAllModules() throws Exception {
84         final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(null, FOO, BAR, BAZ, FOOBAR);
85         assertTrue(lf.isDone());
86         final EffectiveModelContext schemaContext = lf.get();
87         assertNotNull(schemaContext);
88
89         assertAbsent(schemaContext, MY_FOO_CONT_A);
90         assertAbsent(schemaContext, MY_FOO_CONT_B);
91         assertAbsent(schemaContext, MY_FOO_CONT_C);
92         assertAbsent(schemaContext, MY_BAR_CONT_A);
93         assertAbsent(schemaContext, MY_BAR_CONT_B);
94     }
95
96     @Test
97     public void testDeviationsSupportedInNoModule() throws Exception {
98         final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(ImmutableSetMultimap.of(), FOO, BAR, BAZ,
99             FOOBAR);
100         assertTrue(lf.isDone());
101         final EffectiveModelContext schemaContext = lf.get();
102         assertNotNull(schemaContext);
103
104         assertPresent(schemaContext, MY_FOO_CONT_A);
105         assertPresent(schemaContext, MY_FOO_CONT_B);
106         assertPresent(schemaContext, MY_FOO_CONT_C);
107         assertPresent(schemaContext, MY_BAR_CONT_A);
108         assertPresent(schemaContext, MY_BAR_CONT_B);
109     }
110
111     @Test
112     public void shouldFailOnAttemptToDeviateTheSameModule2() throws Exception {
113         final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(null, BAR_INVALID, BAZ_INVALID);
114         assertTrue(lf.isDone());
115
116         final ExecutionException ex = assertThrows(ExecutionException.class, lf::get);
117         final Throwable cause = Throwables.getRootCause(ex);
118         assertThat(cause, instanceOf(InferenceException.class));
119         assertThat(cause.getMessage(),
120             startsWith("Deviation must not target the same module as the one it is defined in"));
121     }
122
123     private static void assertAbsent(final EffectiveModelContext schemaContext, final QName qname) {
124         assertEquals(Optional.empty(), schemaContext.findDataTreeChild(qname));
125     }
126
127     private static void assertPresent(final EffectiveModelContext schemaContext, final QName qname) {
128         assertNotEquals(Optional.empty(), schemaContext.findDataTreeChild(qname));
129     }
130
131     private static SettableSchemaProvider<IRSchemaSource> getImmediateYangSourceProviderFromResource(
132             final String resourceName) throws Exception {
133         final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
134         return SettableSchemaProvider.createImmediate(TextToIRTransformer.transformText(yangSource),
135                 IRSchemaSource.class);
136     }
137
138     private static ListenableFuture<EffectiveModelContext> createSchemaContext(
139             final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations, final String... resources)
140             throws Exception {
141         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
142                 "shared-schema-repo-with-deviations-test");
143
144         final Collection<SourceIdentifier> requiredSources = new ArrayList<>();
145         for (final String resource : resources) {
146             final SettableSchemaProvider<IRSchemaSource> yangSource = getImmediateYangSourceProviderFromResource(
147                     resource);
148             yangSource.register(sharedSchemaRepository);
149             yangSource.setResult();
150             requiredSources.add(yangSource.getId());
151         }
152
153         final SchemaContextFactoryConfiguration config = SchemaContextFactoryConfiguration.builder()
154                 .setModulesDeviatedByModules(modulesWithSupportedDeviations).build();
155         return sharedSchemaRepository.createEffectiveModelContextFactory(config).createEffectiveModelContext(
156             requiredSources);
157     }
158 }