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.io.FileNotFoundException;
19 import java.net.URISyntaxException;
20 import java.util.HashSet;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
29 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
35 public class Bug6868Test {
36 private static final String FOO_NS = "foo";
37 private static final String FOO_REV = "1970-01-01";
38 private static final String IMP_NS = "imp";
39 private static final String IMP_REV = "2017-01-09";
40 private static final Set<String> ALL_CONTAINERS = ImmutableSet.of("my-container-1", "my-container-2",
41 "my-container-3", "foo", "not-foo", "imp-bar", "imp-bar-2");
44 public void ifFeatureYang11ResolutionTest() throws ReactorException, SourceException, FileNotFoundException,
46 assertSchemaContextFor(null, ALL_CONTAINERS);
47 assertSchemaContextFor(ImmutableSet.of(), ImmutableSet.of("my-container-1", "my-container-2", "not-foo"));
48 assertSchemaContextFor(ImmutableSet.of("foo"), ImmutableSet.of("foo"));
49 assertSchemaContextFor(ImmutableSet.of("baz"),
50 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "not-foo"));
51 assertSchemaContextFor(ImmutableSet.of("bar", "baz"),
52 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "not-foo"));
53 assertSchemaContextFor(ImmutableSet.of("foo", "bar", "baz"),
54 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "foo"));
55 assertSchemaContextFor(ImmutableSet.of("foo", "bar", "baz", "imp:bar"),
56 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "foo", "imp-bar"));
57 assertSchemaContextFor(ImmutableSet.of("foo", "baz", "imp:bar"), ImmutableSet.of("foo", "imp-bar", "imp-bar-2"));
60 private static void assertSchemaContextFor(final Set<String> supportedFeatures, final Set<String> expectedContainers)
61 throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
62 final SchemaContext schemaContext = StmtTestUtils.parseYangSources("/rfc7950/bug6868/yang11",
63 supportedFeatures != null ? createFeaturesSet(supportedFeatures) : null,
64 StatementParserMode.DEFAULT_MODE);
65 assertNotNull(schemaContext);
67 for (final String expectedContainer : expectedContainers) {
68 assertTrue(String.format("Expected container %s not found.", expectedContainer),
69 findNode(schemaContext, expectedContainer) instanceof ContainerSchemaNode);
72 final Set<String> unexpectedContainers = Sets.difference(ALL_CONTAINERS, expectedContainers);
73 for (final String unexpectedContainer : unexpectedContainers) {
74 assertNull(String.format("Unexpected container %s.", unexpectedContainer),
75 findNode(schemaContext, unexpectedContainer));
79 private static Set<QName> createFeaturesSet(final Set<String> featureNames) {
80 final Set<QName> supportedFeatures = new HashSet<>();
81 for (final String featureName : featureNames) {
82 if (featureName.indexOf(':') == -1) {
83 supportedFeatures.add(QName.create(FOO_NS, FOO_REV, featureName));
86 .add(QName.create(IMP_NS, IMP_REV, featureName.substring(featureName.indexOf(':') + 1)));
90 return ImmutableSet.copyOf(supportedFeatures);
93 private static SchemaNode findNode(final SchemaContext context, final String localName) {
94 return SchemaContextUtil.findDataSchemaNode(context,
95 SchemaPath.create(true, QName.create(FOO_NS, FOO_REV, localName)));
99 public void invalidYang10Test() throws ReactorException, SourceException, FileNotFoundException, URISyntaxException {
101 StmtTestUtils.parseYangSource("/rfc7950/bug6868/invalid10.yang");
102 fail("Test should fail due to invalid Yang 1.0");
103 } catch (final SomeModifiersUnresolvedException e) {
104 assertTrue(e.getCause().getMessage()
105 .startsWith("Parameter 'localName':'(not foo) or (bar and baz)' contains illegal character '('"));