2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.stmt.yin;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
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 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.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.stmt.TestUtils;
33 import org.xml.sax.SAXException;
35 public class YinFileListStmtTest {
37 private SchemaContext context;
40 public void init() throws URISyntaxException, ReactorException, SAXException, IOException {
41 context = TestUtils.loadYinModules(getClass().getResource("/semantic-statement-parser/yin/modules").toURI());
42 assertEquals(9, context.getModules().size());
46 public void testListAndLeaves() {
47 final Module testModule = TestUtils.findModule(context, "config").get();
48 assertNotNull(testModule);
50 final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(QName.create(
51 testModule.getQNameModule(), "modules"));
52 assertNotNull(container);
54 final ListSchemaNode list = (ListSchemaNode) container.getDataChildByName(
55 QName.create(testModule.getQNameModule(), "module"));
57 final List<QName> keys = list.getKeyDefinition();
58 assertEquals(1, keys.size());
59 assertEquals("name", keys.get(0).getLocalName());
61 final Collection<DataSchemaNode> children = list.getChildNodes();
62 assertEquals(4, children.size());
64 final Iterator<DataSchemaNode> childrenIterator = children.iterator();
65 LeafSchemaNode leaf = (LeafSchemaNode) childrenIterator.next();
66 assertEquals("name", leaf.getQName().getLocalName());
67 assertEquals("Unique module instance name", leaf.getDescription());
68 assertEquals(BaseTypes.stringType(), leaf.getType());
69 assertTrue(leaf.getConstraints().isMandatory());
71 leaf = (LeafSchemaNode) childrenIterator.next();
72 assertEquals("type", leaf.getQName().getLocalName());
74 final TypeDefinition<?> leafType = leaf.getType();
75 assertTrue(leafType instanceof IdentityrefTypeDefinition);
76 assertEquals("module-type", ((IdentityrefTypeDefinition)leafType).getIdentity().getQName().getLocalName());
77 assertTrue(leaf.getConstraints().isMandatory());