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.CheckedFuture;
18 import com.google.common.util.concurrent.ListenableFuture;
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.SchemaResolutionException;
27 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
28 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
29 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
30 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
31 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
33 public class MultipleRevImportBug6875Test {
34 private static final String BAR_NS = "bar";
35 private static final String BAR_REV_1 = "2017-02-06";
36 private static final String BAR_REV_2 = "1999-01-01";
37 private static final String BAR_REV_3 = "1970-01-01";
38 private static final String FOO_NS = "foo";
39 private static final String FOO_REV = "1970-01-01";
42 public void testYang11() throws Exception {
43 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
44 "shared-schema-repo-multiple-rev-import-test");
46 final SettableSchemaProvider<ASTSchemaSource> foo = getSourceProvider(
47 "/rfc7950/bug6875/yang1-1/foo.yang");
48 final SettableSchemaProvider<ASTSchemaSource> bar1 = getSourceProvider(
49 "/rfc7950/bug6875/yang1-1/bar@1999-01-01.yang");
50 final SettableSchemaProvider<ASTSchemaSource> bar2 = getSourceProvider(
51 "/rfc7950/bug6875/yang1-1/bar@2017-02-06.yang");
52 final SettableSchemaProvider<ASTSchemaSource> bar3 = getSourceProvider(
53 "/rfc7950/bug6875/yang1-1/bar@1970-01-01.yang");
55 setAndRegister(sharedSchemaRepository, foo);
56 setAndRegister(sharedSchemaRepository, bar1);
57 setAndRegister(sharedSchemaRepository, bar2);
58 setAndRegister(sharedSchemaRepository, bar3);
60 final SchemaContextFactory fact = sharedSchemaRepository
61 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
63 final ListenableFuture<SchemaContext> schemaContextFuture = fact
64 .createSchemaContext(ImmutableList.of(foo.getId(), bar1.getId(), bar2.getId(), bar3.getId()));
65 assertTrue(schemaContextFuture.isDone());
67 final SchemaContext context = schemaContextFuture.get();
68 assertEquals(context.getModules().size(), 4);
70 assertTrue(findNode(context, ImmutableList.of(foo("root"), foo("my-container-1")))
71 instanceof ContainerSchemaNode);
72 assertTrue(findNode(context, ImmutableList.of(foo("root"), foo("my-container-2")))
73 instanceof ContainerSchemaNode);
75 assertTrue(findNode(context, ImmutableList.of(bar3("root"), foo("my-container-1")))
76 instanceof ContainerSchemaNode);
77 assertTrue(findNode(context, ImmutableList.of(bar3("root"), foo("my-container-2")))
78 instanceof ContainerSchemaNode);
80 assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-1"))));
81 assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-2"))));
83 assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-1"))));
84 assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-2"))));
88 public void testYang10() throws Exception {
89 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
90 "shared-schema-repo-multiple-rev-import-test");
92 final SettableSchemaProvider<ASTSchemaSource> foo = getSourceProvider(
93 "/rfc7950/bug6875/yang1-0/foo.yang");
94 final SettableSchemaProvider<ASTSchemaSource> bar1 = getSourceProvider(
95 "/rfc7950/bug6875/yang1-0/bar@1999-01-01.yang");
96 final SettableSchemaProvider<ASTSchemaSource> bar2 = getSourceProvider(
97 "/rfc7950/bug6875/yang1-0/bar@2017-02-06.yang");
99 setAndRegister(sharedSchemaRepository, foo);
100 setAndRegister(sharedSchemaRepository, bar1);
101 setAndRegister(sharedSchemaRepository, bar2);
103 final SchemaContextFactory fact = sharedSchemaRepository
104 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
106 final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContextFuture = fact
107 .createSchemaContext(ImmutableList.of(foo.getId(), bar1.getId(), bar2.getId()));
108 assertTrue(schemaContextFuture.isDone());
111 schemaContextFuture.checkedGet();
112 fail("Test should fail due to invalid imports of yang source.");
113 } catch (final SchemaResolutionException e) {
114 assertTrue(e.getCause().getMessage().startsWith("Module:bar imported twice with different revisions"));
118 private static void setAndRegister(final SharedSchemaRepository sharedSchemaRepository,
119 final SettableSchemaProvider<ASTSchemaSource> source) {
120 source.register(sharedSchemaRepository);
124 private static SettableSchemaProvider<ASTSchemaSource> getSourceProvider(final String resourceName)
126 final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
127 return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
128 ASTSchemaSource.class);
131 private static SchemaNode findNode(final SchemaContext context, final Iterable<QName> qnames) {
132 return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(qnames, true));
135 private static QName foo(final String localName) {
136 return QName.create(FOO_NS, FOO_REV, localName);
139 private static QName bar1(final String localName) {
140 return QName.create(BAR_NS, BAR_REV_1, localName);
143 private static QName bar2(final String localName) {
144 return QName.create(BAR_NS, BAR_REV_2, localName);
147 private static QName bar3(final String localName) {
148 return QName.create(BAR_NS, BAR_REV_3, localName);