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.assertThrows;
15 import static org.junit.Assert.assertTrue;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.util.Optional;
19 import java.util.concurrent.ExecutionException;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.repo.api.YangIRSchemaSource;
26 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
29 public class MultipleRevImportBug6875Test {
30 private static final String BAR_NS = "bar";
31 private static final String BAR_REV_1 = "2017-02-06";
32 private static final String BAR_REV_2 = "1999-01-01";
33 private static final String BAR_REV_3 = "1970-01-01";
34 private static final String FOO_NS = "foo";
37 public void testYang11() throws Exception {
38 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
39 "shared-schema-repo-multiple-rev-import-test");
41 final SettableSchemaProvider<YangIRSchemaSource> foo = getSourceProvider(
42 "/rfc7950/bug6875/yang1-1/foo.yang");
43 final SettableSchemaProvider<YangIRSchemaSource> bar1 = getSourceProvider(
44 "/rfc7950/bug6875/yang1-1/bar@1999-01-01.yang");
45 final SettableSchemaProvider<YangIRSchemaSource> bar2 = getSourceProvider(
46 "/rfc7950/bug6875/yang1-1/bar@2017-02-06.yang");
47 final SettableSchemaProvider<YangIRSchemaSource> bar3 = getSourceProvider(
48 "/rfc7950/bug6875/yang1-1/bar@1970-01-01.yang");
50 setAndRegister(sharedSchemaRepository, foo);
51 setAndRegister(sharedSchemaRepository, bar1);
52 setAndRegister(sharedSchemaRepository, bar2);
53 setAndRegister(sharedSchemaRepository, bar3);
55 final ListenableFuture<EffectiveModelContext> schemaContextFuture =
56 sharedSchemaRepository.createEffectiveModelContextFactory().createEffectiveModelContext(
57 foo.getId(), bar1.getId(), bar2.getId(), bar3.getId());
58 assertTrue(schemaContextFuture.isDone());
60 final SchemaContext context = schemaContextFuture.get();
61 assertEquals(context.getModules().size(), 4);
63 assertThat(context.findDataTreeChild(foo("root"), foo("my-container-1")).orElse(null),
64 instanceOf(ContainerSchemaNode.class));
65 assertThat(context.findDataTreeChild(foo("root"), foo("my-container-2")).orElse(null),
66 instanceOf(ContainerSchemaNode.class));
68 assertThat(context.findDataTreeChild(bar3("root"), foo("my-container-1")).orElse(null),
69 instanceOf(ContainerSchemaNode.class));
70 assertThat(context.findDataTreeChild(bar3("root"), foo("my-container-2")).orElse(null),
71 instanceOf(ContainerSchemaNode.class));
73 assertEquals(Optional.empty(), context.findDataTreeChild(bar2("root"), foo("my-container-1")));
74 assertEquals(Optional.empty(), context.findDataTreeChild(bar2("root"), foo("my-container-2")));
76 assertEquals(Optional.empty(), context.findDataTreeChild(bar1("root"), foo("my-container-1")));
77 assertEquals(Optional.empty(), context.findDataTreeChild(bar1("root"), foo("my-container-2")));
81 public void testYang10() throws Exception {
82 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
83 "shared-schema-repo-multiple-rev-import-test");
85 final SettableSchemaProvider<YangIRSchemaSource> foo = getSourceProvider(
86 "/rfc7950/bug6875/yang1-0/foo.yang");
87 final SettableSchemaProvider<YangIRSchemaSource> bar1 = getSourceProvider(
88 "/rfc7950/bug6875/yang1-0/bar@1999-01-01.yang");
89 final SettableSchemaProvider<YangIRSchemaSource> bar2 = getSourceProvider(
90 "/rfc7950/bug6875/yang1-0/bar@2017-02-06.yang");
92 setAndRegister(sharedSchemaRepository, foo);
93 setAndRegister(sharedSchemaRepository, bar1);
94 setAndRegister(sharedSchemaRepository, bar2);
96 final ListenableFuture<EffectiveModelContext> schemaContextFuture =
97 sharedSchemaRepository.createEffectiveModelContextFactory().createEffectiveModelContext(
98 foo.getId(), bar1.getId(), bar2.getId());
99 assertTrue(schemaContextFuture.isDone());
101 final ExecutionException ex = assertThrows(ExecutionException.class, schemaContextFuture::get);
102 final Throwable cause = ex.getCause();
103 assertThat(cause, instanceOf(IllegalArgumentException.class));
104 assertThat(cause.getMessage(), startsWith("Module:bar imported twice with different revisions"));
107 private static void setAndRegister(final SharedSchemaRepository sharedSchemaRepository,
108 final SettableSchemaProvider<YangIRSchemaSource> source) {
109 source.register(sharedSchemaRepository);
113 private static SettableSchemaProvider<YangIRSchemaSource> getSourceProvider(final String resourceName)
115 final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
116 return SettableSchemaProvider.createImmediate(TextToIRTransformer.transformText(yangSource),
117 YangIRSchemaSource.class);
120 private static QName foo(final String localName) {
121 return QName.create(FOO_NS, localName);
124 private static QName bar1(final String localName) {
125 return QName.create(BAR_NS, BAR_REV_1, localName);
128 private static QName bar2(final String localName) {
129 return QName.create(BAR_NS, BAR_REV_2, localName);
132 private static QName bar3(final String localName) {
133 return QName.create(BAR_NS, BAR_REV_3, localName);