Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-parser-rfc7950 / 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.hamcrest.Matchers.isA;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15
16 import java.util.Collection;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Optional;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
29 import org.opendaylight.yangtools.yang.stmt.TestUtils;
30
31 public class YinFileListStmtTest extends AbstractYinModulesTest {
32     @Test
33     public void testListAndLeaves() {
34         final Module testModule = TestUtils.findModule(context, "config").get();
35         assertNotNull(testModule);
36
37         final ListSchemaNode list = (ListSchemaNode) testModule.findDataChildByName(QName.create(
38                 testModule.getQNameModule(), "modules"), QName.create(testModule.getQNameModule(), "module")).get();
39         final List<QName> keys = list.getKeyDefinition();
40         assertEquals(1, keys.size());
41         assertEquals("name", keys.get(0).getLocalName());
42
43         final Collection<DataSchemaNode> children = list.getChildNodes();
44         assertEquals(4, children.size());
45
46         final Iterator<DataSchemaNode> childrenIterator = children.iterator();
47         LeafSchemaNode leaf = (LeafSchemaNode) childrenIterator.next();
48         assertEquals("name", leaf.getQName().getLocalName());
49         assertEquals(Optional.of("Unique module instance name"), leaf.getDescription());
50         assertEquals(BaseTypes.stringType(), leaf.getType());
51         assertTrue(leaf.isMandatory());
52
53         leaf = (LeafSchemaNode) childrenIterator.next();
54         assertEquals("type", leaf.getQName().getLocalName());
55
56         final TypeDefinition<?> leafType = leaf.getType();
57         assertThat(leafType, isA(IdentityrefTypeDefinition.class));
58         assertEquals("module-type", ((IdentityrefTypeDefinition)leafType).getIdentities().iterator().next().getQName()
59             .getLocalName());
60         assertTrue(leaf.isMandatory());
61     }
62 }