BUG-4688: Rework SchemaContext module lookups
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / SharedSchemaRepositoryWithFeaturesTest.java
1 /*
2  * Copyright (c) 2016 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.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import java.util.Set;
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.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
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.parser.util.ASTSchemaSource;
30 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
31
32 public class SharedSchemaRepositoryWithFeaturesTest {
33
34     @Test
35     public void testSharedSchemaRepositoryWithSomeFeaturesSupported() throws Exception {
36         final Set<QName> supportedFeatures = ImmutableSet.of(QName.create("foobar-namespace", "1970-01-01", "test-feature-1"));
37
38         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
39                 "shared-schema-repo-with-features-test");
40
41         final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
42                 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
43         foobar.register(sharedSchemaRepository);
44         foobar.setResult();
45
46         final SchemaContextFactory fact = sharedSchemaRepository
47                 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
48
49         final ListenableFuture<SchemaContext> testSchemaContextFuture =
50                 fact.createSchemaContext(ImmutableList.of(foobar.getId()), supportedFeatures);
51         assertTrue(testSchemaContextFuture.isDone());
52         assertSchemaContext(testSchemaContextFuture.get(), 1);
53
54         final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
55         assertNotNull(module);
56         assertEquals(2, module.getChildNodes().size());
57
58         final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
59                 QName.create(module.getQNameModule(), "test-container-a"));
60         assertNotNull(testContainerA);
61         final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
62                 QName.create(module.getQNameModule(), "test-leaf-a"));
63         assertNotNull(testLeafA);
64
65         final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
66                 QName.create(module.getQNameModule(), "test-container-b"));
67         assertNull(testContainerB);
68
69         final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
70                 QName.create(module.getQNameModule(), "test-container-c"));
71         assertNotNull(testContainerC);
72         final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
73                 QName.create(module.getQNameModule(), "test-leaf-c"));
74         assertNotNull(testLeafC);
75     }
76
77     @Test
78     public void testSharedSchemaRepositoryWithAllFeaturesSupported() throws Exception {
79         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
80                 "shared-schema-repo-with-features-test");
81
82         final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
83                 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
84         foobar.register(sharedSchemaRepository);
85         foobar.setResult();
86
87         final SchemaContextFactory fact = sharedSchemaRepository
88                 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
89
90         final ListenableFuture<SchemaContext> testSchemaContextFuture =
91                 fact.createSchemaContext(ImmutableList.of(foobar.getId()));
92         assertTrue(testSchemaContextFuture.isDone());
93         assertSchemaContext(testSchemaContextFuture.get(), 1);
94
95         final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
96         assertNotNull(module);
97         assertEquals(3, module.getChildNodes().size());
98
99         final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
100                 QName.create(module.getQNameModule(), "test-container-a"));
101         assertNotNull(testContainerA);
102         final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
103                 QName.create(module.getQNameModule(), "test-leaf-a"));
104         assertNotNull(testLeafA);
105
106         final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
107                 QName.create(module.getQNameModule(), "test-container-b"));
108         assertNotNull(testContainerB);
109         final LeafSchemaNode testLeafB = (LeafSchemaNode) testContainerB.getDataChildByName(
110                 QName.create(module.getQNameModule(), "test-leaf-b"));
111         assertNotNull(testLeafB);
112
113         final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
114                 QName.create(module.getQNameModule(), "test-container-c"));
115         assertNotNull(testContainerC);
116         final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
117                 QName.create(module.getQNameModule(), "test-leaf-c"));
118         assertNotNull(testLeafC);
119     }
120
121     @Test
122     public void testSharedSchemaRepositoryWithNoFeaturesSupported() throws Exception {
123         final Set<QName> supportedFeatures = ImmutableSet.of();
124
125         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
126                 "shared-schema-repo-with-features-test");
127
128         final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
129                 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
130         foobar.register(sharedSchemaRepository);
131         foobar.setResult();
132
133         final SchemaContextFactory fact = sharedSchemaRepository
134                 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
135
136         final ListenableFuture<SchemaContext> testSchemaContextFuture =
137                 fact.createSchemaContext(ImmutableList.of(foobar.getId()), supportedFeatures);
138         assertTrue(testSchemaContextFuture.isDone());
139         assertSchemaContext(testSchemaContextFuture.get(), 1);
140
141         final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
142         assertNotNull(module);
143         assertEquals(1, module.getChildNodes().size());
144
145         final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
146                 QName.create(module.getQNameModule(), "test-container-c"));
147         assertNotNull(testContainerC);
148         final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
149                 QName.create(module.getQNameModule(), "test-leaf-c"));
150         assertNotNull(testLeafC);
151     }
152
153     private static SettableSchemaProvider<ASTSchemaSource> getImmediateYangSourceProviderFromResource(
154             final String resourceName) throws Exception {
155         final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
156         return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
157             ASTSchemaSource.class);
158     }
159
160     private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
161         assertNotNull(schemaContext);
162         assertEquals(moduleSize, schemaContext.getModules().size());
163     }
164 }