[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-utilities-lib / src / test / java / org / openecomp / core / utilities / json / JsonSchemaDataGeneratorTest.java
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 = 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")));
39
40   @Test
41   public void testSchemaWithoutDefaults() {
42     testGenerate(SCHEMA_WITHOUT_DEFAULTS, new JSONObject());
43   }
44
45   @Test
46   public void testSchemaWithRefsAndDefaults() {
47     testGenerate(SCHEMA_WITH_REFS_AND_DEFAULTS,
48         new JSONObject(
49             "{\"cityOfBirth\":\"Tel Aviv\",\"address\":{\"city\":\"Tel Aviv\"},\"phoneNumber\":[{\"code\":1,\"location\":\"Home\"},{\"code\":2,\"location\":\"Office\"}]}"));
50   }
51
52   @Test(expectedExceptions = JSONException.class)
53   public void testSchemaWithInvalidDefault() {
54     testGenerate(SCHEMA_WITH_INVALID_DEFAULT, null);
55   }
56
57   @Test
58   public void testNicQuestionnaireSchema() {
59     testGenerate(SCHEMA_NIC,
60         new JSONObject("{\"ipConfiguration\":{\"ipv4Required\":true,\"ipv6Required\":false}}"));
61   }
62
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));
69   }
70 }