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.instanceOf;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertThrows;
17 import static org.junit.Assert.assertTrue;
18 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
20 import com.google.common.collect.Iterables;
21 import java.net.URISyntaxException;
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.InferenceException;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
33 public class EffectiveIdentityTest {
35 private static final StatementStreamSource IDENTITY_TEST = sourceForResource(
36 "/stmt-test/identity/identity-test.yang");
38 private static final StatementStreamSource CYCLIC_IDENTITY_TEST = sourceForResource(
39 "/stmt-test/identity/cyclic-identity-test.yang");
42 public void cyclicefineTest() throws SourceException, ReactorException, URISyntaxException {
43 final var reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(CYCLIC_IDENTITY_TEST);
44 final var cause = assertThrows(SomeModifiersUnresolvedException.class, reactor::buildEffective).getCause();
45 assertThat(cause, instanceOf(InferenceException.class));
46 assertThat(cause.getMessage(), startsWith("Yang model processing phase STATEMENT_DEFINITION failed [at "));
48 final var cause1 = cause.getCause();
49 assertThat(cause1, instanceOf(InferenceException.class));
50 assertThat(cause1.getMessage(), startsWith("Unable to resolve identity (cyclic.identity.test)child-identity-1 "
51 + "and base identity (cyclic.identity.test)child-identity-2 [at "));
55 public void identityTest() throws SourceException, ReactorException,
57 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild().addSources(IDENTITY_TEST).buildEffective();
58 assertNotNull(result);
60 Module module = result.findModule("identity-test").orElseThrow();
61 var identities = module.getIdentities();
63 assertNotNull(identities);
64 assertEquals(4, identities.size());
66 IdentitySchemaNode root = null;
67 IdentitySchemaNode child1 = null;
68 IdentitySchemaNode child2 = null;
69 IdentitySchemaNode child12 = null;
70 for (IdentitySchemaNode identitySchemaNode : identities) {
71 switch (identitySchemaNode.getQName().getLocalName()) {
73 root = identitySchemaNode;
75 case "child-identity-1":
76 child1 = identitySchemaNode;
78 case "child-identity-2":
79 child2 = identitySchemaNode;
81 case "child-identity-1-2":
82 child12 = identitySchemaNode;
90 assertNotNull(child1);
91 assertNotNull(child2);
92 assertNotNull(child12);
94 assertTrue(root.getBaseIdentities().isEmpty());
96 var rootDerivedIdentities = result.getDerivedIdentities(root);
97 assertEquals(2, rootDerivedIdentities.size());
99 assertTrue(rootDerivedIdentities.contains(child1));
100 assertTrue(rootDerivedIdentities.contains(child2));
101 assertFalse(rootDerivedIdentities.contains(child12));
102 assertFalse(child1.equals(child2));
104 assertTrue(root == Iterables.getOnlyElement(child1.getBaseIdentities()));
105 assertTrue(root == Iterables.getOnlyElement(child2.getBaseIdentities()));
107 assertEquals(0, result.getDerivedIdentities(child2).size());
109 var child1DerivedIdentities = result.getDerivedIdentities(child1);
110 assertEquals(1, child1DerivedIdentities.size());
111 assertTrue(child1DerivedIdentities.contains(child12));
112 assertFalse(child1DerivedIdentities.contains(child1));
114 assertTrue(child1 == Iterables.getOnlyElement(child12.getBaseIdentities()));
115 assertTrue(child12 == child1DerivedIdentities.iterator().next());