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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
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 import static org.junit.Assert.assertThrows;
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.collect.Sets;
19 import java.util.HashSet;
20 import java.util.Optional;
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.parser.api.YangParserConfiguration;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
28 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
30 public class Bug6868Test {
31 private static final String FOO_NS = "foo";
32 private static final String IMP_NS = "imp";
33 private static final String IMP_REV = "2017-01-09";
34 private static final Set<String> ALL_CONTAINERS = ImmutableSet.of("my-container-1", "my-container-2",
35 "my-container-3", "foo", "not-foo", "imp-bar", "imp-bar-2");
38 public void ifFeatureYang11ResolutionTest() throws Exception {
39 assertSchemaContextFor(null, ALL_CONTAINERS);
40 assertSchemaContextFor(ImmutableSet.of(), ImmutableSet.of("my-container-1", "my-container-2", "not-foo"));
41 assertSchemaContextFor(ImmutableSet.of("foo"), ImmutableSet.of("foo"));
42 assertSchemaContextFor(ImmutableSet.of("baz"),
43 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "not-foo"));
44 assertSchemaContextFor(ImmutableSet.of("bar", "baz"),
45 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "not-foo"));
46 assertSchemaContextFor(ImmutableSet.of("foo", "bar", "baz"),
47 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "foo"));
48 assertSchemaContextFor(ImmutableSet.of("foo", "bar", "baz", "imp:bar"),
49 ImmutableSet.of("my-container-1", "my-container-2", "my-container-3", "foo", "imp-bar"));
50 assertSchemaContextFor(ImmutableSet.of("foo", "baz", "imp:bar"),
51 ImmutableSet.of("foo", "imp-bar", "imp-bar-2"));
54 private static void assertSchemaContextFor(final Set<String> supportedFeatures,
55 final Set<String> expectedContainers) throws Exception {
56 final SchemaContext schemaContext = StmtTestUtils.parseYangSources("/rfc7950/bug6868/yang11",
57 supportedFeatures != null ? createFeaturesSet(supportedFeatures) : null, YangParserConfiguration.DEFAULT);
58 assertNotNull(schemaContext);
60 for (final String expectedContainer : expectedContainers) {
61 assertThat(String.format("Expected container %s not found.", expectedContainer),
62 schemaContext.findDataTreeChild(QName.create(FOO_NS, expectedContainer)).get(),
63 instanceOf(ContainerSchemaNode.class));
66 final Set<String> unexpectedContainers = Sets.difference(ALL_CONTAINERS, expectedContainers);
67 for (final String unexpectedContainer : unexpectedContainers) {
68 assertEquals(String.format("Unexpected container %s.", unexpectedContainer), Optional.empty(),
69 schemaContext.findDataTreeChild(QName.create(FOO_NS, unexpectedContainer)));
73 private static Set<QName> createFeaturesSet(final Set<String> featureNames) {
74 final Set<QName> supportedFeatures = new HashSet<>();
75 for (final String featureName : featureNames) {
76 if (featureName.indexOf(':') == -1) {
77 supportedFeatures.add(QName.create(FOO_NS, featureName));
80 .add(QName.create(IMP_NS, IMP_REV, featureName.substring(featureName.indexOf(':') + 1)));
84 return ImmutableSet.copyOf(supportedFeatures);
88 public void invalidYang10Test() {
89 SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
90 () -> StmtTestUtils.parseYangSource("/rfc7950/bug6868/invalid10.yang"));
91 assertThat(ex.getCause().getMessage(), startsWith("Invalid identifier '(not foo) or (bar and baz)' [at "));