/* * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.stmt; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource; import com.google.common.collect.Iterables; import java.net.URISyntaxException; import java.util.Collection; import org.junit.Test; import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource; import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor; public class EffectiveIdentityTest { private static final StatementStreamSource IDENTITY_TEST = sourceForResource( "/stmt-test/identity/identity-test.yang"); private static final StatementStreamSource CYCLIC_IDENTITY_TEST = sourceForResource( "/stmt-test/identity/cyclic-identity-test.yang"); @Test(expected = SomeModifiersUnresolvedException.class) public void cyclicefineTest() throws SourceException, ReactorException, URISyntaxException { CrossSourceStatementReactor.BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild() .addSources(CYCLIC_IDENTITY_TEST); try { reactor.buildEffective(); } catch (SomeModifiersUnresolvedException e) { StmtTestUtils.log(e, " "); throw e; } } @Test public void identityTest() throws SourceException, ReactorException, URISyntaxException { SchemaContext result = RFC7950Reactors.defaultReactor().newBuild().addSources(IDENTITY_TEST).buildEffective(); assertNotNull(result); Module module = result.findModule("identity-test").get(); Collection identities = module.getIdentities(); assertNotNull(identities); assertEquals(4, identities.size()); IdentitySchemaNode root = null; IdentitySchemaNode child1 = null; IdentitySchemaNode child2 = null; IdentitySchemaNode child12 = null; for (IdentitySchemaNode identitySchemaNode : identities) { switch (identitySchemaNode.getQName().getLocalName()) { case "root-identity": root = identitySchemaNode; break; case "child-identity-1": child1 = identitySchemaNode; break; case "child-identity-2": child2 = identitySchemaNode; break; case "child-identity-1-2": child12 = identitySchemaNode; break; default: break; } } assertNotNull(root); assertNotNull(child1); assertNotNull(child2); assertNotNull(child12); assertTrue(root.getBaseIdentities().isEmpty()); Collection rootDerivedIdentities = result.getDerivedIdentities(root); assertEquals(2, rootDerivedIdentities.size()); assertTrue(rootDerivedIdentities.contains(child1)); assertTrue(rootDerivedIdentities.contains(child2)); assertFalse(rootDerivedIdentities.contains(child12)); assertFalse(child1.equals(child2)); assertTrue(root == Iterables.getOnlyElement(child1.getBaseIdentities())); assertTrue(root == Iterables.getOnlyElement(child2.getBaseIdentities())); assertEquals(0, result.getDerivedIdentities(child2).size()); Collection child1DerivedIdentities = result.getDerivedIdentities(child1); assertEquals(1, child1DerivedIdentities.size()); assertTrue(child1DerivedIdentities.contains(child12)); assertFalse(child1DerivedIdentities.contains(child1)); assertTrue(child1 == Iterables.getOnlyElement(child12.getBaseIdentities())); assertTrue(child12 == child1DerivedIdentities.iterator().next()); } }