Simplify createFeaturesSet()
[yangtools.git] / parser / yang-parser-rfc7950 / 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 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertTrue;
17
18 import com.google.common.collect.ImmutableSet;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.stmt.AbstractYangTest;
31 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
32
33 public class Bug6869Test extends AbstractYangTest {
34     private static final QName ROOT = QName.create("foo", "root");
35     private static final QName GRP_LEAF = QName.create("foo", "grp-leaf");
36
37     @Test
38     public void identityNoFeaureTest() throws Exception {
39         final EffectiveModelContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang",
40                 ImmutableSet.of());
41         assertNotNull(schemaContext);
42
43         final Collection<? extends IdentitySchemaNode> identities = getIdentities(schemaContext);
44         assertEquals(0, identities.size());
45
46         final DataSchemaNode findNode = schemaContext.findDataTreeChild(ROOT, GRP_LEAF).orElse(null);
47         assertThat(findNode, instanceOf(LeafSchemaNode.class));
48         final LeafSchemaNode grpLeaf = (LeafSchemaNode) findNode;
49         assertFalse(grpLeaf.isMandatory());
50     }
51
52     @Test
53     public void identityAllFeauresTest() throws Exception {
54         final EffectiveModelContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang",
55                 createFeaturesSet("identity-feature", "mandatory-leaf", "tls", "ssh", "two", "three"));
56         assertNotNull(schemaContext);
57
58         final Collection<? extends IdentitySchemaNode> identities = getIdentities(schemaContext);
59         assertEquals(1, identities.size());
60
61         final DataSchemaNode findNode = schemaContext.findDataTreeChild(ROOT, GRP_LEAF).orElse(null);
62         assertThat(findNode, instanceOf(LeafSchemaNode.class));
63         final LeafSchemaNode grpLeaf = (LeafSchemaNode) findNode;
64         assertTrue(grpLeaf.isMandatory());
65     }
66
67     private static Collection<? extends IdentitySchemaNode> getIdentities(final EffectiveModelContext schemaContext) {
68         final Collection<? extends 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         return Arrays.stream(featureNames)
76             .map(featureName -> QName.create("foo", featureName))
77             .collect(Collectors.toUnmodifiableSet());
78     }
79
80     @Test
81     public void invalidYang10Test() {
82         assertInvalidSubstatementException(startsWith("IF_FEATURE is not valid for IDENTITY"),
83             "/rfc7950/bug6869/invalid10.yang");
84     }
85 }