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.util.HashSet;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
30 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
32 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
34 public class Bug6869Test {
35 private static final String FOO_NS = "foo";
36 private static final String FOO_REV = "1970-01-01";
39 public void identityNoFeaureTest() throws Exception {
40 final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang",
42 assertNotNull(schemaContext);
44 final Set<IdentitySchemaNode> identities = getIdentities(schemaContext);
45 assertEquals(0, identities.size());
47 final SchemaNode findNode = findNode(schemaContext, ImmutableList.of("root", "grp-leaf"));
48 assertTrue(findNode instanceof LeafSchemaNode);
49 final LeafSchemaNode grpLeaf = (LeafSchemaNode) findNode;
50 assertFalse(grpLeaf.getConstraints().isMandatory());
54 public void identityAllFeauresTest() throws Exception {
55 final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang",
56 createFeaturesSet("identity-feature", "mandatory-leaf", "tls", "ssh", "two", "three"));
57 assertNotNull(schemaContext);
59 final Set<IdentitySchemaNode> identities = getIdentities(schemaContext);
60 assertEquals(1, identities.size());
62 final SchemaNode findNode = findNode(schemaContext, ImmutableList.of("root", "grp-leaf"));
63 assertTrue(findNode instanceof LeafSchemaNode);
64 final LeafSchemaNode grpLeaf = (LeafSchemaNode) findNode;
65 assertTrue(grpLeaf.getConstraints().isMandatory());
68 private static Set<IdentitySchemaNode> getIdentities(final SchemaContext schemaContext) {
69 final Set<Module> modules = schemaContext.getModules();
70 assertEquals(1, modules.size());
71 final Module module = modules.iterator().next();
72 return module.getIdentities();
75 private static Set<QName> createFeaturesSet(final String... featureNames) {
76 final Set<QName> supportedFeatures = new HashSet<>();
77 for (final String featureName : featureNames) {
78 supportedFeatures.add(QName.create(FOO_NS, FOO_REV, featureName));
81 return ImmutableSet.copyOf(supportedFeatures);
84 private static SchemaNode findNode(final SchemaContext context, final Iterable<String> localNamesPath) {
85 final Iterable<QName> qNames = Iterables.transform(localNamesPath,
86 localName -> QName.create(FOO_NS, FOO_REV, localName));
87 return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(qNames, true));
91 public void invalidYang10Test() throws Exception {
93 StmtTestUtils.parseYangSource("/rfc7950/bug6869/invalid10.yang");
94 fail("Test should fail due to invalid Yang 1.0");
95 } catch (final SomeModifiersUnresolvedException e) {
96 assertTrue(e.getCause().getMessage().startsWith("IF_FEATURE is not valid for IDENTITY"));