Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / IdentityStmtTest.java
1 /*
2  * Copyright (c) 2015 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.test;
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
17 import java.util.Iterator;
18 import java.util.Set;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
24 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
28
29 public class IdentityStmtTest {
30
31     private static final YangStatementSourceImpl ILLEGAL_IDENTITY_MODULE = new YangStatementSourceImpl
32             ("/identity/identitytest.yang", false);
33     private static final YangStatementSourceImpl ILLEGAL_IDENTITY_MODULE2 = new YangStatementSourceImpl
34             ("/identity/prefixidentitytest.yang", false);
35     private static final YangStatementSourceImpl LEGAL_IDENTITY_MODULE = new YangStatementSourceImpl
36             ("/identity/import/dummy.yang", false);
37     private static final YangStatementSourceImpl LEGAL_IDENTITY_MODULE2 = new YangStatementSourceImpl
38             ("/identity/import/prefiximportidentitytest.yang", false);
39     private static final YangStatementSourceImpl ILLEGAL_IDENTITY_MODULE3 = new YangStatementSourceImpl
40             ("/identity/illegal-chained-identity-test.yang", false);
41     private static final YangStatementSourceImpl LEGAL_IDENTITY_MODULE3 = new YangStatementSourceImpl
42             ("/identity/legal-chained-identity-test.yang", false);
43     private static final YangStatementSourceImpl DUPLICATE_IDENTITY_MODULE = new YangStatementSourceImpl
44             ("/identity/duplicate-identity-test.yang", false);
45
46     @Test(expected = SomeModifiersUnresolvedException.class)
47     public void selfReferencingIdentityTest() throws ReactorException {
48         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
49         StmtTestUtils.addSources(reactor, ILLEGAL_IDENTITY_MODULE);
50
51         EffectiveSchemaContext result = reactor.buildEffective();
52         assertNotNull(result);
53     }
54
55     @Test(expected = SomeModifiersUnresolvedException.class)
56     public void selfReferencingIdentityWithPrefixTest() throws ReactorException {
57         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
58         StmtTestUtils.addSources(reactor, ILLEGAL_IDENTITY_MODULE2);
59
60         EffectiveSchemaContext result = reactor.buildEffective();
61         assertNotNull(result);
62     }
63
64     @Test
65     public void importedIdentityTest() throws ReactorException {
66         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
67         StmtTestUtils.addSources(reactor, LEGAL_IDENTITY_MODULE, LEGAL_IDENTITY_MODULE2);
68
69         EffectiveSchemaContext result = reactor.buildEffective();
70         assertNotNull(result);
71     }
72
73     @Test(expected = SomeModifiersUnresolvedException.class)
74     public void selfReferencingIdentityThroughChaining() throws ReactorException {
75         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
76         StmtTestUtils.addSources(reactor, ILLEGAL_IDENTITY_MODULE3);
77
78         EffectiveSchemaContext result = reactor.buildEffective();
79         assertNotNull(result);
80     }
81
82     @Test
83     public void chainedIdentityTest() throws ReactorException {
84         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
85         StmtTestUtils.addSources(reactor, LEGAL_IDENTITY_MODULE3);
86
87         EffectiveSchemaContext result = reactor.buildEffective();
88         assertNotNull(result);
89
90         Module testModule = result.findModuleByName("legal-chained-identity-test", null);
91         assertNotNull(testModule);
92
93         Set<IdentitySchemaNode> identities = testModule.getIdentities();
94         assertEquals(4, identities.size());
95
96         Iterator<IdentitySchemaNode> identitiesIterator = identities.iterator();
97         IdentitySchemaNode identity = identitiesIterator.next();
98         assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"), is
99                 ("third-identity"), is("fourth-identity")));
100
101         identity = identitiesIterator.next();
102         assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"), is
103                 ("third-identity"), is("fourth-identity")));
104
105         identity = identitiesIterator.next();
106         assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"), is
107                 ("third-identity"), is("fourth-identity")));
108
109         identity = identitiesIterator.next();
110         assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"), is
111                 ("third-identity"), is("fourth-identity")));
112     }
113
114     @Test
115     public void duplicateIdentityTest() throws ReactorException {
116         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
117         StmtTestUtils.addSources(reactor, DUPLICATE_IDENTITY_MODULE);
118
119         EffectiveSchemaContext result = reactor.buildEffective();
120         assertNotNull(result);
121
122         Module testModule = result.findModuleByName("duplicate-identity-test", null);
123         Set<IdentitySchemaNode> identities = testModule.getIdentities();
124         assertEquals(1, identities.size());
125     }
126 }