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