268ed0c6781ed4add6cf2ee0c86d604bfa1cabe0
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / MultipleRevImportBug6875Test.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.parser.repo;
10
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;
15
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.Lists;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import com.google.common.util.concurrent.ListenableFuture;
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.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
27 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
28 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
29 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
30 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
31 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
32 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
33
34 public class MultipleRevImportBug6875Test {
35     private static final String BAR_NS = "bar";
36     private static final String BAR_REV_1 = "2017-02-06";
37     private static final String BAR_REV_2 = "1999-01-01";
38     private static final String BAR_REV_3 = "1970-01-01";
39     private static final String FOO_NS = "foo";
40     private static final String FOO_REV = "1970-01-01";
41
42     @Test
43     public void testYang11() throws Exception {
44         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
45                 "shared-schema-repo-multiple-rev-import-test");
46
47         final SettableSchemaProvider<ASTSchemaSource> foo = getSourceProvider(
48             "/rfc7950/bug6875/yang1-1/foo.yang");
49         final SettableSchemaProvider<ASTSchemaSource> bar1 = getSourceProvider(
50             "/rfc7950/bug6875/yang1-1/bar@1999-01-01.yang");
51         final SettableSchemaProvider<ASTSchemaSource> bar2 = getSourceProvider(
52             "/rfc7950/bug6875/yang1-1/bar@2017-02-06.yang");
53         final SettableSchemaProvider<ASTSchemaSource> bar3 = getSourceProvider(
54             "/rfc7950/bug6875/yang1-1/bar@1970-01-01.yang");
55
56         setAndRegister(sharedSchemaRepository, foo);
57         setAndRegister(sharedSchemaRepository, bar1);
58         setAndRegister(sharedSchemaRepository, bar2);
59         setAndRegister(sharedSchemaRepository, bar3);
60
61         final SchemaContextFactory fact = sharedSchemaRepository
62                 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
63
64         final ListenableFuture<SchemaContext> schemaContextFuture = fact
65                 .createSchemaContext(ImmutableList.of(foo.getId(), bar1.getId(), bar2.getId(), bar3.getId()));
66         assertTrue(schemaContextFuture.isDone());
67
68         final SchemaContext context = schemaContextFuture.get();
69         assertEquals(context.getModules().size(), 4);
70
71         assertTrue(findNode(context, ImmutableList.of(foo("root"), foo("my-container-1")))
72             instanceof ContainerSchemaNode);
73         assertTrue(findNode(context, ImmutableList.of(foo("root"), foo("my-container-2")))
74             instanceof ContainerSchemaNode);
75
76         assertTrue(findNode(context, ImmutableList.of(bar3("root"), foo("my-container-1")))
77             instanceof ContainerSchemaNode);
78         assertTrue(findNode(context, ImmutableList.of(bar3("root"), foo("my-container-2")))
79             instanceof ContainerSchemaNode);
80
81         assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-1"))));
82         assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-2"))));
83
84         assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-1"))));
85         assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-2"))));
86     }
87
88     @Test
89     public void testYang10() throws Exception {
90         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
91                 "shared-schema-repo-multiple-rev-import-test");
92
93         final SettableSchemaProvider<ASTSchemaSource> foo = getSourceProvider(
94             "/rfc7950/bug6875/yang1-0/foo.yang");
95         final SettableSchemaProvider<ASTSchemaSource> bar1 = getSourceProvider(
96             "/rfc7950/bug6875/yang1-0/bar@1999-01-01.yang");
97         final SettableSchemaProvider<ASTSchemaSource> bar2 = getSourceProvider(
98             "/rfc7950/bug6875/yang1-0/bar@2017-02-06.yang");
99
100         setAndRegister(sharedSchemaRepository, foo);
101         setAndRegister(sharedSchemaRepository, bar1);
102         setAndRegister(sharedSchemaRepository, bar2);
103
104         final SchemaContextFactory fact = sharedSchemaRepository
105                 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
106
107         final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContextFuture = fact
108                 .createSchemaContext(Lists.newArrayList(foo.getId(), bar1.getId(), bar2.getId()));
109         assertTrue(schemaContextFuture.isDone());
110
111         try {
112             schemaContextFuture.checkedGet();
113             fail("Test should fail due to invalid imports of yang source.");
114         } catch (final SchemaResolutionException e) {
115             assertTrue(e.getCause().getMessage().startsWith("Module:bar imported twice with different revisions"));
116         }
117     }
118
119     private static void setAndRegister(final SharedSchemaRepository sharedSchemaRepository,
120             final SettableSchemaProvider<ASTSchemaSource> source) {
121         source.register(sharedSchemaRepository);
122         source.setResult();
123     }
124
125     private static SettableSchemaProvider<ASTSchemaSource> getSourceProvider(final String resourceName)
126             throws Exception {
127         final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
128         return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
129             ASTSchemaSource.class);
130     }
131
132     private static SchemaNode findNode(final SchemaContext context, final Iterable<QName> qnames) {
133         return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(qnames, true));
134     }
135
136     private static QName foo(final String localName) {
137         return QName.create(FOO_NS, FOO_REV, localName);
138     }
139
140     private static QName bar1(final String localName) {
141         return QName.create(BAR_NS, BAR_REV_1, localName);
142     }
143
144     private static QName bar2(final String localName) {
145         return QName.create(BAR_NS, BAR_REV_2, localName);
146     }
147
148     private static QName bar3(final String localName) {
149         return QName.create(BAR_NS, BAR_REV_3, localName);
150     }
151 }