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