Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / ListTest.java
1 /*
2  * Copyright (c) 2015 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.stmt.test;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
23 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
27
28 public class ListTest {
29
30     private static final YangStatementSourceImpl LIST_MODULE = new YangStatementSourceImpl("/list-test/list-test.yang",
31             false);
32
33     @Test
34     public void listAndLeavesTest() throws ReactorException {
35         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
36         StmtTestUtils.addSources(reactor, LIST_MODULE);
37
38         EffectiveSchemaContext result = reactor.buildEffective();
39         assertNotNull(result);
40
41         Module testModule = result.findModuleByName("list-test", null);
42         assertNotNull(testModule);
43
44         ListSchemaNode list = (ListSchemaNode) testModule.getDataChildByName("simple-list");
45         assertNotNull(list);
46
47         assertTrue(list.isUserOrdered());
48         assertTrue(list.isConfiguration());
49         List<QName> keys = list.getKeyDefinition();
50         assertEquals(2, keys.size());
51
52         assertEquals("key1", keys.get(0).getLocalName());
53         assertEquals("key2", keys.get(1).getLocalName());
54
55         assertEquals(1, list.getConstraints().getMinElements().intValue());
56         assertEquals(10, list.getConstraints().getMaxElements().intValue());
57
58         assertEquals(5, list.getChildNodes().size());
59
60         LeafSchemaNode leaf = (LeafSchemaNode) list.getDataChildByName("key1");
61         assertNotNull(leaf);
62         assertTrue(leaf.getConstraints().isMandatory());
63         assertEquals("int32", leaf.getType().getQName().getLocalName());
64
65         leaf = (LeafSchemaNode) list.getDataChildByName("key2");
66         assertNotNull(leaf);
67         assertTrue(leaf.getConstraints().isMandatory());
68         assertEquals("int16", leaf.getType().getQName().getLocalName());
69
70         leaf = (LeafSchemaNode) list.getDataChildByName("old-leaf");
71         assertNotNull(leaf);
72         assertFalse(leaf.getConstraints().isMandatory());
73         assertEquals("string", leaf.getType().getQName().getLocalName());
74
75         leaf = (LeafSchemaNode) list.getDataChildByName("young-leaf");
76         assertNotNull(leaf);
77         assertFalse(leaf.getConstraints().isMandatory());
78         assertEquals("young-leaf", leaf.getType().getQName().getLocalName());
79         assertEquals("default-value", leaf.getDefault());
80
81         LeafListSchemaNode leafList = (LeafListSchemaNode) list.getDataChildByName("list-of-leaves");
82         assertNotNull(leafList);
83         assertTrue(leafList.getConstraints().isMandatory());
84         assertTrue(leafList.isUserOrdered());
85         assertEquals(2, leafList.getConstraints().getMinElements().intValue());
86         assertEquals(20, leafList.getConstraints().getMaxElements().intValue());
87         assertEquals("string", leafList.getType().getQName().getLocalName());
88     }
89 }