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.test.TestUtils;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
19 import org.opendaylight.yangtools.yang.data.api.Node;
20 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.gson.JsonSyntaxException;
27 public class JsonToCnSnTest {
29 private static final Logger LOG = LoggerFactory.getLogger(JsonToCnSnTest.class);
32 public void simpleListTest() {
33 simpleTest("/json-to-cnsn/simple-list.json", "/json-to-cnsn/simple-list-yang/1", "lst", "simple:list:yang1",
38 public void simpleContainerTest() {
39 simpleTest("/json-to-cnsn/simple-container.json", "/json-to-cnsn/simple-container-yang", "cont",
40 "simple:container:yang", "simple-container-yang");
44 * test if for every leaf list item is simple node instance created
47 public void multipleItemsInLeafList() {
48 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/multiple-leaflist-items.json", true,
49 JsonToCompositeNodeProvider.INSTANCE);
50 assertNotNull(compositeNode);
51 assertEquals(3, compositeNode.getChildren().size());
53 boolean lflst1_1 = false;
54 boolean lflst1_2 = false;
55 boolean lflst1_3 = false;
57 for (Node<?> node : compositeNode.getChildren()) {
58 assertEquals("lflst1", node.getNodeType().getLocalName());
59 assertTrue(node instanceof SimpleNode<?>);
60 SimpleNode<?> simpleNode = (SimpleNode<?>) node;
61 if (simpleNode.getValue().equals("45")) {
63 } else if (simpleNode.getValue().equals("55")) {
65 } else if (simpleNode.getValue().equals("66")) {
77 * List contains 4 items and in every item are other elements. It is
78 * supposed that there should be: lf11, lflst11, cont11, lst11
81 public void multipleItemsInListTest() {
82 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/multiple-items-in-list.json", true,
83 JsonToCompositeNodeProvider.INSTANCE);
85 assertNotNull(compositeNode);
86 assertEquals("lst", compositeNode.getNodeType().getLocalName());
88 verityMultipleItemsInList(compositeNode);
92 public void nullArrayToSimpleNodeWithNullValueTest() {
93 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/array-with-null.json", true,
94 JsonToCompositeNodeProvider.INSTANCE);
95 assertNotNull(compositeNode);
96 assertEquals("cont", compositeNode.getNodeType().getLocalName());
98 assertNotNull(compositeNode.getChildren());
99 assertEquals(1, compositeNode.getChildren().size());
100 Node<?> lfNode = compositeNode.getChildren().iterator().next();
102 assertTrue(lfNode instanceof SimpleNode<?>);
103 assertEquals(null, ((SimpleNode<?>) lfNode).getValue());
108 public void incorrectTopLevelElementsTest() {
109 Throwable cause1 = null;
112 .readInputToCnSn("/json-to-cnsn/wrong-top-level1.json", true, JsonToCompositeNodeProvider.INSTANCE);
113 } catch (WebApplicationException e) {
117 assertNotNull(cause1);
122 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
124 Throwable cause2 = null;
127 .readInputToCnSn("/json-to-cnsn/wrong-top-level2.json", true, JsonToCompositeNodeProvider.INSTANCE);
128 } catch (WebApplicationException e) {
131 assertNotNull(cause2);
132 assertTrue(cause2.getCause().getMessage().contains("Json Object should contain one element"));
134 Throwable cause3 = null;
137 .readInputToCnSn("/json-to-cnsn/wrong-top-level3.json", true, JsonToCompositeNodeProvider.INSTANCE);
138 } catch (WebApplicationException e) {
141 assertNotNull(cause3);
146 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
151 * if leaf list with no data is in json then no corresponding data is
152 * created in composite node. if leaf with no data then exception is raised
155 public void emptyDataReadTest() {
156 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/empty-data.json", true,
157 JsonToCompositeNodeProvider.INSTANCE);
159 assertNotNull(compositeNode);
161 assertEquals("cont", compositeNode.getNodeType().getLocalName());
162 assertTrue(compositeNode instanceof CompositeNode);
163 List<Node<?>> children = ((CompositeNode) compositeNode).getChildren();
164 assertEquals(1, children.size());
165 assertEquals("lflst2", children.get(0).getNodeType().getLocalName());
166 assertEquals("45", children.get(0).getValue());
168 String reason = null;
170 TestUtils.readInputToCnSn("/json-to-cnsn/empty-data1.json", true, JsonToCompositeNodeProvider.INSTANCE);
171 } catch (JsonSyntaxException e) {
172 reason = e.getMessage();
175 assertTrue(reason.contains("Expected value at line"));
180 * Tests whether namespace <b>stay unchanged</b> if concrete values are
181 * present in composite or simple node and if the method for update is
186 public void notSupplyNamespaceIfAlreadySupplied() {
188 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/simple-list.json", false,
189 JsonToCompositeNodeProvider.INSTANCE);
190 assertNotNull(compositeNode);
192 // supplement namespaces according to first data schema -
193 // "simple:data:types1"
194 Set<Module> modules1 = new HashSet<>();
195 Set<Module> modules2 = new HashSet<>();
196 modules1 = TestUtils.loadModulesFrom("/json-to-cnsn/simple-list-yang/1");
197 modules2 = TestUtils.loadModulesFrom("/json-to-cnsn/simple-list-yang/2");
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 TestUtils.normalizeCompositeNode(compositeNode, modules2, "simple-list-yang2:lst");
210 verifyCompositeNode(compNode, "simple:list:yang1");
214 public void jsonIdentityrefToCompositeNode() {
215 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/identityref/json/data.json", false,
216 JsonToCompositeNodeProvider.INSTANCE);
217 assertNotNull(compositeNode);
219 Set<Module> modules = TestUtils.loadModulesFrom("/json-to-cnsn/identityref");
220 assertEquals(2, modules.size());
222 TestUtils.normalizeCompositeNode(compositeNode, modules, "identityref-module:cont");
224 assertEquals("cont", compositeNode.getNodeType().getLocalName());
226 List<Node<?>> childs = compositeNode.getChildren();
227 assertEquals(1, childs.size());
228 Node<?> nd = childs.iterator().next();
229 assertTrue(nd instanceof CompositeNode);
230 assertEquals("cont1", nd.getNodeType().getLocalName());
232 childs = ((CompositeNode) nd).getChildren();
233 assertEquals(4, childs.size());
234 SimpleNode<?> lf11 = null;
235 SimpleNode<?> lf12 = null;
236 SimpleNode<?> lf13 = null;
237 SimpleNode<?> lf14 = null;
238 for (Node<?> child : childs) {
239 assertTrue(child instanceof SimpleNode);
240 if (child.getNodeType().getLocalName().equals("lf11")) {
241 lf11 = (SimpleNode<?>) child;
242 } else if (child.getNodeType().getLocalName().equals("lf12")) {
243 lf12 = (SimpleNode<?>) child;
244 } else if (child.getNodeType().getLocalName().equals("lf13")) {
245 lf13 = (SimpleNode<?>) child;
246 } else if (child.getNodeType().getLocalName().equals("lf14")) {
247 lf14 = (SimpleNode<?>) child;
251 assertTrue(lf11.getValue() instanceof QName);
252 assertEquals("iden", ((QName) lf11.getValue()).getLocalName());
253 assertEquals("identity:module", ((QName) lf11.getValue()).getNamespace().toString());
255 assertTrue(lf12.getValue() instanceof QName);
256 assertEquals("iden_local", ((QName) lf12.getValue()).getLocalName());
257 assertEquals("identityref:module", ((QName) lf12.getValue()).getNamespace().toString());
259 assertTrue(lf13.getValue() instanceof QName);
260 assertEquals("iden_local", ((QName) lf13.getValue()).getLocalName());
261 assertEquals("identityref:module", ((QName) lf13.getValue()).getNamespace().toString());
263 assertTrue(lf14.getValue() instanceof QName);
264 assertEquals("iden_local", ((QName) lf14.getValue()).getLocalName());
265 assertEquals("identity:module", ((QName) lf14.getValue()).getNamespace().toString());
268 private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace,
270 CompositeNode compositeNode = TestUtils.readInputToCnSn(jsonPath, false, JsonToCompositeNodeProvider.INSTANCE);
271 assertNotNull(compositeNode);
273 Set<Module> modules = null;
274 modules = TestUtils.loadModulesFrom(yangPath);
275 assertNotNull(modules);
277 TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + topLevelElementName);
279 assertTrue(compositeNode instanceof CompositeNodeWrapper);
280 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
282 assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
283 verifyCompositeNode(compNode, namespace);
286 private void verityMultipleItemsInList(CompositeNode compositeNode) {
287 List<Node<?>> childrenNodes = compositeNode.getChildren();
288 assertEquals(4, childrenNodes.size());
289 boolean lf11Found = false;
290 boolean cont11Found = false;
291 boolean lst11Found = false;
292 for (Node<?> lst1Item : childrenNodes) {
293 assertEquals("lst1", lst1Item.getNodeType().getLocalName());
294 assertTrue(lst1Item instanceof CompositeNode);
296 List<Node<?>> childrenLst1 = ((CompositeNode) lst1Item).getChildren();
297 assertEquals(1, childrenLst1.size());
298 String localName = childrenLst1.get(0).getNodeType().getLocalName();
299 if (localName.equals("lf11")) {
300 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
302 } else if (localName.equals("lflst11")) {
303 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
304 assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
306 } else if (localName.equals("cont11")) {
307 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
309 } else if (localName.equals("lst11")) {
311 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
312 assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size());
316 assertTrue(lf11Found);
317 assertTrue(cont11Found);
318 assertTrue(lst11Found);
321 private void verifyCompositeNode(CompositeNode compositeNode, String namespace) {
322 boolean cont1Found = false;
323 boolean lst1Found = false;
324 boolean lflst1_1Found = false;
325 boolean lflst1_2Found = false;
326 boolean lf1Found = false;
328 // assertEquals(namespace,
329 // compositeNode.getNodeType().getNamespace().toString());
331 for (Node<?> node : compositeNode.getChildren()) {
332 if (node.getNodeType().getLocalName().equals("cont1")) {
333 if (node instanceof CompositeNode) {
335 assertEquals(0, ((CompositeNode) node).getChildren().size());
337 } else if (node.getNodeType().getLocalName().equals("lst1")) {
338 if (node instanceof CompositeNode) {
340 assertEquals(0, ((CompositeNode) node).getChildren().size());
342 } else if (node.getNodeType().getLocalName().equals("lflst1")) {
343 if (node instanceof SimpleNode) {
344 if (((SimpleNode<?>) node).getValue().equals("lflst1_1")) {
345 lflst1_1Found = true;
346 } else if (((SimpleNode<?>) node).getValue().equals("lflst1_2")) {
347 lflst1_2Found = true;
351 } else if (node.getNodeType().getLocalName().equals("lf1")) {
352 if (node instanceof SimpleNode) {
353 if (((SimpleNode<?>) node).getValue().equals("lf1")) {
358 assertEquals(namespace, node.getNodeType().getNamespace().toString());
360 assertTrue(cont1Found);
361 assertTrue(lst1Found);
362 assertTrue(lflst1_1Found);
363 assertTrue(lflst1_2Found);
364 assertTrue(lf1Found);
368 public void unsupportedDataFormatTest() {
369 String exceptionMessage = "";
371 TestUtils.readInputToCnSn("/json-to-cnsn/unsupported-json-format.json", true,
372 JsonToCompositeNodeProvider.INSTANCE);
373 } catch (WebApplicationException e) {
374 exceptionMessage = e.getCause().getMessage();
376 assertTrue(exceptionMessage.contains("Root element of Json has to be Object"));