1 package org.opendaylight.controller.sal.restconf.impl.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.*;
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 RestconfImpl restconf = RestconfImpl.getInstance();
224 ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext(modules));
226 TestUtils.prepareMockForRestconfBeforeNormalization(modules, dataSchemaNode, restconf);
228 restconf.createConfigurationData("identityref-module:cont", compositeNode);
230 assertEquals("cont", compositeNode.getNodeType().getLocalName());
232 List<Node<?>> childs = compositeNode.getChildren();
233 assertEquals(1, childs.size());
234 Node<?> nd = childs.iterator().next();
235 assertTrue(nd instanceof CompositeNode);
236 assertEquals("cont1", nd.getNodeType().getLocalName());
238 childs = ((CompositeNode) nd).getChildren();
239 assertEquals(4, childs.size());
240 SimpleNode<?> lf11 = null;
241 SimpleNode<?> lf12 = null;
242 SimpleNode<?> lf13 = null;
243 SimpleNode<?> lf14 = null;
244 for (Node<?> child : childs) {
245 assertTrue(child instanceof SimpleNode);
246 if (child.getNodeType().getLocalName().equals("lf11")) {
247 lf11 = (SimpleNode<?>) child;
248 } else if (child.getNodeType().getLocalName().equals("lf12")) {
249 lf12 = (SimpleNode<?>) child;
250 } else if (child.getNodeType().getLocalName().equals("lf13")) {
251 lf13 = (SimpleNode<?>) child;
252 } else if (child.getNodeType().getLocalName().equals("lf14")) {
253 lf14 = (SimpleNode<?>) child;
257 assertTrue(lf11.getValue() instanceof QName);
258 assertEquals("iden", ((QName) lf11.getValue()).getLocalName());
259 assertEquals("identity:module", ((QName) lf11.getValue()).getNamespace().toString());
261 assertTrue(lf12.getValue() instanceof QName);
262 assertEquals("iden_local", ((QName) lf12.getValue()).getLocalName());
263 assertEquals("identityref:module", ((QName) lf12.getValue()).getNamespace().toString());
265 assertTrue(lf13.getValue() instanceof QName);
266 assertEquals("iden_local", ((QName) lf13.getValue()).getLocalName());
267 assertEquals("identityref:module", ((QName) lf13.getValue()).getNamespace().toString());
269 assertTrue(lf14.getValue() instanceof QName);
270 assertEquals("iden_local", ((QName) lf14.getValue()).getLocalName());
271 assertEquals("identity:module", ((QName) lf14.getValue()).getNamespace().toString());
274 private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace,
276 CompositeNode compositeNode = compositeContainerFromJson(jsonPath);
277 assertNotNull(compositeNode);
279 DataSchemaNode dataSchemaNode = null;
281 dataSchemaNode = TestUtils.obtainSchemaFromYang(yangPath, moduleName);
282 } catch (FileNotFoundException e) {
283 LOG.error(e.getMessage());
286 assertNotNull(dataSchemaNode);
288 TestUtils.supplementNamespace(dataSchemaNode, compositeNode);
290 assertTrue(compositeNode instanceof CompositeNodeWrapper);
291 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
293 assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
294 verifyCompositeNode(compNode, namespace);
297 private void verityMultipleItemsInList(CompositeNode compositeNode) {
298 List<Node<?>> childrenNodes = compositeNode.getChildren();
299 assertEquals(4, childrenNodes.size());
300 boolean lf11Found = false;
301 boolean cont11Found = false;
302 boolean lst11Found = false;
303 for (Node<?> lst1Item : childrenNodes) {
304 assertEquals("lst1", lst1Item.getNodeType().getLocalName());
305 assertTrue(lst1Item instanceof CompositeNode);
307 List<Node<?>> childrenLst1 = ((CompositeNode) lst1Item).getChildren();
308 assertEquals(1, childrenLst1.size());
309 String localName = childrenLst1.get(0).getNodeType().getLocalName();
310 if (localName.equals("lf11")) {
311 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
313 } else if (localName.equals("lflst11")) {
314 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
315 assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
317 } else if (localName.equals("cont11")) {
318 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
320 } else if (localName.equals("lst11")) {
322 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
323 assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size());
327 assertTrue(lf11Found);
328 assertTrue(cont11Found);
329 assertTrue(lst11Found);
332 private void verifyCompositeNode(CompositeNode compositeNode, String namespace) {
333 boolean cont1Found = false;
334 boolean lst1Found = false;
335 boolean lflst1_1Found = false;
336 boolean lflst1_2Found = false;
337 boolean lf1Found = false;
339 assertEquals(namespace, compositeNode.getNodeType().getNamespace().toString());
341 for (Node<?> node : compositeNode.getChildren()) {
342 if (node.getNodeType().getLocalName().equals("cont1")) {
343 if (node instanceof CompositeNode) {
345 assertEquals(0, ((CompositeNode) node).getChildren().size());
347 } else if (node.getNodeType().getLocalName().equals("lst1")) {
348 if (node instanceof CompositeNode) {
350 assertEquals(0, ((CompositeNode) node).getChildren().size());
352 } else if (node.getNodeType().getLocalName().equals("lflst1")) {
353 if (node instanceof SimpleNode) {
354 if (((SimpleNode<?>) node).getValue().equals("lflst1_1")) {
355 lflst1_1Found = true;
356 } else if (((SimpleNode<?>) node).getValue().equals("lflst1_2")) {
357 lflst1_2Found = true;
361 } else if (node.getNodeType().getLocalName().equals("lf1")) {
362 if (node instanceof SimpleNode) {
363 if (((SimpleNode<?>) node).getValue().equals("lf1")) {
368 assertEquals(namespace, node.getNodeType().getNamespace().toString());
370 assertTrue(cont1Found);
371 assertTrue(lst1Found);
372 assertTrue(lflst1_1Found);
373 assertTrue(lflst1_2Found);
374 assertTrue(lf1Found);
377 private CompositeNode compositeContainerFromJson(String jsonPath) {
378 return compositeContainerFromJson(jsonPath, false);
381 private CompositeNode compositeContainerFromJson(String jsonPath, boolean dummyNamespaces)
382 throws WebApplicationException {
384 JsonToCompositeNodeProvider jsonToCompositeNodeProvider = JsonToCompositeNodeProvider.INSTANCE;
385 InputStream jsonStream = JsonToCnSnTest.class.getResourceAsStream(jsonPath);
387 CompositeNode compositeNode = jsonToCompositeNodeProvider
388 .readFrom(null, null, null, null, null, jsonStream);
389 assertTrue(compositeNode instanceof CompositeNodeWrapper);
390 if (dummyNamespaces) {
392 TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
393 return ((CompositeNodeWrapper) compositeNode).unwrap();
394 } catch (URISyntaxException e) {
395 LOG.error(e.getMessage());
396 assertTrue(e.getMessage(), false);
399 return compositeNode;
400 } catch (IOException e) {
401 LOG.error(e.getMessage());
402 assertTrue(e.getMessage(), false);