Promote SchemaSourceRepresentation
[yangtools.git] / parser / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / SharedSchemaRepositoryWithFeaturesTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.repo;
9
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;
15
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;
26
27 public class SharedSchemaRepositoryWithFeaturesTest {
28     @Test
29     public void testSharedSchemaRepositoryWithSomeFeaturesSupported() throws Exception {
30         final var supportedFeatures = FeatureSet.of(QName.create("foobar-namespace", "test-feature-1"));
31
32         final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-with-features-test");
33
34         final var foobar = getImmediateYangSourceProviderFromResource(
35             "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
36         foobar.register(sharedSchemaRepository);
37         foobar.setResult();
38
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);
44
45         final var module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
46         assertNotNull(module);
47         assertEquals(2, module.getChildNodes().size());
48
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")));
53
54         assertNull(module.dataChildByName(QName.create(module.getQNameModule(), "test-container-b")));
55
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")));
60     }
61
62     @Test
63     public void testSharedSchemaRepositoryWithAllFeaturesSupported() throws Exception {
64         final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-with-features-test");
65
66         final var foobar = getImmediateYangSourceProviderFromResource(
67             "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
68         foobar.register(sharedSchemaRepository);
69         foobar.setResult();
70
71         final var fact = sharedSchemaRepository.createEffectiveModelContextFactory();
72         final var testSchemaContextFuture = fact.createEffectiveModelContext(foobar.getId());
73         assertTrue(testSchemaContextFuture.isDone());
74         assertSchemaContext(testSchemaContextFuture.get(), 1);
75
76         final var module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
77         assertNotNull(module);
78         assertEquals(3, module.getChildNodes().size());
79
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")));
84
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")));
89
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")));
94     }
95
96     @Test
97     public void testSharedSchemaRepositoryWithNoFeaturesSupported() throws Exception {
98         final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-with-features-test");
99
100         final var foobar = getImmediateYangSourceProviderFromResource(
101             "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
102         foobar.register(sharedSchemaRepository);
103         foobar.setResult();
104
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);
110
111         final var module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
112         assertNotNull(module);
113         assertEquals(1, module.getChildNodes().size());
114
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")));
119     }
120
121     private static SettableSchemaProvider<YangIRSchemaSource> getImmediateYangSourceProviderFromResource(
122             final String resourceName) throws Exception {
123         return SettableSchemaProvider.createImmediate(
124             TextToIRTransformer.transformText(YangTextSource.forResource(resourceName)), YangIRSchemaSource.class);
125     }
126
127     private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
128         assertNotNull(schemaContext);
129         assertEquals(moduleSize, schemaContext.getModules().size());
130     }
131 }