Bug 5085: Clean-up test and retest JUnit tests
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / GroupingAndUsesStmtTest.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
9 package org.opendaylight.yangtools.yang.stmt;
10
11 import static org.hamcrest.CoreMatchers.anyOf;
12 import static org.hamcrest.CoreMatchers.is;
13 import static org.hamcrest.MatcherAssert.assertThat;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
26 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.UsesNode;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
34 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
37 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
38
39 public class GroupingAndUsesStmtTest {
40
41     private static final YangStatementSourceImpl MODULE = new YangStatementSourceImpl("/model/bar.yang", false);
42     private static final YangStatementSourceImpl SUBMODULE = new YangStatementSourceImpl("/model/subfoo.yang", false);
43     private static final YangStatementSourceImpl GROUPING_MODULE = new YangStatementSourceImpl("/model/baz.yang",
44             false);
45     private static final YangStatementSourceImpl USES_MODULE = new YangStatementSourceImpl("/model/foo.yang", false);
46
47     @Test
48     public void groupingTest() throws ReactorException {
49         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
50         StmtTestUtils.addSources(reactor, MODULE, GROUPING_MODULE);
51
52         EffectiveSchemaContext result = reactor.buildEffective();
53         assertNotNull(result);
54
55         Module testModule = result.findModuleByName("baz", null);
56         assertNotNull(testModule);
57
58         Set<GroupingDefinition> groupings = testModule.getGroupings();
59         assertEquals(1, groupings.size());
60
61         Iterator<GroupingDefinition> groupingsIterator = groupings.iterator();
62         GroupingDefinition grouping = groupingsIterator.next();
63         assertEquals("target", grouping.getQName().getLocalName());
64         assertEquals(5, grouping.getChildNodes().size());
65
66         AnyXmlSchemaNode anyXmlNode = (AnyXmlSchemaNode) grouping.getDataChildByName("data");
67         assertNotNull(anyXmlNode);
68         ChoiceSchemaNode choiceNode = (ChoiceSchemaNode) grouping.getDataChildByName("how");
69         assertNotNull(choiceNode);
70         LeafSchemaNode leafNode = (LeafSchemaNode) grouping.getDataChildByName("address");
71         assertNotNull(leafNode);
72         ContainerSchemaNode containerNode = (ContainerSchemaNode) grouping.getDataChildByName("port");
73         assertNotNull(containerNode);
74         ListSchemaNode listNode = (ListSchemaNode) grouping.getDataChildByName("addresses");
75         assertNotNull(listNode);
76
77         assertEquals(1, grouping.getGroupings().size());
78         assertEquals("target-inner", grouping.getGroupings().iterator().next().getQName().getLocalName());
79
80         assertEquals(1, grouping.getTypeDefinitions().size());
81         assertEquals("group-type", grouping.getTypeDefinitions().iterator().next().getQName().getLocalName());
82
83         List<UnknownSchemaNode> unknownSchemaNodes = grouping.getUnknownSchemaNodes();
84         assertEquals(1, unknownSchemaNodes.size());
85         UnknownSchemaNode extensionUse = unknownSchemaNodes.get(0);
86         assertEquals("opendaylight", extensionUse.getExtensionDefinition().getQName().getLocalName());
87     }
88
89     @Test
90     public void usesAndRefinesTest() throws ReactorException {
91         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
92         StmtTestUtils.addSources(reactor, MODULE, SUBMODULE, GROUPING_MODULE, USES_MODULE);
93
94         EffectiveSchemaContext result = reactor.buildEffective();
95         assertNotNull(result);
96
97         Module testModule = result.findModuleByName("foo", null);
98         assertNotNull(testModule);
99
100         Set<UsesNode> usesNodes = testModule.getUses();
101         assertEquals(1, usesNodes.size());
102
103         UsesNode usesNode = usesNodes.iterator().next();
104         assertEquals("target", usesNode.getGroupingPath().getLastComponent().getLocalName());
105         assertEquals(1, usesNode.getAugmentations().size());
106
107         ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName("peer");
108         assertNotNull(container);
109         container = (ContainerSchemaNode) container.getDataChildByName("destination");
110         assertEquals(1, container.getUses().size());
111
112         usesNode = container.getUses().iterator().next();
113         assertEquals("target", usesNode.getGroupingPath().getLastComponent().getLocalName());
114
115         Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
116         assertEquals(4, refines.size());
117
118         Iterator<SchemaPath> refinesKeysIterator = refines.keySet().iterator();
119         SchemaPath path = refinesKeysIterator.next();
120         assertThat(path.getLastComponent().getLocalName(), anyOf(is("port"), is("address"), is("addresses"), is("id")));
121         path = refinesKeysIterator.next();
122         assertThat(path.getLastComponent().getLocalName(), anyOf(is("port"), is("address"), is("addresses"), is("id")));
123         path = refinesKeysIterator.next();
124         assertThat(path.getLastComponent().getLocalName(), anyOf(is("port"), is("address"), is("addresses"), is("id")));
125         path = refinesKeysIterator.next();
126         assertThat(path.getLastComponent().getLocalName(), anyOf(is("port"), is("address"), is("addresses"), is("id")));
127     }
128 }