8a912acc0433ab6b4102d292414228f9a8d2557d
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / yin / YinFileChoiceStmtTest.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.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.net.URISyntaxException;
16 import java.util.Iterator;
17 import java.util.Set;
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.RevisionAwareXPath;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.opendaylight.yangtools.yang.stmt.TestUtils;
29
30 public class YinFileChoiceStmtTest {
31
32     private Set<Module> modules;
33
34     @Before
35     public void init() throws URISyntaxException, ReactorException {
36         modules = TestUtils.loadYinModules(getClass().getResource("/semantic-statement-parser/yin/modules").toURI());
37         assertEquals(9, modules.size());
38     }
39
40     @Test
41     public void testChoiceAndCases() {
42         final Module testModule = TestUtils.findModule(modules, "config");
43         assertNotNull(testModule);
44
45         final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(QName.create(
46                 testModule.getQNameModule(), "modules"));
47         assertNotNull(container);
48
49         final ListSchemaNode list = (ListSchemaNode) container.getDataChildByName(QName.create(
50                 testModule.getQNameModule(), "module"));
51         assertNotNull(list);
52
53         ChoiceSchemaNode choice = (ChoiceSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(),
54                 "configuration"));
55         assertNotNull(choice);
56
57         assertEquals("configuration", choice.getQName().getLocalName());
58         assertTrue(choice.getConstraints().isMandatory());
59         assertTrue(choice.isConfiguration());
60         assertEquals(1, choice.getCases().size());
61
62         // this choice is augmented (see main-impl.yang.xml)
63         final Iterator<ChoiceCaseNode> casesIterator = choice.getCases().iterator();
64         final ChoiceCaseNode caseNode = casesIterator.next();
65         assertEquals("main-impl", caseNode.getQName().getLocalName());
66         assertEquals(13, caseNode.getChildNodes().size());
67
68         final RevisionAwareXPath whenCondition = caseNode.getConstraints().getWhenCondition();
69         assertNotNull(whenCondition);
70
71         choice = (ChoiceSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "state"));
72         assertNotNull(choice);
73
74         assertEquals("state", choice.getQName().getLocalName());
75         assertFalse(choice.getConstraints().isMandatory());
76         assertFalse(choice.isConfiguration());
77         assertTrue(choice.getCases().isEmpty());
78     }
79 }