Modernize Bug6869Test, Bug6880Test
[yangtools.git] / yang / 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.assertThrows;
17 import static org.junit.Assert.assertTrue;
18
19 import com.google.common.collect.ImmutableSet;
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.Set;
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.parser.spi.meta.ReactorException;
31 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
33
34 public class Bug6869Test {
35     private static final QName ROOT = QName.create("foo", "root");
36     private static final QName GRP_LEAF = QName.create("foo", "grp-leaf");
37
38     @Test
39     public void identityNoFeaureTest() throws Exception {
40         final EffectiveModelContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang",
41                 ImmutableSet.of());
42         assertNotNull(schemaContext);
43
44         final Collection<? extends IdentitySchemaNode> identities = getIdentities(schemaContext);
45         assertEquals(0, identities.size());
46
47         final DataSchemaNode findNode = schemaContext.findDataTreeChild(ROOT, GRP_LEAF).orElse(null);
48         assertThat(findNode, instanceOf(LeafSchemaNode.class));
49         final LeafSchemaNode grpLeaf = (LeafSchemaNode) findNode;
50         assertFalse(grpLeaf.isMandatory());
51     }
52
53     @Test
54     public void identityAllFeauresTest() throws Exception {
55         final EffectiveModelContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang",
56                 createFeaturesSet("identity-feature", "mandatory-leaf", "tls", "ssh", "two", "three"));
57         assertNotNull(schemaContext);
58
59         final Collection<? extends IdentitySchemaNode> identities = getIdentities(schemaContext);
60         assertEquals(1, identities.size());
61
62         final DataSchemaNode findNode = schemaContext.findDataTreeChild(ROOT, GRP_LEAF).orElse(null);
63         assertThat(findNode, instanceOf(LeafSchemaNode.class));
64         final LeafSchemaNode grpLeaf = (LeafSchemaNode) findNode;
65         assertTrue(grpLeaf.isMandatory());
66     }
67
68     private static Collection<? extends IdentitySchemaNode> getIdentities(final EffectiveModelContext schemaContext) {
69         final Collection<? extends 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", featureName));
79         }
80
81         return ImmutableSet.copyOf(supportedFeatures);
82     }
83
84     @Test
85     public void invalidYang10Test() {
86         final ReactorException ex = assertThrows(ReactorException.class,
87             () -> StmtTestUtils.parseYangSource("/rfc7950/bug6869/invalid10.yang"));
88         final Throwable cause = ex.getCause();
89         assertThat(cause, instanceOf(SourceException.class));
90         assertThat(cause.getMessage(), startsWith("IF_FEATURE is not valid for IDENTITY"));
91     }
92 }