Test for some parts of RefineUtils class
[mdsal.git] / code-generator / binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / generator / impl / RefineTest.java
1 /*
2  * Copyright (c) 2013 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.sal.binding.generator.impl;
9
10 import static org.junit.Assert.*;
11 import static org.opendaylight.yangtools.sal.binding.generator.impl.SupportTestUtil.*;
12
13 import java.io.File;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Set;
17
18 import org.junit.Test;
19 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
20 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
22 import org.opendaylight.yangtools.sal.binding.model.api.Type;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
24 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
30 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
31 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
32
33 //Test for class RefineUtils
34 public class RefineTest {
35
36     private static List<File> testModels = new ArrayList<>();
37
38     private void loadTestResources() {
39         final File listModelFile = new File(RefineTest.class.getResource("/refine.yang").getPath());
40         testModels.add(listModelFile);
41     }
42
43     private void findUnknownNode(DataSchemaNode childNode, String unknownNodeValue, String unknownNodeName) {
44         List<UnknownSchemaNode> unknownSchemaNodes = childNode.getUnknownSchemaNodes();
45         boolean refinedUnknownNodeLflstFound = false;
46
47         for (UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
48             if (unknownSchemaNode.getNodeType().getLocalName().equals(unknownNodeName)
49                     && unknownSchemaNode.getQName().getLocalName().equals(unknownNodeValue)) {
50                 refinedUnknownNodeLflstFound = true;
51             }
52         }
53         assertTrue("Unknown node " + unknownNodeName + " with value " + unknownNodeValue + " wasn't found.",
54                 refinedUnknownNodeLflstFound);
55     }
56
57     private void findMustConstraint(ConstraintDefinition conDef, String mustValue) {
58         boolean mustLflstFound = false;
59         for (MustDefinition mustDef : conDef.getMustConstraints()) {
60             if (mustDef.toString().equals(mustValue)) {
61                 mustLflstFound = true;
62                 break;
63             }
64         }
65         assertTrue("Must element in 'lflst' is missing.", mustLflstFound);
66     }
67
68     @Test
69     public void usesInGroupingDependenciesTest() {
70         loadTestResources();
71         final YangModelParser parser = new YangParserImpl();
72         final Set<Module> modules = parser.parseYangModels(testModels);
73
74         Module refineModule = null;
75         for (Module module : modules) {
76             if (module.getName().equals("module-refine")) {
77                 refineModule = module;
78             }
79         }
80         assertNotNull("Refine module wasn't found.", refineModule);
81         Set<DataSchemaNode> moduleChilds = refineModule.getChildNodes();
82         DataSchemaNode lflstNode = null;
83         DataSchemaNode chcNode = null;
84         DataSchemaNode chc2Node = null;
85         DataSchemaNode dataNode = null;
86         for (DataSchemaNode childNode : moduleChilds) {
87             if (childNode.getQName().getLocalName().equals("lflst")) {
88                 lflstNode = childNode;
89             } else if (childNode.getQName().getLocalName().equals("chc")) {
90                 chcNode = childNode;
91             } else if (childNode.getQName().getLocalName().equals("chc2")) {
92                 chc2Node = childNode;
93             } else if (childNode.getQName().getLocalName().equals("data")) {
94                 dataNode = childNode;
95             }
96         }
97
98         // lflst node
99         assertNotNull("Node 'lflst' wasn't found.", lflstNode);
100         ConstraintDefinition conDefLflst = lflstNode.getConstraints();
101         assertEquals("Max elements number in 'lflst' is incorrect.", new Integer(64), conDefLflst.getMaxElements());
102         assertEquals("Max elements number in 'lflst' is incorrect.", new Integer(32), conDefLflst.getMinElements());
103
104         findMustConstraint(conDefLflst, "new = 57");
105
106         boolean mustLflstFound = false;
107         for (MustDefinition mustDef : conDefLflst.getMustConstraints()) {
108             if (mustDef.toString().equals("new = 57")) {
109                 mustLflstFound = true;
110                 break;
111             }
112         }
113         assertTrue("Must element in 'lflst' is missing.", mustLflstFound);
114
115         findUnknownNode(lflstNode, "some value from lflst", "new-subnode");
116
117         // chc node
118         assertNotNull("Node 'chc' wasn't found.", chcNode);
119         ChoiceNode choiceNode = null;
120         if (chcNode instanceof ChoiceNode) {
121             choiceNode = (ChoiceNode) chcNode;
122         }
123         assertNotNull("Choice node chc isn't of type ChoiceNode", choiceNode);
124         assertEquals("chc node has incorrect default node.", "first", choiceNode.getDefaultCase());
125         String unknownNodeChcValue = "some value from chc";
126         String unknownNodeChcName = "new-subnode-chc";
127         findUnknownNode(chcNode, unknownNodeChcValue, unknownNodeChcName);
128
129         // chc2 node
130         assertNotNull("Node 'chc2' wasn't found.", chc2Node);
131         ConstraintDefinition conDefChc2 = chc2Node.getConstraints();
132         assertFalse("'chc2' has incorrect value for 'mandatory'", conDefChc2.isMandatory());
133
134         // data node
135         assertNotNull("Node 'data' wasn't found.", dataNode);
136         ConstraintDefinition conDefData = dataNode.getConstraints();
137         assertFalse("'data' has incorrect value for 'mandatory'", conDefData.isMandatory());
138
139         String unknownNodeDataValue = "some value from data";
140         String unknownNodeDataName = "new-subnode-data";
141         findUnknownNode(dataNode, unknownNodeDataValue, unknownNodeDataName);
142         findMustConstraint(conDefData, "something-else = 9");
143     }
144 }