YANGTOOLS-706: Refactor YangInferencePipeline
[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.impl.DefaultReactors;
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 = DefaultReactors.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         assertEquals(1, list.getConstraints().getMinElements().intValue());
54         assertEquals(10, list.getConstraints().getMaxElements().intValue());
55
56         assertEquals(5, list.getChildNodes().size());
57
58         LeafSchemaNode leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(),
59             "key1"));
60         assertNotNull(leaf);
61         assertTrue(leaf.isMandatory());
62         assertEquals("int32", leaf.getType().getQName().getLocalName());
63
64         leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "key2"));
65         assertNotNull(leaf);
66         assertTrue(leaf.isMandatory());
67         assertEquals("int16", leaf.getType().getQName().getLocalName());
68
69         leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "old-leaf"));
70         assertNotNull(leaf);
71         assertFalse(leaf.isMandatory());
72         assertEquals("string", leaf.getType().getQName().getLocalName());
73
74         leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "young-leaf"));
75         assertNotNull(leaf);
76         assertFalse(leaf.isMandatory());
77         assertEquals("young-leaf", leaf.getType().getQName().getLocalName());
78         assertEquals(Optional.of("default-value"), leaf.getType().getDefaultValue());
79
80         final LeafListSchemaNode leafList = (LeafListSchemaNode) list.getDataChildByName(
81             QName.create(testModule.getQNameModule(), "list-of-leaves"));
82         assertNotNull(leafList);
83         assertTrue(leafList.isUserOrdered());
84         assertEquals(2, leafList.getConstraints().getMinElements().intValue());
85         assertEquals(20, leafList.getConstraints().getMaxElements().intValue());
86         assertEquals("string", leafList.getType().getQName().getLocalName());
87     }
88 }