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.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;
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);
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)
62 final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(modulesWithSupportedDeviations, FOO, BAR,
64 assertTrue(lf.isDone());
65 final EffectiveModelContext schemaContext = lf.get();
66 assertNotNull(schemaContext);
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);
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);
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);
90 public void testDeviationsSupportedInNoModule() throws Exception {
91 final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(ImmutableSetMultimap.of(), FOO, BAR, BAZ,
93 assertTrue(lf.isDone());
94 final EffectiveModelContext schemaContext = lf.get();
95 assertNotNull(schemaContext);
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);
105 public void shouldFailOnAttemptToDeviateTheSameModule2() throws Exception {
106 final ListenableFuture<EffectiveModelContext> lf = createSchemaContext(null, BAR_INVALID, BAZ_INVALID);
107 assertTrue(lf.isDone());
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"));
116 private static void assertAbsent(final EffectiveModelContext schemaContext, final QName qname) {
117 assertEquals(Optional.empty(), schemaContext.findDataTreeChild(qname));
120 private static void assertPresent(final EffectiveModelContext schemaContext, final QName qname) {
121 assertNotEquals(Optional.empty(), schemaContext.findDataTreeChild(qname));