1 package org.opendaylight.controller.sal.restconf.impl.test;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import static org.mockito.Matchers.any;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.when;
13 import java.util.concurrent.*;
15 import javax.ws.rs.WebApplicationException;
18 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
19 import org.opendaylight.controller.sal.restconf.impl.*;
20 import org.opendaylight.yangtools.yang.data.api.*;
22 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.model.api.*;
26 import com.google.gson.JsonSyntaxException;
28 public class FromJsonToCompositeNodeTest {
30 private static final Logger LOG = LoggerFactory.getLogger(FromJsonToCompositeNodeTest.class);
33 public void simpleListTest() {
34 simpleTest("/json-to-composite-node/simple-list.json", "/json-to-composite-node/simple-list-yang", "lst",
35 "simple:list:yang1", "simple-list-yang1");
39 public void simpleContainerTest() {
40 simpleTest("/json-to-composite-node/simple-container.json", "/json-to-composite-node/simple-container-yang",
41 "cont", "simple:container:yang", "simple-container-yang");
45 * test if for every leaf list item is simple node instance created
48 public void multipleItemsInLeafList() {
49 CompositeNode compositeNode = compositeContainerFromJson(
50 "/json-to-composite-node/multiple-leaflist-items.json", true);
51 assertNotNull(compositeNode);
52 assertEquals(3, compositeNode.getChildren().size());
54 boolean lflst1_1 = false;
55 boolean lflst1_2 = false;
56 boolean lflst1_3 = false;
58 for (Node<?> node : compositeNode.getChildren()) {
59 assertEquals("lflst1", node.getNodeType().getLocalName());
60 assertTrue(node instanceof SimpleNode<?>);
61 SimpleNode<?> simpleNode = (SimpleNode<?>) node;
62 if (simpleNode.getValue().equals("45")) {
64 } else if (simpleNode.getValue().equals("55")) {
66 } else if (simpleNode.getValue().equals("66")) {
78 * List contains 4 items and in every item are other elements. It is
79 * supposed that there should be: lf11, lflst11, cont11, lst11
82 public void multipleItemsInListTest() {
83 CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/multiple-items-in-list.json",
85 assertNotNull(compositeNode);
87 assertEquals("lst", compositeNode.getNodeType().getLocalName());
89 verityMultipleItemsInList(compositeNode);
93 public void nullArrayToCompositeNodeWithNullValueTest() {
94 CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/array-with-null.json", true);
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;
111 compositeContainerFromJson("/json-to-composite-node/wrong-top-level1.json", true);
112 } catch (WebApplicationException e) {
116 assertNotNull(cause1);
121 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
123 Throwable cause2 = null;
125 compositeContainerFromJson("/json-to-composite-node/wrong-top-level2.json", true);
126 } catch (WebApplicationException e) {
129 assertNotNull(cause2);
130 assertTrue(cause2.getCause().getMessage().contains("Json Object should contain one element"));
132 Throwable cause3 = null;
134 compositeContainerFromJson("/json-to-composite-node/wrong-top-level3.json", true);
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 = compositeContainerFromJson("/json-to-composite-node/empty-data.json", true);
155 assertNotNull(compositeNode);
157 assertEquals("cont", compositeNode.getNodeType().getLocalName());
158 assertTrue(compositeNode instanceof CompositeNode);
159 List<Node<?>> children = ((CompositeNode) compositeNode).getChildren();
160 assertEquals(1, children.size());
161 assertEquals("lflst2", children.get(0).getNodeType().getLocalName());
162 assertEquals("45", children.get(0).getValue());
164 String reason = null;
166 compositeContainerFromJson("/json-to-composite-node/empty-data1.json", true);
167 } catch (JsonSyntaxException e) {
168 reason = e.getMessage();
171 assertTrue(reason.contains("Expected value at line"));
176 * Tests whether namespace <b>stay unchanged</b> if concrete values are
177 * present in composite or simple node and if the method for update is
182 public void notSupplyNamespaceIfAlreadySupplied() {
184 CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/simple-list.json");
185 assertNotNull(compositeNode);
187 DataSchemaNode dataSchemaNode1 = null;
188 DataSchemaNode dataSchemaNode2 = null;
190 dataSchemaNode1 = TestUtils.obtainSchemaFromYang("/json-to-composite-node/simple-list-yang",
191 "simple-list-yang1");
192 dataSchemaNode2 = TestUtils.obtainSchemaFromYang("/json-to-composite-node/simple-list-yang",
193 "simple-list-yang2");
194 } catch (FileNotFoundException e) {
195 LOG.error(e.getMessage());
198 assertNotNull(dataSchemaNode1);
199 assertNotNull(dataSchemaNode2);
201 // supplement namespaces according to first data schema -
202 // "simple:data:types1"
203 TestUtils.supplementNamespace(dataSchemaNode1, compositeNode);
205 assertTrue(compositeNode instanceof CompositeNodeWrapper);
206 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap(null);
208 assertEquals("lst", compNode.getNodeType().getLocalName());
209 verifyCompositeNode(compNode, "simple:list:yang1");
211 // dataSchemaNode2 should't be taken into account, because compNode
212 // isn't CompositeNodeWrapper
213 TestUtils.supplementNamespace(dataSchemaNode2, compNode);
214 verifyCompositeNode(compNode, "simple:list:yang1");
218 private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace,
220 CompositeNode compositeNode = compositeContainerFromJson(jsonPath);
221 assertNotNull(compositeNode);
223 DataSchemaNode dataSchemaNode = null;
225 dataSchemaNode = TestUtils.obtainSchemaFromYang(yangPath, moduleName);
226 } catch (FileNotFoundException e) {
227 LOG.error(e.getMessage());
230 assertNotNull(dataSchemaNode);
232 TestUtils.supplementNamespace(dataSchemaNode, compositeNode);
234 assertTrue(compositeNode instanceof CompositeNodeWrapper);
235 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap(null);
237 assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
238 verifyCompositeNode(compNode, namespace);
241 private void verityMultipleItemsInList(CompositeNode compositeNode) {
242 List<Node<?>> childrenNodes = compositeNode.getChildren();
243 assertEquals(4, childrenNodes.size());
244 boolean lf11Found = false;
245 boolean cont11Found = false;
246 boolean lst11Found = false;
247 for (Node<?> lst1Item : childrenNodes) {
248 assertEquals("lst1", lst1Item.getNodeType().getLocalName());
249 assertTrue(lst1Item instanceof CompositeNode);
251 List<Node<?>> childrenLst1 = ((CompositeNode) lst1Item).getChildren();
252 assertEquals(1, childrenLst1.size());
253 String localName = childrenLst1.get(0).getNodeType().getLocalName();
254 if (localName.equals("lf11")) {
255 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
257 } else if (localName.equals("lflst11")) {
258 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
259 assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
261 } else if (localName.equals("cont11")) {
262 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
264 } else if (localName.equals("lst11")) {
266 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
267 assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size());
271 assertTrue(lf11Found);
272 assertTrue(cont11Found);
273 assertTrue(lst11Found);
276 private void verifyCompositeNode(CompositeNode compositeNode, String namespace) {
277 boolean cont1Found = false;
278 boolean lst1Found = false;
279 boolean lflst1_1Found = false;
280 boolean lflst1_2Found = false;
281 boolean lf1Found = false;
283 assertEquals(namespace, compositeNode.getNodeType().getNamespace().toString());
285 for (Node<?> node : compositeNode.getChildren()) {
286 if (node.getNodeType().getLocalName().equals("cont1")) {
287 if (node instanceof CompositeNode) {
289 assertEquals(0, ((CompositeNode) node).getChildren().size());
291 } else if (node.getNodeType().getLocalName().equals("lst1")) {
292 if (node instanceof CompositeNode) {
294 assertEquals(0, ((CompositeNode) node).getChildren().size());
296 } else if (node.getNodeType().getLocalName().equals("lflst1")) {
297 if (node instanceof SimpleNode) {
298 if (((SimpleNode<?>) node).getValue().equals("lflst1_1")) {
299 lflst1_1Found = true;
300 } else if (((SimpleNode<?>) node).getValue().equals("lflst1_2")) {
301 lflst1_2Found = true;
305 } else if (node.getNodeType().getLocalName().equals("lf1")) {
306 if (node instanceof SimpleNode) {
307 if (((SimpleNode<?>) node).getValue().equals("lf1")) {
312 assertEquals(namespace, node.getNodeType().getNamespace().toString());
314 assertTrue(cont1Found);
315 assertTrue(lst1Found);
316 assertTrue(lflst1_1Found);
317 assertTrue(lflst1_2Found);
318 assertTrue(lf1Found);
321 private CompositeNode compositeContainerFromJson(String jsonPath) {
322 return compositeContainerFromJson(jsonPath, false);
325 private CompositeNode compositeContainerFromJson(String jsonPath, boolean dummyNamespaces)
326 throws WebApplicationException {
328 JsonToCompositeNodeProvider jsonToCompositeNodeProvider = JsonToCompositeNodeProvider.INSTANCE;
329 InputStream jsonStream = FromJsonToCompositeNodeTest.class.getResourceAsStream(jsonPath);
331 CompositeNode compositeNode = jsonToCompositeNodeProvider
332 .readFrom(null, null, null, null, null, jsonStream);
333 assertTrue(compositeNode instanceof CompositeNodeWrapper);
334 if (dummyNamespaces) {
336 TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
337 return ((CompositeNodeWrapper) compositeNode).unwrap(null);
338 } catch (URISyntaxException e) {
339 LOG.error(e.getMessage());
340 assertTrue(e.getMessage(), false);
343 return compositeNode;
344 } catch (IOException e) {
345 LOG.error(e.getMessage());
346 assertTrue(e.getMessage(), false);