2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.core.utilities.json;
24 import java.io.ByteArrayInputStream;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.stream.Collectors;
28 import java.util.stream.Stream;
30 import org.openecomp.core.utilities.file.FileUtils;
31 import org.testng.Assert;
32 import org.testng.annotations.Test;
34 public class JsonUtilTest {
37 public void testValidJsonValidate() {
40 String json = FileUtils.readViaInputStream("jsonUtil/json/a.json",
41 stream -> new String(FileUtils.toByteArray(stream)));
43 String jsonSchema = FileUtils.readViaInputStream("jsonUtil/json_schema/aSchema.json",
44 stream -> new String(FileUtils.toByteArray(stream)));
46 List<String> validationErrors = JsonUtil.validate(json, jsonSchema);
47 Assert.assertNull(validationErrors);
51 public void testInValidJsonValidate() {
53 String json = FileUtils.readViaInputStream("jsonUtil/json/a_invalid.json",
54 stream -> new String(FileUtils.toByteArray(stream)));
55 String jsonSchema = FileUtils.readViaInputStream("jsonUtil/json_schema/aSchema.json",
56 stream -> new String(FileUtils.toByteArray(stream)));
58 List<String> validationErrors = JsonUtil.validate(json, jsonSchema);
59 Assert.assertNotNull(validationErrors);
60 Assert.assertEquals(validationErrors.size(), 5);
61 Assert.assertEquals(validationErrors.get(0),
62 "#/cityOfBirth: Paris is not a valid value. Possible values: New York,Tel Aviv,London");
63 Assert.assertEquals(validationErrors.get(1),
64 "#/address: {\"streetAddress\":\"21 2nd Street\",\"city\":\"Paris\"} is not a valid value. {\"streetAddress\":\"21 2nd Street\",\"city\":\"New York\"} is the only possible value for this field");
65 Assert.assertEquals(validationErrors.get(2),
66 "#/phoneNumber/0/code: expected type: Number, found: String");
67 Assert.assertEquals(validationErrors.get(3),
68 "#/gender: expected type: String, found: Integer");
69 Assert.assertEquals(validationErrors.get(4), "#/dateOfBirth: [20101988] is not valid value. "
70 + "It does not match pattern (0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)");
73 @Test(expectedExceptions = IllegalArgumentException.class)
74 public void testInValidJsonValidateNullJson() {
75 JsonUtil.validate(null, null);
79 public void testObject2Json() {
80 List<String> list = Stream.of("Json", "Util", "Test").collect(Collectors.toList());
82 String resultStr = JsonUtil.object2Json(list);
83 Assert.assertNotNull(resultStr);
84 Assert.assertTrue(resultStr.contains("Json") && resultStr.contains("Util"));
88 public void testSbObject2Json() {
89 List<String> list = Stream.of("Json", "Util", "Test").collect(Collectors.toList());
91 StringBuilder resultStr = JsonUtil.sbObject2Json(list);
92 Assert.assertNotNull(resultStr);
93 Assert.assertTrue(resultStr.toString().contains("Json")
94 && resultStr.toString().contains("Util"));
98 public void testJson2Object() {
99 String inputStr = "[Json, Util, Test]";
100 List list = JsonUtil.json2Object(inputStr, ArrayList.class);
101 Assert.assertNotNull(list);
102 Assert.assertEquals(list.size(), 3);
105 @Test(expectedExceptions = RuntimeException.class)
106 public void testJson2ObjectIncorrectJson() {
107 String inputStr = "{[Json, Util, Test]}";
108 List list = JsonUtil.json2Object(inputStr, ArrayList.class);
109 Assert.assertNull(list);
113 public void testJson2ObjectInputStream() {
114 String inputStr = "[Json, Util, Test]";
115 List list = JsonUtil.json2Object(new ByteArrayInputStream(inputStr.getBytes()), ArrayList.class);
116 Assert.assertNotNull(list);
117 Assert.assertEquals(list.size(), 3);
120 @Test(expectedExceptions = RuntimeException.class)
121 public void testJson2ObjectIncorrectJsonInputStream() {
122 String inputStr = "{[Json, Util, Test]}";
123 List list = JsonUtil.json2Object(new ByteArrayInputStream(inputStr.getBytes()), ArrayList.class);
124 Assert.assertNull(list);
128 public void testIsValidJson() {
129 String inputStr = "{\n"
130 + "\t\"obj\": [\"Json\", \"Util\", \"Test\"]\n"
132 Assert.assertTrue(JsonUtil.isValidJson(inputStr));
135 public void testIsValidJsonFail() {
136 String inputStr = "{[Json, Util, Test]}";
137 Assert.assertFalse(JsonUtil.isValidJson(inputStr));