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