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;
15 import com.google.common.collect.Lists;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import java.util.HashSet;
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;
33 public class SharedSchemaRepositoryWithFeaturesTest {
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"));
41 return supportedFeatures.contains(qName);
44 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
45 "shared-schema-repo-with-features-test");
47 final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
48 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
49 foobar.register(sharedSchemaRepository);
52 final SchemaContextFactory fact = sharedSchemaRepository
53 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
55 final CheckedFuture<SchemaContext, SchemaResolutionException> testSchemaContextFuture =
56 fact.createSchemaContext(Lists.newArrayList(foobar.getId()), isFeatureSupported);
57 assertTrue(testSchemaContextFuture.isDone());
58 assertSchemaContext(testSchemaContextFuture.checkedGet(), 1);
60 Module module = testSchemaContextFuture.checkedGet().findModuleByName("foobar", null);
61 assertNotNull(module);
62 assertEquals(2, module.getChildNodes().size());
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);
71 ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
72 QName.create(module.getQNameModule(), "test-container-b"));
73 assertNull(testContainerB);
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);
84 public void testSharedSchemaRepositoryWithAllFeaturesSupported() throws Exception {
85 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
86 "shared-schema-repo-with-features-test");
88 final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
89 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
90 foobar.register(sharedSchemaRepository);
93 final SchemaContextFactory fact = sharedSchemaRepository
94 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
96 final CheckedFuture<SchemaContext, SchemaResolutionException> testSchemaContextFuture = fact
97 .createSchemaContext(Lists.newArrayList(foobar.getId()));
98 assertTrue(testSchemaContextFuture.isDone());
99 assertSchemaContext(testSchemaContextFuture.checkedGet(), 1);
101 Module module = testSchemaContextFuture.checkedGet().findModuleByName("foobar", null);
102 assertNotNull(module);
103 assertEquals(3, module.getChildNodes().size());
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);
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);
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);
128 public void testSharedSchemaRepositoryWithNoFeaturesSupported() throws Exception {
129 Predicate<QName> isFeatureSupported = qName -> false;
131 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
132 "shared-schema-repo-with-features-test");
134 final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
135 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
136 foobar.register(sharedSchemaRepository);
139 final SchemaContextFactory fact = sharedSchemaRepository
140 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
142 final CheckedFuture<SchemaContext, SchemaResolutionException> testSchemaContextFuture = fact
143 .createSchemaContext(Lists.newArrayList(foobar.getId()), isFeatureSupported);
144 assertTrue(testSchemaContextFuture.isDone());
145 assertSchemaContext(testSchemaContextFuture.checkedGet(), 1);
147 Module module = testSchemaContextFuture.checkedGet().findModuleByName("foobar", null);
148 assertNotNull(module);
149 assertEquals(1, module.getChildNodes().size());
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);
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);
166 private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
167 assertNotNull(schemaContext);
168 assertEquals(moduleSize, schemaContext.getModules().size());