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
9 package org.opendaylight.yangtools.yang.parser.repo;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.util.concurrent.ExecutionException;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
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.SchemaContextFactory;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
27 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
28 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
29 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
30 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
32 public class MultipleRevImportBug6875Test {
33 private static final String BAR_NS = "bar";
34 private static final String BAR_REV_1 = "2017-02-06";
35 private static final String BAR_REV_2 = "1999-01-01";
36 private static final String BAR_REV_3 = "1970-01-01";
37 private static final String FOO_NS = "foo";
40 public void testYang11() throws Exception {
41 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
42 "shared-schema-repo-multiple-rev-import-test");
44 final SettableSchemaProvider<ASTSchemaSource> foo = getSourceProvider(
45 "/rfc7950/bug6875/yang1-1/foo.yang");
46 final SettableSchemaProvider<ASTSchemaSource> bar1 = getSourceProvider(
47 "/rfc7950/bug6875/yang1-1/bar@1999-01-01.yang");
48 final SettableSchemaProvider<ASTSchemaSource> bar2 = getSourceProvider(
49 "/rfc7950/bug6875/yang1-1/bar@2017-02-06.yang");
50 final SettableSchemaProvider<ASTSchemaSource> bar3 = getSourceProvider(
51 "/rfc7950/bug6875/yang1-1/bar@1970-01-01.yang");
53 setAndRegister(sharedSchemaRepository, foo);
54 setAndRegister(sharedSchemaRepository, bar1);
55 setAndRegister(sharedSchemaRepository, bar2);
56 setAndRegister(sharedSchemaRepository, bar3);
58 final SchemaContextFactory fact = sharedSchemaRepository
59 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
61 final ListenableFuture<SchemaContext> schemaContextFuture = fact
62 .createSchemaContext(ImmutableList.of(foo.getId(), bar1.getId(), bar2.getId(), bar3.getId()));
63 assertTrue(schemaContextFuture.isDone());
65 final SchemaContext context = schemaContextFuture.get();
66 assertEquals(context.getModules().size(), 4);
68 assertTrue(findNode(context, ImmutableList.of(foo("root"), foo("my-container-1")))
69 instanceof ContainerSchemaNode);
70 assertTrue(findNode(context, ImmutableList.of(foo("root"), foo("my-container-2")))
71 instanceof ContainerSchemaNode);
73 assertTrue(findNode(context, ImmutableList.of(bar3("root"), foo("my-container-1")))
74 instanceof ContainerSchemaNode);
75 assertTrue(findNode(context, ImmutableList.of(bar3("root"), foo("my-container-2")))
76 instanceof ContainerSchemaNode);
78 assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-1"))));
79 assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-2"))));
81 assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-1"))));
82 assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-2"))));
86 public void testYang10() throws Exception {
87 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
88 "shared-schema-repo-multiple-rev-import-test");
90 final SettableSchemaProvider<ASTSchemaSource> foo = getSourceProvider(
91 "/rfc7950/bug6875/yang1-0/foo.yang");
92 final SettableSchemaProvider<ASTSchemaSource> bar1 = getSourceProvider(
93 "/rfc7950/bug6875/yang1-0/bar@1999-01-01.yang");
94 final SettableSchemaProvider<ASTSchemaSource> bar2 = getSourceProvider(
95 "/rfc7950/bug6875/yang1-0/bar@2017-02-06.yang");
97 setAndRegister(sharedSchemaRepository, foo);
98 setAndRegister(sharedSchemaRepository, bar1);
99 setAndRegister(sharedSchemaRepository, bar2);
101 final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory(
102 SchemaSourceFilter.ALWAYS_ACCEPT);
103 final ListenableFuture<SchemaContext> schemaContextFuture = fact.createSchemaContext(
104 ImmutableList.of(foo.getId(), bar1.getId(), bar2.getId()));
105 assertTrue(schemaContextFuture.isDone());
108 schemaContextFuture.get();
109 fail("Test should fail due to invalid imports of yang source.");
110 } catch (final ExecutionException e) {
111 assertTrue(e.getCause().getMessage().startsWith(
112 "Module:bar imported twice with different revisions"));
116 private static void setAndRegister(final SharedSchemaRepository sharedSchemaRepository,
117 final SettableSchemaProvider<ASTSchemaSource> source) {
118 source.register(sharedSchemaRepository);
122 private static SettableSchemaProvider<ASTSchemaSource> getSourceProvider(final String resourceName)
124 final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
125 return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
126 ASTSchemaSource.class);
129 private static SchemaNode findNode(final SchemaContext context, final Iterable<QName> qnames) {
130 return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(qnames, true));
133 private static QName foo(final String localName) {
134 return QName.create(FOO_NS, localName);
137 private static QName bar1(final String localName) {
138 return QName.create(BAR_NS, BAR_REV_1, localName);
141 private static QName bar2(final String localName) {
142 return QName.create(BAR_NS, BAR_REV_2, localName);
145 private static QName bar3(final String localName) {
146 return QName.create(BAR_NS, BAR_REV_3, localName);