YANGTOOLS-706: Split out yang-parser-rfc7950
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / ChoiceStmtTest.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;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
13
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
17 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
23 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
24
25 public class ChoiceStmtTest {
26
27     private static final StatementStreamSource CHOICE_MODULE = sourceForResource("/model/foo.yang");
28     private static final StatementStreamSource IMPORTED_MODULE1 = sourceForResource("/model/bar.yang");
29     private static final StatementStreamSource IMPORTED_MODULE2 = sourceForResource("/model/baz.yang");
30     private static final StatementStreamSource INCLUDED_MODULE = sourceForResource("/model/subfoo.yang");
31
32     @Test
33     public void choiceAndCaseTest() throws ReactorException {
34         final SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
35                 .addSources(CHOICE_MODULE, IMPORTED_MODULE1, IMPORTED_MODULE2, INCLUDED_MODULE)
36                 .buildEffective();
37         assertNotNull(result);
38
39         final Module testModule = result.findModules("foo").iterator().next();
40         assertNotNull(testModule);
41
42         final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(QName.create(
43                 testModule.getQNameModule(), "transfer"));
44         assertNotNull(container);
45
46         final ChoiceSchemaNode choice = (ChoiceSchemaNode) container.getDataChildByName(QName.create(
47                 testModule.getQNameModule(), "how"));
48         assertNotNull(choice);
49         assertEquals(5, choice.getCases().size());
50
51         ChoiceCaseNode caseNode = choice.findCaseNodes("input").iterator().next();
52         assertNotNull(caseNode);
53         caseNode = choice.findCaseNodes("output").iterator().next();
54         assertNotNull(caseNode);
55         caseNode = choice.findCaseNodes("interval").iterator().next();
56         assertNotNull(caseNode);
57         caseNode = choice.findCaseNodes("daily").iterator().next();
58         assertNotNull(caseNode);
59         caseNode = choice.findCaseNodes("manual").iterator().next();
60         assertNotNull(caseNode);
61         assertEquals("interval", choice.getDefaultCase().get().getQName().getLocalName());
62     }
63 }