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;
16 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
18 import java.util.Iterator;
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.impl.DefaultReactors;
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;
29 public class IdentityStmtTest {
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");
46 @Test(expected = SomeModifiersUnresolvedException.class)
47 public void selfReferencingIdentityTest() throws ReactorException {
48 DefaultReactors.defaultReactor().newBuild().addSource(ILLEGAL_IDENTITY_MODULE).buildEffective();
51 @Test(expected = SomeModifiersUnresolvedException.class)
52 public void selfReferencingIdentityWithPrefixTest() throws ReactorException {
53 DefaultReactors.defaultReactor().newBuild().addSource(ILLEGAL_IDENTITY_MODULE2).buildEffective();
57 public void importedIdentityTest() throws ReactorException {
58 SchemaContext result = DefaultReactors.defaultReactor().newBuild()
59 .addSources(LEGAL_IDENTITY_MODULE, LEGAL_IDENTITY_MODULE2)
61 assertNotNull(result);
64 @Test(expected = SomeModifiersUnresolvedException.class)
65 public void selfReferencingIdentityThroughChaining() throws ReactorException {
66 SchemaContext result = DefaultReactors.defaultReactor().newBuild()
67 .addSource(ILLEGAL_IDENTITY_MODULE3)
69 assertNotNull(result);
73 public void chainedIdentityTest() throws ReactorException {
74 SchemaContext result = DefaultReactors.defaultReactor().newBuild()
75 .addSource(LEGAL_IDENTITY_MODULE3)
77 assertNotNull(result);
79 Module testModule = result.findModules("legal-chained-identity-test").iterator().next();
80 assertNotNull(testModule);
82 Set<IdentitySchemaNode> identities = testModule.getIdentities();
83 assertEquals(4, identities.size());
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")));
90 identity = identitiesIterator.next();
91 assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"),
92 is("third-identity"), is("fourth-identity")));
94 identity = identitiesIterator.next();
95 assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"),
96 is("third-identity"), is("fourth-identity")));
98 identity = identitiesIterator.next();
99 assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"),
100 is("third-identity"), is("fourth-identity")));
104 public void duplicateIdentityTest() throws ReactorException {
105 SchemaContext result = DefaultReactors.defaultReactor().newBuild()
106 .addSource(DUPLICATE_IDENTITY_MODULE)
108 assertNotNull(result);
110 Module testModule = result.findModules("duplicate-identity-test").iterator().next();
111 Set<IdentitySchemaNode> identities = testModule.getIdentities();
112 assertEquals(1, identities.size());