Further yang-parser-impl checkstyle fixes
[yangtools.git] / yang / yang-parser-impl / 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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import com.google.common.collect.ImmutableSet;
17 import com.google.common.collect.Sets;
18 import java.util.HashSet;
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.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
27 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
29 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
30
31 public class Bug6868Test {
32     private static final String FOO_NS = "foo";
33     private static final String FOO_REV = "1970-01-01";
34     private static final String IMP_NS = "imp";
35     private static final String IMP_REV = "2017-01-09";
36     private static final Set<String> ALL_CONTAINERS = ImmutableSet.of("my-container-1", "my-container-2",
37             "my-container-3", "foo", "not-foo", "imp-bar", "imp-bar-2");
38
39     @Test
40     public void ifFeatureYang11ResolutionTest() throws Exception {
41         assertSchemaContextFor(null, ALL_CONTAINERS);
42         assertSchemaContextFor(ImmutableSet.of(), ImmutableSet.of("my-container-1", "my-container-2", "not-foo"));
43         assertSchemaContextFor(ImmutableSet.of("foo"), ImmutableSet.of("foo"));
44         assertSchemaContextFor(ImmutableSet.of("baz"),
45                 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "not-foo"));
46         assertSchemaContextFor(ImmutableSet.of("bar", "baz"),
47                 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "not-foo"));
48         assertSchemaContextFor(ImmutableSet.of("foo", "bar", "baz"),
49                 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "foo"));
50         assertSchemaContextFor(ImmutableSet.of("foo", "bar", "baz", "imp:bar"),
51                 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "foo", "imp-bar"));
52         assertSchemaContextFor(ImmutableSet.of("foo", "baz", "imp:bar"),
53             ImmutableSet.of("foo", "imp-bar", "imp-bar-2"));
54     }
55
56     private static void assertSchemaContextFor(final Set<String> supportedFeatures,
57             final Set<String> expectedContainers) throws Exception {
58         final SchemaContext schemaContext = StmtTestUtils.parseYangSources("/rfc7950/bug6868/yang11",
59                 supportedFeatures != null ? createFeaturesSet(supportedFeatures) : null,
60                 StatementParserMode.DEFAULT_MODE);
61         assertNotNull(schemaContext);
62
63         for (final String expectedContainer : expectedContainers) {
64             assertTrue(String.format("Expected container %s not found.", expectedContainer),
65                     findNode(schemaContext, expectedContainer) instanceof ContainerSchemaNode);
66         }
67
68         final Set<String> unexpectedContainers = Sets.difference(ALL_CONTAINERS, expectedContainers);
69         for (final String unexpectedContainer : unexpectedContainers) {
70             assertNull(String.format("Unexpected container %s.", unexpectedContainer),
71                     findNode(schemaContext, unexpectedContainer));
72         }
73     }
74
75     private static Set<QName> createFeaturesSet(final Set<String> featureNames) {
76         final Set<QName> supportedFeatures = new HashSet<>();
77         for (final String featureName : featureNames) {
78             if (featureName.indexOf(':') == -1) {
79                 supportedFeatures.add(QName.create(FOO_NS, FOO_REV, featureName));
80             } else {
81                 supportedFeatures
82                         .add(QName.create(IMP_NS, IMP_REV, featureName.substring(featureName.indexOf(':') + 1)));
83             }
84         }
85
86         return ImmutableSet.copyOf(supportedFeatures);
87     }
88
89     private static SchemaNode findNode(final SchemaContext context, final String localName) {
90         return SchemaContextUtil.findDataSchemaNode(context,
91                 SchemaPath.create(true, QName.create(FOO_NS, FOO_REV, localName)));
92     }
93
94     @Test
95     public void invalidYang10Test() throws Exception {
96         try {
97             StmtTestUtils.parseYangSource("/rfc7950/bug6868/invalid10.yang");
98             fail("Test should fail due to invalid Yang 1.0");
99         } catch (final SomeModifiersUnresolvedException e) {
100             assertTrue(e.getCause().getMessage()
101                     .startsWith("Parameter 'localName':'(not foo) or (bar and baz)' contains illegal character '('"));
102         }
103     }
104 }