[SDC-29] Amdocs OnBoard 1707 initial commit.
[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 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     String json =
35         new String(FileUtils.toByteArray(FileUtils.getFileInputStream("jsonUtil/json/a.json")));
36     String jsonSchema = new String(
37         FileUtils.toByteArray(FileUtils.getFileInputStream("jsonUtil/json_schema/aSchema.json")));
38
39     List<String> validationErrors = JsonUtil.validate(json, jsonSchema);
40     Assert.assertNull(validationErrors);
41   }
42
43   @Test
44   public void testInValidJsonValidate() throws Exception {
45     String json = new String(
46         FileUtils.toByteArray(FileUtils.getFileInputStream("jsonUtil/json/a_invalid.json")));
47     String jsonSchema = new String(
48         FileUtils.toByteArray(FileUtils.getFileInputStream("jsonUtil/json_schema/aSchema.json")));
49
50     List<String> validationErrors = JsonUtil.validate(json, jsonSchema);
51     Assert.assertNotNull(validationErrors);
52     Assert.assertEquals(validationErrors.size(), 3);
53     Assert.assertEquals(validationErrors.get(0),
54         "#/cityOfBirth: Paris is not a valid value. Possible values: New York,Tel Aviv,London");
55     Assert.assertEquals(validationErrors.get(1),
56         "#/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");
57     Assert.assertEquals(validationErrors.get(2),
58         "#/phoneNumber/0/code: expected type: Number, found: String");
59   }
60 }