2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.parser.repo;
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;
16 import com.google.common.collect.Lists;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import java.util.HashSet;
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;
34 public class SharedSchemaRepositoryWithFeaturesTest {
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"));
42 return supportedFeatures.contains(qName);
45 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
46 "shared-schema-repo-with-features-test");
48 final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
49 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
50 foobar.register(sharedSchemaRepository);
53 final SchemaContextFactory fact = sharedSchemaRepository
54 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
56 final CheckedFuture<SchemaContext, SchemaResolutionException> testSchemaContextFuture =
57 fact.createSchemaContext(Lists.newArrayList(foobar.getId()), isFeatureSupported);
58 assertTrue(testSchemaContextFuture.isDone());
59 assertSchemaContext(testSchemaContextFuture.checkedGet(), 1);
61 Module module = testSchemaContextFuture.checkedGet().findModuleByName("foobar", null);
62 assertNotNull(module);
63 assertEquals(2, module.getChildNodes().size());
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);
72 ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
73 QName.create(module.getQNameModule(), "test-container-b"));
74 assertNull(testContainerB);
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);
85 public void testSharedSchemaRepositoryWithAllFeaturesSupported() throws Exception {
86 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
87 "shared-schema-repo-with-features-test");
89 final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
90 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
91 foobar.register(sharedSchemaRepository);
94 final SchemaContextFactory fact = sharedSchemaRepository
95 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
97 final CheckedFuture<SchemaContext, SchemaResolutionException> testSchemaContextFuture = fact
98 .createSchemaContext(Lists.newArrayList(foobar.getId()));
99 assertTrue(testSchemaContextFuture.isDone());
100 assertSchemaContext(testSchemaContextFuture.checkedGet(), 1);
102 Module module = testSchemaContextFuture.checkedGet().findModuleByName("foobar", null);
103 assertNotNull(module);
104 assertEquals(3, module.getChildNodes().size());
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);
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);
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);
129 public void testSharedSchemaRepositoryWithNoFeaturesSupported() throws Exception {
130 Predicate<QName> isFeatureSupported = qName -> false;
132 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
133 "shared-schema-repo-with-features-test");
135 final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
136 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
137 foobar.register(sharedSchemaRepository);
140 final SchemaContextFactory fact = sharedSchemaRepository
141 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
143 final CheckedFuture<SchemaContext, SchemaResolutionException> testSchemaContextFuture = fact
144 .createSchemaContext(Lists.newArrayList(foobar.getId()), isFeatureSupported);
145 assertTrue(testSchemaContextFuture.isDone());
146 assertSchemaContext(testSchemaContextFuture.checkedGet(), 1);
148 Module module = testSchemaContextFuture.checkedGet().findModuleByName("foobar", null);
149 assertNotNull(module);
150 assertEquals(1, module.getChildNodes().size());
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);
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);
167 private void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
168 assertNotNull(schemaContext);
169 assertEquals(moduleSize, schemaContext.getModules().size());