Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / Bug6869Test.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
10
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;
16
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;
21 import java.util.Set;
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;
33
34 public class Bug6869Test {
35     private static final String FOO_NS = "foo";
36
37     @Test
38     public void identityNoFeaureTest() throws Exception {
39         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang",
40                 ImmutableSet.of());
41         assertNotNull(schemaContext);
42
43         final Set<IdentitySchemaNode> identities = getIdentities(schemaContext);
44         assertEquals(0, identities.size());
45
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());
50     }
51
52     @Test
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);
57
58         final Set<IdentitySchemaNode> identities = getIdentities(schemaContext);
59         assertEquals(1, identities.size());
60
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());
65     }
66
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();
72     }
73
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));
78         }
79
80         return ImmutableSet.copyOf(supportedFeatures);
81     }
82
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));
87     }
88
89     @Test
90     public void invalidYang10Test() throws Exception {
91         try {
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"));
96         }
97     }
98 }