2 * Copyright (c) 2017 Cisco Systems, Inc. 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.parser.repo;
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;
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;
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);
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)
69 final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(modulesWithSupportedDeviations, FOO, BAR,
71 assertTrue(lf.isDone());
72 final EffectiveModelContext schemaContext = lf.get();
73 assertNotNull(schemaContext);
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);
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);
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);
97 public void testDeviationsSupportedInNoModule() throws Exception {
98 final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(ImmutableSetMultimap.of(), FOO, BAR, BAZ,
100 assertTrue(lf.isDone());
101 final EffectiveModelContext schemaContext = lf.get();
102 assertNotNull(schemaContext);
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);
112 public void shouldFailOnAttemptToDeviateTheSameModule2() throws Exception {
113 final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(null, BAR_INVALID, BAZ_INVALID);
114 assertTrue(lf.isDone());
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"));
123 private static void assertAbsent(final EffectiveModelContext schemaContext, final QName qname) {
124 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(qname));
127 private static void assertPresent(final EffectiveModelContext schemaContext, final QName qname) {
128 assertNotEquals(Optional.empty(), schemaContext.findDataTreeChild(qname));
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);
138 private static ListenableFuture<EffectiveModelContext> createSchemaContext(
139 final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations, final String... resources)
141 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
142 "shared-schema-repo-with-deviations-test");
144 final Collection<SourceIdentifier> requiredSources = new ArrayList<>();
145 for (final String resource : resources) {
146 final SettableSchemaProvider<IRSchemaSource> yangSource = getImmediateYangSourceProviderFromResource(
148 yangSource.register(sharedSchemaRepository);
149 yangSource.setResult();
150 requiredSources.add(yangSource.getId());
153 final SchemaContextFactoryConfiguration config = SchemaContextFactoryConfiguration.builder()
154 .setModulesDeviatedByModules(modulesWithSupportedDeviations).build();
155 return sharedSchemaRepository.createEffectiveModelContextFactory(config).createEffectiveModelContext(