8dffff3c9ae9873fbf1b4e23348137338b994b9e
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / yin / YinFileListStmtTest.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.yin;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.net.URISyntaxException;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Set;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31 import org.opendaylight.yangtools.yang.stmt.TestUtils;
32
33 public class YinFileListStmtTest {
34
35     private Set<Module> modules;
36
37     @Before
38     public void init() throws URISyntaxException, ReactorException {
39         modules = TestUtils.loadYinModules(getClass().getResource("/semantic-statement-parser/yin/modules").toURI());
40         assertEquals(9, modules.size());
41     }
42
43     @Test
44     public void testListAndLeaves() throws URISyntaxException {
45         final Module testModule = TestUtils.findModule(modules, "config");
46         assertNotNull(testModule);
47
48         final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(QName.create(
49                 testModule.getQNameModule(), "modules"));
50         assertNotNull(container);
51
52         final ListSchemaNode list = (ListSchemaNode) container.getDataChildByName(QName.create(testModule.getQNameModule(),
53                 "module"));
54         assertNotNull(list);
55         final List<QName> keys = list.getKeyDefinition();
56         assertEquals(1, keys.size());
57         assertEquals("name", keys.get(0).getLocalName());
58
59         final Collection<DataSchemaNode> children = list.getChildNodes();
60         assertEquals(4, children.size());
61
62         final Iterator<DataSchemaNode> childrenIterator = children.iterator();
63         LeafSchemaNode leaf = (LeafSchemaNode) childrenIterator.next();
64         assertEquals("name", leaf.getQName().getLocalName());
65         assertEquals("Unique module instance name", leaf.getDescription());
66         assertEquals(BaseTypes.stringType(), leaf.getType());
67         assertTrue(leaf.getConstraints().isMandatory());
68
69         leaf = (LeafSchemaNode) childrenIterator.next();
70         assertEquals("type", leaf.getQName().getLocalName());
71
72         final TypeDefinition<?> leafType = leaf.getType();
73         assertTrue(leafType instanceof IdentityrefTypeDefinition);
74         assertEquals("module-type", ((IdentityrefTypeDefinition)leafType).getIdentity().getQName().getLocalName());
75         assertTrue(leaf.getConstraints().isMandatory());
76     }
77
78 }