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 {
37 String json = FileUtils.readViaInputStream("jsonUtil/json/a.json",
38 stream -> new String(FileUtils.toByteArray(stream)));
40 String jsonSchema = FileUtils.readViaInputStream("jsonUtil/json_schema/aSchema.json",
41 stream -> new String(FileUtils.toByteArray(stream)));
43 List<String> validationErrors = JsonUtil.validate(json, jsonSchema);
44 Assert.assertNull(validationErrors);
48 public void testInValidJsonValidate() throws Exception {
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)));
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");