1 package org.opendaylight.controller.sal.restconf.impl.json.to.cnsn.test;
3 import static org.junit.Assert.*;
5 import java.io.FileNotFoundException;
8 import javax.ws.rs.WebApplicationException;
10 import org.junit.Test;
11 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
12 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
13 import org.opendaylight.controller.sal.restconf.impl.ResponseException;
14 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.*;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.Module;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import com.google.gson.JsonSyntaxException;
24 public class JsonToCnSnTest {
26 private static final Logger LOG = LoggerFactory.getLogger(JsonToCnSnTest.class);
29 public void simpleListTest() {
30 simpleTest("/json-to-cnsn/simple-list.json", "/json-to-cnsn/simple-list-yang/1", "lst", "simple:list:yang1",
35 public void simpleContainerTest() {
36 simpleTest("/json-to-cnsn/simple-container.json", "/json-to-cnsn/simple-container-yang", "cont",
37 "simple:container:yang", "simple-container-yang");
41 * test if for every leaf list item is simple node instance created
44 public void multipleItemsInLeafList() {
45 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/multiple-leaflist-items.json", true,
46 JsonToCompositeNodeProvider.INSTANCE);
47 assertNotNull(compositeNode);
48 assertEquals(3, compositeNode.getChildren().size());
50 boolean lflst1_1 = false;
51 boolean lflst1_2 = false;
52 boolean lflst1_3 = false;
54 for (Node<?> node : compositeNode.getChildren()) {
55 assertEquals("lflst1", node.getNodeType().getLocalName());
56 assertTrue(node instanceof SimpleNode<?>);
57 SimpleNode<?> simpleNode = (SimpleNode<?>) node;
58 if (simpleNode.getValue().equals("45")) {
60 } else if (simpleNode.getValue().equals("55")) {
62 } else if (simpleNode.getValue().equals("66")) {
74 * List contains 4 items and in every item are other elements. It is
75 * supposed that there should be: lf11, lflst11, cont11, lst11
78 public void multipleItemsInListTest() {
79 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/multiple-items-in-list.json", true,
80 JsonToCompositeNodeProvider.INSTANCE);
82 assertNotNull(compositeNode);
83 assertEquals("lst", compositeNode.getNodeType().getLocalName());
85 verityMultipleItemsInList(compositeNode);
89 public void nullArrayToSimpleNodeWithNullValueTest() {
90 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/array-with-null.json", true,
91 JsonToCompositeNodeProvider.INSTANCE);
92 assertNotNull(compositeNode);
93 assertEquals("cont", compositeNode.getNodeType().getLocalName());
95 assertNotNull(compositeNode.getChildren());
96 assertEquals(1, compositeNode.getChildren().size());
97 Node<?> lfNode = compositeNode.getChildren().iterator().next();
99 assertTrue(lfNode instanceof SimpleNode<?>);
100 assertEquals(null, ((SimpleNode<?>) lfNode).getValue());
105 public void incorrectTopLevelElementsTest() {
106 Throwable cause1 = null;
109 .readInputToCnSn("/json-to-cnsn/wrong-top-level1.json", true, JsonToCompositeNodeProvider.INSTANCE);
110 } catch (WebApplicationException e) {
114 assertNotNull(cause1);
119 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
121 Throwable cause2 = null;
124 .readInputToCnSn("/json-to-cnsn/wrong-top-level2.json", true, JsonToCompositeNodeProvider.INSTANCE);
125 } catch (WebApplicationException e) {
128 assertNotNull(cause2);
129 assertTrue(cause2.getCause().getMessage().contains("Json Object should contain one element"));
131 Throwable cause3 = null;
134 .readInputToCnSn("/json-to-cnsn/wrong-top-level3.json", true, JsonToCompositeNodeProvider.INSTANCE);
135 } catch (WebApplicationException e) {
138 assertNotNull(cause3);
143 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
148 * if leaf list with no data is in json then no corresponding data is
149 * created in composite node. if leaf with no data then exception is raised
152 public void emptyDataReadTest() {
153 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/empty-data.json", true,
154 JsonToCompositeNodeProvider.INSTANCE);
156 assertNotNull(compositeNode);
158 assertEquals("cont", compositeNode.getNodeType().getLocalName());
159 assertTrue(compositeNode instanceof CompositeNode);
160 List<Node<?>> children = ((CompositeNode) compositeNode).getChildren();
161 assertEquals(1, children.size());
162 assertEquals("lflst2", children.get(0).getNodeType().getLocalName());
163 assertEquals("45", children.get(0).getValue());
165 String reason = null;
167 TestUtils.readInputToCnSn("/json-to-cnsn/empty-data1.json", true, JsonToCompositeNodeProvider.INSTANCE);
168 } catch (JsonSyntaxException e) {
169 reason = e.getMessage();
172 assertTrue(reason.contains("Expected value at line"));
177 * Tests whether namespace <b>stay unchanged</b> if concrete values are
178 * present in composite or simple node and if the method for update is
183 public void notSupplyNamespaceIfAlreadySupplied() {
185 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/simple-list.json", false,
186 JsonToCompositeNodeProvider.INSTANCE);
187 assertNotNull(compositeNode);
189 // supplement namespaces according to first data schema -
190 // "simple:data:types1"
191 Set<Module> modules1 = new HashSet<>();
192 Set<Module> modules2 = new HashSet<>();
194 modules1 = TestUtils.loadModules(TestUtils.class.getResource("/json-to-cnsn/simple-list-yang/1").getPath());
195 modules2 = TestUtils.loadModules(TestUtils.class.getResource("/json-to-cnsn/simple-list-yang/2").getPath());
196 } catch (FileNotFoundException e) {
198 assertNotNull(modules1);
199 assertNotNull(modules2);
201 TestUtils.normalizeCompositeNode(compositeNode, modules1, "simple-list-yang1:lst");
203 assertTrue(compositeNode instanceof CompositeNodeWrapper);
204 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
206 assertEquals("lst", compNode.getNodeType().getLocalName());
207 verifyCompositeNode(compNode, "simple:list:yang1");
209 String exceptionMessage = "";
211 TestUtils.normalizeCompositeNode(compositeNode, modules2, "simple-list-yang2:lst");
212 } catch (ResponseException e) {
213 exceptionMessage = String.valueOf(e.getResponse().getEntity());
215 assertTrue(exceptionMessage
216 .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."));
221 public void jsonIdentityrefToCompositeNode() {
222 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/identityref/json/data.json", false,
223 JsonToCompositeNodeProvider.INSTANCE);
224 assertNotNull(compositeNode);
226 Set<Module> modules = TestUtils.resolveModulesFrom("/json-to-cnsn/identityref");
227 assertEquals(2, modules.size());
228 Module module = TestUtils.resolveModule("identityref-module", modules);
229 assertNotNull(module);
230 DataSchemaNode dataSchemaNode = TestUtils.resolveDataSchemaNode(null, module);
231 assertNotNull(dataSchemaNode);
233 TestUtils.normalizeCompositeNode(compositeNode, modules, "identityref-module:cont");
235 assertEquals("cont", compositeNode.getNodeType().getLocalName());
237 List<Node<?>> childs = compositeNode.getChildren();
238 assertEquals(1, childs.size());
239 Node<?> nd = childs.iterator().next();
240 assertTrue(nd instanceof CompositeNode);
241 assertEquals("cont1", nd.getNodeType().getLocalName());
243 childs = ((CompositeNode) nd).getChildren();
244 assertEquals(4, childs.size());
245 SimpleNode<?> lf11 = null;
246 SimpleNode<?> lf12 = null;
247 SimpleNode<?> lf13 = null;
248 SimpleNode<?> lf14 = null;
249 for (Node<?> child : childs) {
250 assertTrue(child instanceof SimpleNode);
251 if (child.getNodeType().getLocalName().equals("lf11")) {
252 lf11 = (SimpleNode<?>) child;
253 } else if (child.getNodeType().getLocalName().equals("lf12")) {
254 lf12 = (SimpleNode<?>) child;
255 } else if (child.getNodeType().getLocalName().equals("lf13")) {
256 lf13 = (SimpleNode<?>) child;
257 } else if (child.getNodeType().getLocalName().equals("lf14")) {
258 lf14 = (SimpleNode<?>) child;
262 assertTrue(lf11.getValue() instanceof QName);
263 assertEquals("iden", ((QName) lf11.getValue()).getLocalName());
264 assertEquals("identity:module", ((QName) lf11.getValue()).getNamespace().toString());
266 assertTrue(lf12.getValue() instanceof QName);
267 assertEquals("iden_local", ((QName) lf12.getValue()).getLocalName());
268 assertEquals("identityref:module", ((QName) lf12.getValue()).getNamespace().toString());
270 assertTrue(lf13.getValue() instanceof QName);
271 assertEquals("iden_local", ((QName) lf13.getValue()).getLocalName());
272 assertEquals("identityref:module", ((QName) lf13.getValue()).getNamespace().toString());
274 assertTrue(lf14.getValue() instanceof QName);
275 assertEquals("iden_local", ((QName) lf14.getValue()).getLocalName());
276 assertEquals("identity:module", ((QName) lf14.getValue()).getNamespace().toString());
279 private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace,
281 CompositeNode compositeNode = TestUtils.readInputToCnSn(jsonPath, false, JsonToCompositeNodeProvider.INSTANCE);
282 assertNotNull(compositeNode);
284 Set<Module> modules = null;
286 modules = TestUtils.loadModules(TestUtils.class.getResource(yangPath).getPath());
287 } catch (FileNotFoundException e) {
288 LOG.error(e.getMessage());
291 assertNotNull(modules);
293 TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + topLevelElementName);
294 // TestUtils.supplementNamespace(dataSchemaNode, compositeNode);
296 assertTrue(compositeNode instanceof CompositeNodeWrapper);
297 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
299 assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
300 verifyCompositeNode(compNode, namespace);
303 private void verityMultipleItemsInList(CompositeNode compositeNode) {
304 List<Node<?>> childrenNodes = compositeNode.getChildren();
305 assertEquals(4, childrenNodes.size());
306 boolean lf11Found = false;
307 boolean cont11Found = false;
308 boolean lst11Found = false;
309 for (Node<?> lst1Item : childrenNodes) {
310 assertEquals("lst1", lst1Item.getNodeType().getLocalName());
311 assertTrue(lst1Item instanceof CompositeNode);
313 List<Node<?>> childrenLst1 = ((CompositeNode) lst1Item).getChildren();
314 assertEquals(1, childrenLst1.size());
315 String localName = childrenLst1.get(0).getNodeType().getLocalName();
316 if (localName.equals("lf11")) {
317 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
319 } else if (localName.equals("lflst11")) {
320 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
321 assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
323 } else if (localName.equals("cont11")) {
324 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
326 } else if (localName.equals("lst11")) {
328 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
329 assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size());
333 assertTrue(lf11Found);
334 assertTrue(cont11Found);
335 assertTrue(lst11Found);
338 private void verifyCompositeNode(CompositeNode compositeNode, String namespace) {
339 boolean cont1Found = false;
340 boolean lst1Found = false;
341 boolean lflst1_1Found = false;
342 boolean lflst1_2Found = false;
343 boolean lf1Found = false;
345 // assertEquals(namespace,
346 // compositeNode.getNodeType().getNamespace().toString());
348 for (Node<?> node : compositeNode.getChildren()) {
349 if (node.getNodeType().getLocalName().equals("cont1")) {
350 if (node instanceof CompositeNode) {
352 assertEquals(0, ((CompositeNode) node).getChildren().size());
354 } else if (node.getNodeType().getLocalName().equals("lst1")) {
355 if (node instanceof CompositeNode) {
357 assertEquals(0, ((CompositeNode) node).getChildren().size());
359 } else if (node.getNodeType().getLocalName().equals("lflst1")) {
360 if (node instanceof SimpleNode) {
361 if (((SimpleNode<?>) node).getValue().equals("lflst1_1")) {
362 lflst1_1Found = true;
363 } else if (((SimpleNode<?>) node).getValue().equals("lflst1_2")) {
364 lflst1_2Found = true;
368 } else if (node.getNodeType().getLocalName().equals("lf1")) {
369 if (node instanceof SimpleNode) {
370 if (((SimpleNode<?>) node).getValue().equals("lf1")) {
375 assertEquals(namespace, node.getNodeType().getNamespace().toString());
377 assertTrue(cont1Found);
378 assertTrue(lst1Found);
379 assertTrue(lflst1_1Found);
380 assertTrue(lflst1_2Found);
381 assertTrue(lf1Found);
385 public void unsupportedDataFormatTest() {
386 String exceptionMessage = "";
388 TestUtils.readInputToCnSn("/json-to-cnsn/unsupported-json-format.json", true,
389 JsonToCompositeNodeProvider.INSTANCE);
390 } catch (WebApplicationException e) {
391 exceptionMessage = e.getCause().getMessage();
393 assertTrue(exceptionMessage.contains("Root element of Json has to be Object"));