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.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertThrows;
16 import static org.junit.Assert.assertTrue;
18 import com.google.common.base.Throwables;
19 import com.google.common.collect.ImmutableSetMultimap;
20 import com.google.common.collect.SetMultimap;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import java.util.ArrayList;
23 import java.util.Collection;
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.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
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.model.util.SchemaContextUtil;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
41 public class SchemaContextFactoryDeviationsTest {
42 private static final String FOO = "/bug9195/foo.yang";
43 private static final String BAR = "/bug9195/bar.yang";
44 private static final String BAZ = "/bug9195/baz.yang";
45 private static final String FOOBAR = "/bug9195/foobar.yang";
46 private static final String BAR_INVALID = "/bug9195/bar-invalid.yang";
47 private static final String BAZ_INVALID = "/bug9195/baz-invalid.yang";
48 private static final XMLNamespace FOO_NS = XMLNamespace.of("foo-ns");
49 private static final XMLNamespace BAR_NS = XMLNamespace.of("bar-ns");
50 private static final XMLNamespace BAZ_NS = XMLNamespace.of("baz-ns");
51 private static final Revision REVISION = Revision.of("2017-05-16");
52 private static final QNameModule FOO_MODULE = QNameModule.create(FOO_NS, REVISION);
53 private static final QName MY_FOO_CONT_A = QName.create(FOO_MODULE, "my-foo-cont-a");
54 private static final QName MY_FOO_CONT_B = QName.create(FOO_MODULE, "my-foo-cont-b");
55 private static final QName MY_FOO_CONT_C = QName.create(FOO_MODULE, "my-foo-cont-c");
56 private static final QNameModule BAR_MODULE = QNameModule.create(BAR_NS, REVISION);
57 private static final QName MY_BAR_CONT_A = QName.create(BAR_MODULE, "my-bar-cont-a");
58 private static final QName MY_BAR_CONT_B = QName.create(BAR_MODULE, "my-bar-cont-b");
59 private static final QNameModule BAZ_MODULE = QNameModule.create(BAZ_NS, REVISION);
62 public void testDeviationsSupportedInSomeModules() throws Exception {
63 final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations =
64 ImmutableSetMultimap.<QNameModule, QNameModule>builder()
65 .put(FOO_MODULE, BAR_MODULE)
66 .put(FOO_MODULE, BAZ_MODULE)
67 .put(BAR_MODULE, BAZ_MODULE)
70 final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(modulesWithSupportedDeviations, FOO, BAR,
72 assertTrue(lf.isDone());
73 final SchemaContext schemaContext = lf.get();
74 assertNotNull(schemaContext);
76 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_A)));
77 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_B)));
78 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_C)));
79 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_BAR_CONT_A)));
80 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_BAR_CONT_B)));
84 public void testDeviationsSupportedInAllModules() throws Exception {
85 final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(null, FOO, BAR, BAZ, FOOBAR);
86 assertTrue(lf.isDone());
87 final SchemaContext schemaContext = lf.get();
88 assertNotNull(schemaContext);
90 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_A)));
91 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_B)));
92 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_C)));
93 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_BAR_CONT_A)));
94 assertNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_BAR_CONT_B)));
98 public void testDeviationsSupportedInNoModule() throws Exception {
99 final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(ImmutableSetMultimap.of(), FOO, BAR, BAZ,
101 assertTrue(lf.isDone());
102 final SchemaContext schemaContext = lf.get();
103 assertNotNull(schemaContext);
105 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_A)));
106 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_B)));
107 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_FOO_CONT_C)));
108 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_BAR_CONT_A)));
109 assertNotNull(SchemaContextUtil.findDataSchemaNode(schemaContext, SchemaPath.create(true, MY_BAR_CONT_B)));
113 public void shouldFailOnAttemptToDeviateTheSameModule2() throws Exception {
114 final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(null, BAR_INVALID, BAZ_INVALID);
115 assertTrue(lf.isDone());
117 final ExecutionException ex = assertThrows(ExecutionException.class, lf::get);
118 final Throwable cause = Throwables.getRootCause(ex);
119 assertThat(cause, instanceOf(InferenceException.class));
120 assertThat(cause.getMessage(),
121 startsWith("Deviation must not target the same module as the one it is defined in"));
124 private static SettableSchemaProvider<IRSchemaSource> getImmediateYangSourceProviderFromResource(
125 final String resourceName) throws Exception {
126 final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
127 return SettableSchemaProvider.createImmediate(TextToIRTransformer.transformText(yangSource),
128 IRSchemaSource.class);
131 private static ListenableFuture<EffectiveModelContext> createSchemaContext(
132 final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations, final String... resources)
134 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
135 "shared-schema-repo-with-deviations-test");
137 final Collection<SourceIdentifier> requiredSources = new ArrayList<>();
138 for (final String resource : resources) {
139 final SettableSchemaProvider<IRSchemaSource> yangSource = getImmediateYangSourceProviderFromResource(
141 yangSource.register(sharedSchemaRepository);
142 yangSource.setResult();
143 requiredSources.add(yangSource.getId());
146 final SchemaContextFactoryConfiguration config = SchemaContextFactoryConfiguration.builder()
147 .setModulesDeviatedByModules(modulesWithSupportedDeviations).build();
148 return sharedSchemaRepository.createEffectiveModelContextFactory(config).createEffectiveModelContext(