a10faff04bae3c01132cf9b93edbcdf45858be3e
[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 IMP_NS = "imp";
34     private static final String IMP_REV = "2017-01-09";
35     private static final Set<String> ALL_CONTAINERS = ImmutableSet.of("my-container-1", "my-container-2",
36             "my-container-3", "foo", "not-foo", "imp-bar", "imp-bar-2");
37
38     @Test
39     public void ifFeatureYang11ResolutionTest() throws Exception {
40         assertSchemaContextFor(null, ALL_CONTAINERS);
41         assertSchemaContextFor(ImmutableSet.of(), ImmutableSet.of("my-container-1", "my-container-2", "not-foo"));
42         assertSchemaContextFor(ImmutableSet.of("foo"), ImmutableSet.of("foo"));
43         assertSchemaContextFor(ImmutableSet.of("baz"),
44                 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "not-foo"));
45         assertSchemaContextFor(ImmutableSet.of("bar", "baz"),
46                 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "not-foo"));
47         assertSchemaContextFor(ImmutableSet.of("foo", "bar", "baz"),
48                 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "foo"));
49         assertSchemaContextFor(ImmutableSet.of("foo", "bar", "baz", "imp:bar"),
50                 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "foo", "imp-bar"));
51         assertSchemaContextFor(ImmutableSet.of("foo", "baz", "imp:bar"),
52             ImmutableSet.of("foo", "imp-bar", "imp-bar-2"));
53     }
54
55     private static void assertSchemaContextFor(final Set<String> supportedFeatures,
56             final Set<String> expectedContainers) throws Exception {
57         final SchemaContext schemaContext = StmtTestUtils.parseYangSources("/rfc7950/bug6868/yang11",
58                 supportedFeatures != null ? createFeaturesSet(supportedFeatures) : null,
59                 StatementParserMode.DEFAULT_MODE);
60         assertNotNull(schemaContext);
61
62         for (final String expectedContainer : expectedContainers) {
63             assertTrue(String.format("Expected container %s not found.", expectedContainer),
64                     findNode(schemaContext, expectedContainer) instanceof ContainerSchemaNode);
65         }
66
67         final Set<String> unexpectedContainers = Sets.difference(ALL_CONTAINERS, expectedContainers);
68         for (final String unexpectedContainer : unexpectedContainers) {
69             assertNull(String.format("Unexpected container %s.", unexpectedContainer),
70                     findNode(schemaContext, unexpectedContainer));
71         }
72     }
73
74     private static Set<QName> createFeaturesSet(final Set<String> featureNames) {
75         final Set<QName> supportedFeatures = new HashSet<>();
76         for (final String featureName : featureNames) {
77             if (featureName.indexOf(':') == -1) {
78                 supportedFeatures.add(QName.create(FOO_NS, featureName));
79             } else {
80                 supportedFeatures
81                         .add(QName.create(IMP_NS, IMP_REV, featureName.substring(featureName.indexOf(':') + 1)));
82             }
83         }
84
85         return ImmutableSet.copyOf(supportedFeatures);
86     }
87
88     private static SchemaNode findNode(final SchemaContext context, final String localName) {
89         return SchemaContextUtil.findDataSchemaNode(context,
90                 SchemaPath.create(true, QName.create(FOO_NS, localName)));
91     }
92
93     @Test
94     public void invalidYang10Test() throws Exception {
95         try {
96             StmtTestUtils.parseYangSource("/rfc7950/bug6868/invalid10.yang");
97             fail("Test should fail due to invalid Yang 1.0");
98         } catch (final SomeModifiersUnresolvedException e) {
99             assertTrue(e.getCause().getMessage()
100                     .startsWith("Parameter 'localName':'(not foo) or (bar and baz)' contains illegal character '('"));
101         }
102     }
103 }