9b21f632cce2eb0065b4bd9d74d592cd5ba16b52
[sdc.git] /
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 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;
28
29 public class JsonSchemaDataGeneratorTest {
30
31   public static final String SCHEMA_WITHOUT_DEFAULTS =
32           readFromFile("jsonUtil/json_schema/aSchema.json");
33
34   public static final String SCHEMA_WITH_REFS_AND_DEFAULTS =
35           readFromFile("jsonUtil/json_schema/schemaWithRefsAndDefaults.json");
36
37   public static final String SCHEMA_WITH_INVALID_DEFAULT =
38           readFromFile("jsonUtil/json_schema/schemaWithInvalidDefault.json");
39
40   public static final String SCHEMA_NIC =
41           readFromFile("jsonUtil/json_schema/nicSchema.json");
42
43   @Test
44   public void testSchemaWithoutDefaults() {
45     testGenerate(SCHEMA_WITHOUT_DEFAULTS, new JSONObject());
46   }
47
48   @Test
49   public void testSchemaWithRefsAndDefaults() {
50     testGenerate(SCHEMA_WITH_REFS_AND_DEFAULTS,
51         new JSONObject(
52             "{\"cityOfBirth\":\"Tel Aviv\",\"address\":{\"city\":\"Tel Aviv\"},\"phoneNumber\":[{\"code\":1,\"location\":\"Home\"},{\"code\":2,\"location\":\"Office\"}]}"));
53   }
54
55   @Test(expectedExceptions = JSONException.class)
56   public void testSchemaWithInvalidDefault() {
57     testGenerate(SCHEMA_WITH_INVALID_DEFAULT, null);
58   }
59
60   @Test
61   public void testNicQuestionnaireSchema() {
62     testGenerate(SCHEMA_NIC,
63         new JSONObject("{\"ipConfiguration\":{\"ipv4Required\":true,\"ipv6Required\":false}}"));
64   }
65
66   private void testGenerate(String schema, JSONObject expectedData) {
67     JsonSchemaDataGenerator jsonSchemaDataGenerator = new JsonSchemaDataGenerator(schema);
68     String data = jsonSchemaDataGenerator.generateData();
69     System.out.println(data);
70     JSONObject dataJson = new JSONObject(data);
71     Assert.assertTrue(expectedData.similar(dataJson));
72   }
73
74   private static String readFromFile(String filename) {
75     return FileUtils.readViaInputStream(filename, stream -> new String(FileUtils.toByteArray(stream)));
76   }
77 }