00ffe4e194c9c9082e5dc868cb4a945eae6f34d3
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / IdentityrefStatementTest.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
9
10 import static org.hamcrest.MatcherAssert.assertThat;
11 import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16
17 import java.util.Collection;
18 import java.util.Set;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.Revision;
22 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
29
30 public class IdentityrefStatementTest {
31
32     @Test
33     public void testIdentityrefWithMultipleBaseIdentities() throws Exception {
34         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/identityref-stmt/foo.yang");
35         assertNotNull(schemaContext);
36
37         final Module foo = schemaContext.findModule("foo", Revision.of("2017-01-11")).get();
38         final Collection<? extends IdentitySchemaNode> identities = foo.getIdentities();
39         assertEquals(3, identities.size());
40
41         final LeafSchemaNode idrefLeaf = (LeafSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
42                 "idref-leaf"));
43         final IdentityrefTypeDefinition idrefType = (IdentityrefTypeDefinition) idrefLeaf.getType();
44         final Set<? extends IdentitySchemaNode> referencedIdentities = idrefType.getIdentities();
45         assertEquals(3, referencedIdentities.size());
46         assertThat(referencedIdentities, containsInAnyOrder(identities.toArray()));
47         assertEquals("id-a", idrefType.getIdentities().iterator().next().getQName().getLocalName());
48     }
49
50     @Test
51     public void testInvalidYang10() throws Exception {
52         try {
53             StmtTestUtils.parseYangSource("/rfc7950/identityref-stmt/foo10.yang");
54             fail("Test should fail due to invalid Yang 1.0");
55         } catch (final ReactorException e) {
56             assertTrue(e.getCause().getMessage().startsWith("Maximal count of BASE for TYPE is 1, detected 3."));
57         }
58     }
59 }