Fix checkstyle in yang-parser-impl and enable enforcement
[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.spi.meta.ReactorException;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
26 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
27 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
29
30 public class IdentityStmtTest {
31
32     private static final StatementStreamSource ILLEGAL_IDENTITY_MODULE =
33             sourceForResource("/identity/identitytest.yang");
34
35     private static final StatementStreamSource ILLEGAL_IDENTITY_MODULE2 = sourceForResource(
36         "/identity/prefixidentitytest.yang");
37     private static final StatementStreamSource LEGAL_IDENTITY_MODULE = sourceForResource(
38         "/identity/import/dummy.yang");
39     private static final StatementStreamSource LEGAL_IDENTITY_MODULE2 = sourceForResource(
40         "/identity/import/prefiximportidentitytest.yang");
41     private static final StatementStreamSource ILLEGAL_IDENTITY_MODULE3 = sourceForResource(
42         "/identity/illegal-chained-identity-test.yang");
43     private static final StatementStreamSource LEGAL_IDENTITY_MODULE3 = sourceForResource(
44         "/identity/legal-chained-identity-test.yang");
45     private static final StatementStreamSource DUPLICATE_IDENTITY_MODULE = sourceForResource(
46         "/identity/duplicate-identity-test.yang");
47
48     @Test(expected = SomeModifiersUnresolvedException.class)
49     public void selfReferencingIdentityTest() throws ReactorException {
50         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
51         reactor.addSources(ILLEGAL_IDENTITY_MODULE);
52
53         SchemaContext result = reactor.buildEffective();
54         assertNotNull(result);
55     }
56
57     @Test(expected = SomeModifiersUnresolvedException.class)
58     public void selfReferencingIdentityWithPrefixTest() throws ReactorException {
59         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
60         reactor.addSources(ILLEGAL_IDENTITY_MODULE2);
61
62         SchemaContext result = reactor.buildEffective();
63         assertNotNull(result);
64     }
65
66     @Test
67     public void importedIdentityTest() throws ReactorException {
68         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
69         reactor.addSources(LEGAL_IDENTITY_MODULE, LEGAL_IDENTITY_MODULE2);
70
71         SchemaContext result = reactor.buildEffective();
72         assertNotNull(result);
73     }
74
75     @Test(expected = SomeModifiersUnresolvedException.class)
76     public void selfReferencingIdentityThroughChaining() throws ReactorException {
77         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
78         reactor.addSources(ILLEGAL_IDENTITY_MODULE3);
79
80         SchemaContext result = reactor.buildEffective();
81         assertNotNull(result);
82     }
83
84     @Test
85     public void chainedIdentityTest() throws ReactorException {
86         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
87         reactor.addSources(LEGAL_IDENTITY_MODULE3);
88
89         SchemaContext result = reactor.buildEffective();
90         assertNotNull(result);
91
92         Module testModule = result.findModules("legal-chained-identity-test").iterator().next();
93         assertNotNull(testModule);
94
95         Set<IdentitySchemaNode> identities = testModule.getIdentities();
96         assertEquals(4, identities.size());
97
98         Iterator<IdentitySchemaNode> identitiesIterator = identities.iterator();
99         IdentitySchemaNode identity = identitiesIterator.next();
100         assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"),
101             is("third-identity"), is("fourth-identity")));
102
103         identity = identitiesIterator.next();
104         assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"),
105             is("third-identity"), is("fourth-identity")));
106
107         identity = identitiesIterator.next();
108         assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"),
109             is("third-identity"), is("fourth-identity")));
110
111         identity = identitiesIterator.next();
112         assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"),
113             is("third-identity"), is("fourth-identity")));
114     }
115
116     @Test
117     public void duplicateIdentityTest() throws ReactorException {
118         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
119         reactor.addSources(DUPLICATE_IDENTITY_MODULE);
120
121         SchemaContext result = reactor.buildEffective();
122         assertNotNull(result);
123
124         Module testModule = result.findModules("duplicate-identity-test").iterator().next();
125         Set<IdentitySchemaNode> identities = testModule.getIdentities();
126         assertEquals(1, identities.size());
127     }
128 }