2 * Copyright (c) 2016 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.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
17 import com.google.common.collect.ImmutableList;
18 import com.google.common.collect.ImmutableSet;
19 import com.google.common.collect.Iterables;
20 import java.io.FileNotFoundException;
21 import java.net.URISyntaxException;
22 import java.util.HashSet;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
35 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
36 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
38 public class Bug6869Test {
39 private static final String FOO_NS = "foo";
40 private static final String FOO_REV = "1970-01-01";
43 public void identityNoFeaureTest() throws ReactorException, SourceException, FileNotFoundException,
45 final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang",
47 assertNotNull(schemaContext);
49 final Set<IdentitySchemaNode> identities = getIdentities(schemaContext);
50 assertEquals(0, identities.size());
52 final SchemaNode findNode = findNode(schemaContext, ImmutableList.of("root", "grp-leaf"));
53 assertTrue(findNode instanceof LeafSchemaNode);
54 final LeafSchemaNode grpLeaf = (LeafSchemaNode) findNode;
55 assertFalse(grpLeaf.getConstraints().isMandatory());
59 public void identityAllFeauresTest() throws ReactorException, SourceException, FileNotFoundException,
61 final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang",
62 createFeaturesSet("identity-feature", "mandatory-leaf", "tls", "ssh", "two", "three"));
63 assertNotNull(schemaContext);
65 final Set<IdentitySchemaNode> identities = getIdentities(schemaContext);
66 assertEquals(1, identities.size());
68 final SchemaNode findNode = findNode(schemaContext, ImmutableList.of("root", "grp-leaf"));
69 assertTrue(findNode instanceof LeafSchemaNode);
70 final LeafSchemaNode grpLeaf = (LeafSchemaNode) findNode;
71 assertTrue(grpLeaf.getConstraints().isMandatory());
74 private static Set<IdentitySchemaNode> getIdentities(final SchemaContext schemaContext) {
75 final Set<Module> modules = schemaContext.getModules();
76 assertEquals(1, modules.size());
77 final Module module = modules.iterator().next();
78 return module.getIdentities();
81 private static Set<QName> createFeaturesSet(final String... featureNames) {
82 final Set<QName> supportedFeatures = new HashSet<>();
83 for (final String featureName : featureNames) {
84 supportedFeatures.add(QName.create(FOO_NS, FOO_REV, featureName));
87 return ImmutableSet.copyOf(supportedFeatures);
90 private static SchemaNode findNode(final SchemaContext context, final Iterable<String> localNamesPath) {
91 final Iterable<QName> qNames = Iterables.transform(localNamesPath,
92 localName -> QName.create(FOO_NS, FOO_REV, localName));
93 return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(qNames, true));
97 public void invalidYang10Test() throws ReactorException, SourceException, FileNotFoundException, URISyntaxException {
99 StmtTestUtils.parseYangSource("/rfc7950/bug6869/invalid10.yang");
100 fail("Test should fail due to invalid Yang 1.0");
101 } catch (final SomeModifiersUnresolvedException e) {
102 assertTrue(e.getCause().getMessage().startsWith("IF_FEATURE is not valid for IDENTITY"));