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