Merge branch 'master' of ../controller
[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 package org.opendaylight.yangtools.yang.parser.repo;
9
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;
14
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;
29
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";
36
37     @Test
38     public void testYang11() throws Exception {
39         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
40                 "shared-schema-repo-multiple-rev-import-test");
41
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");
50
51         setAndRegister(sharedSchemaRepository, foo);
52         setAndRegister(sharedSchemaRepository, bar1);
53         setAndRegister(sharedSchemaRepository, bar2);
54         setAndRegister(sharedSchemaRepository, bar3);
55
56         final ListenableFuture<EffectiveModelContext> schemaContextFuture =
57                 sharedSchemaRepository.createEffectiveModelContextFactory().createEffectiveModelContext(
58                     foo.getId(), bar1.getId(), bar2.getId(), bar3.getId());
59         assertTrue(schemaContextFuture.isDone());
60
61         final SchemaContext context = schemaContextFuture.get();
62         assertEquals(context.getModules().size(), 4);
63
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);
68
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);
73
74         assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-1"))));
75         assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-2"))));
76
77         assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-1"))));
78         assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-2"))));
79     }
80
81     @Test
82     public void testYang10() throws Exception {
83         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
84                 "shared-schema-repo-multiple-rev-import-test");
85
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");
92
93         setAndRegister(sharedSchemaRepository, foo);
94         setAndRegister(sharedSchemaRepository, bar1);
95         setAndRegister(sharedSchemaRepository, bar2);
96
97         final ListenableFuture<EffectiveModelContext> schemaContextFuture =
98                 sharedSchemaRepository.createEffectiveModelContextFactory().createEffectiveModelContext(
99                     foo.getId(), bar1.getId(), bar2.getId());
100         assertTrue(schemaContextFuture.isDone());
101
102         try {
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"));
108         }
109     }
110
111     private static void setAndRegister(final SharedSchemaRepository sharedSchemaRepository,
112             final SettableSchemaProvider<ASTSchemaSource> source) {
113         source.register(sharedSchemaRepository);
114         source.setResult();
115     }
116
117     private static SettableSchemaProvider<ASTSchemaSource> getSourceProvider(final String resourceName)
118             throws Exception {
119         final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
120         return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
121             ASTSchemaSource.class);
122     }
123
124     private static SchemaNode findNode(final SchemaContext context, final Iterable<QName> qnames) {
125         return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(qnames, true));
126     }
127
128     private static QName foo(final String localName) {
129         return QName.create(FOO_NS, localName);
130     }
131
132     private static QName bar1(final String localName) {
133         return QName.create(BAR_NS, BAR_REV_1, localName);
134     }
135
136     private static QName bar2(final String localName) {
137         return QName.create(BAR_NS, BAR_REV_2, localName);
138     }
139
140     private static QName bar3(final String localName) {
141         return QName.create(BAR_NS, BAR_REV_3, localName);
142     }
143 }