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.SchemaSourceFilter;
28 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
29 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
30 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
32 public class SharedSchemaRepositoryWithFeaturesTest {
35 public void testSharedSchemaRepositoryWithSomeFeaturesSupported() throws Exception {
36 final Set<QName> supportedFeatures = ImmutableSet.of(QName.create("foobar-namespace", "1970-01-01", "test-feature-1"));
38 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
39 "shared-schema-repo-with-features-test");
41 final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
42 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
43 foobar.register(sharedSchemaRepository);
46 final SchemaContextFactory fact = sharedSchemaRepository
47 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
49 final ListenableFuture<SchemaContext> testSchemaContextFuture =
50 fact.createSchemaContext(ImmutableList.of(foobar.getId()), supportedFeatures);
51 assertTrue(testSchemaContextFuture.isDone());
52 assertSchemaContext(testSchemaContextFuture.get(), 1);
54 final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
55 assertNotNull(module);
56 assertEquals(2, module.getChildNodes().size());
58 final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
59 QName.create(module.getQNameModule(), "test-container-a"));
60 assertNotNull(testContainerA);
61 final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
62 QName.create(module.getQNameModule(), "test-leaf-a"));
63 assertNotNull(testLeafA);
65 final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
66 QName.create(module.getQNameModule(), "test-container-b"));
67 assertNull(testContainerB);
69 final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
70 QName.create(module.getQNameModule(), "test-container-c"));
71 assertNotNull(testContainerC);
72 final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
73 QName.create(module.getQNameModule(), "test-leaf-c"));
74 assertNotNull(testLeafC);
78 public void testSharedSchemaRepositoryWithAllFeaturesSupported() throws Exception {
79 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
80 "shared-schema-repo-with-features-test");
82 final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
83 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
84 foobar.register(sharedSchemaRepository);
87 final SchemaContextFactory fact = sharedSchemaRepository
88 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
90 final ListenableFuture<SchemaContext> testSchemaContextFuture =
91 fact.createSchemaContext(ImmutableList.of(foobar.getId()));
92 assertTrue(testSchemaContextFuture.isDone());
93 assertSchemaContext(testSchemaContextFuture.get(), 1);
95 final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
96 assertNotNull(module);
97 assertEquals(3, module.getChildNodes().size());
99 final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
100 QName.create(module.getQNameModule(), "test-container-a"));
101 assertNotNull(testContainerA);
102 final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
103 QName.create(module.getQNameModule(), "test-leaf-a"));
104 assertNotNull(testLeafA);
106 final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
107 QName.create(module.getQNameModule(), "test-container-b"));
108 assertNotNull(testContainerB);
109 final LeafSchemaNode testLeafB = (LeafSchemaNode) testContainerB.getDataChildByName(
110 QName.create(module.getQNameModule(), "test-leaf-b"));
111 assertNotNull(testLeafB);
113 final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
114 QName.create(module.getQNameModule(), "test-container-c"));
115 assertNotNull(testContainerC);
116 final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
117 QName.create(module.getQNameModule(), "test-leaf-c"));
118 assertNotNull(testLeafC);
122 public void testSharedSchemaRepositoryWithNoFeaturesSupported() throws Exception {
123 final Set<QName> supportedFeatures = ImmutableSet.of();
125 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
126 "shared-schema-repo-with-features-test");
128 final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
129 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
130 foobar.register(sharedSchemaRepository);
133 final SchemaContextFactory fact = sharedSchemaRepository
134 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
136 final ListenableFuture<SchemaContext> testSchemaContextFuture =
137 fact.createSchemaContext(ImmutableList.of(foobar.getId()), supportedFeatures);
138 assertTrue(testSchemaContextFuture.isDone());
139 assertSchemaContext(testSchemaContextFuture.get(), 1);
141 final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
142 assertNotNull(module);
143 assertEquals(1, module.getChildNodes().size());
145 final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
146 QName.create(module.getQNameModule(), "test-container-c"));
147 assertNotNull(testContainerC);
148 final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
149 QName.create(module.getQNameModule(), "test-leaf-c"));
150 assertNotNull(testLeafC);
153 private static SettableSchemaProvider<ASTSchemaSource> getImmediateYangSourceProviderFromResource(
154 final String resourceName) throws Exception {
155 final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
156 return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
157 ASTSchemaSource.class);
160 private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
161 assertNotNull(schemaContext);
162 assertEquals(moduleSize, schemaContext.getModules().size());