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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.util.concurrent.ExecutionException;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
26 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToASTTransformer;
30 public class MultipleRevImportBug6875Test {
31 private static final String BAR_NS = "bar";
32 private static final String BAR_REV_1 = "2017-02-06";
33 private static final String BAR_REV_2 = "1999-01-01";
34 private static final String BAR_REV_3 = "1970-01-01";
35 private static final String FOO_NS = "foo";
38 public void testYang11() throws Exception {
39 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
40 "shared-schema-repo-multiple-rev-import-test");
42 final SettableSchemaProvider<ASTSchemaSource> foo = getSourceProvider(
43 "/rfc7950/bug6875/yang1-1/foo.yang");
44 final SettableSchemaProvider<ASTSchemaSource> bar1 = getSourceProvider(
45 "/rfc7950/bug6875/yang1-1/bar@1999-01-01.yang");
46 final SettableSchemaProvider<ASTSchemaSource> bar2 = getSourceProvider(
47 "/rfc7950/bug6875/yang1-1/bar@2017-02-06.yang");
48 final SettableSchemaProvider<ASTSchemaSource> bar3 = getSourceProvider(
49 "/rfc7950/bug6875/yang1-1/bar@1970-01-01.yang");
51 setAndRegister(sharedSchemaRepository, foo);
52 setAndRegister(sharedSchemaRepository, bar1);
53 setAndRegister(sharedSchemaRepository, bar2);
54 setAndRegister(sharedSchemaRepository, bar3);
56 final ListenableFuture<EffectiveModelContext> schemaContextFuture =
57 sharedSchemaRepository.createEffectiveModelContextFactory().createEffectiveModelContext(
58 foo.getId(), bar1.getId(), bar2.getId(), bar3.getId());
59 assertTrue(schemaContextFuture.isDone());
61 final SchemaContext context = schemaContextFuture.get();
62 assertEquals(context.getModules().size(), 4);
64 assertTrue(findNode(context, ImmutableList.of(foo("root"), foo("my-container-1")))
65 instanceof ContainerSchemaNode);
66 assertTrue(findNode(context, ImmutableList.of(foo("root"), foo("my-container-2")))
67 instanceof ContainerSchemaNode);
69 assertTrue(findNode(context, ImmutableList.of(bar3("root"), foo("my-container-1")))
70 instanceof ContainerSchemaNode);
71 assertTrue(findNode(context, ImmutableList.of(bar3("root"), foo("my-container-2")))
72 instanceof ContainerSchemaNode);
74 assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-1"))));
75 assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-2"))));
77 assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-1"))));
78 assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-2"))));
82 public void testYang10() throws Exception {
83 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
84 "shared-schema-repo-multiple-rev-import-test");
86 final SettableSchemaProvider<ASTSchemaSource> foo = getSourceProvider(
87 "/rfc7950/bug6875/yang1-0/foo.yang");
88 final SettableSchemaProvider<ASTSchemaSource> bar1 = getSourceProvider(
89 "/rfc7950/bug6875/yang1-0/bar@1999-01-01.yang");
90 final SettableSchemaProvider<ASTSchemaSource> bar2 = getSourceProvider(
91 "/rfc7950/bug6875/yang1-0/bar@2017-02-06.yang");
93 setAndRegister(sharedSchemaRepository, foo);
94 setAndRegister(sharedSchemaRepository, bar1);
95 setAndRegister(sharedSchemaRepository, bar2);
97 final ListenableFuture<EffectiveModelContext> schemaContextFuture =
98 sharedSchemaRepository.createEffectiveModelContextFactory().createEffectiveModelContext(
99 foo.getId(), bar1.getId(), bar2.getId());
100 assertTrue(schemaContextFuture.isDone());
103 schemaContextFuture.get();
104 fail("Test should fail due to invalid imports of yang source.");
105 } catch (final ExecutionException e) {
106 assertTrue(e.getCause().getMessage().startsWith(
107 "Module:bar imported twice with different revisions"));
111 private static void setAndRegister(final SharedSchemaRepository sharedSchemaRepository,
112 final SettableSchemaProvider<ASTSchemaSource> source) {
113 source.register(sharedSchemaRepository);
117 private static SettableSchemaProvider<ASTSchemaSource> getSourceProvider(final String resourceName)
119 final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
120 return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
121 ASTSchemaSource.class);
124 private static SchemaNode findNode(final SchemaContext context, final Iterable<QName> qnames) {
125 return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(qnames, true));
128 private static QName foo(final String localName) {
129 return QName.create(FOO_NS, localName);
132 private static QName bar1(final String localName) {
133 return QName.create(BAR_NS, BAR_REV_1, localName);
136 private static QName bar2(final String localName) {
137 return QName.create(BAR_NS, BAR_REV_2, localName);
140 private static QName bar3(final String localName) {
141 return QName.create(BAR_NS, BAR_REV_3, localName);