Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
Copyright (c) 2018 Vereign AG [https://www.vereign.com]
This is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package core;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import exceptions.RAFException;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JsonUtils {
/**
* Validate a JSON against a JSON schema
*
* @param jsonData the JSON as string
* @param jsonSchema the JSON schema as string
* @return true if the validation passes, false otherwise
* @throws IOException
* @throws ProcessingException
*/
public static boolean validateJsonSchema(String jsonData, String jsonSchema) throws IOException, ProcessingException {
JsonNode schemaNode = JsonLoader.fromString(jsonSchema);
JsonNode dataNode = JsonLoader.fromString(jsonData);
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonSchema schema = factory.getJsonSchema(schemaNode);
ProcessingReport report = schema.validate(dataNode);
System.out.println(report.toString());
return report.isSuccess();
}
/**
* @param key The key you want to find inside the JSON
* @param json The JSON you want to search in
* @return NULL if specified key is not found inside the JSON provided. Otherwise the Value of the key provided.
* @throws IOException
*/
public static Object getValueOfKeyFromJson(String key, String json) throws IOException {
ObjectNode object = (ObjectNode) (JsonLoader.fromString(json));
JsonNode valueOfKey = object.get(key);
if (valueOfKey == null) {
return null;
} else {
return valueOfKey.asText();
}
}
/**
* Removes the key from the JSON
*
* @param key the key that will be removed
* @param json the json as String
* @return the JSON as String after the key was removed
* @throws IOException
*/
public static String removeKeyFromJson(String key, String json) throws IOException {
ObjectNode object = (ObjectNode) (JsonLoader.fromString(json));
object.remove(key);
return object.toString();
}
/**
* Loads a map from a json file
* The json file should be located in main/resources
* <p>
* We are using multiple "profiles" in a json in order to easily manage different test data
* used by the same scenario.The json format should be:
* {
* "profile_name1" : {some json},
* "profile_name2" : {some other json}
* }
*
* @param jsonName the name of the json file. If the file is in a subfolder, include the folder in the name
* Example: "/REST.GID.schemas/example_schema.json"
* @param profileName the name of the json "profile"
* @return a map with the key value pairs found in the json "profile"
* @throws IOException
*/
public static Map<String, String> loadMapFromResource(String jsonName, String profileName) throws IOException {
Gson gson = new GsonBuilder().disableHtmlEscaping().serializeNulls().create();
return gson.fromJson(getProfileFromJson(jsonName, profileName),
new TypeToken<HashMap<String, String>>() {
}.getType());
}
/**
* Gets a "profile" from a json
* <p>
* We are using multiple "profiles" in a json in order to easily manage different test data
* used by the same scenario.The json format should be:
* {
* "profile_name1" : {some json},
* "profile_name2" : {some other json}
* }
*
* @param jsonName the name of the json file. If the file is in a subfolder, include the folder in the name
* Example: "/test data/.json"
* @param profileName the name of the json "profile"
* @return the loaded JSON "profile" as string
* @throws IOException
*/
public static String getProfileFromJson(String jsonName, String profileName) throws IOException {
String json = loadJson(jsonName);
Gson gson = new GsonBuilder().disableHtmlEscaping().serializeNulls().create();
Map<String, JsonElement> retMap = gson.fromJson(json, new TypeToken<HashMap<String, JsonElement>>() {
}.getType());
return gson.toJson(retMap.get(profileName));
}
/**
* Loads a json from a file
* @param jsonName the name of the json file. If the file is in a subfolder, include the folder in the name
* Example: "/test data/.json"
* @return the loaded JSON as string
* @throws IOException
*/
public static String loadJson(String jsonName) throws IOException {
return JsonLoader.fromResource(jsonName).toString();
}
/**
* Maps the response body to a Java object
*
* Example: Member currentMember = getResourceFromResponse(response, Member.class);
* @param response the Response object
* @param resourceClass the class of object
* @param <T> the type of the object being returned
* @return an object of the specified type
*/
public static <T> T getResourceFromResponse(final Response response, final Class<T> resourceClass) {
return new Gson().fromJson(response.getBody(), resourceClass);
}
/**
* Maps the response body to a Java object
*
* Example: Member currentMember = getResourceFromRequest(currentRequest, Member.class);
* @param request the Response object
* @param resourceClass the class of object
* @param <T> the type of the object being returned
* @return an object of the specified type
*/
public static <T> T getResourceFromRequest(final Request request, final Class<T> resourceClass) {
return new Gson().fromJson(request.getBody(), resourceClass);
}
/**
* Get the base uri from the "env" property
* @return the base uri
*/
public static String getBaseUri() {
return System.getProperty("env");
}
/**
* Get the RoundCube test instance from the "env" property
* @return the roundcube url
*/
public static String getRoundcubeUrl() {
return System.getProperty("roundcube");
}
/**
* Get the project location
* @return the uri
*/
public static String getProjectLocation() {
return System.getProperty("projectLocation");
}
/**
* Get the project location
* @return the uri
*/
public static String getChromeProfileLocation() {
return System.getProperty("chromeProfileLocation");
}
/**
* Get the size of the screen that should be set when the isMaximized is set to true
* @return the size of the window
*/
public static String getDeviceType(){
return System.getProperty("deviceType");
}
/**
}
/**
* Get the url for "TSA Cache"
* @return the uri
*/
public static String getTSACache() {
/**
* Get the url for "TSA Signer"
* @return the uri
*/
public static String getTSASigner() {
return System.getProperty("baseUrl") + "/signer";
}
/**
* Get the url for "TSA Task"
* @return the uri
*/
public static String getTSATask() {
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/**
* UTF8 encodes a string
*
* @param stringToEncode the string that will be encoded
* @return the encoded string
*/
public static String encodeUTF8(String stringToEncode) {
String encodedString;
try {
encodedString = URLEncoder.encode(stringToEncode, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RAFException(e, JsonUtils.class);
}
return encodedString;
}
/**
* Decode a UTF8 encoded string
*
* @param stringToDecode the string to be decoded
* @return the decoded string
*/
public static String decodeUTF8(String stringToDecode) {
String decodedString;
try {
decodedString = URLDecoder.decode(stringToDecode, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RAFException(e, JsonUtils.class);
}
return decodedString;
}
/**
* Encodes the values from the Map using UTF-8.
*
* @param map The Map that will be converted.
* @return String representation of the map in format:
* key1=value1&key2=value2&key3=value3
*/
public static String mapToUrlEncodedString(Map<?, ?> map) {
StringBuilder sb = new StringBuilder();
String urlencodedPairs; // store all params in one string (appended with &); values are Encoded. keys are NOT for now.
for (Map.Entry currentEntry : map.entrySet()) {
sb.append("&").append(currentEntry.getKey()).append("=").append(JsonUtils.encodeUTF8(currentEntry.getValue().toString()));
}
urlencodedPairs = sb.toString().substring(1); //remove the 1st character '&'
return urlencodedPairs;
}
/**
* Converts an URL encoded (UTF-8) key value pairs String into a Map with keys and values (Decoded).
* @param keyValuePairsAsEncodedString The string that has to be converted to map. E.g.
* key1=value1&key2=value2&email=mail%40example.com&key4=value+4
* @return Map with decoded entries. E.g.:
* <br/> key1 : value1
* <br/> key2 : value2
* <br/> email : mail@example.com
* <br/> key4 : value 4
*
*/
public static Map<String, String> urlEncodedStringToMap(String keyValuePairsAsEncodedString) {
Map<String, String> keyValueMap = new HashMap<>();
List<NameValuePair> listOfNameValuePairs = URLEncodedUtils.parse(keyValuePairsAsEncodedString, Charset.forName("UTF-8"));
for (NameValuePair pair : listOfNameValuePairs) {
keyValueMap.put(pair.getName(), pair.getValue());
}
return keyValueMap;
}
/**
* Takes the Query param part from the URL, decodes it using UTF-8 and converts it into a map which contains the key/value paris.
* @param url String representation of valid URL. E.g. "http://domain.com/error?error=E02&detail=pr_email%3dblank%26pr_dob_yyyy%3dinvalid&key+3=value+3"
* @return Map with decoded entries. E.g.:
* <br/> error : E02
* <br/> detail : pr_email=blank&pr_dob_yyyy=invalid
* <br/> key 3 : value 3
* @throws URISyntaxException
*/
public static Map<String, String> urlQueryParamsToMap(String url) throws URISyntaxException {
Map<String, String> keyValueMap = new HashMap<>();
URI uri = new URI(url);
List<NameValuePair> listOfNameValuePairs = URLEncodedUtils.parse(uri,"UTF-8");
for (NameValuePair pair : listOfNameValuePairs) {
keyValueMap.put(pair.getName(), pair.getValue());
}
return keyValueMap;
}
/**
* Encodes Map keys and values using UTF-8 encoding.
*
* @param mapToEncode the map to encode
* @return The Map with encoded keys and values.
*/
public static Map<String, Object> encodeMap(Map<String, Object> mapToEncode) {
Map<String, Object> objectHashMap = new HashMap<>();
mapToEncode.entrySet().stream().forEach(entry -> objectHashMap.put(
encodeUTF8(entry.getKey()),
encodeUTF8(entry.getValue().toString() ) ));
return objectHashMap;
}
/**
* Formats the map for printing
*
* @param mapTopPrint the map to format
* @return a String containing a line for each key-value
*/
public static String prettyPrintMap(Map<?, ?> mapTopPrint) {
if (mapTopPrint.isEmpty())
return "none";
StringBuilder sb = new StringBuilder();
mapTopPrint.entrySet().forEach(pair -> sb.append(String.format("\n %s:%s", pair.getKey(), pair.getValue())));
return sb.toString();
}
/**
* Formats the json string for printing
*
* @param json the json to be formatted
* @return a String containing the formatted json
*/
public static String prettyPrintJson(String json) {
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().serializeNulls().setLenient().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(json);
return gson.toJson(je);
}
/**
* Get the roundcubeUsername from the build.gradle file
* @return the roundcube username
*/
public static String getRoundcubeUsername() {
return System.getProperty("roundcubeUsername");
}
/**
* Get the roundcubePassword from the build.gradle file
* @return the roundcube password
*/
public static String getRoundcubePassword() {
return System.getProperty("roundcubePassword");
}
/**
* Get the secondRoundcubeUsername from the build.gradle file
* @return the roundcube username
*/
public static String getSecondRoundcubeUsername() {
return System.getProperty("secondRoundcubeUsername");
}
/**
* Get the secondRoundcubePassword from the build.gradle file
* @return the roundcube password
*/
public static String getSecondRoundcubePassword() {
return System.getProperty("secondRoundcubePassword");
}
/**
* Get the gmailPassword from the build.gradle file
* @return the gmail password
*/
public static String getGmailPassword(){
return System.getProperty("gmailPassword");
}
/**
* Get the outlookPassword from the build.gradle file
* @return the outlook password
*/
public static String getOutlookPassword(){
return System.getProperty("outlookPassword");
}
/**
* Get the outlookManifestfrom the build.gradle file
* @return the outlook Manifest
*/
public static String getOutlookManifest(){
return System.getProperty("outlookManifest");
}
public static boolean getIsHeadless(){
if(System.getProperty("isHeadless").equalsIgnoreCase("true")){
return true;
}else{
return false;
}
}
}