Cleanup use of Guava library
[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.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;
32
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";
40
41     @Test
42     public void testYang11() throws Exception {
43         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
44                 "shared-schema-repo-multiple-rev-import-test");
45
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");
54
55         setAndRegister(sharedSchemaRepository, foo);
56         setAndRegister(sharedSchemaRepository, bar1);
57         setAndRegister(sharedSchemaRepository, bar2);
58         setAndRegister(sharedSchemaRepository, bar3);
59
60         final SchemaContextFactory fact = sharedSchemaRepository
61                 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
62
63         final ListenableFuture<SchemaContext> schemaContextFuture = fact
64                 .createSchemaContext(ImmutableList.of(foo.getId(), bar1.getId(), bar2.getId(), bar3.getId()));
65         assertTrue(schemaContextFuture.isDone());
66
67         final SchemaContext context = schemaContextFuture.get();
68         assertEquals(context.getModules().size(), 4);
69
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);
74
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);
79
80         assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-1"))));
81         assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-2"))));
82
83         assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-1"))));
84         assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-2"))));
85     }
86
87     @Test
88     public void testYang10() throws Exception {
89         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
90                 "shared-schema-repo-multiple-rev-import-test");
91
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");
98
99         setAndRegister(sharedSchemaRepository, foo);
100         setAndRegister(sharedSchemaRepository, bar1);
101         setAndRegister(sharedSchemaRepository, bar2);
102
103         final SchemaContextFactory fact = sharedSchemaRepository
104                 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
105
106         final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContextFuture = fact
107                 .createSchemaContext(ImmutableList.of(foo.getId(), bar1.getId(), bar2.getId()));
108         assertTrue(schemaContextFuture.isDone());
109
110         try {
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"));
115         }
116     }
117
118     private static void setAndRegister(final SharedSchemaRepository sharedSchemaRepository,
119             final SettableSchemaProvider<ASTSchemaSource> source) {
120         source.register(sharedSchemaRepository);
121         source.setResult();
122     }
123
124     private static SettableSchemaProvider<ASTSchemaSource> getSourceProvider(final String resourceName)
125             throws Exception {
126         final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
127         return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
128             ASTSchemaSource.class);
129     }
130
131     private static SchemaNode findNode(final SchemaContext context, final Iterable<QName> qnames) {
132         return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(qnames, true));
133     }
134
135     private static QName foo(final String localName) {
136         return QName.create(FOO_NS, FOO_REV, localName);
137     }
138
139     private static QName bar1(final String localName) {
140         return QName.create(BAR_NS, BAR_REV_1, localName);
141     }
142
143     private static QName bar2(final String localName) {
144         return QName.create(BAR_NS, BAR_REV_2, localName);
145     }
146
147     private static QName bar3(final String localName) {
148         return QName.create(BAR_NS, BAR_REV_3, localName);
149     }
150 }