d89019baec40c04161f9fdab3620d41464e998f4
[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 org.openecomp.core.utilities.file.FileUtils;
25 import org.testng.Assert;
26 import org.testng.annotations.Test;
27
28 import java.util.List;
29
30 public class JsonUtilTest {
31
32   @Test
33   public void testValidJsonValidate() throws Exception {
34
35
36
37     String json = FileUtils.readViaInputStream("jsonUtil/json/a.json",
38             stream -> new String(FileUtils.toByteArray(stream)));
39
40     String jsonSchema = FileUtils.readViaInputStream("jsonUtil/json_schema/aSchema.json",
41             stream -> new String(FileUtils.toByteArray(stream)));
42
43     List<String> validationErrors = JsonUtil.validate(json, jsonSchema);
44     Assert.assertNull(validationErrors);
45   }
46
47   @Test
48   public void testInValidJsonValidate() throws Exception {
49
50     String json = FileUtils.readViaInputStream("jsonUtil/json/a_invalid.json",
51             stream -> new String(FileUtils.toByteArray(stream)));
52     String jsonSchema = FileUtils.readViaInputStream("jsonUtil/json_schema/aSchema.json",
53             stream -> new String(FileUtils.toByteArray(stream)));
54
55     List<String> validationErrors = JsonUtil.validate(json, jsonSchema);
56     Assert.assertNotNull(validationErrors);
57     Assert.assertEquals(validationErrors.size(), 3);
58     Assert.assertEquals(validationErrors.get(0),
59         "#/cityOfBirth: Paris is not a valid value. Possible values: New York,Tel Aviv,London");
60     Assert.assertEquals(validationErrors.get(1),
61         "#/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");
62     Assert.assertEquals(validationErrors.get(2),
63         "#/phoneNumber/0/code: expected type: Number, found: String");
64   }
65 }