Sorted out unit-test libraries in onboarding
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-utilities-lib / src / test / java / org / openecomp / core / utilities / json / JsonUtilTest.java
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 import org.junit.Assert;
30 import org.junit.Test;
31 import org.openecomp.core.utilities.file.FileUtils;
32
33 public class JsonUtilTest {
34
35     @Test
36     public void testValidJsonValidate() {
37
38
39         String json = FileUtils.readViaInputStream("jsonUtil/json/a.json",
40                 stream -> new String(FileUtils.toByteArray(stream)));
41
42         String jsonSchema = FileUtils.readViaInputStream("jsonUtil/json_schema/aSchema.json",
43                 stream -> new String(FileUtils.toByteArray(stream)));
44
45         List<String> validationErrors = JsonUtil.validate(json, jsonSchema);
46         Assert.assertNull(validationErrors);
47     }
48
49     @Test
50     public void testInValidJsonValidate() {
51
52         String json = FileUtils.readViaInputStream("jsonUtil/json/a_invalid.json",
53                 stream -> new String(FileUtils.toByteArray(stream)));
54         String jsonSchema = FileUtils.readViaInputStream("jsonUtil/json_schema/aSchema.json",
55                 stream -> new String(FileUtils.toByteArray(stream)));
56
57         List<String> validationErrors = JsonUtil.validate(json, jsonSchema);
58         Assert.assertNotNull(validationErrors);
59         Assert.assertEquals(validationErrors.size(), 5);
60         Assert.assertEquals(validationErrors.get(0),
61                 "#/cityOfBirth: Paris is not a valid value. Possible values: New York,Tel Aviv,London");
62         Assert.assertEquals(validationErrors.get(1),
63                 "#/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");
64         Assert.assertEquals(validationErrors.get(2),
65                 "#/phoneNumber/0/code: expected type: Number, found: String");
66         Assert.assertEquals(validationErrors.get(3),
67                 "#/gender: expected type: String, found: Integer");
68         Assert.assertEquals(validationErrors.get(4), "#/dateOfBirth: [20101988] is not valid value. "
69                 + "It does not match pattern (0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)");
70     }
71
72     @Test(expected = IllegalArgumentException.class)
73     public void testInValidJsonValidateNullJson() {
74         JsonUtil.validate(null, null);
75     }
76
77     @Test
78     public void testObject2Json() {
79         List<String> list = Stream.of("Json", "Util", "Test").collect(Collectors.toList());
80
81         String resultStr = JsonUtil.object2Json(list);
82         Assert.assertNotNull(resultStr);
83         Assert.assertTrue(resultStr.contains("Json") && resultStr.contains("Util"));
84     }
85
86     @Test
87     public void testSbObject2Json() {
88         List<String> list = Stream.of("Json", "Util", "Test").collect(Collectors.toList());
89
90         StringBuilder resultStr = JsonUtil.sbObject2Json(list);
91         Assert.assertNotNull(resultStr);
92         Assert.assertTrue(resultStr.toString().contains("Json")
93                 && resultStr.toString().contains("Util"));
94     }
95
96     @Test
97     public void testJson2Object() {
98         String inputStr = "[Json, Util, Test]";
99         List list = JsonUtil.json2Object(inputStr, ArrayList.class);
100         Assert.assertNotNull(list);
101         Assert.assertEquals(list.size(), 3);
102     }
103
104     @Test(expected = RuntimeException.class)
105     public void testJson2ObjectIncorrectJson() {
106         String inputStr = "{[Json, Util, Test]}";
107         List list = JsonUtil.json2Object(inputStr, ArrayList.class);
108         Assert.assertNull(list);
109     }
110
111     @Test
112     public void testJson2ObjectInputStream() {
113         String inputStr = "[Json, Util, Test]";
114         List list = JsonUtil.json2Object(new ByteArrayInputStream(inputStr.getBytes()), ArrayList.class);
115         Assert.assertNotNull(list);
116         Assert.assertEquals(list.size(), 3);
117     }
118
119     @Test(expected = RuntimeException.class)
120     public void testJson2ObjectIncorrectJsonInputStream() {
121         String inputStr = "{[Json, Util, Test]}";
122         List list = JsonUtil.json2Object(new ByteArrayInputStream(inputStr.getBytes()), ArrayList.class);
123         Assert.assertNull(list);
124     }
125
126     @Test
127     public void testIsValidJson() {
128         String inputStr = "{\n"
129                 + "\t\"obj\": [\"Json\", \"Util\", \"Test\"]\n"
130                 + "}";
131         Assert.assertTrue(JsonUtil.isValidJson(inputStr));
132     }
133     @Test
134     public void testIsValidJsonFail() {
135         String inputStr = "{[Json, Util, Test]}";
136         Assert.assertFalse(JsonUtil.isValidJson(inputStr));
137     }
138
139 }