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