Split out YangIRSchemaSource
[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.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.collect.ImmutableSet;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.util.Set;
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.YangIRSchemaSource;
28 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
29 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
30
31 public class SharedSchemaRepositoryWithFeaturesTest {
32
33     @Test
34     public void testSharedSchemaRepositoryWithSomeFeaturesSupported() throws Exception {
35         final Set<QName> supportedFeatures = ImmutableSet.of(QName.create("foobar-namespace", "test-feature-1"));
36
37         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
38                 "shared-schema-repo-with-features-test");
39
40         final SettableSchemaProvider<YangIRSchemaSource> foobar = getImmediateYangSourceProviderFromResource(
41             "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
42         foobar.register(sharedSchemaRepository);
43         foobar.setResult();
44
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);
51
52         final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
53         assertNotNull(module);
54         assertEquals(2, module.getChildNodes().size());
55
56         final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
57                 QName.create(module.getQNameModule(), "test-container-a"));
58         final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
59                 QName.create(module.getQNameModule(), "test-leaf-a"));
60
61         final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.dataChildByName(
62                 QName.create(module.getQNameModule(), "test-container-b"));
63         assertNull(testContainerB);
64
65         final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
66                 QName.create(module.getQNameModule(), "test-container-c"));
67         final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
68                 QName.create(module.getQNameModule(), "test-leaf-c"));
69     }
70
71     @Test
72     public void testSharedSchemaRepositoryWithAllFeaturesSupported() throws Exception {
73         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
74                 "shared-schema-repo-with-features-test");
75
76         final SettableSchemaProvider<YangIRSchemaSource> foobar = getImmediateYangSourceProviderFromResource(
77             "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
78         foobar.register(sharedSchemaRepository);
79         foobar.setResult();
80
81         final EffectiveModelContextFactory fact = sharedSchemaRepository.createEffectiveModelContextFactory();
82         final ListenableFuture<EffectiveModelContext> testSchemaContextFuture =
83                 fact.createEffectiveModelContext(foobar.getId());
84         assertTrue(testSchemaContextFuture.isDone());
85         assertSchemaContext(testSchemaContextFuture.get(), 1);
86
87         final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
88         assertNotNull(module);
89         assertEquals(3, module.getChildNodes().size());
90
91         final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
92                 QName.create(module.getQNameModule(), "test-container-a"));
93         final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
94                 QName.create(module.getQNameModule(), "test-leaf-a"));
95
96         final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
97                 QName.create(module.getQNameModule(), "test-container-b"));
98         final LeafSchemaNode testLeafB = (LeafSchemaNode) testContainerB.getDataChildByName(
99                 QName.create(module.getQNameModule(), "test-leaf-b"));
100
101         final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
102                 QName.create(module.getQNameModule(), "test-container-c"));
103         final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
104                 QName.create(module.getQNameModule(), "test-leaf-c"));
105     }
106
107     @Test
108     public void testSharedSchemaRepositoryWithNoFeaturesSupported() throws Exception {
109         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
110                 "shared-schema-repo-with-features-test");
111
112         final SettableSchemaProvider<YangIRSchemaSource> foobar = getImmediateYangSourceProviderFromResource(
113             "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
114         foobar.register(sharedSchemaRepository);
115         foobar.setResult();
116
117         final ListenableFuture<EffectiveModelContext> testSchemaContextFuture =
118                 sharedSchemaRepository.createEffectiveModelContextFactory(
119                     SchemaContextFactoryConfiguration.builder().setSupportedFeatures(ImmutableSet.of()).build())
120                 .createEffectiveModelContext(foobar.getId());
121         assertTrue(testSchemaContextFuture.isDone());
122         assertSchemaContext(testSchemaContextFuture.get(), 1);
123
124         final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
125         assertNotNull(module);
126         assertEquals(1, module.getChildNodes().size());
127
128         final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
129                 QName.create(module.getQNameModule(), "test-container-c"));
130         final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
131                 QName.create(module.getQNameModule(), "test-leaf-c"));
132     }
133
134     private static SettableSchemaProvider<YangIRSchemaSource> getImmediateYangSourceProviderFromResource(
135             final String resourceName) throws Exception {
136         final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
137         return SettableSchemaProvider.createImmediate(TextToIRTransformer.transformText(yangSource),
138             YangIRSchemaSource.class);
139     }
140
141     private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
142         assertNotNull(schemaContext);
143         assertEquals(moduleSize, schemaContext.getModules().size());
144     }
145 }