Add utility YangTextSchemaSource factories
[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.ImmutableSet;
17 import com.google.common.collect.Lists;
18 import com.google.common.util.concurrent.CheckedFuture;
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.SchemaResolutionException;
28 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
29 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
30 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
31 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
32
33 public class SharedSchemaRepositoryWithFeaturesTest {
34
35     @Test
36     public void testSharedSchemaRepositoryWithSomeFeaturesSupported() throws Exception {
37         final Set<QName> supportedFeatures = ImmutableSet.of(QName.create("foobar-namespace", "1970-01-01", "test-feature-1"));
38
39         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
40                 "shared-schema-repo-with-features-test");
41
42         final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
43                 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
44         foobar.register(sharedSchemaRepository);
45         foobar.setResult();
46
47         final SchemaContextFactory fact = sharedSchemaRepository
48                 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
49
50         final CheckedFuture<SchemaContext, SchemaResolutionException> testSchemaContextFuture =
51                 fact.createSchemaContext(Lists.newArrayList(foobar.getId()), supportedFeatures);
52         assertTrue(testSchemaContextFuture.isDone());
53         assertSchemaContext(testSchemaContextFuture.checkedGet(), 1);
54
55         final Module module = testSchemaContextFuture.checkedGet().findModuleByName("foobar", null);
56         assertNotNull(module);
57         assertEquals(2, module.getChildNodes().size());
58
59         final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
60                 QName.create(module.getQNameModule(), "test-container-a"));
61         assertNotNull(testContainerA);
62         final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
63                 QName.create(module.getQNameModule(), "test-leaf-a"));
64         assertNotNull(testLeafA);
65
66         final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
67                 QName.create(module.getQNameModule(), "test-container-b"));
68         assertNull(testContainerB);
69
70         final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
71                 QName.create(module.getQNameModule(), "test-container-c"));
72         assertNotNull(testContainerC);
73         final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
74                 QName.create(module.getQNameModule(), "test-leaf-c"));
75         assertNotNull(testLeafC);
76     }
77
78     @Test
79     public void testSharedSchemaRepositoryWithAllFeaturesSupported() throws Exception {
80         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
81                 "shared-schema-repo-with-features-test");
82
83         final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
84                 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
85         foobar.register(sharedSchemaRepository);
86         foobar.setResult();
87
88         final SchemaContextFactory fact = sharedSchemaRepository
89                 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
90
91         final CheckedFuture<SchemaContext, SchemaResolutionException> testSchemaContextFuture = fact
92                 .createSchemaContext(Lists.newArrayList(foobar.getId()));
93         assertTrue(testSchemaContextFuture.isDone());
94         assertSchemaContext(testSchemaContextFuture.checkedGet(), 1);
95
96         final Module module = testSchemaContextFuture.checkedGet().findModuleByName("foobar", null);
97         assertNotNull(module);
98         assertEquals(3, module.getChildNodes().size());
99
100         final ContainerSchemaNode testContainerA = (ContainerSchemaNode) module.getDataChildByName(
101                 QName.create(module.getQNameModule(), "test-container-a"));
102         assertNotNull(testContainerA);
103         final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
104                 QName.create(module.getQNameModule(), "test-leaf-a"));
105         assertNotNull(testLeafA);
106
107         final ContainerSchemaNode testContainerB = (ContainerSchemaNode) module.getDataChildByName(
108                 QName.create(module.getQNameModule(), "test-container-b"));
109         assertNotNull(testContainerB);
110         final LeafSchemaNode testLeafB = (LeafSchemaNode) testContainerB.getDataChildByName(
111                 QName.create(module.getQNameModule(), "test-leaf-b"));
112         assertNotNull(testLeafB);
113
114         final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
115                 QName.create(module.getQNameModule(), "test-container-c"));
116         assertNotNull(testContainerC);
117         final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
118                 QName.create(module.getQNameModule(), "test-leaf-c"));
119         assertNotNull(testLeafC);
120     }
121
122     @Test
123     public void testSharedSchemaRepositoryWithNoFeaturesSupported() throws Exception {
124         final Set<QName> supportedFeatures = ImmutableSet.of();
125
126         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
127                 "shared-schema-repo-with-features-test");
128
129         final SettableSchemaProvider<ASTSchemaSource> foobar = getImmediateYangSourceProviderFromResource
130                 ("/if-feature-resolution-test/shared-schema-repository/foobar.yang");
131         foobar.register(sharedSchemaRepository);
132         foobar.setResult();
133
134         final SchemaContextFactory fact = sharedSchemaRepository
135                 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
136
137         final CheckedFuture<SchemaContext, SchemaResolutionException> testSchemaContextFuture = fact
138                 .createSchemaContext(Lists.newArrayList(foobar.getId()), supportedFeatures);
139         assertTrue(testSchemaContextFuture.isDone());
140         assertSchemaContext(testSchemaContextFuture.checkedGet(), 1);
141
142         final Module module = testSchemaContextFuture.checkedGet().findModuleByName("foobar", null);
143         assertNotNull(module);
144         assertEquals(1, module.getChildNodes().size());
145
146         final ContainerSchemaNode testContainerC = (ContainerSchemaNode) module.getDataChildByName(
147                 QName.create(module.getQNameModule(), "test-container-c"));
148         assertNotNull(testContainerC);
149         final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
150                 QName.create(module.getQNameModule(), "test-leaf-c"));
151         assertNotNull(testLeafC);
152     }
153
154     private static SettableSchemaProvider<ASTSchemaSource> getImmediateYangSourceProviderFromResource(
155             final String resourceName) throws Exception {
156         final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
157         return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
158             ASTSchemaSource.class);
159     }
160
161     private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
162         assertNotNull(schemaContext);
163         assertEquals(moduleSize, schemaContext.getModules().size());
164     }
165 }