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.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.jupiter.api.Assertions.assertEquals;
13 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
14 import static org.junit.jupiter.api.Assertions.assertThrows;
15 import static org.junit.jupiter.api.Assertions.assertTrue;
17 import java.util.Optional;
18 import java.util.concurrent.ExecutionException;
19 import org.junit.jupiter.api.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.ir.YangIRSchemaSource;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
24 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
26 public class MultipleRevImportBug6875Test {
27 private static final String BAR_NS = "bar";
28 private static final String BAR_REV_1 = "2017-02-06";
29 private static final String BAR_REV_2 = "1999-01-01";
30 private static final String BAR_REV_3 = "1970-01-01";
31 private static final String FOO_NS = "foo";
34 public void testYang11() throws Exception {
35 final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-multiple-rev-import-test");
37 final var foo = getSourceProvider("/rfc7950/bug6875/yang1-1/foo.yang");
38 final var bar1 = getSourceProvider("/rfc7950/bug6875/yang1-1/bar@1999-01-01.yang");
39 final var bar2 = getSourceProvider("/rfc7950/bug6875/yang1-1/bar@2017-02-06.yang");
40 final var bar3 = getSourceProvider("/rfc7950/bug6875/yang1-1/bar@1970-01-01.yang");
42 setAndRegister(sharedSchemaRepository, foo);
43 setAndRegister(sharedSchemaRepository, bar1);
44 setAndRegister(sharedSchemaRepository, bar2);
45 setAndRegister(sharedSchemaRepository, bar3);
47 final var schemaContextFuture = sharedSchemaRepository.createEffectiveModelContextFactory()
48 .createEffectiveModelContext(foo.getId(), bar1.getId(), bar2.getId(), bar3.getId());
49 assertTrue(schemaContextFuture.isDone());
51 final var context = schemaContextFuture.get();
52 assertEquals(context.getModules().size(), 4);
54 assertInstanceOf(ContainerSchemaNode.class,
55 context.findDataTreeChild(foo("root"), foo("my-container-1")).orElse(null));
56 assertInstanceOf(ContainerSchemaNode.class,
57 context.findDataTreeChild(foo("root"), foo("my-container-2")).orElse(null));
59 assertInstanceOf(ContainerSchemaNode.class,
60 context.findDataTreeChild(bar3("root"), foo("my-container-1")).orElse(null));
61 assertInstanceOf(ContainerSchemaNode.class,
62 context.findDataTreeChild(bar3("root"), foo("my-container-2")).orElse(null));
64 assertEquals(Optional.empty(), context.findDataTreeChild(bar2("root"), foo("my-container-1")));
65 assertEquals(Optional.empty(), context.findDataTreeChild(bar2("root"), foo("my-container-2")));
67 assertEquals(Optional.empty(), context.findDataTreeChild(bar1("root"), foo("my-container-1")));
68 assertEquals(Optional.empty(), context.findDataTreeChild(bar1("root"), foo("my-container-2")));
72 public void testYang10() throws Exception {
73 final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-multiple-rev-import-test");
75 final var foo = getSourceProvider("/rfc7950/bug6875/yang1-0/foo.yang");
76 final var bar1 = getSourceProvider("/rfc7950/bug6875/yang1-0/bar@1999-01-01.yang");
77 final var bar2 = getSourceProvider("/rfc7950/bug6875/yang1-0/bar@2017-02-06.yang");
79 setAndRegister(sharedSchemaRepository, foo);
80 setAndRegister(sharedSchemaRepository, bar1);
81 setAndRegister(sharedSchemaRepository, bar2);
83 final var schemaContextFuture = sharedSchemaRepository.createEffectiveModelContextFactory()
84 .createEffectiveModelContext(foo.getId(), bar1.getId(), bar2.getId());
85 assertTrue(schemaContextFuture.isDone());
87 final var ex = assertThrows(ExecutionException.class, schemaContextFuture::get);
88 final var cause = assertInstanceOf(IllegalArgumentException.class, ex.getCause());
89 assertThat(cause.getMessage(), startsWith("Module:bar imported twice with different revisions"));
92 private static void setAndRegister(final SharedSchemaRepository sharedSchemaRepository,
93 final SettableSchemaProvider<YangIRSchemaSource> source) {
94 source.register(sharedSchemaRepository);
98 private static SettableSchemaProvider<YangIRSchemaSource> getSourceProvider(final String resourceName)
100 return SettableSchemaProvider.createImmediate(
101 TextToIRTransformer.transformText(YangTextSource.forResource(resourceName)),
102 YangIRSchemaSource.class);
105 private static QName foo(final String localName) {
106 return QName.create(FOO_NS, localName);
109 private static QName bar1(final String localName) {
110 return QName.create(BAR_NS, BAR_REV_1, localName);
113 private static QName bar2(final String localName) {
114 return QName.create(BAR_NS, BAR_REV_2, localName);
117 private static QName bar3(final String localName) {
118 return QName.create(BAR_NS, BAR_REV_3, localName);