Deprecate simple DataTreeFactory.create()
[yangtools.git] / parser / 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.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
13 import static org.junit.jupiter.api.Assertions.assertTrue;
14
15 import java.util.Optional;
16 import org.junit.jupiter.api.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
22 class ListTest extends AbstractYangTest {
23     @Test
24     void listAndLeavesTest() {
25         final var result = assertEffectiveModel("/list-test/list-test.yang");
26         final var testModule = result.findModules("list-test").iterator().next();
27
28         final var list = assertInstanceOf(ListSchemaNode.class,
29             testModule.getDataChildByName(QName.create(testModule.getQNameModule(), "simple-list")));
30
31         assertTrue(list.isUserOrdered());
32         assertEquals(Optional.of(Boolean.TRUE), list.effectiveConfig());
33         final var keys = list.getKeyDefinition();
34         assertEquals(2, keys.size());
35
36         assertEquals("key1", keys.get(0).getLocalName());
37         assertEquals("key2", keys.get(1).getLocalName());
38
39         var constraint = list.getElementCountConstraint().orElseThrow();
40         assertEquals((Object) 1, constraint.getMinElements());
41         assertEquals((Object) 10, constraint.getMaxElements());
42
43         assertEquals(5, list.getChildNodes().size());
44
45         var leaf = assertInstanceOf(LeafSchemaNode.class,
46             list.getDataChildByName(QName.create(testModule.getQNameModule(), "key1")));
47         assertTrue(leaf.isMandatory());
48         assertEquals("int32", leaf.getType().getQName().getLocalName());
49
50         leaf = assertInstanceOf(LeafSchemaNode.class,
51             list.getDataChildByName(QName.create(testModule.getQNameModule(), "key2")));
52         assertTrue(leaf.isMandatory());
53         assertEquals("int16", leaf.getType().getQName().getLocalName());
54
55         leaf = assertInstanceOf(LeafSchemaNode.class,
56             list.getDataChildByName(QName.create(testModule.getQNameModule(), "old-leaf")));
57         assertFalse(leaf.isMandatory());
58         assertEquals("string", leaf.getType().getQName().getLocalName());
59
60         leaf = assertInstanceOf(LeafSchemaNode.class,
61             list.getDataChildByName(QName.create(testModule.getQNameModule(), "young-leaf")));
62         assertFalse(leaf.isMandatory());
63         assertEquals("young-leaf", leaf.getType().getQName().getLocalName());
64         assertEquals(Optional.of("default-value"), leaf.getType().getDefaultValue());
65
66         final var leafList = assertInstanceOf(LeafListSchemaNode.class,
67             list.getDataChildByName(QName.create(testModule.getQNameModule(), "list-of-leaves")));
68         assertTrue(leafList.isUserOrdered());
69
70         constraint = leafList.getElementCountConstraint().orElseThrow();
71         assertEquals(2, constraint.getMinElements().intValue());
72         assertEquals(20, constraint.getMaxElements().intValue());
73         assertEquals("string", leafList.getType().getQName().getLocalName());
74     }
75 }