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.openecomp.core.utilities.file.FileUtils;
26 import org.testng.Assert;
27 import org.testng.annotations.Test;
29 public class JsonSchemaDataGeneratorTest {
31 public static final String SCHEMA_WITHOUT_DEFAULTS = new String(
32 FileUtils.toByteArray(FileUtils.getFileInputStream("jsonUtil/json_schema/aSchema.json")));
33 public static final String SCHEMA_WITH_REFS_AND_DEFAULTS = new String(FileUtils.toByteArray(
34 FileUtils.getFileInputStream("jsonUtil/json_schema/schemaWithRefsAndDefaults.json")));
35 public static final String SCHEMA_WITH_INVALID_DEFAULT = new String(FileUtils.toByteArray(
36 FileUtils.getFileInputStream("jsonUtil/json_schema/schemaWithInvalidDefault.json")));
37 public static final String SCHEMA_NIC = new String(
38 FileUtils.toByteArray(FileUtils.getFileInputStream("jsonUtil/json_schema/nicSchema.json")));
41 public void testSchemaWithoutDefaults() {
42 testGenerate(SCHEMA_WITHOUT_DEFAULTS, new JSONObject());
46 public void testSchemaWithRefsAndDefaults() {
47 testGenerate(SCHEMA_WITH_REFS_AND_DEFAULTS,
49 "{\"cityOfBirth\":\"Tel Aviv\",\"address\":{\"city\":\"Tel Aviv\"},\"phoneNumber\":[{\"code\":1,\"location\":\"Home\"},{\"code\":2,\"location\":\"Office\"}]}"));
52 @Test(expectedExceptions = JSONException.class)
53 public void testSchemaWithInvalidDefault() {
54 testGenerate(SCHEMA_WITH_INVALID_DEFAULT, null);
58 public void testNicQuestionnaireSchema() {
59 testGenerate(SCHEMA_NIC,
60 new JSONObject("{\"ipConfiguration\":{\"ipv4Required\":true,\"ipv6Required\":false}}"));
63 private void testGenerate(String schema, JSONObject expectedData) {
64 JsonSchemaDataGenerator jsonSchemaDataGenerator = new JsonSchemaDataGenerator(schema);
65 String data = jsonSchemaDataGenerator.generateData();
66 System.out.println(data);
67 JSONObject dataJson = new JSONObject(data);
68 Assert.assertTrue(expectedData.similar(dataJson));