Update StmtTestUtils
[yangtools.git] / yang / yang-parser-impl / 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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import java.util.Date;
17 import java.util.Set;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
21 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
28
29 public class IdentityrefStatementTest {
30
31     @Test
32     public void testIdentityrefWithMultipleBaseIdentities() throws Exception {
33         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/identityref-stmt/foo.yang");
34         assertNotNull(schemaContext);
35
36         final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2017-01-11");
37
38         final Module foo = schemaContext.findModuleByName("foo", revision);
39         assertNotNull(foo);
40
41         final Set<IdentitySchemaNode> identities = foo.getIdentities();
42         assertEquals(3, identities.size());
43
44         final LeafSchemaNode idrefLeaf = (LeafSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
45                 "idref-leaf"));
46         assertNotNull(idrefLeaf);
47
48         final IdentityrefTypeDefinition idrefType = (IdentityrefTypeDefinition) idrefLeaf.getType();
49         final Set<IdentitySchemaNode> referencedIdentities = idrefType.getIdentities();
50         assertEquals(3, referencedIdentities.size());
51         assertEquals(identities, referencedIdentities);
52         assertEquals("id-a", idrefType.getIdentity().getQName().getLocalName());
53     }
54
55     @Test
56     public void testInvalidYang10() throws Exception {
57         try {
58             StmtTestUtils.parseYangSource("/rfc7950/identityref-stmt/foo10.yang");
59             fail("Test should fail due to invalid Yang 1.0");
60         } catch (final ReactorException e) {
61             assertTrue(e.getCause().getMessage().startsWith("Maximal count of BASE for TYPE is 1, detected 3."));
62         }
63     }
64 }