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.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
13 import static org.junit.jupiter.api.Assertions.assertNull;
14 import static org.junit.jupiter.api.Assertions.assertTrue;
16 import org.junit.jupiter.api.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.ir.YangIRSchemaSource;
19 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
24 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
27 public class SharedSchemaRepositoryWithFeaturesTest {
29 public void testSharedSchemaRepositoryWithSomeFeaturesSupported() throws Exception {
30 final var supportedFeatures = FeatureSet.of(QName.create("foobar-namespace", "test-feature-1"));
32 final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-with-features-test");
34 final var foobar = getImmediateYangSourceProviderFromResource(
35 "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
36 foobar.register(sharedSchemaRepository);
39 final var testSchemaContextFuture = sharedSchemaRepository.createEffectiveModelContextFactory(
40 SchemaContextFactoryConfiguration.builder().setSupportedFeatures(supportedFeatures).build())
41 .createEffectiveModelContext(foobar.getId());
42 assertTrue(testSchemaContextFuture.isDone());
43 assertSchemaContext(testSchemaContextFuture.get(), 1);
45 final var module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
46 assertNotNull(module);
47 assertEquals(2, module.getChildNodes().size());
49 final var testContainerA = assertInstanceOf(ContainerSchemaNode.class,
50 module.dataChildByName(QName.create(module.getQNameModule(), "test-container-a")));
51 assertInstanceOf(LeafSchemaNode.class,
52 testContainerA.dataChildByName(QName.create(module.getQNameModule(), "test-leaf-a")));
54 assertNull(module.dataChildByName(QName.create(module.getQNameModule(), "test-container-b")));
56 final var testContainerC = assertInstanceOf(ContainerSchemaNode.class,
57 module.dataChildByName(QName.create(module.getQNameModule(), "test-container-c")));
58 assertInstanceOf(LeafSchemaNode.class,
59 testContainerC.dataChildByName(QName.create(module.getQNameModule(), "test-leaf-c")));
63 public void testSharedSchemaRepositoryWithAllFeaturesSupported() throws Exception {
64 final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-with-features-test");
66 final var foobar = getImmediateYangSourceProviderFromResource(
67 "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
68 foobar.register(sharedSchemaRepository);
71 final var fact = sharedSchemaRepository.createEffectiveModelContextFactory();
72 final var testSchemaContextFuture = fact.createEffectiveModelContext(foobar.getId());
73 assertTrue(testSchemaContextFuture.isDone());
74 assertSchemaContext(testSchemaContextFuture.get(), 1);
76 final var module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
77 assertNotNull(module);
78 assertEquals(3, module.getChildNodes().size());
80 final var testContainerA = assertInstanceOf(ContainerSchemaNode.class,
81 module.dataChildByName(QName.create(module.getQNameModule(), "test-container-a")));
82 assertInstanceOf(LeafSchemaNode.class,
83 testContainerA.dataChildByName(QName.create(module.getQNameModule(), "test-leaf-a")));
85 final var testContainerB = assertInstanceOf(ContainerSchemaNode.class,
86 module.dataChildByName(QName.create(module.getQNameModule(), "test-container-b")));
87 assertInstanceOf(LeafSchemaNode.class,
88 testContainerB.dataChildByName(QName.create(module.getQNameModule(), "test-leaf-b")));
90 final var testContainerC = assertInstanceOf(ContainerSchemaNode.class,
91 module.dataChildByName(QName.create(module.getQNameModule(), "test-container-c")));
92 assertInstanceOf(LeafSchemaNode.class,
93 testContainerC.dataChildByName(QName.create(module.getQNameModule(), "test-leaf-c")));
97 public void testSharedSchemaRepositoryWithNoFeaturesSupported() throws Exception {
98 final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-with-features-test");
100 final var foobar = getImmediateYangSourceProviderFromResource(
101 "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
102 foobar.register(sharedSchemaRepository);
105 final var testSchemaContextFuture = sharedSchemaRepository.createEffectiveModelContextFactory(
106 SchemaContextFactoryConfiguration.builder().setSupportedFeatures(FeatureSet.of()).build())
107 .createEffectiveModelContext(foobar.getId());
108 assertTrue(testSchemaContextFuture.isDone());
109 assertSchemaContext(testSchemaContextFuture.get(), 1);
111 final var module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
112 assertNotNull(module);
113 assertEquals(1, module.getChildNodes().size());
115 final var testContainerC = assertInstanceOf(ContainerSchemaNode.class,
116 module.dataChildByName(QName.create(module.getQNameModule(), "test-container-c")));
117 assertInstanceOf(LeafSchemaNode.class,
118 testContainerC.dataChildByName(QName.create(module.getQNameModule(), "test-leaf-c")));
121 private static SettableSchemaProvider<YangIRSchemaSource> getImmediateYangSourceProviderFromResource(
122 final String resourceName) throws Exception {
123 return SettableSchemaProvider.createImmediate(
124 TextToIRTransformer.transformText(YangTextSource.forResource(resourceName)), YangIRSchemaSource.class);
127 private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
128 assertNotNull(schemaContext);
129 assertEquals(moduleSize, schemaContext.getModules().size());