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