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