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 org.openecomp.core.utilities.file.FileUtils;
25 import org.testng.Assert;
26 import org.testng.annotations.Test;
28 import java.util.List;
30 public class JsonUtilTest {
33 public void testValidJsonValidate() throws Exception {
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")));
39 List<String> validationErrors = JsonUtil.validate(json, jsonSchema);
40 Assert.assertNull(validationErrors);
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")));
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");