Update TypeDefinition design
[yangtools.git] / yang / yang-parser-impl / 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.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.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
28 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
30
31 public class ListTest {
32
33     private static final StatementStreamSource LIST_MODULE = sourceForResource("/list-test/list-test.yang");
34
35     @Test
36     public void listAndLeavesTest() throws ReactorException {
37         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
38         reactor.addSources(LIST_MODULE);
39
40         final SchemaContext result = reactor.buildEffective();
41         assertNotNull(result);
42
43         final Module testModule = result.findModules("list-test").iterator().next();
44         assertNotNull(testModule);
45
46         final ListSchemaNode list = (ListSchemaNode) testModule.getDataChildByName(
47             QName.create(testModule.getQNameModule(), "simple-list"));
48         assertNotNull(list);
49
50         assertTrue(list.isUserOrdered());
51         assertTrue(list.isConfiguration());
52         final List<QName> keys = list.getKeyDefinition();
53         assertEquals(2, keys.size());
54
55         assertEquals("key1", keys.get(0).getLocalName());
56         assertEquals("key2", keys.get(1).getLocalName());
57
58         assertEquals(1, list.getConstraints().getMinElements().intValue());
59         assertEquals(10, list.getConstraints().getMaxElements().intValue());
60
61         assertEquals(5, list.getChildNodes().size());
62
63         LeafSchemaNode leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(),
64             "key1"));
65         assertNotNull(leaf);
66         assertTrue(leaf.getConstraints().isMandatory());
67         assertEquals("int32", leaf.getType().getQName().getLocalName());
68
69         leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "key2"));
70         assertNotNull(leaf);
71         assertTrue(leaf.getConstraints().isMandatory());
72         assertEquals("int16", leaf.getType().getQName().getLocalName());
73
74         leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "old-leaf"));
75         assertNotNull(leaf);
76         assertFalse(leaf.getConstraints().isMandatory());
77         assertEquals("string", leaf.getType().getQName().getLocalName());
78
79         leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "young-leaf"));
80         assertNotNull(leaf);
81         assertFalse(leaf.getConstraints().isMandatory());
82         assertEquals("young-leaf", leaf.getType().getQName().getLocalName());
83         assertEquals(Optional.of("default-value"), leaf.getType().getDefaultValue());
84
85         final LeafListSchemaNode leafList = (LeafListSchemaNode) list.getDataChildByName(
86             QName.create(testModule.getQNameModule(), "list-of-leaves"));
87         assertNotNull(leafList);
88         assertTrue(leafList.getConstraints().isMandatory());
89         assertTrue(leafList.isUserOrdered());
90         assertEquals(2, leafList.getConstraints().getMinElements().intValue());
91         assertEquals(20, leafList.getConstraints().getMaxElements().intValue());
92         assertEquals("string", leafList.getType().getQName().getLocalName());
93     }
94 }