Further cleanup of tests
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / ControllerStmtParserTest.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.junit.Assert.assertTrue;
13
14 import java.util.Collection;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.Revision;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
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.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.UsesNode;
28 import org.opendaylight.yangtools.yang.model.api.stmt.RefineEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.UsesEffectiveStatement;
31
32 public class ControllerStmtParserTest extends AbstractYangTest {
33     @Test
34     public void test() {
35         final var context = assertEffectiveModelDir("/sal-broker-impl");
36         salDomBrokerImplModuleTest(context);
37         configModuleTest(context);
38     }
39
40     private static void salDomBrokerImplModuleTest(final SchemaContext context) {
41         final Module module = context.findModule("opendaylight-sal-dom-broker-impl", Revision.of("2013-10-28")).get();
42
43         boolean checked = false;
44         for (final AugmentationSchemaNode augmentationSchema : module.getAugmentations()) {
45             final DataSchemaNode dataNode = augmentationSchema.dataChildByName(
46                 QName.create(module.getQNameModule(), "dom-broker-impl"));
47             if (dataNode instanceof CaseSchemaNode) {
48                 final CaseSchemaNode caseNode = (CaseSchemaNode) dataNode;
49                 final DataSchemaNode dataNode2 = caseNode.dataChildByName(
50                     QName.create(module.getQNameModule(), "async-data-broker"));
51                 if (dataNode2 instanceof ContainerSchemaNode) {
52                     final ContainerSchemaNode containerNode = (ContainerSchemaNode) dataNode2;
53                     final DataSchemaNode leaf = containerNode
54                             .getDataChildByName(QName.create(module.getQNameModule(), "type"));
55                     assertEquals(0, leaf.getUnknownSchemaNodes().size());
56
57                     final Collection<? extends UnrecognizedStatement> unknownSchemaNodes =
58                         containerNode.asEffectiveStatement()
59                             .findFirstEffectiveSubstatement(UsesEffectiveStatement.class).orElseThrow()
60                             .findFirstEffectiveSubstatement(RefineEffectiveStatement.class).orElseThrow()
61                             .getDeclared().declaredSubstatements(UnrecognizedStatement.class);
62
63
64                     final UnrecognizedStatement unknownSchemaNode = unknownSchemaNodes.iterator().next();
65                     assertEquals("sal:dom-async-data-broker", unknownSchemaNode.argument());
66                     checked = true;
67                 }
68             }
69         }
70         assertTrue(checked);
71     }
72
73     private static void configModuleTest(final SchemaContext context) {
74         final Module configModule = context.findModule("config", Revision.of("2013-04-05")).get();
75         final Module module = context.findModule("opendaylight-sal-dom-broker-impl", Revision.of("2013-10-28")).get();
76
77         final DataSchemaNode dataNode = configModule.getDataChildByName(QName.create(configModule.getQNameModule(),
78             "modules"));
79         assertTrue(dataNode instanceof ContainerSchemaNode);
80
81         final ContainerSchemaNode moduleContainer = (ContainerSchemaNode) dataNode;
82         final DataSchemaNode dataChildList = moduleContainer
83                 .getDataChildByName(QName.create(configModule.getQNameModule(), "module"));
84
85         assertTrue(dataChildList instanceof ListSchemaNode);
86
87         final ListSchemaNode listModule = (ListSchemaNode) dataChildList;
88         final DataSchemaNode dataChildChoice = listModule
89                 .getDataChildByName(QName.create(configModule.getQNameModule(), "configuration"));
90
91         assertTrue(dataChildChoice instanceof ChoiceSchemaNode);
92
93         final ChoiceSchemaNode confChoice = (ChoiceSchemaNode) dataChildChoice;
94         final CaseSchemaNode caseNodeByName = confChoice.findCaseNodes("dom-broker-impl").iterator().next();
95
96         assertNotNull(caseNodeByName);
97         final DataSchemaNode dataNode2 = caseNodeByName
98                 .getDataChildByName(QName.create(module.getQNameModule(), "async-data-broker"));
99         assertTrue(dataNode2 instanceof ContainerSchemaNode);
100
101         final ContainerSchemaNode containerNode = (ContainerSchemaNode) dataNode2;
102         final DataSchemaNode leaf = containerNode.getDataChildByName(QName.create(module.getQNameModule(), "type"));
103         assertEquals(0, leaf.getUnknownSchemaNodes().size());
104
105         final CaseSchemaNode domInmemoryDataBroker = confChoice.findCaseNodes("dom-inmemory-data-broker").iterator()
106                 .next();
107
108         assertNotNull(domInmemoryDataBroker);
109         final DataSchemaNode schemaService = domInmemoryDataBroker
110                 .getDataChildByName(QName.create(module.getQNameModule(), "schema-service"));
111         assertTrue(schemaService instanceof ContainerSchemaNode);
112
113         final ContainerSchemaNode schemaServiceContainer = (ContainerSchemaNode) schemaService;
114
115         assertEquals(1, schemaServiceContainer.getUses().size());
116         final UsesNode uses = schemaServiceContainer.getUses().iterator().next();
117         final QName groupingQName = QName.create("urn:opendaylight:params:xml:ns:yang:controller:config", "2013-04-05",
118             "service-ref");
119         assertEquals(groupingQName, uses.getSourceGrouping().getQName());
120         assertEquals(0, getChildNodeSizeWithoutUses(schemaServiceContainer));
121
122         final DataSchemaNode type = schemaServiceContainer.getDataChildByName(QName.create(module.getQNameModule(),
123             "type"));
124         assertEquals(0, type.getUnknownSchemaNodes().size());
125
126         final Collection<? extends UnrecognizedStatement> typeUnknownSchemaNodes =
127             schemaServiceContainer.asEffectiveStatement()
128                 .findFirstEffectiveSubstatement(UsesEffectiveStatement.class).orElseThrow()
129                 .findFirstEffectiveSubstatement(RefineEffectiveStatement.class).orElseThrow()
130                 .getDeclared().declaredSubstatements(UnrecognizedStatement.class);
131         assertEquals(1, typeUnknownSchemaNodes.size());
132
133
134         final UnrecognizedStatement typeUnknownSchemaNode = typeUnknownSchemaNodes.iterator().next();
135         assertEquals("sal:schema-service", typeUnknownSchemaNode.argument());
136         assertEquals(QName.create(groupingQName, "required-identity"),
137             typeUnknownSchemaNode.statementDefinition().getStatementName());
138     }
139
140     private static int getChildNodeSizeWithoutUses(final DataNodeContainer csn) {
141         int result = 0;
142         for (final DataSchemaNode dsn : csn.getChildNodes()) {
143             if (!dsn.isAddedByUses()) {
144                 result++;
145             }
146         }
147         return result;
148     }
149 }