Fix get{Min,Max}Elements() usage
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / ListTest.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.yangtools.yang.stmt;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
15
16 import java.util.List;
17 import java.util.Optional;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
21 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28
29 public class ListTest {
30
31     @Test
32     public void listAndLeavesTest() throws ReactorException {
33         final SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
34                 .addSource(sourceForResource("/list-test/list-test.yang"))
35                 .buildEffective();
36         assertNotNull(result);
37
38         final Module testModule = result.findModules("list-test").iterator().next();
39         assertNotNull(testModule);
40
41         final ListSchemaNode list = (ListSchemaNode) testModule.getDataChildByName(
42             QName.create(testModule.getQNameModule(), "simple-list"));
43         assertNotNull(list);
44
45         assertTrue(list.isUserOrdered());
46         assertTrue(list.isConfiguration());
47         final List<QName> keys = list.getKeyDefinition();
48         assertEquals(2, keys.size());
49
50         assertEquals("key1", keys.get(0).getLocalName());
51         assertEquals("key2", keys.get(1).getLocalName());
52
53         ElementCountConstraint constraint = list.getElementCountConstraint().get();
54         assertEquals((Object) 1, constraint.getMinElements());
55         assertEquals((Object) 10, constraint.getMaxElements());
56
57         assertEquals(5, list.getChildNodes().size());
58
59         LeafSchemaNode leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(),
60             "key1"));
61         assertNotNull(leaf);
62         assertTrue(leaf.isMandatory());
63         assertEquals("int32", leaf.getType().getQName().getLocalName());
64
65         leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "key2"));
66         assertNotNull(leaf);
67         assertTrue(leaf.isMandatory());
68         assertEquals("int16", leaf.getType().getQName().getLocalName());
69
70         leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "old-leaf"));
71         assertNotNull(leaf);
72         assertFalse(leaf.isMandatory());
73         assertEquals("string", leaf.getType().getQName().getLocalName());
74
75         leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "young-leaf"));
76         assertNotNull(leaf);
77         assertFalse(leaf.isMandatory());
78         assertEquals("young-leaf", leaf.getType().getQName().getLocalName());
79         assertEquals(Optional.of("default-value"), leaf.getType().getDefaultValue());
80
81         final LeafListSchemaNode leafList = (LeafListSchemaNode) list.getDataChildByName(
82             QName.create(testModule.getQNameModule(), "list-of-leaves"));
83         assertNotNull(leafList);
84         assertTrue(leafList.isUserOrdered());
85
86         constraint = leafList.getElementCountConstraint().get();
87         assertEquals(2, constraint.getMinElements().intValue());
88         assertEquals(20, constraint.getMaxElements().intValue());
89         assertEquals("string", leafList.getType().getQName().getLocalName());
90     }
91 }