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