Bug 6868: If-feature argument may be boolean expression
[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.io.FileNotFoundException;
21 import java.net.URISyntaxException;
22 import java.util.HashSet;
23 import java.util.Set;
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;
37
38 public class Bug6869Test {
39     private static final String FOO_NS = "foo";
40     private static final String FOO_REV = "1970-01-01";
41
42     @Test
43     public void identityNoFeaureTest() throws ReactorException, SourceException, FileNotFoundException,
44             URISyntaxException {
45         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang",
46                 ImmutableSet.of());
47         assertNotNull(schemaContext);
48
49         final Set<IdentitySchemaNode> identities = getIdentities(schemaContext);
50         assertEquals(0, identities.size());
51
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());
56     }
57
58     @Test
59     public void identityAllFeauresTest() throws ReactorException, SourceException, FileNotFoundException,
60             URISyntaxException {
61         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang",
62                 createFeaturesSet("identity-feature", "mandatory-leaf", "tls", "ssh", "two", "three"));
63         assertNotNull(schemaContext);
64
65         final Set<IdentitySchemaNode> identities = getIdentities(schemaContext);
66         assertEquals(1, identities.size());
67
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());
72     }
73
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();
79     }
80
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));
85         }
86
87         return ImmutableSet.copyOf(supportedFeatures);
88     }
89
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));
94     }
95
96     @Test
97     public void invalidYang10Test() throws ReactorException, SourceException, FileNotFoundException, URISyntaxException {
98         try {
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"));
103         }
104     }
105 }