95fbb4175027be4fef1c0ef2840578e41930c79e
[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     private static final String FOO_REV = "1970-01-01";
37
38     @Test
39     public void identityNoFeaureTest() throws Exception {
40         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang",
41                 ImmutableSet.of());
42         assertNotNull(schemaContext);
43
44         final Set<IdentitySchemaNode> identities = getIdentities(schemaContext);
45         assertEquals(0, identities.size());
46
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());
51     }
52
53     @Test
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);
58
59         final Set<IdentitySchemaNode> identities = getIdentities(schemaContext);
60         assertEquals(1, identities.size());
61
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());
66     }
67
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();
73     }
74
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));
79         }
80
81         return ImmutableSet.copyOf(supportedFeatures);
82     }
83
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));
88     }
89
90     @Test
91     public void invalidYang10Test() throws Exception {
92         try {
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"));
97         }
98     }
99 }