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";
38 public void identityNoFeaureTest() throws Exception {
39 final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang",
41 assertNotNull(schemaContext);
43 final Set<IdentitySchemaNode> identities = getIdentities(schemaContext);
44 assertEquals(0, identities.size());
46 final SchemaNode findNode = findNode(schemaContext, ImmutableList.of("root", "grp-leaf"));
47 assertTrue(findNode instanceof LeafSchemaNode);
48 final LeafSchemaNode grpLeaf = (LeafSchemaNode) findNode;
49 assertFalse(grpLeaf.isMandatory());
53 public void identityAllFeauresTest() throws Exception {
54 final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang",
55 createFeaturesSet("identity-feature", "mandatory-leaf", "tls", "ssh", "two", "three"));
56 assertNotNull(schemaContext);
58 final Set<IdentitySchemaNode> identities = getIdentities(schemaContext);
59 assertEquals(1, identities.size());
61 final SchemaNode findNode = findNode(schemaContext, ImmutableList.of("root", "grp-leaf"));
62 assertTrue(findNode instanceof LeafSchemaNode);
63 final LeafSchemaNode grpLeaf = (LeafSchemaNode) findNode;
64 assertTrue(grpLeaf.isMandatory());
67 private static Set<IdentitySchemaNode> getIdentities(final SchemaContext schemaContext) {
68 final Set<Module> modules = schemaContext.getModules();
69 assertEquals(1, modules.size());
70 final Module module = modules.iterator().next();
71 return module.getIdentities();
74 private static Set<QName> createFeaturesSet(final String... featureNames) {
75 final Set<QName> supportedFeatures = new HashSet<>();
76 for (final String featureName : featureNames) {
77 supportedFeatures.add(QName.create(FOO_NS, featureName));
80 return ImmutableSet.copyOf(supportedFeatures);
83 private static SchemaNode findNode(final SchemaContext context, final Iterable<String> localNamesPath) {
84 final Iterable<QName> qNames = Iterables.transform(localNamesPath,
85 localName -> QName.create(FOO_NS, localName));
86 return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(qNames, true));
90 public void invalidYang10Test() throws Exception {
92 StmtTestUtils.parseYangSource("/rfc7950/bug6869/invalid10.yang");
93 fail("Test should fail due to invalid Yang 1.0");
94 } catch (final SomeModifiersUnresolvedException e) {
95 assertTrue(e.getCause().getMessage().startsWith("IF_FEATURE is not valid for IDENTITY"));