2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.stmt;
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;
17 import java.util.Iterator;
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;
29 public class IdentityStmtTest {
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);
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);
51 EffectiveSchemaContext result = reactor.buildEffective();
52 assertNotNull(result);
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);
60 EffectiveSchemaContext result = reactor.buildEffective();
61 assertNotNull(result);
65 public void importedIdentityTest() throws ReactorException {
66 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
67 StmtTestUtils.addSources(reactor, LEGAL_IDENTITY_MODULE, LEGAL_IDENTITY_MODULE2);
69 EffectiveSchemaContext result = reactor.buildEffective();
70 assertNotNull(result);
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);
78 EffectiveSchemaContext result = reactor.buildEffective();
79 assertNotNull(result);
83 public void chainedIdentityTest() throws ReactorException {
84 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
85 StmtTestUtils.addSources(reactor, LEGAL_IDENTITY_MODULE3);
87 EffectiveSchemaContext result = reactor.buildEffective();
88 assertNotNull(result);
90 Module testModule = result.findModuleByName("legal-chained-identity-test", null);
91 assertNotNull(testModule);
93 Set<IdentitySchemaNode> identities = testModule.getIdentities();
94 assertEquals(4, identities.size());
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")));
101 identity = identitiesIterator.next();
102 assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"), is
103 ("third-identity"), is("fourth-identity")));
105 identity = identitiesIterator.next();
106 assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"), is
107 ("third-identity"), is("fourth-identity")));
109 identity = identitiesIterator.next();
110 assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"), is
111 ("third-identity"), is("fourth-identity")));
115 public void duplicateIdentityTest() throws ReactorException {
116 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
117 StmtTestUtils.addSources(reactor, DUPLICATE_IDENTITY_MODULE);
119 EffectiveSchemaContext result = reactor.buildEffective();
120 assertNotNull(result);
122 Module testModule = result.findModuleByName("duplicate-identity-test", null);
123 Set<IdentitySchemaNode> identities = testModule.getIdentities();
124 assertEquals(1, identities.size());