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;
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.*;
16 import org.opendaylight.yangtools.yang.data.api.*;
19 import com.google.gson.JsonSyntaxException;
21 public class FromJsonToCompositeNode {
22 Logger LOG = LoggerFactory.getLogger(FromJsonToCompositeNode.class);
25 public void simpleListTest() {
26 CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/simple-list.json");
27 assertNotNull(compositeNode);
29 assertEquals("lst", compositeNode.getNodeType().getLocalName());
30 verifyCompositeNode(compositeNode);
34 public void simpleContainerTest() {
35 CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/simple-container.json");
36 assertNotNull(compositeNode);
38 assertEquals("cont", compositeNode.getNodeType().getLocalName());
40 verifyCompositeNode(compositeNode);
44 * List contains 4 items and in every item are other elements. It is
45 * supposed that there should be: lf11, lflst11, cont11, lst11
48 public void multipleItemsInListTest() {
49 CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/multiple-items-in-list.json");
50 assertNotNull(compositeNode);
52 assertEquals("lst", compositeNode.getNodeType().getLocalName());
54 verityMultipleItemsInList(compositeNode);
58 public void incorrectTopLevelElementsTest() {
59 Throwable cause1 = null;
61 compositeContainerFromJson("/json-to-composite-node/wrong-top-level1.json");
62 } catch (WebApplicationException e) {
66 assertNotNull(cause1);
71 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
73 Throwable cause2 = null;
75 compositeContainerFromJson("/json-to-composite-node/wrong-top-level2.json");
76 } catch (WebApplicationException e) {
79 assertNotNull(cause2);
80 assertTrue(cause2.getCause().getMessage().contains("Json Object should contain one element"));
82 Throwable cause3 = null;
84 compositeContainerFromJson("/json-to-composite-node/wrong-top-level3.json");
85 } catch (WebApplicationException e) {
88 assertNotNull(cause3);
93 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
98 * if leaf list with no data is in json then no corresponding data is
99 * created in composite node. if leaf with no data then exception is raised
102 public void emptyDataReadTest() {
103 CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/empty-data.json");
105 assertNotNull(compositeNode);
107 assertEquals("cont", compositeNode.getNodeType().getLocalName());
108 assertTrue(compositeNode instanceof CompositeNode);
109 List<Node<?>> children = ((CompositeNode) compositeNode).getChildren();
110 assertEquals(1, children.size());
111 assertEquals("lflst2", children.get(0).getNodeType().getLocalName());
112 assertEquals("45", children.get(0).getValue());
114 String reason = null;
116 compositeContainerFromJson("/json-to-composite-node/empty-data1.json");
117 } catch (JsonSyntaxException e) {
118 reason = e.getMessage();
121 assertTrue(reason.contains("Expected value at line"));
125 private void verityMultipleItemsInList(CompositeNode compositeNode) {
126 List<Node<?>> childrenNodes = compositeNode.getChildren();
127 assertEquals(4, childrenNodes.size());
128 boolean lf11Found = false;
129 boolean cont11Found = false;
130 boolean lst11Found = false;
131 for (Node<?> lst1Item : childrenNodes) {
132 assertEquals("lst1", lst1Item.getNodeType().getLocalName());
133 assertTrue(lst1Item instanceof CompositeNode);
135 List<Node<?>> childrenLst1 = ((CompositeNode) lst1Item).getChildren();
136 assertEquals(1, childrenLst1.size());
137 String localName = childrenLst1.get(0).getNodeType().getLocalName();
138 if (localName.equals("lf11")) {
139 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
141 } else if (localName.equals("lflst11")) {
142 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
143 assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
145 } else if (localName.equals("cont11")) {
146 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
148 } else if (localName.equals("lst11")) {
150 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
151 assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size());
156 assertTrue(lf11Found);
157 assertTrue(cont11Found);
158 assertTrue(lst11Found);
162 private void verifyCompositeNode(CompositeNode compositeNode) {
163 boolean cont1Found = false;
164 boolean lst1Found = false;
165 boolean lflst1_1Found = false;
166 boolean lflst1_2Found = false;
167 boolean lf1Found = false;
169 for (Node<?> node : compositeNode.getChildren()) {
170 if (node.getNodeType().getLocalName().equals("cont1")) {
171 if (node instanceof CompositeNode) {
173 assertEquals(0, ((CompositeNode) node).getChildren().size());
175 } else if (node.getNodeType().getLocalName().equals("lst1")) {
176 if (node instanceof CompositeNode) {
178 assertEquals(0, ((CompositeNode) node).getChildren().size());
180 } else if (node.getNodeType().getLocalName().equals("lflst1")) {
181 if (node instanceof SimpleNode) {
182 if (((SimpleNode<?>) node).getValue().equals("lflst1_1")) {
183 lflst1_1Found = true;
184 } else if (((SimpleNode<?>) node).getValue().equals("lflst1_2")) {
185 lflst1_2Found = true;
189 } else if (node.getNodeType().getLocalName().equals("lf1")) {
190 if (node instanceof SimpleNode) {
191 if (((SimpleNode<?>) node).getValue().equals("lf1")) {
197 assertTrue(cont1Found);
198 assertTrue(lst1Found);
199 assertTrue(lflst1_1Found);
200 assertTrue(lflst1_2Found);
201 assertTrue(lf1Found);
204 private CompositeNode compositeContainerFromJson(String jsonPath) throws WebApplicationException {
206 JsonToCompositeNodeProvider jsonToCompositeNodeProvider = JsonToCompositeNodeProvider.INSTANCE;
207 InputStream jsonStream = FromJsonToCompositeNode.class.getResourceAsStream(jsonPath);
209 CompositeNode compositeNode = jsonToCompositeNodeProvider
210 .readFrom(null, null, null, null, null, jsonStream);
211 assertTrue(compositeNode instanceof CompositeNodeWrapper);
213 addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
214 return ((CompositeNodeWrapper) compositeNode).unwrap(null);
215 } catch (URISyntaxException e) {
216 LOG.error(e.getMessage());
217 assertTrue(e.getMessage(), false);
219 } catch (IOException e) {
220 LOG.error(e.getMessage());
221 assertTrue(e.getMessage(), false);
226 private void addDummyNamespaceToAllNodes(NodeWrapper<?> wrappedNode) throws URISyntaxException {
227 wrappedNode.setNamespace(new URI(""));
228 if (wrappedNode instanceof CompositeNodeWrapper) {
229 for (NodeWrapper<?> childNodeWrapper : ((CompositeNodeWrapper) wrappedNode).getValues()) {
230 addDummyNamespaceToAllNodes(childNodeWrapper);