1 package org.opendaylight.controller.sal.restconf.impl.json.to.cnsn.test;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
7 import java.util.HashSet;
11 import javax.ws.rs.WebApplicationException;
13 import org.junit.Test;
14 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
15 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
16 import org.opendaylight.controller.sal.restconf.impl.ResponseException;
17 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
20 import org.opendaylight.yangtools.yang.data.api.Node;
21 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.google.gson.JsonSyntaxException;
28 public class JsonToCnSnTest {
30 private static final Logger LOG = LoggerFactory.getLogger(JsonToCnSnTest.class);
33 public void simpleListTest() {
34 simpleTest("/json-to-cnsn/simple-list.json", "/json-to-cnsn/simple-list-yang/1", "lst", "simple:list:yang1",
39 public void simpleContainerTest() {
40 simpleTest("/json-to-cnsn/simple-container.json", "/json-to-cnsn/simple-container-yang", "cont",
41 "simple:container:yang", "simple-container-yang");
45 * test if for every leaf list item is simple node instance created
48 public void multipleItemsInLeafList() {
49 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/multiple-leaflist-items.json", true,
50 JsonToCompositeNodeProvider.INSTANCE);
51 assertNotNull(compositeNode);
52 assertEquals(3, compositeNode.getChildren().size());
54 boolean lflst1_1 = false;
55 boolean lflst1_2 = false;
56 boolean lflst1_3 = false;
58 for (Node<?> node : compositeNode.getChildren()) {
59 assertEquals("lflst1", node.getNodeType().getLocalName());
60 assertTrue(node instanceof SimpleNode<?>);
61 SimpleNode<?> simpleNode = (SimpleNode<?>) node;
62 if (simpleNode.getValue().equals("45")) {
64 } else if (simpleNode.getValue().equals("55")) {
66 } else if (simpleNode.getValue().equals("66")) {
78 * List contains 4 items and in every item are other elements. It is
79 * supposed that there should be: lf11, lflst11, cont11, lst11
82 public void multipleItemsInListTest() {
83 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/multiple-items-in-list.json", true,
84 JsonToCompositeNodeProvider.INSTANCE);
86 assertNotNull(compositeNode);
87 assertEquals("lst", compositeNode.getNodeType().getLocalName());
89 verityMultipleItemsInList(compositeNode);
93 public void nullArrayToSimpleNodeWithNullValueTest() {
94 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/array-with-null.json", true,
95 JsonToCompositeNodeProvider.INSTANCE);
96 assertNotNull(compositeNode);
97 assertEquals("cont", compositeNode.getNodeType().getLocalName());
99 assertNotNull(compositeNode.getChildren());
100 assertEquals(1, compositeNode.getChildren().size());
101 Node<?> lfNode = compositeNode.getChildren().iterator().next();
103 assertTrue(lfNode instanceof SimpleNode<?>);
104 assertEquals(null, ((SimpleNode<?>) lfNode).getValue());
109 public void incorrectTopLevelElementsTest() {
110 Throwable cause1 = null;
113 .readInputToCnSn("/json-to-cnsn/wrong-top-level1.json", true, JsonToCompositeNodeProvider.INSTANCE);
114 } catch (WebApplicationException e) {
118 assertNotNull(cause1);
123 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
125 Throwable cause2 = null;
128 .readInputToCnSn("/json-to-cnsn/wrong-top-level2.json", true, JsonToCompositeNodeProvider.INSTANCE);
129 } catch (WebApplicationException e) {
132 assertNotNull(cause2);
133 assertTrue(cause2.getCause().getMessage().contains("Json Object should contain one element"));
135 Throwable cause3 = null;
138 .readInputToCnSn("/json-to-cnsn/wrong-top-level3.json", true, JsonToCompositeNodeProvider.INSTANCE);
139 } catch (WebApplicationException e) {
142 assertNotNull(cause3);
147 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
152 * if leaf list with no data is in json then no corresponding data is
153 * created in composite node. if leaf with no data then exception is raised
156 public void emptyDataReadTest() {
157 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/empty-data.json", true,
158 JsonToCompositeNodeProvider.INSTANCE);
160 assertNotNull(compositeNode);
162 assertEquals("cont", compositeNode.getNodeType().getLocalName());
163 assertTrue(compositeNode instanceof CompositeNode);
164 List<Node<?>> children = ((CompositeNode) compositeNode).getChildren();
165 assertEquals(1, children.size());
166 assertEquals("lflst2", children.get(0).getNodeType().getLocalName());
167 assertEquals("45", children.get(0).getValue());
169 String reason = null;
171 TestUtils.readInputToCnSn("/json-to-cnsn/empty-data1.json", true, JsonToCompositeNodeProvider.INSTANCE);
172 } catch (JsonSyntaxException e) {
173 reason = e.getMessage();
176 assertTrue(reason.contains("Expected value at line"));
181 * Tests whether namespace <b>stay unchanged</b> if concrete values are
182 * present in composite or simple node and if the method for update is
187 public void notSupplyNamespaceIfAlreadySupplied() {
189 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/simple-list.json", false,
190 JsonToCompositeNodeProvider.INSTANCE);
191 assertNotNull(compositeNode);
193 // supplement namespaces according to first data schema -
194 // "simple:data:types1"
195 Set<Module> modules1 = new HashSet<>();
196 Set<Module> modules2 = new HashSet<>();
197 modules1 = TestUtils.loadModulesFrom("/json-to-cnsn/simple-list-yang/1");
198 modules2 = TestUtils.loadModulesFrom("/json-to-cnsn/simple-list-yang/2");
199 assertNotNull(modules1);
200 assertNotNull(modules2);
202 TestUtils.normalizeCompositeNode(compositeNode, modules1, "simple-list-yang1:lst");
204 assertTrue(compositeNode instanceof CompositeNodeWrapper);
205 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
207 assertEquals("lst", compNode.getNodeType().getLocalName());
208 verifyCompositeNode(compNode, "simple:list:yang1");
210 String exceptionMessage = "";
212 TestUtils.normalizeCompositeNode(compositeNode, modules2, "simple-list-yang2:lst");
213 } catch (ResponseException e) {
214 exceptionMessage = String.valueOf(e.getResponse().getEntity());
216 assertTrue(exceptionMessage
217 .contains("Data has bad format\nIf data is in XML format then namespace for lst should be simple:list:yang2.\n If data is in Json format then module name for lst should be simple-list-yang2."));
222 public void jsonIdentityrefToCompositeNode() {
223 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/identityref/json/data.json", false,
224 JsonToCompositeNodeProvider.INSTANCE);
225 assertNotNull(compositeNode);
227 Set<Module> modules = TestUtils.loadModulesFrom("/json-to-cnsn/identityref");
228 assertEquals(2, modules.size());
230 TestUtils.normalizeCompositeNode(compositeNode, modules, "identityref-module:cont");
232 assertEquals("cont", compositeNode.getNodeType().getLocalName());
234 List<Node<?>> childs = compositeNode.getChildren();
235 assertEquals(1, childs.size());
236 Node<?> nd = childs.iterator().next();
237 assertTrue(nd instanceof CompositeNode);
238 assertEquals("cont1", nd.getNodeType().getLocalName());
240 childs = ((CompositeNode) nd).getChildren();
241 assertEquals(4, childs.size());
242 SimpleNode<?> lf11 = null;
243 SimpleNode<?> lf12 = null;
244 SimpleNode<?> lf13 = null;
245 SimpleNode<?> lf14 = null;
246 for (Node<?> child : childs) {
247 assertTrue(child instanceof SimpleNode);
248 if (child.getNodeType().getLocalName().equals("lf11")) {
249 lf11 = (SimpleNode<?>) child;
250 } else if (child.getNodeType().getLocalName().equals("lf12")) {
251 lf12 = (SimpleNode<?>) child;
252 } else if (child.getNodeType().getLocalName().equals("lf13")) {
253 lf13 = (SimpleNode<?>) child;
254 } else if (child.getNodeType().getLocalName().equals("lf14")) {
255 lf14 = (SimpleNode<?>) child;
259 assertTrue(lf11.getValue() instanceof QName);
260 assertEquals("iden", ((QName) lf11.getValue()).getLocalName());
261 assertEquals("identity:module", ((QName) lf11.getValue()).getNamespace().toString());
263 assertTrue(lf12.getValue() instanceof QName);
264 assertEquals("iden_local", ((QName) lf12.getValue()).getLocalName());
265 assertEquals("identityref:module", ((QName) lf12.getValue()).getNamespace().toString());
267 assertTrue(lf13.getValue() instanceof QName);
268 assertEquals("iden_local", ((QName) lf13.getValue()).getLocalName());
269 assertEquals("identityref:module", ((QName) lf13.getValue()).getNamespace().toString());
271 assertTrue(lf14.getValue() instanceof QName);
272 assertEquals("iden_local", ((QName) lf14.getValue()).getLocalName());
273 assertEquals("identity:module", ((QName) lf14.getValue()).getNamespace().toString());
276 private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace,
278 CompositeNode compositeNode = TestUtils.readInputToCnSn(jsonPath, false, JsonToCompositeNodeProvider.INSTANCE);
279 assertNotNull(compositeNode);
281 Set<Module> modules = null;
282 modules = TestUtils.loadModulesFrom(yangPath);
283 assertNotNull(modules);
285 TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + topLevelElementName);
287 assertTrue(compositeNode instanceof CompositeNodeWrapper);
288 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
290 assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
291 verifyCompositeNode(compNode, namespace);
294 private void verityMultipleItemsInList(CompositeNode compositeNode) {
295 List<Node<?>> childrenNodes = compositeNode.getChildren();
296 assertEquals(4, childrenNodes.size());
297 boolean lf11Found = false;
298 boolean cont11Found = false;
299 boolean lst11Found = false;
300 for (Node<?> lst1Item : childrenNodes) {
301 assertEquals("lst1", lst1Item.getNodeType().getLocalName());
302 assertTrue(lst1Item instanceof CompositeNode);
304 List<Node<?>> childrenLst1 = ((CompositeNode) lst1Item).getChildren();
305 assertEquals(1, childrenLst1.size());
306 String localName = childrenLst1.get(0).getNodeType().getLocalName();
307 if (localName.equals("lf11")) {
308 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
310 } else if (localName.equals("lflst11")) {
311 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
312 assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
314 } else if (localName.equals("cont11")) {
315 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
317 } else if (localName.equals("lst11")) {
319 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
320 assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size());
324 assertTrue(lf11Found);
325 assertTrue(cont11Found);
326 assertTrue(lst11Found);
329 private void verifyCompositeNode(CompositeNode compositeNode, String namespace) {
330 boolean cont1Found = false;
331 boolean lst1Found = false;
332 boolean lflst1_1Found = false;
333 boolean lflst1_2Found = false;
334 boolean lf1Found = false;
336 // assertEquals(namespace,
337 // compositeNode.getNodeType().getNamespace().toString());
339 for (Node<?> node : compositeNode.getChildren()) {
340 if (node.getNodeType().getLocalName().equals("cont1")) {
341 if (node instanceof CompositeNode) {
343 assertEquals(0, ((CompositeNode) node).getChildren().size());
345 } else if (node.getNodeType().getLocalName().equals("lst1")) {
346 if (node instanceof CompositeNode) {
348 assertEquals(0, ((CompositeNode) node).getChildren().size());
350 } else if (node.getNodeType().getLocalName().equals("lflst1")) {
351 if (node instanceof SimpleNode) {
352 if (((SimpleNode<?>) node).getValue().equals("lflst1_1")) {
353 lflst1_1Found = true;
354 } else if (((SimpleNode<?>) node).getValue().equals("lflst1_2")) {
355 lflst1_2Found = true;
359 } else if (node.getNodeType().getLocalName().equals("lf1")) {
360 if (node instanceof SimpleNode) {
361 if (((SimpleNode<?>) node).getValue().equals("lf1")) {
366 assertEquals(namespace, node.getNodeType().getNamespace().toString());
368 assertTrue(cont1Found);
369 assertTrue(lst1Found);
370 assertTrue(lflst1_1Found);
371 assertTrue(lflst1_2Found);
372 assertTrue(lf1Found);
376 public void unsupportedDataFormatTest() {
377 String exceptionMessage = "";
379 TestUtils.readInputToCnSn("/json-to-cnsn/unsupported-json-format.json", true,
380 JsonToCompositeNodeProvider.INSTANCE);
381 } catch (WebApplicationException e) {
382 exceptionMessage = e.getCause().getMessage();
384 assertTrue(exceptionMessage.contains("Root element of Json has to be Object"));