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