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
9 package org.opendaylight.yangtools.yang.stmt;
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;
17 import java.util.List;
18 import java.util.Optional;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
28 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
31 public class ListTest {
33 private static final StatementStreamSource LIST_MODULE = sourceForResource("/list-test/list-test.yang");
36 public void listAndLeavesTest() throws ReactorException {
37 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
38 reactor.addSources(LIST_MODULE);
40 final SchemaContext result = reactor.buildEffective();
41 assertNotNull(result);
43 final Module testModule = result.findModules("list-test").iterator().next();
44 assertNotNull(testModule);
46 final ListSchemaNode list = (ListSchemaNode) testModule.getDataChildByName(
47 QName.create(testModule.getQNameModule(), "simple-list"));
50 assertTrue(list.isUserOrdered());
51 assertTrue(list.isConfiguration());
52 final List<QName> keys = list.getKeyDefinition();
53 assertEquals(2, keys.size());
55 assertEquals("key1", keys.get(0).getLocalName());
56 assertEquals("key2", keys.get(1).getLocalName());
58 assertEquals(1, list.getConstraints().getMinElements().intValue());
59 assertEquals(10, list.getConstraints().getMaxElements().intValue());
61 assertEquals(5, list.getChildNodes().size());
63 LeafSchemaNode leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(),
66 assertTrue(leaf.getConstraints().isMandatory());
67 assertEquals("int32", leaf.getType().getQName().getLocalName());
69 leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "key2"));
71 assertTrue(leaf.getConstraints().isMandatory());
72 assertEquals("int16", leaf.getType().getQName().getLocalName());
74 leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "old-leaf"));
76 assertFalse(leaf.getConstraints().isMandatory());
77 assertEquals("string", leaf.getType().getQName().getLocalName());
79 leaf = (LeafSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "young-leaf"));
81 assertFalse(leaf.getConstraints().isMandatory());
82 assertEquals("young-leaf", leaf.getType().getQName().getLocalName());
83 assertEquals(Optional.of("default-value"), leaf.getType().getDefaultValue());
85 final LeafListSchemaNode leafList = (LeafListSchemaNode) list.getDataChildByName(
86 QName.create(testModule.getQNameModule(), "list-of-leaves"));
87 assertNotNull(leafList);
88 assertTrue(leafList.getConstraints().isMandatory());
89 assertTrue(leafList.isUserOrdered());
90 assertEquals(2, leafList.getConstraints().getMinElements().intValue());
91 assertEquals(20, leafList.getConstraints().getMaxElements().intValue());
92 assertEquals("string", leafList.getType().getQName().getLocalName());