02bc568c39af47f452011b38411862235dfda712
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.openecomp.core.utilities.json;
22
23
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;
29
30 import org.openecomp.core.utilities.file.FileUtils;
31 import org.testng.Assert;
32 import org.testng.annotations.Test;
33
34 public class JsonUtilTest {
35
36     @Test
37     public void testValidJsonValidate() {
38
39
40         String json = FileUtils.readViaInputStream("jsonUtil/json/a.json",
41                 stream -> new String(FileUtils.toByteArray(stream)));
42
43         String jsonSchema = FileUtils.readViaInputStream("jsonUtil/json_schema/aSchema.json",
44                 stream -> new String(FileUtils.toByteArray(stream)));
45
46         List<String> validationErrors = JsonUtil.validate(json, jsonSchema);
47         Assert.assertNull(validationErrors);
48     }
49
50     @Test
51     public void testInValidJsonValidate() {
52
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)));
57
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)");
71     }
72
73     @Test(expectedExceptions = IllegalArgumentException.class)
74     public void testInValidJsonValidateNullJson() {
75         JsonUtil.validate(null, null);
76     }
77
78     @Test
79     public void testObject2Json() {
80         List<String> list = Stream.of("Json", "Util", "Test").collect(Collectors.toList());
81
82         String resultStr = JsonUtil.object2Json(list);
83         Assert.assertNotNull(resultStr);
84         Assert.assertTrue(resultStr.contains("Json") && resultStr.contains("Util"));
85     }
86
87     @Test
88     public void testSbObject2Json() {
89         List<String> list = Stream.of("Json", "Util", "Test").collect(Collectors.toList());
90
91         StringBuilder resultStr = JsonUtil.sbObject2Json(list);
92         Assert.assertNotNull(resultStr);
93         Assert.assertTrue(resultStr.toString().contains("Json")
94                 && resultStr.toString().contains("Util"));
95     }
96
97     @Test
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);
103     }
104
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);
110     }
111
112     @Test
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);
118     }
119
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);
125     }
126
127     @Test
128     public void testIsValidJson() {
129         String inputStr = "{\n"
130                 + "\t\"obj\": [\"Json\", \"Util\", \"Test\"]\n"
131                 + "}";
132         Assert.assertTrue(JsonUtil.isValidJson(inputStr));
133     }
134     @Test
135     public void testIsValidJsonFail() {
136         String inputStr = "{[Json, Util, Test]}";
137         Assert.assertFalse(JsonUtil.isValidJson(inputStr));
138     }
139
140 }