Replace calls of StmtTestUtils.parseYangSource(String)
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / Bug6868Test.java
1 /*
2  * Copyright (c) 2017 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.stmt.rfc7950;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15
16 import com.google.common.collect.ImmutableSet;
17 import com.google.common.collect.Sets;
18 import java.util.HashSet;
19 import java.util.Optional;
20 import java.util.Set;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
26 import org.opendaylight.yangtools.yang.stmt.AbstractYangTest;
27 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
28
29 public class Bug6868Test extends AbstractYangTest {
30     private static final String FOO_NS = "foo";
31     private static final String IMP_NS = "imp";
32     private static final String IMP_REV = "2017-01-09";
33     private static final Set<String> ALL_CONTAINERS = ImmutableSet.of("my-container-1", "my-container-2",
34             "my-container-3", "foo", "not-foo", "imp-bar", "imp-bar-2");
35
36     @Test
37     public void ifFeatureYang11ResolutionTest() throws Exception {
38         assertSchemaContextFor(null, ALL_CONTAINERS);
39         assertSchemaContextFor(ImmutableSet.of(), ImmutableSet.of("my-container-1", "my-container-2", "not-foo"));
40         assertSchemaContextFor(ImmutableSet.of("foo"), ImmutableSet.of("foo"));
41         assertSchemaContextFor(ImmutableSet.of("baz"),
42                 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "not-foo"));
43         assertSchemaContextFor(ImmutableSet.of("bar", "baz"),
44                 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "not-foo"));
45         assertSchemaContextFor(ImmutableSet.of("foo", "bar", "baz"),
46                 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "foo"));
47         assertSchemaContextFor(ImmutableSet.of("foo", "bar", "baz", "imp:bar"),
48                 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "foo", "imp-bar"));
49         assertSchemaContextFor(ImmutableSet.of("foo", "baz", "imp:bar"),
50             ImmutableSet.of("foo", "imp-bar", "imp-bar-2"));
51     }
52
53     private static void assertSchemaContextFor(final Set<String> supportedFeatures,
54             final Set<String> expectedContainers) throws Exception {
55         final SchemaContext schemaContext = StmtTestUtils.parseYangSources("/rfc7950/bug6868/yang11",
56             supportedFeatures != null ? createFeaturesSet(supportedFeatures) : null, YangParserConfiguration.DEFAULT);
57         assertNotNull(schemaContext);
58
59         for (final String expectedContainer : expectedContainers) {
60             assertThat(String.format("Expected container %s not found.", expectedContainer),
61                     schemaContext.findDataTreeChild(QName.create(FOO_NS, expectedContainer)).get(),
62                     instanceOf(ContainerSchemaNode.class));
63         }
64
65         final Set<String> unexpectedContainers = Sets.difference(ALL_CONTAINERS, expectedContainers);
66         for (final String unexpectedContainer : unexpectedContainers) {
67             assertEquals(String.format("Unexpected container %s.", unexpectedContainer), Optional.empty(),
68                     schemaContext.findDataTreeChild(QName.create(FOO_NS, unexpectedContainer)));
69         }
70     }
71
72     private static Set<QName> createFeaturesSet(final Set<String> featureNames) {
73         final Set<QName> supportedFeatures = new HashSet<>();
74         for (final String featureName : featureNames) {
75             if (featureName.indexOf(':') == -1) {
76                 supportedFeatures.add(QName.create(FOO_NS, featureName));
77             } else {
78                 supportedFeatures
79                         .add(QName.create(IMP_NS, IMP_REV, featureName.substring(featureName.indexOf(':') + 1)));
80             }
81         }
82
83         return ImmutableSet.copyOf(supportedFeatures);
84     }
85
86     @Test
87     public void invalidYang10Test() {
88         assertSourceException(startsWith("Invalid identifier '(not foo) or (bar and baz)' [at "),
89                 "/rfc7950/bug6868/invalid10.yang");
90     }
91 }