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