Integrate aai-schema-ingest library into aai-core
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / introspection / JSONStrategyTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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 package org.onap.aai.introspection;
21
22 import org.json.simple.JSONArray;
23
24 import org.json.simple.JSONObject;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.onap.aai.AAISetup;
29
30 import java.util.HashSet;
31 import java.util.Set;
32
33 public class JSONStrategyTest extends AAISetup{
34     private JSONStrategy jsonStrategy;
35     private JSONStrategy jsonStrategyContainer;
36     private JSONStrategy jsonStrategyComplex;
37
38     @Before
39     public void setup(){
40         try {
41             JSONObject pserver = new JSONObject();
42             pserver.put("hostname", "value1");
43             pserver.put("numberofCpus", 4);
44             jsonStrategy = new JSONStrategy(pserver, "pserver-type");
45
46             // The values of this object are arrays containing JSONObjects
47             JSONArray pservers = new JSONArray();
48             pservers.add(pserver);
49             JSONObject container = new JSONObject();
50             container.put("pservers", pservers);
51             jsonStrategyContainer = new JSONStrategy(container, "pservers-type");
52
53             // The values of this object are JSONObjects
54             JSONObject complex = new JSONObject();
55             complex.put("pserver", pserver);
56             jsonStrategyComplex = new JSONStrategy(complex, "pservers-type");
57         }
58         catch (Exception e){
59             System.out.println("error during setup: " + e.getMessage());
60         }
61     }
62
63     @Test
64     public void getSetTest(){
65         jsonStrategy.setValue("ramInMegabytes", 1024);
66         Assert.assertEquals("value1", jsonStrategy.getValue("hostname"));
67         Assert.assertEquals(4, jsonStrategy.getValue("numberofCpus"));
68         Assert.assertEquals(1024, jsonStrategy.getValue("ramInMegabytes"));
69     }
70
71     @Test
72     public void getPropertiesTest() {
73         Set<String> expected = new HashSet<>();
74         expected.add("hostname");
75         expected.add("numberofCpus");
76         Assert.assertEquals(expected, jsonStrategy.getProperties());
77     }
78
79     @Test
80     public void getGenericTypeTest() {
81         // If the values of this object are arrays, return the type within the array
82         Assert.assertEquals("class org.json.simple.JSONObject" , jsonStrategyContainer.getGenericTypeClass("pservers").toString());
83     }
84
85     @Test
86     public void getJavaClassNameTest() {
87         Assert.assertEquals("org.json.simple.JSONObject", jsonStrategy.getJavaClassName());
88         Assert.assertEquals("org.json.simple.JSONObject", jsonStrategyContainer.getJavaClassName());
89     }
90     @Test
91     public void getTypeTest() {
92         Assert.assertEquals("java.lang.String", jsonStrategy.getType("hostname"));
93         Assert.assertEquals("java.lang.Integer", jsonStrategy.getType("numberofCpus"));
94     }
95
96     @Test
97     public void isContainerTest() {
98         Assert.assertTrue(jsonStrategyContainer.isContainer());
99     }
100
101     @Test
102     public void newInstanceOfPropertyTest() {
103         Assert.assertEquals("class org.json.simple.JSONArray", jsonStrategyContainer.newInstanceOfProperty("pservers").getClass().toString());
104     }
105
106     @Test(expected = NullPointerException.class)
107     public void newInvalidInstanceOfPropertyTest() {
108         Assert.assertEquals(null, jsonStrategyContainer.newInstanceOfProperty("invalid").getClass().toString());
109     }
110     @Test
111     public void newInstanceOfNestedPropertyTest() {
112         Assert.assertEquals("class org.json.simple.JSONObject", jsonStrategyContainer.newInstanceOfNestedProperty("pservers").getClass().toString());
113     }
114
115     @Test(expected = NullPointerException.class)
116     public void newInvalidInstanceOfNestedPropertyTest() {
117         jsonStrategyContainer.newInstanceOfNestedProperty("invalid").getClass().toString();
118     }
119
120     @Test
121     public void isComplexTypeTest() {
122         // Complex: The value of this key contains a JSONObject
123         Assert.assertTrue(jsonStrategyComplex.isComplexType("pserver"));
124         Assert.assertFalse(jsonStrategyContainer.isComplexType("pservers"));
125         Assert.assertFalse(jsonStrategy.isComplexType("hostname"));
126     }
127
128     @Test
129     public void isComplexGenericTypeTest() {
130         // Complex Generic: The value of this key contains an array of JSONObjects
131         Assert.assertTrue(jsonStrategyContainer.isComplexGenericType("pservers"));
132         Assert.assertFalse(jsonStrategyComplex.isComplexGenericType("pserver"));
133         Assert.assertFalse(jsonStrategy.isComplexGenericType("hostname"));
134     }
135 }