Bug 2366 - Effective statements impl for new yang parser.
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / AugmentProcessTest.java
1 package org.opendaylight.yangtools.yang.stmt.test;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5
6 import org.junit.Test;
7 import org.opendaylight.yangtools.yang.common.QName;
8 import org.opendaylight.yangtools.yang.common.QNameModule;
9 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
10 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
11 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
12 import org.opendaylight.yangtools.yang.model.api.Module;
13 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
14 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
15 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
16 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
17 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
18 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
19 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
20
21 import java.net.URI;
22
23 public class AugmentProcessTest {
24
25     private static final YangStatementSourceImpl AUGMENTED = new YangStatementSourceImpl(
26             "/stmt-test/effective-build/augmented.yang");
27     private static final YangStatementSourceImpl ROOT = new YangStatementSourceImpl(
28             "/stmt-test/effective-build/aug-root.yang");
29
30     private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule.create(URI.create("aug"), null);
31
32     QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE, "aug-parent1");
33     QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE, "aug-parent2");
34     QName contTarget = QName.create(AUGMENTED_QNAME_MODULE, "cont-target");
35
36     QName contAdded1 = QName.create(AUGMENTED_QNAME_MODULE, "cont-added1");
37     QName contAdded2 = QName.create(AUGMENTED_QNAME_MODULE, "cont-added2");
38
39     QName list1 = QName.create(AUGMENTED_QNAME_MODULE, "list1");
40     QName axml = QName.create(AUGMENTED_QNAME_MODULE, "axml");
41
42     QName contGrp = QName.create(AUGMENTED_QNAME_MODULE, "cont-grp");
43     QName axmlGrp = QName.create(AUGMENTED_QNAME_MODULE, "axml-grp");
44
45     @Test
46     public void readAndParseYangFileTest() throws SourceException, ReactorException {
47
48         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
49         addSources(reactor, AUGMENTED, ROOT);
50
51         final EffectiveSchemaContext result = reactor.buildEffective();
52
53         assertNotNull(result);
54
55         Module augmentedModule = result.findModuleByName("augmented", null);
56         assertNotNull(augmentedModule);
57
58         ContainerSchemaNode augParent1Node = (ContainerSchemaNode) result.getDataChildByName(augParent1);
59         ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node.getDataChildByName(augParent2);
60         ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node.getDataChildByName(contTarget);
61         assertNotNull(targetContNode);
62
63         assertNotNull(targetContNode.getChildNodes());
64         assertEquals(3, targetContNode.getChildNodes().size());
65
66         ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded1);
67         assertNotNull(contAdded1Node);
68         ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node.getDataChildByName(list1);
69         assertNotNull(list1Node);
70
71         ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded2);
72         assertNotNull(contAdded2Node);
73         AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node.getDataChildByName(axml);
74         assertNotNull(axmlNode);
75
76         ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode.getDataChildByName(contGrp);
77         assertNotNull(contGrpNode);
78         AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode.getDataChildByName(axmlGrp);
79         assertNotNull(axmlGrpNode);
80     }
81
82     private void addSources(CrossSourceStatementReactor.BuildAction reactor, StatementStreamSource... sources) {
83         for (StatementStreamSource source : sources) {
84             reactor.addSource(source);
85         }
86     }
87 }