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 FromJsonToCompositeNode {
30 private static Logger LOG = LoggerFactory.getLogger(FromJsonToCompositeNode.class);
33 public void simpleListTest() {
34 simpleTest("/json-to-composite-node/simple-list.json", "/json-to-composite-node/simple-list-yang", "lst",
39 public void simpleContainerTest() {
40 simpleTest("/json-to-composite-node/simple-container.json", "/json-to-composite-node/simple-container-yang",
41 "cont", "simple:data:types");
45 * List contains 4 items and in every item are other elements. It is
46 * supposed that there should be: lf11, lflst11, cont11, lst11
49 public void multipleItemsInListTest() {
50 CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/multiple-items-in-list.json",
52 assertNotNull(compositeNode);
54 assertEquals("lst", compositeNode.getNodeType().getLocalName());
56 verityMultipleItemsInList(compositeNode);
60 public void incorrectTopLevelElementsTest() {
61 Throwable cause1 = null;
63 compositeContainerFromJson("/json-to-composite-node/wrong-top-level1.json", true);
64 } catch (WebApplicationException e) {
68 assertNotNull(cause1);
73 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
75 Throwable cause2 = null;
77 compositeContainerFromJson("/json-to-composite-node/wrong-top-level2.json", true);
78 } catch (WebApplicationException e) {
81 assertNotNull(cause2);
82 assertTrue(cause2.getCause().getMessage().contains("Json Object should contain one element"));
84 Throwable cause3 = null;
86 compositeContainerFromJson("/json-to-composite-node/wrong-top-level3.json", true);
87 } catch (WebApplicationException e) {
90 assertNotNull(cause3);
95 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
100 * if leaf list with no data is in json then no corresponding data is
101 * created in composite node. if leaf with no data then exception is raised
104 public void emptyDataReadTest() {
105 CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/empty-data.json", true);
107 assertNotNull(compositeNode);
109 assertEquals("cont", compositeNode.getNodeType().getLocalName());
110 assertTrue(compositeNode instanceof CompositeNode);
111 List<Node<?>> children = ((CompositeNode) compositeNode).getChildren();
112 assertEquals(1, children.size());
113 assertEquals("lflst2", children.get(0).getNodeType().getLocalName());
114 assertEquals("45", children.get(0).getValue());
116 String reason = null;
118 compositeContainerFromJson("/json-to-composite-node/empty-data1.json", true);
119 } catch (JsonSyntaxException e) {
120 reason = e.getMessage();
123 assertTrue(reason.contains("Expected value at line"));
127 private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace) {
128 CompositeNode compositeNode = compositeContainerFromJson(jsonPath);
129 assertNotNull(compositeNode);
131 DataSchemaNode dataSchemaNode = null;
133 dataSchemaNode = TestUtils.obtainSchemaFromYang(yangPath);
134 } catch (FileNotFoundException e) {
135 LOG.error(e.getMessage());
138 assertNotNull(dataSchemaNode);
140 TestUtils.supplementNamespace(dataSchemaNode, compositeNode);
142 assertTrue(compositeNode instanceof CompositeNodeWrapper);
143 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap(null);
145 assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
146 verifyCompositeNode(compNode, namespace);
149 private void verityMultipleItemsInList(CompositeNode compositeNode) {
150 List<Node<?>> childrenNodes = compositeNode.getChildren();
151 assertEquals(4, childrenNodes.size());
152 boolean lf11Found = false;
153 boolean cont11Found = false;
154 boolean lst11Found = false;
155 for (Node<?> lst1Item : childrenNodes) {
156 assertEquals("lst1", lst1Item.getNodeType().getLocalName());
157 assertTrue(lst1Item instanceof CompositeNode);
159 List<Node<?>> childrenLst1 = ((CompositeNode) lst1Item).getChildren();
160 assertEquals(1, childrenLst1.size());
161 String localName = childrenLst1.get(0).getNodeType().getLocalName();
162 if (localName.equals("lf11")) {
163 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
165 } else if (localName.equals("lflst11")) {
166 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
167 assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
169 } else if (localName.equals("cont11")) {
170 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
172 } else if (localName.equals("lst11")) {
174 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
175 assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size());
179 assertTrue(lf11Found);
180 assertTrue(cont11Found);
181 assertTrue(lst11Found);
184 private void verifyCompositeNode(CompositeNode compositeNode, String namespace) {
185 boolean cont1Found = false;
186 boolean lst1Found = false;
187 boolean lflst1_1Found = false;
188 boolean lflst1_2Found = false;
189 boolean lf1Found = false;
191 assertEquals(namespace, compositeNode.getNodeType().getNamespace().toString());
193 for (Node<?> node : compositeNode.getChildren()) {
194 if (node.getNodeType().getLocalName().equals("cont1")) {
195 if (node instanceof CompositeNode) {
197 assertEquals(0, ((CompositeNode) node).getChildren().size());
199 } else if (node.getNodeType().getLocalName().equals("lst1")) {
200 if (node instanceof CompositeNode) {
202 assertEquals(0, ((CompositeNode) node).getChildren().size());
204 } else if (node.getNodeType().getLocalName().equals("lflst1")) {
205 if (node instanceof SimpleNode) {
206 if (((SimpleNode<?>) node).getValue().equals("lflst1_1")) {
207 lflst1_1Found = true;
208 } else if (((SimpleNode<?>) node).getValue().equals("lflst1_2")) {
209 lflst1_2Found = true;
213 } else if (node.getNodeType().getLocalName().equals("lf1")) {
214 if (node instanceof SimpleNode) {
215 if (((SimpleNode<?>) node).getValue().equals("lf1")) {
220 assertEquals(namespace, node.getNodeType().getNamespace().toString());
222 assertTrue(cont1Found);
223 assertTrue(lst1Found);
224 assertTrue(lflst1_1Found);
225 assertTrue(lflst1_2Found);
226 assertTrue(lf1Found);
229 private CompositeNode compositeContainerFromJson(String jsonPath) {
230 return compositeContainerFromJson(jsonPath, false);
233 private CompositeNode compositeContainerFromJson(String jsonPath, boolean dummyNamespaces)
234 throws WebApplicationException {
236 JsonToCompositeNodeProvider jsonToCompositeNodeProvider = JsonToCompositeNodeProvider.INSTANCE;
237 InputStream jsonStream = FromJsonToCompositeNode.class.getResourceAsStream(jsonPath);
239 CompositeNode compositeNode = jsonToCompositeNodeProvider
240 .readFrom(null, null, null, null, null, jsonStream);
241 assertTrue(compositeNode instanceof CompositeNodeWrapper);
242 if (dummyNamespaces) {
244 TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
245 return ((CompositeNodeWrapper) compositeNode).unwrap(null);
246 } catch (URISyntaxException e) {
247 LOG.error(e.getMessage());
248 assertTrue(e.getMessage(), false);
251 return compositeNode;
252 } catch (IOException e) {
253 LOG.error(e.getMessage());
254 assertTrue(e.getMessage(), false);