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.ImmutableSet;
17 import com.google.common.collect.Lists;
18 import com.google.common.util.concurrent.CheckedFuture;
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 final Set<QName> supportedFeatures = ImmutableSet.of(QName.create("foobar-namespace", "1970-01-01", "test-feature-1"));
39 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
40 "shared-schema-repo-with-features-test");
42 final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
43 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
44 foobar.register(sharedSchemaRepository);
47 final SchemaContextFactory fact = sharedSchemaRepository
48 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
50 final CheckedFuture<SchemaContext, SchemaResolutionException> testSchemaContextFuture =
51 fact.createSchemaContext(Lists.newArrayList(foobar.getId()), supportedFeatures);
52 assertTrue(testSchemaContextFuture.isDone());
53 assertSchemaContext(testSchemaContextFuture.checkedGet(), 1);
55 final Module module = testSchemaContextFuture.checkedGet().findModuleByName("foobar", null);
56 assertNotNull(module);
57 assertEquals(2, module.getChildNodes().size());
59 final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
60 QName.create(module.getQNameModule(), "test-container-a"));
61 assertNotNull(testContainerA);
62 final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
63 QName.create(module.getQNameModule(), "test-leaf-a"));
64 assertNotNull(testLeafA);
66 final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
67 QName.create(module.getQNameModule(), "test-container-b"));
68 assertNull(testContainerB);
70 final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
71 QName.create(module.getQNameModule(), "test-container-c"));
72 assertNotNull(testContainerC);
73 final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
74 QName.create(module.getQNameModule(), "test-leaf-c"));
75 assertNotNull(testLeafC);
79 public void testSharedSchemaRepositoryWithAllFeaturesSupported() throws Exception {
80 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
81 "shared-schema-repo-with-features-test");
83 final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
84 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
85 foobar.register(sharedSchemaRepository);
88 final SchemaContextFactory fact = sharedSchemaRepository
89 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
91 final CheckedFuture<SchemaContext, SchemaResolutionException> testSchemaContextFuture = fact
92 .createSchemaContext(Lists.newArrayList(foobar.getId()));
93 assertTrue(testSchemaContextFuture.isDone());
94 assertSchemaContext(testSchemaContextFuture.checkedGet(), 1);
96 final Module module = testSchemaContextFuture.checkedGet().findModuleByName("foobar", null);
97 assertNotNull(module);
98 assertEquals(3, module.getChildNodes().size());
100 final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
101 QName.create(module.getQNameModule(), "test-container-a"));
102 assertNotNull(testContainerA);
103 final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
104 QName.create(module.getQNameModule(), "test-leaf-a"));
105 assertNotNull(testLeafA);
107 final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
108 QName.create(module.getQNameModule(), "test-container-b"));
109 assertNotNull(testContainerB);
110 final LeafSchemaNode testLeafB = (LeafSchemaNode) testContainerB.getDataChildByName(
111 QName.create(module.getQNameModule(), "test-leaf-b"));
112 assertNotNull(testLeafB);
114 final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
115 QName.create(module.getQNameModule(), "test-container-c"));
116 assertNotNull(testContainerC);
117 final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
118 QName.create(module.getQNameModule(), "test-leaf-c"));
119 assertNotNull(testLeafC);
123 public void testSharedSchemaRepositoryWithNoFeaturesSupported() throws Exception {
124 final Set<QName> supportedFeatures = ImmutableSet.of();
126 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
127 "shared-schema-repo-with-features-test");
129 final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
130 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
131 foobar.register(sharedSchemaRepository);
134 final SchemaContextFactory fact = sharedSchemaRepository
135 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
137 final CheckedFuture<SchemaContext, SchemaResolutionException> testSchemaContextFuture = fact
138 .createSchemaContext(Lists.newArrayList(foobar.getId()), supportedFeatures);
139 assertTrue(testSchemaContextFuture.isDone());
140 assertSchemaContext(testSchemaContextFuture.checkedGet(), 1);
142 final Module module = testSchemaContextFuture.checkedGet().findModuleByName("foobar", null);
143 assertNotNull(module);
144 assertEquals(1, module.getChildNodes().size());
146 final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
147 QName.create(module.getQNameModule(), "test-container-c"));
148 assertNotNull(testContainerC);
149 final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
150 QName.create(module.getQNameModule(), "test-leaf-c"));
151 assertNotNull(testLeafC);
154 private static SettableSchemaProvider<ASTSchemaSource> getImmediateYangSourceProviderFromResource(
155 final String resourceName) throws Exception {
156 final ResourceYangSource yangSource = new ResourceYangSource(resourceName);
157 final CheckedFuture<ASTSchemaSource, SchemaSourceException> aSTSchemaSource = TextToASTTransformer.TRANSFORMATION.apply(yangSource);
158 return SettableSchemaProvider.createImmediate(aSTSchemaSource.get(), ASTSchemaSource.class);
161 private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
162 assertNotNull(schemaContext);
163 assertEquals(moduleSize, schemaContext.getModules().size());