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
8 package org.opendaylight.yangtools.yang.parser.repo;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.collect.ImmutableSet;
16 import com.google.common.util.concurrent.ListenableFuture;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
27 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
29 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
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<IRSchemaSource> foobar = getImmediateYangSourceProviderFromResource(
41 "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
42 foobar.register(sharedSchemaRepository);
45 final ListenableFuture<EffectiveModelContext> testSchemaContextFuture =
46 sharedSchemaRepository.createEffectiveModelContextFactory(
47 SchemaContextFactoryConfiguration.builder().setSupportedFeatures(supportedFeatures).build())
48 .createEffectiveModelContext(foobar.getId());
49 assertTrue(testSchemaContextFuture.isDone());
50 assertSchemaContext(testSchemaContextFuture.get(), 1);
52 final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
53 assertNotNull(module);
54 assertEquals(2, module.getChildNodes().size());
56 final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
57 QName.create(module.getQNameModule(), "test-container-a"));
58 assertNotNull(testContainerA);
59 final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
60 QName.create(module.getQNameModule(), "test-leaf-a"));
61 assertNotNull(testLeafA);
63 final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.dataChildByName(
64 QName.create(module.getQNameModule(), "test-container-b"));
65 assertNull(testContainerB);
67 final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
68 QName.create(module.getQNameModule(), "test-container-c"));
69 assertNotNull(testContainerC);
70 final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
71 QName.create(module.getQNameModule(), "test-leaf-c"));
72 assertNotNull(testLeafC);
76 public void testSharedSchemaRepositoryWithAllFeaturesSupported() throws Exception {
77 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
78 "shared-schema-repo-with-features-test");
80 final SettableSchemaProvider<IRSchemaSource> foobar = getImmediateYangSourceProviderFromResource(
81 "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
82 foobar.register(sharedSchemaRepository);
85 final EffectiveModelContextFactory fact = sharedSchemaRepository.createEffectiveModelContextFactory();
86 final ListenableFuture<EffectiveModelContext> testSchemaContextFuture =
87 fact.createEffectiveModelContext(foobar.getId());
88 assertTrue(testSchemaContextFuture.isDone());
89 assertSchemaContext(testSchemaContextFuture.get(), 1);
91 final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
92 assertNotNull(module);
93 assertEquals(3, module.getChildNodes().size());
95 final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
96 QName.create(module.getQNameModule(), "test-container-a"));
97 assertNotNull(testContainerA);
98 final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
99 QName.create(module.getQNameModule(), "test-leaf-a"));
100 assertNotNull(testLeafA);
102 final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
103 QName.create(module.getQNameModule(), "test-container-b"));
104 assertNotNull(testContainerB);
105 final LeafSchemaNode testLeafB = (LeafSchemaNode) testContainerB.getDataChildByName(
106 QName.create(module.getQNameModule(), "test-leaf-b"));
107 assertNotNull(testLeafB);
109 final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
110 QName.create(module.getQNameModule(), "test-container-c"));
111 assertNotNull(testContainerC);
112 final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
113 QName.create(module.getQNameModule(), "test-leaf-c"));
114 assertNotNull(testLeafC);
118 public void testSharedSchemaRepositoryWithNoFeaturesSupported() throws Exception {
119 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
120 "shared-schema-repo-with-features-test");
122 final SettableSchemaProvider<IRSchemaSource> foobar = getImmediateYangSourceProviderFromResource(
123 "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
124 foobar.register(sharedSchemaRepository);
127 final ListenableFuture<EffectiveModelContext> testSchemaContextFuture =
128 sharedSchemaRepository.createEffectiveModelContextFactory(
129 SchemaContextFactoryConfiguration.builder().setSupportedFeatures(ImmutableSet.of()).build())
130 .createEffectiveModelContext(foobar.getId());
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<IRSchemaSource> getImmediateYangSourceProviderFromResource(
147 final String resourceName) throws Exception {
148 final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
149 return SettableSchemaProvider.createImmediate(TextToIRTransformer.transformText(yangSource),
150 IRSchemaSource.class);
153 private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
154 assertNotNull(schemaContext);
155 assertEquals(moduleSize, schemaContext.getModules().size());