Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / IdentityStmtTest.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.stmt;
10
11 import static org.hamcrest.CoreMatchers.anyOf;
12 import static org.hamcrest.CoreMatchers.is;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertThat;
16 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
17
18 import java.util.Iterator;
19 import java.util.Set;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
28
29 public class IdentityStmtTest {
30
31     private static final StatementStreamSource ILLEGAL_IDENTITY_MODULE = sourceForResource(
32         "/identity/identitytest.yang");
33     private static final StatementStreamSource ILLEGAL_IDENTITY_MODULE2 = sourceForResource(
34         "/identity/prefixidentitytest.yang");
35     private static final StatementStreamSource LEGAL_IDENTITY_MODULE = sourceForResource(
36         "/identity/import/dummy.yang");
37     private static final StatementStreamSource LEGAL_IDENTITY_MODULE2 = sourceForResource(
38         "/identity/import/prefiximportidentitytest.yang");
39     private static final StatementStreamSource ILLEGAL_IDENTITY_MODULE3 = sourceForResource(
40         "/identity/illegal-chained-identity-test.yang");
41     private static final StatementStreamSource LEGAL_IDENTITY_MODULE3 = sourceForResource(
42         "/identity/legal-chained-identity-test.yang");
43     private static final StatementStreamSource DUPLICATE_IDENTITY_MODULE = sourceForResource(
44         "/identity/duplicate-identity-test.yang");
45
46     @Test(expected = SomeModifiersUnresolvedException.class)
47     public void selfReferencingIdentityTest() throws ReactorException {
48         RFC7950Reactors.defaultReactor().newBuild().addSource(ILLEGAL_IDENTITY_MODULE).buildEffective();
49     }
50
51     @Test(expected = SomeModifiersUnresolvedException.class)
52     public void selfReferencingIdentityWithPrefixTest() throws ReactorException {
53         RFC7950Reactors.defaultReactor().newBuild().addSource(ILLEGAL_IDENTITY_MODULE2).buildEffective();
54     }
55
56     @Test
57     public void importedIdentityTest() throws ReactorException {
58         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
59                 .addSources(LEGAL_IDENTITY_MODULE, LEGAL_IDENTITY_MODULE2)
60                 .buildEffective();
61         assertNotNull(result);
62     }
63
64     @Test(expected = SomeModifiersUnresolvedException.class)
65     public void selfReferencingIdentityThroughChaining() throws ReactorException {
66         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
67                 .addSource(ILLEGAL_IDENTITY_MODULE3)
68                 .buildEffective();
69         assertNotNull(result);
70     }
71
72     @Test
73     public void chainedIdentityTest() throws ReactorException {
74         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
75                 .addSource(LEGAL_IDENTITY_MODULE3)
76                 .buildEffective();
77         assertNotNull(result);
78
79         Module testModule = result.findModules("legal-chained-identity-test").iterator().next();
80         assertNotNull(testModule);
81
82         Set<IdentitySchemaNode> identities = testModule.getIdentities();
83         assertEquals(4, identities.size());
84
85         Iterator<IdentitySchemaNode> identitiesIterator = identities.iterator();
86         IdentitySchemaNode identity = identitiesIterator.next();
87         assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"),
88             is("third-identity"), is("fourth-identity")));
89
90         identity = identitiesIterator.next();
91         assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"),
92             is("third-identity"), is("fourth-identity")));
93
94         identity = identitiesIterator.next();
95         assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"),
96             is("third-identity"), is("fourth-identity")));
97
98         identity = identitiesIterator.next();
99         assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"),
100             is("third-identity"), is("fourth-identity")));
101     }
102
103     @Test
104     public void duplicateIdentityTest() throws ReactorException {
105         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
106                 .addSource(DUPLICATE_IDENTITY_MODULE)
107                 .buildEffective();
108         assertNotNull(result);
109
110         Module testModule = result.findModules("duplicate-identity-test").iterator().next();
111         Set<IdentitySchemaNode> identities = testModule.getIdentities();
112         assertEquals(1, identities.size());
113     }
114 }