BUG-9043: Remove use of CheckedFuture from YANG components
[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.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;
31
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";
38     private static final String FOO_REV = "1970-01-01";
39
40     @Test
41     public void testYang11() throws Exception {
42         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
43                 "shared-schema-repo-multiple-rev-import-test");
44
45         final SettableSchemaProvider<ASTSchemaSource> foo = getSourceProvider(
46             "/rfc7950/bug6875/yang1-1/foo.yang");
47         final SettableSchemaProvider<ASTSchemaSource> bar1 = getSourceProvider(
48             "/rfc7950/bug6875/yang1-1/bar@1999-01-01.yang");
49         final SettableSchemaProvider<ASTSchemaSource> bar2 = getSourceProvider(
50             "/rfc7950/bug6875/yang1-1/bar@2017-02-06.yang");
51         final SettableSchemaProvider<ASTSchemaSource> bar3 = getSourceProvider(
52             "/rfc7950/bug6875/yang1-1/bar@1970-01-01.yang");
53
54         setAndRegister(sharedSchemaRepository, foo);
55         setAndRegister(sharedSchemaRepository, bar1);
56         setAndRegister(sharedSchemaRepository, bar2);
57         setAndRegister(sharedSchemaRepository, bar3);
58
59         final SchemaContextFactory fact = sharedSchemaRepository
60                 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
61
62         final ListenableFuture<SchemaContext> schemaContextFuture = fact
63                 .createSchemaContext(ImmutableList.of(foo.getId(), bar1.getId(), bar2.getId(), bar3.getId()));
64         assertTrue(schemaContextFuture.isDone());
65
66         final SchemaContext context = schemaContextFuture.get();
67         assertEquals(context.getModules().size(), 4);
68
69         assertTrue(findNode(context, ImmutableList.of(foo("root"), foo("my-container-1")))
70             instanceof ContainerSchemaNode);
71         assertTrue(findNode(context, ImmutableList.of(foo("root"), foo("my-container-2")))
72             instanceof ContainerSchemaNode);
73
74         assertTrue(findNode(context, ImmutableList.of(bar3("root"), foo("my-container-1")))
75             instanceof ContainerSchemaNode);
76         assertTrue(findNode(context, ImmutableList.of(bar3("root"), foo("my-container-2")))
77             instanceof ContainerSchemaNode);
78
79         assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-1"))));
80         assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-2"))));
81
82         assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-1"))));
83         assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-2"))));
84     }
85
86     @Test
87     public void testYang10() throws Exception {
88         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
89                 "shared-schema-repo-multiple-rev-import-test");
90
91         final SettableSchemaProvider<ASTSchemaSource> foo = getSourceProvider(
92             "/rfc7950/bug6875/yang1-0/foo.yang");
93         final SettableSchemaProvider<ASTSchemaSource> bar1 = getSourceProvider(
94             "/rfc7950/bug6875/yang1-0/bar@1999-01-01.yang");
95         final SettableSchemaProvider<ASTSchemaSource> bar2 = getSourceProvider(
96             "/rfc7950/bug6875/yang1-0/bar@2017-02-06.yang");
97
98         setAndRegister(sharedSchemaRepository, foo);
99         setAndRegister(sharedSchemaRepository, bar1);
100         setAndRegister(sharedSchemaRepository, bar2);
101
102         final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory(
103                 SchemaSourceFilter.ALWAYS_ACCEPT);
104         final ListenableFuture<SchemaContext> schemaContextFuture = fact.createSchemaContext(
105                 ImmutableList.of(foo.getId(), bar1.getId(), bar2.getId()));
106         assertTrue(schemaContextFuture.isDone());
107
108         try {
109             schemaContextFuture.get();
110             fail("Test should fail due to invalid imports of yang source.");
111         } catch (final ExecutionException e) {
112             assertTrue(e.getCause().getMessage().startsWith(
113                 "Module:bar imported twice with different revisions"));
114         }
115     }
116
117     private static void setAndRegister(final SharedSchemaRepository sharedSchemaRepository,
118             final SettableSchemaProvider<ASTSchemaSource> source) {
119         source.register(sharedSchemaRepository);
120         source.setResult();
121     }
122
123     private static SettableSchemaProvider<ASTSchemaSource> getSourceProvider(final String resourceName)
124             throws Exception {
125         final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
126         return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
127             ASTSchemaSource.class);
128     }
129
130     private static SchemaNode findNode(final SchemaContext context, final Iterable<QName> qnames) {
131         return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(qnames, true));
132     }
133
134     private static QName foo(final String localName) {
135         return QName.create(FOO_NS, FOO_REV, localName);
136     }
137
138     private static QName bar1(final String localName) {
139         return QName.create(BAR_NS, BAR_REV_1, localName);
140     }
141
142     private static QName bar2(final String localName) {
143         return QName.create(BAR_NS, BAR_REV_2, localName);
144     }
145
146     private static QName bar3(final String localName) {
147         return QName.create(BAR_NS, BAR_REV_3, localName);
148     }
149 }