2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
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;
16 import com.google.common.collect.ImmutableSet;
17 import com.google.common.collect.Sets;
18 import java.util.HashSet;
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;
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");
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"), ImmutableSet.of("foo", "imp-bar", "imp-bar-2"));
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);
62 for (final String expectedContainer : expectedContainers) {
63 assertTrue(String.format("Expected container %s not found.", expectedContainer),
64 findNode(schemaContext, expectedContainer) instanceof ContainerSchemaNode);
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));
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, FOO_REV, featureName));
81 .add(QName.create(IMP_NS, IMP_REV, featureName.substring(featureName.indexOf(':') + 1)));
85 return ImmutableSet.copyOf(supportedFeatures);
88 private static SchemaNode findNode(final SchemaContext context, final String localName) {
89 return SchemaContextUtil.findDataSchemaNode(context,
90 SchemaPath.create(true, QName.create(FOO_NS, FOO_REV, localName)));
94 public void invalidYang10Test() throws Exception {
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 '('"));