Sorted out unit-test libraries in onboarding
[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.junit.Assert;
26 import org.junit.Test;
27 import org.openecomp.core.utilities.file.FileUtils;
28
29 public class JsonSchemaDataGeneratorTest {
30
31     private static final String SCHEMA_WITHOUT_DEFAULTS =
32             readFromFile("jsonUtil/json_schema/aSchema.json");
33
34     private static final String SCHEMA_WITH_REFS_AND_DEFAULTS =
35             readFromFile("jsonUtil/json_schema/schemaWithRefsAndDefaults.json");
36
37     private static final String SCHEMA_WITH_INVALID_DEFAULT =
38             readFromFile("jsonUtil/json_schema/schemaWithInvalidDefault.json");
39
40     private 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("{\"cityOfBirth\":\"Tel Aviv\",\"address\":{\"city\":\"Tel Aviv\"},"
52                         + "\"houseNumber\":1,\"pincode\":111111,"
53                         + "\"phoneNumber\":[{\"code\":1,\"location\":\"Home\"},{\"code\":2,\"location\":\"Office\"}]}"));
54     }
55
56     @Test(expected = JSONException.class)
57     public void testSchemaWithInvalidDefault() {
58         testGenerate(SCHEMA_WITH_INVALID_DEFAULT, null);
59     }
60
61     @Test
62     public void testNicQuestionnaireSchema() {
63         testGenerate(SCHEMA_NIC,
64                 new JSONObject("{\"ipConfiguration\":{\"ipv4Required\":true,\"ipv6Required\":false}}"));
65     }
66
67     @Test(expected = IllegalArgumentException.class)
68     public void testConstructorException() {
69         Assert.assertNull(new JsonSchemaDataGenerator(null));
70     }
71
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));
78     }
79
80     private static String readFromFile(String filename) {
81         return FileUtils.readViaInputStream(filename, stream -> new String(FileUtils.toByteArray(stream)));
82     }
83 }