1 package org.opendaylight.controller.sal.restconf.impl.json.to.cnsn.test;
3 import static org.junit.Assert.*;
6 import java.net.URISyntaxException;
10 import javax.ws.rs.WebApplicationException;
12 import org.junit.Test;
13 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
14 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
15 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.*;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import com.google.gson.JsonSyntaxException;
25 public class JsonToCnSnTest {
27 private static final Logger LOG = LoggerFactory.getLogger(JsonToCnSnTest.class);
30 public void simpleListTest() {
31 simpleTest("/json-to-cnsn/simple-list.json", "/json-to-cnsn/simple-list-yang", "lst", "simple:list:yang1",
36 public void simpleContainerTest() {
37 simpleTest("/json-to-cnsn/simple-container.json", "/json-to-cnsn/simple-container-yang", "cont",
38 "simple:container:yang", "simple-container-yang");
42 * test if for every leaf list item is simple node instance created
45 public void multipleItemsInLeafList() {
46 CompositeNode compositeNode = compositeContainerFromJson("/json-to-cnsn/multiple-leaflist-items.json", true);
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 = compositeContainerFromJson("/json-to-cnsn/multiple-items-in-list.json", true);
80 assertNotNull(compositeNode);
82 assertEquals("lst", compositeNode.getNodeType().getLocalName());
84 verityMultipleItemsInList(compositeNode);
88 public void nullArrayToSimpleNodeWithNullValueTest() {
89 CompositeNode compositeNode = compositeContainerFromJson("/json-to-cnsn/array-with-null.json", true);
90 assertNotNull(compositeNode);
91 assertEquals("cont", compositeNode.getNodeType().getLocalName());
93 assertNotNull(compositeNode.getChildren());
94 assertEquals(1, compositeNode.getChildren().size());
95 Node<?> lfNode = compositeNode.getChildren().iterator().next();
97 assertTrue(lfNode instanceof SimpleNode<?>);
98 assertEquals(null, ((SimpleNode<?>) lfNode).getValue());
103 public void incorrectTopLevelElementsTest() {
104 Throwable cause1 = null;
106 compositeContainerFromJson("/json-to-cnsn/wrong-top-level1.json", true);
107 } catch (WebApplicationException e) {
111 assertNotNull(cause1);
116 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
118 Throwable cause2 = null;
120 compositeContainerFromJson("/json-to-cnsn/wrong-top-level2.json", true);
121 } catch (WebApplicationException e) {
124 assertNotNull(cause2);
125 assertTrue(cause2.getCause().getMessage().contains("Json Object should contain one element"));
127 Throwable cause3 = null;
129 compositeContainerFromJson("/json-to-cnsn/wrong-top-level3.json", true);
130 } catch (WebApplicationException e) {
133 assertNotNull(cause3);
138 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
143 * if leaf list with no data is in json then no corresponding data is
144 * created in composite node. if leaf with no data then exception is raised
147 public void emptyDataReadTest() {
148 CompositeNode compositeNode = compositeContainerFromJson("/json-to-cnsn/empty-data.json", true);
150 assertNotNull(compositeNode);
152 assertEquals("cont", compositeNode.getNodeType().getLocalName());
153 assertTrue(compositeNode instanceof CompositeNode);
154 List<Node<?>> children = ((CompositeNode) compositeNode).getChildren();
155 assertEquals(1, children.size());
156 assertEquals("lflst2", children.get(0).getNodeType().getLocalName());
157 assertEquals("45", children.get(0).getValue());
159 String reason = null;
161 compositeContainerFromJson("/json-to-cnsn/empty-data1.json", true);
162 } catch (JsonSyntaxException e) {
163 reason = e.getMessage();
166 assertTrue(reason.contains("Expected value at line"));
171 * Tests whether namespace <b>stay unchanged</b> if concrete values are
172 * present in composite or simple node and if the method for update is
177 public void notSupplyNamespaceIfAlreadySupplied() {
179 CompositeNode compositeNode = compositeContainerFromJson("/json-to-cnsn/simple-list.json");
180 assertNotNull(compositeNode);
182 DataSchemaNode dataSchemaNode1 = null;
183 DataSchemaNode dataSchemaNode2 = null;
185 dataSchemaNode1 = TestUtils.obtainSchemaFromYang("/json-to-cnsn/simple-list-yang", "simple-list-yang1");
186 dataSchemaNode2 = TestUtils.obtainSchemaFromYang("/json-to-cnsn/simple-list-yang", "simple-list-yang2");
187 } catch (FileNotFoundException e) {
188 LOG.error(e.getMessage());
191 assertNotNull(dataSchemaNode1);
192 assertNotNull(dataSchemaNode2);
194 // supplement namespaces according to first data schema -
195 // "simple:data:types1"
196 TestUtils.supplementNamespace(dataSchemaNode1, compositeNode);
198 assertTrue(compositeNode instanceof CompositeNodeWrapper);
199 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
201 assertEquals("lst", compNode.getNodeType().getLocalName());
202 verifyCompositeNode(compNode, "simple:list:yang1");
204 // dataSchemaNode2 should't be taken into account, because compNode
205 // isn't CompositeNodeWrapper
206 TestUtils.supplementNamespace(dataSchemaNode2, compNode);
207 verifyCompositeNode(compNode, "simple:list:yang1");
212 public void jsonIdentityrefToCompositeNode() {
213 CompositeNode compositeNode = compositeContainerFromJson("/json-to-cnsn/identityref/json/data.json");
214 assertNotNull(compositeNode);
216 Set<Module> modules = TestUtils.resolveModules("/json-to-cnsn/identityref");
217 assertEquals(2, modules.size());
218 Module module = TestUtils.resolveModule("identityref-module", modules);
219 assertNotNull(module);
220 DataSchemaNode dataSchemaNode = TestUtils.resolveDataSchemaNode(module, null);
221 assertNotNull(dataSchemaNode);
223 TestUtils.normalizeCompositeNode(compositeNode, modules, dataSchemaNode, "identityref-module:cont");
225 assertEquals("cont", compositeNode.getNodeType().getLocalName());
227 List<Node<?>> childs = compositeNode.getChildren();
228 assertEquals(1, childs.size());
229 Node<?> nd = childs.iterator().next();
230 assertTrue(nd instanceof CompositeNode);
231 assertEquals("cont1", nd.getNodeType().getLocalName());
233 childs = ((CompositeNode) nd).getChildren();
234 assertEquals(4, childs.size());
235 SimpleNode<?> lf11 = null;
236 SimpleNode<?> lf12 = null;
237 SimpleNode<?> lf13 = null;
238 SimpleNode<?> lf14 = null;
239 for (Node<?> child : childs) {
240 assertTrue(child instanceof SimpleNode);
241 if (child.getNodeType().getLocalName().equals("lf11")) {
242 lf11 = (SimpleNode<?>) child;
243 } else if (child.getNodeType().getLocalName().equals("lf12")) {
244 lf12 = (SimpleNode<?>) child;
245 } else if (child.getNodeType().getLocalName().equals("lf13")) {
246 lf13 = (SimpleNode<?>) child;
247 } else if (child.getNodeType().getLocalName().equals("lf14")) {
248 lf14 = (SimpleNode<?>) child;
252 assertTrue(lf11.getValue() instanceof QName);
253 assertEquals("iden", ((QName) lf11.getValue()).getLocalName());
254 assertEquals("identity:module", ((QName) lf11.getValue()).getNamespace().toString());
256 assertTrue(lf12.getValue() instanceof QName);
257 assertEquals("iden_local", ((QName) lf12.getValue()).getLocalName());
258 assertEquals("identityref:module", ((QName) lf12.getValue()).getNamespace().toString());
260 assertTrue(lf13.getValue() instanceof QName);
261 assertEquals("iden_local", ((QName) lf13.getValue()).getLocalName());
262 assertEquals("identityref:module", ((QName) lf13.getValue()).getNamespace().toString());
264 assertTrue(lf14.getValue() instanceof QName);
265 assertEquals("iden_local", ((QName) lf14.getValue()).getLocalName());
266 assertEquals("identity:module", ((QName) lf14.getValue()).getNamespace().toString());
269 private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace,
271 CompositeNode compositeNode = compositeContainerFromJson(jsonPath);
272 assertNotNull(compositeNode);
274 DataSchemaNode dataSchemaNode = null;
276 dataSchemaNode = TestUtils.obtainSchemaFromYang(yangPath, moduleName);
277 } catch (FileNotFoundException e) {
278 LOG.error(e.getMessage());
281 assertNotNull(dataSchemaNode);
283 TestUtils.supplementNamespace(dataSchemaNode, compositeNode);
285 assertTrue(compositeNode instanceof CompositeNodeWrapper);
286 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
288 assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
289 verifyCompositeNode(compNode, namespace);
292 private void verityMultipleItemsInList(CompositeNode compositeNode) {
293 List<Node<?>> childrenNodes = compositeNode.getChildren();
294 assertEquals(4, childrenNodes.size());
295 boolean lf11Found = false;
296 boolean cont11Found = false;
297 boolean lst11Found = false;
298 for (Node<?> lst1Item : childrenNodes) {
299 assertEquals("lst1", lst1Item.getNodeType().getLocalName());
300 assertTrue(lst1Item instanceof CompositeNode);
302 List<Node<?>> childrenLst1 = ((CompositeNode) lst1Item).getChildren();
303 assertEquals(1, childrenLst1.size());
304 String localName = childrenLst1.get(0).getNodeType().getLocalName();
305 if (localName.equals("lf11")) {
306 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
308 } else if (localName.equals("lflst11")) {
309 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
310 assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
312 } else if (localName.equals("cont11")) {
313 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
315 } else if (localName.equals("lst11")) {
317 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
318 assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size());
322 assertTrue(lf11Found);
323 assertTrue(cont11Found);
324 assertTrue(lst11Found);
327 private void verifyCompositeNode(CompositeNode compositeNode, String namespace) {
328 boolean cont1Found = false;
329 boolean lst1Found = false;
330 boolean lflst1_1Found = false;
331 boolean lflst1_2Found = false;
332 boolean lf1Found = false;
334 assertEquals(namespace, compositeNode.getNodeType().getNamespace().toString());
336 for (Node<?> node : compositeNode.getChildren()) {
337 if (node.getNodeType().getLocalName().equals("cont1")) {
338 if (node instanceof CompositeNode) {
340 assertEquals(0, ((CompositeNode) node).getChildren().size());
342 } else if (node.getNodeType().getLocalName().equals("lst1")) {
343 if (node instanceof CompositeNode) {
345 assertEquals(0, ((CompositeNode) node).getChildren().size());
347 } else if (node.getNodeType().getLocalName().equals("lflst1")) {
348 if (node instanceof SimpleNode) {
349 if (((SimpleNode<?>) node).getValue().equals("lflst1_1")) {
350 lflst1_1Found = true;
351 } else if (((SimpleNode<?>) node).getValue().equals("lflst1_2")) {
352 lflst1_2Found = true;
356 } else if (node.getNodeType().getLocalName().equals("lf1")) {
357 if (node instanceof SimpleNode) {
358 if (((SimpleNode<?>) node).getValue().equals("lf1")) {
363 assertEquals(namespace, node.getNodeType().getNamespace().toString());
365 assertTrue(cont1Found);
366 assertTrue(lst1Found);
367 assertTrue(lflst1_1Found);
368 assertTrue(lflst1_2Found);
369 assertTrue(lf1Found);
372 private CompositeNode compositeContainerFromJson(String jsonPath) {
373 return compositeContainerFromJson(jsonPath, false);
376 private CompositeNode compositeContainerFromJson(String jsonPath, boolean dummyNamespaces)
377 throws WebApplicationException {
379 JsonToCompositeNodeProvider jsonToCompositeNodeProvider = JsonToCompositeNodeProvider.INSTANCE;
380 InputStream jsonStream = JsonToCnSnTest.class.getResourceAsStream(jsonPath);
382 CompositeNode compositeNode = jsonToCompositeNodeProvider
383 .readFrom(null, null, null, null, null, jsonStream);
384 assertTrue(compositeNode instanceof CompositeNodeWrapper);
385 if (dummyNamespaces) {
387 TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
388 return ((CompositeNodeWrapper) compositeNode).unwrap();
389 } catch (URISyntaxException e) {
390 LOG.error(e.getMessage());
391 assertTrue(e.getMessage(), false);
394 return compositeNode;
395 } catch (IOException e) {
396 LOG.error(e.getMessage());
397 assertTrue(e.getMessage(), false);