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.assertFalse;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertTrue;
8 import java.util.HashSet;
12 import javax.ws.rs.WebApplicationException;
14 import org.junit.Ignore;
15 import org.junit.Test;
16 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
17 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
18 import org.opendaylight.controller.sal.restconf.impl.ResponseException;
19 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
22 import org.opendaylight.yangtools.yang.data.api.Node;
23 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.gson.JsonSyntaxException;
30 public class JsonToCnSnTest {
32 private static final Logger LOG = LoggerFactory.getLogger(JsonToCnSnTest.class);
35 public void simpleListTest() {
36 simpleTest("/json-to-cnsn/simple-list.json", "/json-to-cnsn/simple-list-yang/1", "lst", "simple:list:yang1",
41 public void simpleContainerTest() {
42 simpleTest("/json-to-cnsn/simple-container.json", "/json-to-cnsn/simple-container-yang", "cont",
43 "simple:container:yang", "simple-container-yang");
47 * test if for every leaf list item is simple node instance created
50 public void multipleItemsInLeafList() {
51 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/multiple-leaflist-items.json", true,
52 JsonToCompositeNodeProvider.INSTANCE);
53 assertNotNull(compositeNode);
54 assertEquals(3, compositeNode.getChildren().size());
56 boolean lflst1_1 = false;
57 boolean lflst1_2 = false;
58 boolean lflst1_3 = false;
60 for (Node<?> node : compositeNode.getChildren()) {
61 assertEquals("lflst1", node.getNodeType().getLocalName());
62 assertTrue(node instanceof SimpleNode<?>);
63 SimpleNode<?> simpleNode = (SimpleNode<?>) node;
64 if (simpleNode.getValue().equals("45")) {
66 } else if (simpleNode.getValue().equals("55")) {
68 } else if (simpleNode.getValue().equals("66")) {
80 * List contains 4 items and in every item are other elements. It is
81 * supposed that there should be: lf11, lflst11, cont11, lst11
84 public void multipleItemsInListTest() {
85 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/multiple-items-in-list.json", true,
86 JsonToCompositeNodeProvider.INSTANCE);
88 assertNotNull(compositeNode);
89 assertEquals("lst", compositeNode.getNodeType().getLocalName());
91 verityMultipleItemsInList(compositeNode);
95 public void nullArrayToSimpleNodeWithNullValueTest() {
96 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/array-with-null.json", true,
97 JsonToCompositeNodeProvider.INSTANCE);
98 assertNotNull(compositeNode);
99 assertEquals("cont", compositeNode.getNodeType().getLocalName());
101 assertNotNull(compositeNode.getChildren());
102 assertEquals(1, compositeNode.getChildren().size());
103 Node<?> lfNode = compositeNode.getChildren().iterator().next();
105 assertTrue(lfNode instanceof SimpleNode<?>);
106 assertEquals(null, ((SimpleNode<?>) lfNode).getValue());
111 public void incorrectTopLevelElementsTest() {
112 Throwable cause1 = null;
115 .readInputToCnSn("/json-to-cnsn/wrong-top-level1.json", true, JsonToCompositeNodeProvider.INSTANCE);
116 } catch (WebApplicationException e) {
120 assertNotNull(cause1);
125 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
127 Throwable cause2 = null;
130 .readInputToCnSn("/json-to-cnsn/wrong-top-level2.json", true, JsonToCompositeNodeProvider.INSTANCE);
131 } catch (WebApplicationException e) {
134 assertNotNull(cause2);
135 assertTrue(cause2.getCause().getMessage().contains("Json Object should contain one element"));
137 Throwable cause3 = null;
140 .readInputToCnSn("/json-to-cnsn/wrong-top-level3.json", true, JsonToCompositeNodeProvider.INSTANCE);
141 } catch (WebApplicationException e) {
144 assertNotNull(cause3);
149 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
154 * if leaf list with no data is in json then no corresponding data is
155 * created in composite node. if leaf with no data then exception is raised
158 public void emptyDataReadTest() {
159 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/empty-data.json", true,
160 JsonToCompositeNodeProvider.INSTANCE);
162 assertNotNull(compositeNode);
164 assertEquals("cont", compositeNode.getNodeType().getLocalName());
165 assertTrue(compositeNode instanceof CompositeNode);
166 List<Node<?>> children = ((CompositeNode) compositeNode).getChildren();
167 assertEquals(1, children.size());
168 assertEquals("lflst2", children.get(0).getNodeType().getLocalName());
169 assertEquals("45", children.get(0).getValue());
171 String reason = null;
173 TestUtils.readInputToCnSn("/json-to-cnsn/empty-data1.json", true, JsonToCompositeNodeProvider.INSTANCE);
174 } catch (JsonSyntaxException e) {
175 reason = e.getMessage();
178 assertTrue(reason.contains("Expected value at line"));
183 * Tests whether namespace <b>stay unchanged</b> if concrete values are
184 * present in composite or simple node and if the method for update is
189 public void notSupplyNamespaceIfAlreadySupplied() {
191 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/simple-list.json", false,
192 JsonToCompositeNodeProvider.INSTANCE);
193 assertNotNull(compositeNode);
195 // supplement namespaces according to first data schema -
196 // "simple:data:types1"
197 Set<Module> modules1 = new HashSet<>();
198 Set<Module> modules2 = new HashSet<>();
199 modules1 = TestUtils.loadModulesFrom("/json-to-cnsn/simple-list-yang/1");
200 modules2 = TestUtils.loadModulesFrom("/json-to-cnsn/simple-list-yang/2");
201 assertNotNull(modules1);
202 assertNotNull(modules2);
204 TestUtils.normalizeCompositeNode(compositeNode, modules1, "simple-list-yang1:lst");
206 assertTrue(compositeNode instanceof CompositeNodeWrapper);
207 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
209 assertEquals("lst", compNode.getNodeType().getLocalName());
210 verifyCompositeNode(compNode, "simple:list:yang1");
212 TestUtils.normalizeCompositeNode(compositeNode, modules2, "simple-list-yang2:lst");
213 verifyCompositeNode(compNode, "simple:list:yang1");
217 public void jsonIdentityrefToCompositeNode() {
218 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/identityref/json/data.json", false,
219 JsonToCompositeNodeProvider.INSTANCE);
220 assertNotNull(compositeNode);
222 Set<Module> modules = TestUtils.loadModulesFrom("/json-to-cnsn/identityref");
223 assertEquals(2, modules.size());
225 TestUtils.normalizeCompositeNode(compositeNode, modules, "identityref-module:cont");
227 assertEquals("cont", compositeNode.getNodeType().getLocalName());
229 List<Node<?>> childs = compositeNode.getChildren();
230 assertEquals(1, childs.size());
231 Node<?> nd = childs.iterator().next();
232 assertTrue(nd instanceof CompositeNode);
233 assertEquals("cont1", nd.getNodeType().getLocalName());
235 childs = ((CompositeNode) nd).getChildren();
236 assertEquals(4, childs.size());
237 SimpleNode<?> lf11 = null;
238 SimpleNode<?> lf12 = null;
239 SimpleNode<?> lf13 = null;
240 SimpleNode<?> lf14 = null;
241 for (Node<?> child : childs) {
242 assertTrue(child instanceof SimpleNode);
243 if (child.getNodeType().getLocalName().equals("lf11")) {
244 lf11 = (SimpleNode<?>) child;
245 } else if (child.getNodeType().getLocalName().equals("lf12")) {
246 lf12 = (SimpleNode<?>) child;
247 } else if (child.getNodeType().getLocalName().equals("lf13")) {
248 lf13 = (SimpleNode<?>) child;
249 } else if (child.getNodeType().getLocalName().equals("lf14")) {
250 lf14 = (SimpleNode<?>) child;
254 assertTrue(lf11.getValue() instanceof QName);
255 assertEquals("iden", ((QName) lf11.getValue()).getLocalName());
256 assertEquals("identity:module", ((QName) lf11.getValue()).getNamespace().toString());
258 assertTrue(lf12.getValue() instanceof QName);
259 assertEquals("iden_local", ((QName) lf12.getValue()).getLocalName());
260 assertEquals("identityref:module", ((QName) lf12.getValue()).getNamespace().toString());
262 assertTrue(lf13.getValue() instanceof QName);
263 assertEquals("iden_local", ((QName) lf13.getValue()).getLocalName());
264 assertEquals("identityref:module", ((QName) lf13.getValue()).getNamespace().toString());
266 assertTrue(lf14.getValue() instanceof QName);
267 assertEquals("iden_local", ((QName) lf14.getValue()).getLocalName());
268 assertEquals("identity:module", ((QName) lf14.getValue()).getNamespace().toString());
273 public void loadDataAugmentedSchemaMoreEqualNamesTest() {
274 boolean exceptionCaught = false;
276 loadAndNormalizeData("/common/augment/json/dataa.json", "/common/augment/yang", "cont", "main");
277 loadAndNormalizeData("/common/augment/json/datab.json", "/common/augment/yang", "cont", "main");
278 } catch (ResponseException e) {
279 exceptionCaught = true;
282 assertFalse(exceptionCaught);
285 private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace,
287 CompositeNode compNode = loadAndNormalizeData(jsonPath, yangPath, topLevelElementName, moduleName);
288 verifyCompositeNode(compNode, namespace);
291 private CompositeNode loadAndNormalizeData(String jsonPath, String yangPath, String topLevelElementName, String moduleName) {
292 CompositeNode compositeNode = TestUtils.readInputToCnSn(jsonPath, false, JsonToCompositeNodeProvider.INSTANCE);
293 assertNotNull(compositeNode);
295 Set<Module> modules = null;
296 modules = TestUtils.loadModulesFrom(yangPath);
297 assertNotNull(modules);
299 TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + topLevelElementName);
301 assertTrue(compositeNode instanceof CompositeNodeWrapper);
302 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
304 assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
308 private void verityMultipleItemsInList(CompositeNode compositeNode) {
309 List<Node<?>> childrenNodes = compositeNode.getChildren();
310 assertEquals(4, childrenNodes.size());
311 boolean lf11Found = false;
312 boolean cont11Found = false;
313 boolean lst11Found = false;
314 for (Node<?> lst1Item : childrenNodes) {
315 assertEquals("lst1", lst1Item.getNodeType().getLocalName());
316 assertTrue(lst1Item instanceof CompositeNode);
318 List<Node<?>> childrenLst1 = ((CompositeNode) lst1Item).getChildren();
319 assertEquals(1, childrenLst1.size());
320 String localName = childrenLst1.get(0).getNodeType().getLocalName();
321 if (localName.equals("lf11")) {
322 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
324 } else if (localName.equals("lflst11")) {
325 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
326 assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
328 } else if (localName.equals("cont11")) {
329 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
331 } else if (localName.equals("lst11")) {
333 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
334 assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size());
338 assertTrue(lf11Found);
339 assertTrue(cont11Found);
340 assertTrue(lst11Found);
343 private void verifyCompositeNode(CompositeNode compositeNode, String namespace) {
344 boolean cont1Found = false;
345 boolean lst1Found = false;
346 boolean lflst1_1Found = false;
347 boolean lflst1_2Found = false;
348 boolean lf1Found = false;
350 // assertEquals(namespace,
351 // compositeNode.getNodeType().getNamespace().toString());
353 for (Node<?> node : compositeNode.getChildren()) {
354 if (node.getNodeType().getLocalName().equals("cont1")) {
355 if (node instanceof CompositeNode) {
357 assertEquals(0, ((CompositeNode) node).getChildren().size());
359 } else if (node.getNodeType().getLocalName().equals("lst1")) {
360 if (node instanceof CompositeNode) {
362 assertEquals(0, ((CompositeNode) node).getChildren().size());
364 } else if (node.getNodeType().getLocalName().equals("lflst1")) {
365 if (node instanceof SimpleNode) {
366 if (((SimpleNode<?>) node).getValue().equals("lflst1_1")) {
367 lflst1_1Found = true;
368 } else if (((SimpleNode<?>) node).getValue().equals("lflst1_2")) {
369 lflst1_2Found = true;
373 } else if (node.getNodeType().getLocalName().equals("lf1")) {
374 if (node instanceof SimpleNode) {
375 if (((SimpleNode<?>) node).getValue().equals("lf1")) {
380 assertEquals(namespace, node.getNodeType().getNamespace().toString());
382 assertTrue(cont1Found);
383 assertTrue(lst1Found);
384 assertTrue(lflst1_1Found);
385 assertTrue(lflst1_2Found);
386 assertTrue(lf1Found);
390 public void unsupportedDataFormatTest() {
391 String exceptionMessage = "";
393 TestUtils.readInputToCnSn("/json-to-cnsn/unsupported-json-format.json", true,
394 JsonToCompositeNodeProvider.INSTANCE);
395 } catch (WebApplicationException e) {
396 exceptionMessage = e.getCause().getMessage();
398 assertTrue(exceptionMessage.contains("Root element of Json has to be Object"));