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
8 package org.opendaylight.yangtools.yang.stmt;
10 import static org.hamcrest.CoreMatchers.anyOf;
11 import static org.hamcrest.CoreMatchers.is;
12 import static org.hamcrest.CoreMatchers.startsWith;
13 import static org.hamcrest.MatcherAssert.assertThat;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.fail;
17 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
19 import com.google.common.base.Throwables;
20 import java.util.Collection;
21 import java.util.Iterator;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
32 public class IdentityStmtTest {
34 private static final StatementStreamSource ILLEGAL_IDENTITY_MODULE = sourceForResource(
35 "/identity/identitytest.yang");
36 private static final StatementStreamSource ILLEGAL_IDENTITY_MODULE2 = sourceForResource(
37 "/identity/prefixidentitytest.yang");
38 private static final StatementStreamSource LEGAL_IDENTITY_MODULE = sourceForResource(
39 "/identity/import/dummy.yang");
40 private static final StatementStreamSource LEGAL_IDENTITY_MODULE2 = sourceForResource(
41 "/identity/import/prefiximportidentitytest.yang");
42 private static final StatementStreamSource ILLEGAL_IDENTITY_MODULE3 = sourceForResource(
43 "/identity/illegal-chained-identity-test.yang");
44 private static final StatementStreamSource LEGAL_IDENTITY_MODULE3 = sourceForResource(
45 "/identity/legal-chained-identity-test.yang");
46 private static final StatementStreamSource DUPLICATE_IDENTITY_MODULE = sourceForResource(
47 "/identity/duplicate-identity-test.yang");
49 @Test(expected = SomeModifiersUnresolvedException.class)
50 public void selfReferencingIdentityTest() throws ReactorException {
51 RFC7950Reactors.defaultReactor().newBuild().addSource(ILLEGAL_IDENTITY_MODULE).buildEffective();
54 @Test(expected = SomeModifiersUnresolvedException.class)
55 public void selfReferencingIdentityWithPrefixTest() throws ReactorException {
56 RFC7950Reactors.defaultReactor().newBuild().addSource(ILLEGAL_IDENTITY_MODULE2).buildEffective();
60 public void importedIdentityTest() throws ReactorException {
61 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
62 .addSources(LEGAL_IDENTITY_MODULE, LEGAL_IDENTITY_MODULE2)
64 assertNotNull(result);
67 @Test(expected = SomeModifiersUnresolvedException.class)
68 public void selfReferencingIdentityThroughChaining() throws ReactorException {
69 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
70 .addSource(ILLEGAL_IDENTITY_MODULE3)
72 assertNotNull(result);
76 public void chainedIdentityTest() throws ReactorException {
77 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
78 .addSource(LEGAL_IDENTITY_MODULE3)
80 assertNotNull(result);
82 Module testModule = result.findModules("legal-chained-identity-test").iterator().next();
83 assertNotNull(testModule);
85 Collection<? extends IdentitySchemaNode> identities = testModule.getIdentities();
86 assertEquals(4, identities.size());
88 Iterator<? extends IdentitySchemaNode> identitiesIterator = identities.iterator();
89 IdentitySchemaNode identity = identitiesIterator.next();
90 assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"),
91 is("third-identity"), is("fourth-identity")));
93 identity = identitiesIterator.next();
94 assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"),
95 is("third-identity"), is("fourth-identity")));
97 identity = identitiesIterator.next();
98 assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"),
99 is("third-identity"), is("fourth-identity")));
101 identity = identitiesIterator.next();
102 assertThat(identity.getQName().getLocalName(), anyOf(is("first-identity"), is("second-identity"),
103 is("third-identity"), is("fourth-identity")));
107 public void duplicateIdentityTest() throws ReactorException {
109 RFC7950Reactors.defaultReactor().newBuild().addSource(DUPLICATE_IDENTITY_MODULE).buildEffective();
110 fail("Duplicate identities should have been detected");
111 } catch (SomeModifiersUnresolvedException e) {
112 final SourceException cause = Throwables.getCauseAs(e, SourceException.class);
113 assertThat(cause.getMessage(), startsWith("Duplicate identity definition "));