Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / LeafrefStatementTest.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.Set;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.Revision;
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
27
28 public class LeafrefStatementTest {
29
30     @Test
31     public void testRequireInstanceInLeafrefs() throws Exception {
32         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/leafref-stmt/foo.yang");
33         assertNotNull(schemaContext);
34
35         final Module foo = schemaContext.findModule("foo", Revision.of("2016-12-20")).get();
36         final Set<TypeDefinition<?>> typeDefinitions = foo.getTypeDefinitions();
37         assertEquals(1, typeDefinitions.size());
38
39         final TypeDefinition<?> typeDefinition = typeDefinitions.iterator().next();
40         final LeafrefTypeDefinition leafrefTypeDefinition = (LeafrefTypeDefinition) typeDefinition;
41         assertTrue(leafrefTypeDefinition.requireInstance());
42
43         final LeafSchemaNode leafrefA = (LeafSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
44                 "leafref-a"));
45         assertNotNull(leafrefA);
46         assertRequireInstanceInLeafref(leafrefA, true);
47
48         final LeafSchemaNode leafrefB = (LeafSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
49                 "leafref-b"));
50         assertNotNull(leafrefB);
51         assertRequireInstanceInLeafref(leafrefB, true);
52
53         final LeafSchemaNode leafrefC = (LeafSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
54                 "leafref-c"));
55         assertNotNull(leafrefC);
56         assertRequireInstanceInLeafref(leafrefC, false);
57     }
58
59     private static void assertRequireInstanceInLeafref(final LeafSchemaNode leaf, final boolean requireInstance) {
60         final LeafrefTypeDefinition leafrefTypeDefnition = (LeafrefTypeDefinition) leaf.getType();
61         assertEquals(requireInstance, leafrefTypeDefnition.requireInstance());
62     }
63
64     @Test
65     public void testInvalidYang10() throws Exception {
66         try {
67             StmtTestUtils.parseYangSource("/rfc7950/leafref-stmt/foo10.yang");
68             fail("Test should fail due to invalid Yang 1.0");
69         } catch (final ReactorException e) {
70             assertTrue(e.getCause().getMessage().startsWith("REQUIRE_INSTANCE is not valid for TYPE"));
71         }
72     }
73 }