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;
23 import org.json.JSONException;
24 import org.json.JSONObject;
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.openecomp.core.utilities.file.FileUtils;
29 public class JsonSchemaDataGeneratorTest {
31 private static final String SCHEMA_WITHOUT_DEFAULTS =
32 readFromFile("jsonUtil/json_schema/aSchema.json");
34 private static final String SCHEMA_WITH_REFS_AND_DEFAULTS =
35 readFromFile("jsonUtil/json_schema/schemaWithRefsAndDefaults.json");
37 private static final String SCHEMA_WITH_INVALID_DEFAULT =
38 readFromFile("jsonUtil/json_schema/schemaWithInvalidDefault.json");
40 private static final String SCHEMA_NIC =
41 readFromFile("jsonUtil/json_schema/nicSchema.json");
44 public void testSchemaWithoutDefaults() {
45 testGenerate(SCHEMA_WITHOUT_DEFAULTS, new JSONObject());
49 public void testSchemaWithRefsAndDefaults() {
50 testGenerate(SCHEMA_WITH_REFS_AND_DEFAULTS,
51 new JSONObject("{\"cityOfBirth\":\"Tel Aviv\",\"address\":{\"city\":\"Tel Aviv\"},"
52 + "\"houseNumber\":1,\"pincode\":111111,"
53 + "\"phoneNumber\":[{\"code\":1,\"location\":\"Home\"},{\"code\":2,\"location\":\"Office\"}]}"));
56 @Test(expected = JSONException.class)
57 public void testSchemaWithInvalidDefault() {
58 testGenerate(SCHEMA_WITH_INVALID_DEFAULT, null);
62 public void testNicQuestionnaireSchema() {
63 testGenerate(SCHEMA_NIC,
64 new JSONObject("{\"ipConfiguration\":{\"ipv4Required\":true,\"ipv6Required\":false}}"));
67 @Test(expected = IllegalArgumentException.class)
68 public void testConstructorException() {
69 Assert.assertNull(new JsonSchemaDataGenerator(null));
72 private void testGenerate(String schema, JSONObject expectedData) {
73 JsonSchemaDataGenerator jsonSchemaDataGenerator = new JsonSchemaDataGenerator(schema);
74 jsonSchemaDataGenerator.setIncludeDefaults(true);
75 String data = jsonSchemaDataGenerator.generateData();
76 JSONObject dataJson = new JSONObject(data);
77 Assert.assertTrue(expectedData.similar(dataJson));
80 private static String readFromFile(String filename) {
81 return FileUtils.readViaInputStream(filename, stream -> new String(FileUtils.toByteArray(stream)));