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