Migrate getDataChildByName() users
[yangtools.git] / yang / 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.YangTextSchemaSource;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
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<IRSchemaSource> 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         assertNotNull(testContainerA);
59         final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
60                 QName.create(module.getQNameModule(), "test-leaf-a"));
61         assertNotNull(testLeafA);
62
63         final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.dataChildByName(
64                 QName.create(module.getQNameModule(), "test-container-b"));
65         assertNull(testContainerB);
66
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);
73     }
74
75     @Test
76     public void testSharedSchemaRepositoryWithAllFeaturesSupported() throws Exception {
77         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
78                 "shared-schema-repo-with-features-test");
79
80         final SettableSchemaProvider<IRSchemaSource> foobar = getImmediateYangSourceProviderFromResource(
81             "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
82         foobar.register(sharedSchemaRepository);
83         foobar.setResult();
84
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);
90
91         final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
92         assertNotNull(module);
93         assertEquals(3, module.getChildNodes().size());
94
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);
101
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);
108
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);
115     }
116
117     @Test
118     public void testSharedSchemaRepositoryWithNoFeaturesSupported() throws Exception {
119         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
120                 "shared-schema-repo-with-features-test");
121
122         final SettableSchemaProvider<IRSchemaSource> foobar = getImmediateYangSourceProviderFromResource(
123             "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
124         foobar.register(sharedSchemaRepository);
125         foobar.setResult();
126
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);
133
134         final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
135         assertNotNull(module);
136         assertEquals(1, module.getChildNodes().size());
137
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);
144     }
145
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);
151     }
152
153     private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
154         assertNotNull(schemaContext);
155         assertEquals(moduleSize, schemaContext.getModules().size());
156     }
157 }