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