Containerization feature of SO
[so.git] / bpmn / MSOCoreBPMN / src / test / java / org / onap / so / bpmn / core / json / JsonUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.core.json;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.camunda.bpm.engine.delegate.DelegateExecution;
34 import org.camunda.bpm.engine.runtime.Execution;
35 import org.json.JSONArray;
36 import org.json.JSONException;
37 import org.json.JSONObject;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.mockito.Mock;
42 import org.onap.so.exceptions.ValidationException;
43
44 public class JsonUtilsTest {
45
46         @Mock public DelegateExecution execution;
47         @Mock public Execution mockEexecution;
48         private final String fileLocation = "src/test/resources/json-examples/";
49
50         @Rule
51         public ExpectedException expectedException = ExpectedException.none();  
52         
53         @Test
54         public void jsonStringToMapTest() throws IOException {
55
56                 JsonUtils utils = new JsonUtils();
57                 String response = this.getJson("SDNCServiceResponseExample.json");
58                 String entry = JsonUtils.getJsonValue(response, "SDNCServiceResponse.params");
59                 Map<String, String> map = utils.jsonStringToMap(execution, entry);
60                 assertEquals(map.get("e2e-vpn-key"), "my-key");
61         }
62
63         @Test
64         public void entryArrayToMapTest() throws IOException {
65                 JsonUtils utils = new JsonUtils();
66                 String response = this.getJson("SNIROExample.json");
67                 String entry = JsonUtils.getJsonValue(response, "solutionInfo.placementInfo");
68                 JSONArray arr = new JSONArray(entry);
69                 JSONObject homingDataJson = arr.getJSONObject(0);
70                 JSONArray assignmentInfo = homingDataJson.getJSONArray("assignmentInfo");
71                 Map<String, String> map = utils.entryArrayToMap(execution, assignmentInfo.toString(), "variableName", "variableValue");
72                 assertEquals(map.get("cloudOwner"), "att-aic");
73         }
74         @Test
75         public void entryArrayToMapStringTest() throws IOException {
76                 JsonUtils utils = new JsonUtils();
77                 String response = this.getJson("SNIROExample.json");
78                 String entry = JsonUtils.getJsonValue(response, "solutionInfo.placementInfo");
79                 JSONArray arr = new JSONArray(entry);
80                 JSONObject homingDataJson = arr.getJSONObject(0);
81                 JSONArray assignmentInfo = homingDataJson.getJSONArray("assignmentInfo");
82                 Map<String, String> map = utils.entryArrayToMap(assignmentInfo.toString(), "variableName", "variableValue");
83                 assertEquals(map.get("cloudOwner"), "att-aic");
84         }       
85         @Test
86         public void getJsonRootPropertyTest() throws IOException {
87                 String response = this.getJson("SDNCServiceResponseExample.json");
88                 assertEquals("SDNCServiceResponse",JsonUtils.getJsonRootProperty(response));
89         }       
90         @Test
91         public void getJsonRootProperty_ExceptionTest() throws IOException {
92                 expectedException.expect(JSONException.class);
93                 String response = this.getJson("SNIROExample.json");
94                 String root = JsonUtils.getJsonRootProperty(response);
95         }
96         @Test
97         public void getJsonNodeValueTest() throws IOException {
98                 String response = this.getJson("SDNCServiceResponseExample.json");
99                 String code = JsonUtils.getJsonNodeValue(response,"SDNCServiceResponse.responseCode");
100                 assertEquals("200", code); 
101         }       
102         @Test
103         public void  stringArrayToList_jsonStringTest() throws IOException {
104                 String response = this.getJson("SNIROExample.json");
105                 String licenseInfo = JsonUtils.getJsonNodeValue(response,"solutionInfo.licenseInfo");
106                 JsonUtils utils = new JsonUtils();
107                 List<String> listString = utils.StringArrayToList(licenseInfo);
108                 assertNotNull(listString.get(0)); 
109         }       
110         @Test
111         public void  stringArrayToList_JSONArrayTest() throws IOException {
112                 String response = this.getJson("SNIROExample.json");
113                 String licenseInfo = JsonUtils.getJsonNodeValue(response,"solutionInfo.licenseInfo");
114                 JSONArray jsonArray = new JSONArray(licenseInfo);
115                 JsonUtils utils = new JsonUtils();
116                 List<String> listString = utils.StringArrayToList(jsonArray);
117                 assertNotNull(listString.get(0)); 
118         }       
119         @Test
120         public void  stringArrayToList_withExecutionTest() throws IOException {
121                 String response = this.getJson("SNIROExample.json");
122                 String licenseInfo = JsonUtils.getJsonNodeValue(response,"solutionInfo.licenseInfo");
123                 JsonUtils utils = new JsonUtils();
124                 List<String> listString = utils.StringArrayToList(mockEexecution, licenseInfo);
125                 assertNotNull(listString.get(0)); 
126         }
127         @Test
128         public void jsonElementExist_trueTest() throws IOException {
129                 String response = this.getJson("SDNCServiceResponseExample.json");
130                 boolean isExist = JsonUtils.jsonElementExist(response,"SDNCServiceResponse.responseCode");
131                 assertEquals(true, isExist); 
132         }
133         @Test
134         public void jsonElementExist_falseTest() throws IOException {
135                 String response = this.getJson("SDNCServiceResponseExample.json");
136                 boolean isExist = JsonUtils.jsonElementExist(response,"SDNCServiceResponse.responseX");
137                 assertEquals(false, isExist); 
138         }
139         @Test
140         public void jsonElementExist_NullTest() throws IOException  {
141                 String response = this.getJson("SDNCServiceResponseExample.json");
142                 boolean isExist = JsonUtils.jsonElementExist(response, null);
143                 assertEquals(true, isExist);
144         }       
145         @Test
146         public void jsonSchemaValidation_ExceptionTest() throws IOException, ValidationException  {
147                 expectedException.expect(ValidationException.class);
148                 String response = this.getJson("SDNCServiceResponseExample.json");
149                 String isExist = JsonUtils.jsonSchemaValidation(response, fileLocation);
150         }       
151         @Test
152         public void getJsonIntValueTest() throws IOException {
153                 String response = this.getJson("SDNCServiceResponseExample.json");
154                 int intValue = JsonUtils.getJsonIntValue(response,"SDNCServiceResponse.responseCode");
155                 assertEquals(0, intValue); 
156         }
157         @Test
158         public void getJsonBooleanValueTest() throws IOException {
159                 String response = this.getJson("SDNCServiceResponseExample.json");
160                 boolean isBoolean = JsonUtils.getJsonBooleanValue(response,"SDNCServiceResponse.responseCode");
161                 assertEquals(false, isBoolean); 
162         }       
163         @Test
164         public void prettyJsonTest() throws IOException {
165                 String response = this.getJson("SNIROExample.json");
166                 assertNotNull(JsonUtils.prettyJson(response));
167                 String malformedJson = "{\"name\" \"myName\"}";
168                 assertNull(JsonUtils.prettyJson(malformedJson));
169         }
170         @Test
171         public void xml2jsonTest() throws IOException {
172                 String expectedJson = "{\"name\":\"myName\"}";
173                 String xml = "<name>myName</name>";
174                 assertEquals(expectedJson, JsonUtils.xml2json(xml,false));
175         }       
176         @Test
177         public void xml2jsonErrorTest() throws IOException {
178                 String malformedXml = "<name>myName<name>";
179                 assertNull(JsonUtils.xml2json(malformedXml));
180         }
181         @Test
182         public void json2xmlTest() throws IOException {
183                 String expectedXml = "<name>myName</name>";
184                 String malformedJson = "{\"name\":\"myName\"}";
185                 assertEquals(expectedXml, JsonUtils.json2xml(malformedJson, false));
186         }
187         @Test
188         public void json2xmlErrorTest() throws IOException {
189                 String malformedJson = "{\"name\" \"myName\"}";
190                 assertNull(JsonUtils.json2xml(malformedJson));
191         }
192         @Test
193         public void getJsonValueErrorTest() throws IOException {
194                 String response = this.getJson("SDNCServiceResponseExample.json");
195                 assertNull(JsonUtils.getJsonValue(response,null)); 
196         }
197         @Test
198         public void getJsonNodeValueErrorTest() throws IOException {
199                 String response = this.getJson("SDNCServiceResponseExample.json");
200                 assertNull(JsonUtils.getJsonNodeValue(response,null)); 
201         }
202         @Test
203         public void getJsonIntValueErrorTest() throws IOException {
204                 String response = this.getJson("SDNCServiceResponseExample.json");
205                 assertEquals(0, JsonUtils.getJsonIntValue(response,null));
206         }
207         @Test
208         public void getJsonBooleanValueErrorTest() throws IOException {
209                 String response = this.getJson("SDNCServiceResponseExample.json");
210                 assertEquals(false, JsonUtils.getJsonBooleanValue(response,null));
211         }
212         @Test
213         public void getJsonValueForKeyErrorTest() throws IOException {
214                 String malformedJson = "{\"name\" \"myName\"}";
215                 assertNull(JsonUtils.getJsonValueForKey(malformedJson, "name"));
216         }
217         @Test
218         public void updJsonValueTest() throws IOException {
219                 String expectedJson = "{\"name\": \"yourName\"}";
220                 String json = "{\"name\":\"myName\"}";
221                 assertEquals(expectedJson, JsonUtils.updJsonValue(json, "name", "yourName"));
222         }
223         @Test
224         public void updJsonValueErrorTest() throws IOException {
225                 String expectedJson = "{\"name\" \"myName\"}";
226                 String json = "{\"name\" \"myName\"}";
227                 assertEquals(expectedJson, JsonUtils.updJsonValue(json, "name", "yourName"));
228         }
229         
230         private String getJson(String filename) throws IOException {
231                 return new String(Files.readAllBytes(Paths.get(fileLocation + filename)));
232         }
233 }