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.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import java.io.IOException;
16 import java.net.URISyntaxException;
17 import java.util.Iterator;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
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.SchemaContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.opendaylight.yangtools.yang.stmt.TestUtils;
29 import org.xml.sax.SAXException;
31 public class YinFileChoiceStmtTest {
33 private SchemaContext context;
36 public void init() throws ReactorException, SAXException, IOException, URISyntaxException {
37 context = TestUtils.loadYinModules(getClass().getResource("/semantic-statement-parser/yin/modules").toURI());
38 assertEquals(9, context.getModules().size());
42 public void testChoiceAndCases() {
43 final Module testModule = TestUtils.findModule(context, "config").get();
44 assertNotNull(testModule);
46 final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(QName.create(
47 testModule.getQNameModule(), "modules"));
48 assertNotNull(container);
50 final ListSchemaNode list = (ListSchemaNode) container.getDataChildByName(QName.create(
51 testModule.getQNameModule(), "module"));
54 ChoiceSchemaNode choice = (ChoiceSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(),
56 assertNotNull(choice);
58 assertEquals("configuration", choice.getQName().getLocalName());
59 assertTrue(choice.getConstraints().isMandatory());
60 assertTrue(choice.isConfiguration());
61 assertEquals(1, choice.getCases().size());
63 // this choice is augmented (see main-impl.yang.xml)
64 final Iterator<ChoiceCaseNode> casesIterator = choice.getCases().values().iterator();
65 final ChoiceCaseNode caseNode = casesIterator.next();
66 assertEquals("main-impl", caseNode.getQName().getLocalName());
67 assertEquals(13, caseNode.getChildNodes().size());
69 assertTrue(caseNode.getConstraints().getWhenCondition().isPresent());
71 choice = (ChoiceSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "state"));
72 assertNotNull(choice);
74 assertEquals("state", choice.getQName().getLocalName());
75 assertFalse(choice.getConstraints().isMandatory());
76 assertFalse(choice.isConfiguration());
77 assertTrue(choice.getCases().isEmpty());