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.ImmutableList;
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.util.concurrent.ListenableFuture;
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.YangTextSchemaSource;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
29 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToASTTransformer;
31 public class SharedSchemaRepositoryWithFeaturesTest {
34 public void testSharedSchemaRepositoryWithSomeFeaturesSupported() throws Exception {
35 final Set<QName> supportedFeatures = ImmutableSet.of(QName.create("foobar-namespace", "test-feature-1"));
37 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
38 "shared-schema-repo-with-features-test");
40 final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource(
41 "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
42 foobar.register(sharedSchemaRepository);
45 final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory();
46 final ListenableFuture<SchemaContext> testSchemaContextFuture =
47 fact.createSchemaContext(ImmutableList.of(foobar.getId()), supportedFeatures);
48 assertTrue(testSchemaContextFuture.isDone());
49 assertSchemaContext(testSchemaContextFuture.get(), 1);
51 final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
52 assertNotNull(module);
53 assertEquals(2, module.getChildNodes().size());
55 final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
56 QName.create(module.getQNameModule(), "test-container-a"));
57 assertNotNull(testContainerA);
58 final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
59 QName.create(module.getQNameModule(), "test-leaf-a"));
60 assertNotNull(testLeafA);
62 final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
63 QName.create(module.getQNameModule(), "test-container-b"));
64 assertNull(testContainerB);
66 final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
67 QName.create(module.getQNameModule(), "test-container-c"));
68 assertNotNull(testContainerC);
69 final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
70 QName.create(module.getQNameModule(), "test-leaf-c"));
71 assertNotNull(testLeafC);
75 public void testSharedSchemaRepositoryWithAllFeaturesSupported() throws Exception {
76 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
77 "shared-schema-repo-with-features-test");
79 final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource(
80 "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
81 foobar.register(sharedSchemaRepository);
84 final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory();
85 final ListenableFuture<SchemaContext> testSchemaContextFuture =
86 fact.createSchemaContext(ImmutableList.of(foobar.getId()));
87 assertTrue(testSchemaContextFuture.isDone());
88 assertSchemaContext(testSchemaContextFuture.get(), 1);
90 final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
91 assertNotNull(module);
92 assertEquals(3, module.getChildNodes().size());
94 final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
95 QName.create(module.getQNameModule(), "test-container-a"));
96 assertNotNull(testContainerA);
97 final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
98 QName.create(module.getQNameModule(), "test-leaf-a"));
99 assertNotNull(testLeafA);
101 final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
102 QName.create(module.getQNameModule(), "test-container-b"));
103 assertNotNull(testContainerB);
104 final LeafSchemaNode testLeafB = (LeafSchemaNode) testContainerB.getDataChildByName(
105 QName.create(module.getQNameModule(), "test-leaf-b"));
106 assertNotNull(testLeafB);
108 final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
109 QName.create(module.getQNameModule(), "test-container-c"));
110 assertNotNull(testContainerC);
111 final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
112 QName.create(module.getQNameModule(), "test-leaf-c"));
113 assertNotNull(testLeafC);
117 public void testSharedSchemaRepositoryWithNoFeaturesSupported() throws Exception {
118 final Set<QName> supportedFeatures = ImmutableSet.of();
120 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
121 "shared-schema-repo-with-features-test");
123 final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource(
124 "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
125 foobar.register(sharedSchemaRepository);
128 final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory();
129 final ListenableFuture<SchemaContext> testSchemaContextFuture =
130 fact.createSchemaContext(ImmutableList.of(foobar.getId()), supportedFeatures);
131 assertTrue(testSchemaContextFuture.isDone());
132 assertSchemaContext(testSchemaContextFuture.get(), 1);
134 final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
135 assertNotNull(module);
136 assertEquals(1, module.getChildNodes().size());
138 final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
139 QName.create(module.getQNameModule(), "test-container-c"));
140 assertNotNull(testContainerC);
141 final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
142 QName.create(module.getQNameModule(), "test-leaf-c"));
143 assertNotNull(testLeafC);
146 private static SettableSchemaProvider<ASTSchemaSource> getImmediateYangSourceProviderFromResource(
147 final String resourceName) throws Exception {
148 final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
149 return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
150 ASTSchemaSource.class);
153 private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
154 assertNotNull(schemaContext);
155 assertEquals(moduleSize, schemaContext.getModules().size());