Remove deprecated SchemaRepository.createSchemaContextFactory()
[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
9 package org.opendaylight.yangtools.yang.parser.repo;
10
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;
15
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import java.util.Set;
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.YangTextSchemaSource;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
29 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToASTTransformer;
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<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource(
41             "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
42         foobar.register(sharedSchemaRepository);
43         foobar.setResult();
44
45         final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory();
46         final ListenableFuture<SchemaContext> testSchemaContextFuture =
47                 fact.createSchemaContext(ImmutableList.of(foobar.getId()), supportedFeatures);
48         assertTrue(testSchemaContextFuture.isDone());
49         assertSchemaContext(testSchemaContextFuture.get(), 1);
50
51         final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
52         assertNotNull(module);
53         assertEquals(2, module.getChildNodes().size());
54
55         final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
56                 QName.create(module.getQNameModule(), "test-container-a"));
57         assertNotNull(testContainerA);
58         final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
59                 QName.create(module.getQNameModule(), "test-leaf-a"));
60         assertNotNull(testLeafA);
61
62         final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
63                 QName.create(module.getQNameModule(), "test-container-b"));
64         assertNull(testContainerB);
65
66         final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
67                 QName.create(module.getQNameModule(), "test-container-c"));
68         assertNotNull(testContainerC);
69         final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
70                 QName.create(module.getQNameModule(), "test-leaf-c"));
71         assertNotNull(testLeafC);
72     }
73
74     @Test
75     public void testSharedSchemaRepositoryWithAllFeaturesSupported() throws Exception {
76         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
77                 "shared-schema-repo-with-features-test");
78
79         final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource(
80             "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
81         foobar.register(sharedSchemaRepository);
82         foobar.setResult();
83
84         final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory();
85         final ListenableFuture<SchemaContext> testSchemaContextFuture =
86                 fact.createSchemaContext(ImmutableList.of(foobar.getId()));
87         assertTrue(testSchemaContextFuture.isDone());
88         assertSchemaContext(testSchemaContextFuture.get(), 1);
89
90         final Module module = testSchemaContextFuture.get().findModules("foobar").iterator().next();
91         assertNotNull(module);
92         assertEquals(3, module.getChildNodes().size());
93
94         final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
95                 QName.create(module.getQNameModule(), "test-container-a"));
96         assertNotNull(testContainerA);
97         final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
98                 QName.create(module.getQNameModule(), "test-leaf-a"));
99         assertNotNull(testLeafA);
100
101         final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
102                 QName.create(module.getQNameModule(), "test-container-b"));
103         assertNotNull(testContainerB);
104         final LeafSchemaNode testLeafB = (LeafSchemaNode) testContainerB.getDataChildByName(
105                 QName.create(module.getQNameModule(), "test-leaf-b"));
106         assertNotNull(testLeafB);
107
108         final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
109                 QName.create(module.getQNameModule(), "test-container-c"));
110         assertNotNull(testContainerC);
111         final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
112                 QName.create(module.getQNameModule(), "test-leaf-c"));
113         assertNotNull(testLeafC);
114     }
115
116     @Test
117     public void testSharedSchemaRepositoryWithNoFeaturesSupported() throws Exception {
118         final Set<QName> supportedFeatures = ImmutableSet.of();
119
120         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
121                 "shared-schema-repo-with-features-test");
122
123         final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource(
124             "/if-feature-resolution-test/shared-schema-repository/foobar.yang");
125         foobar.register(sharedSchemaRepository);
126         foobar.setResult();
127
128         final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory();
129         final ListenableFuture<SchemaContext> testSchemaContextFuture =
130                 fact.createSchemaContext(ImmutableList.of(foobar.getId()), supportedFeatures);
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<ASTSchemaSource> getImmediateYangSourceProviderFromResource(
147             final String resourceName) throws Exception {
148         final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
149         return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
150             ASTSchemaSource.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 }