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.assertNotNull;
13 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
23 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
24 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
27 public class ChoiceStmtTest {
29 private static final StatementStreamSource CHOICE_MODULE = sourceForResource("/model/foo.yang");
30 private static final StatementStreamSource IMPORTED_MODULE1 = sourceForResource("/model/bar.yang");
31 private static final StatementStreamSource IMPORTED_MODULE2 = sourceForResource("/model/baz.yang");
32 private static final StatementStreamSource INCLUDED_MODULE = sourceForResource("/model/subfoo.yang");
35 public void choiceAndCaseTest() throws ReactorException {
36 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
37 reactor.addSources(CHOICE_MODULE, IMPORTED_MODULE1, IMPORTED_MODULE2, INCLUDED_MODULE);
39 final SchemaContext result = reactor.buildEffective();
40 assertNotNull(result);
42 final Module testModule = result.findModules("foo").iterator().next();
43 assertNotNull(testModule);
45 final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(QName.create(
46 testModule.getQNameModule(), "transfer"));
47 assertNotNull(container);
49 final ChoiceSchemaNode choice = (ChoiceSchemaNode) container.getDataChildByName(QName.create(
50 testModule.getQNameModule(), "how"));
51 assertNotNull(choice);
52 assertEquals(5, choice.getCases().size());
54 ChoiceCaseNode caseNode = choice.findCaseNodes("input").iterator().next();
55 assertNotNull(caseNode);
56 caseNode = choice.findCaseNodes("output").iterator().next();
57 assertNotNull(caseNode);
58 caseNode = choice.findCaseNodes("interval").iterator().next();
59 assertNotNull(caseNode);
60 caseNode = choice.findCaseNodes("daily").iterator().next();
61 assertNotNull(caseNode);
62 caseNode = choice.findCaseNodes("manual").iterator().next();
63 assertNotNull(caseNode);
64 assertEquals("interval", choice.getDefaultCase().get().getQName().getLocalName());