7b15e8caf89a4efcd453b1931fb7307e32702c33
[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
21 package org.onap.aai.introspection;
22
23 import org.json.simple.JSONArray;
24 import org.json.simple.JSONObject;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Ignore;
28 import org.junit.Test;
29 import org.onap.aai.AAISetup;
30
31 import java.util.HashSet;
32 import java.util.Set;
33
34 @Ignore("Not a used/flushed out feature")
35 public class JSONStrategyTest extends AAISetup {
36     private JSONStrategy jsonStrategy;
37     private JSONStrategy jsonStrategyContainer;
38     private JSONStrategy jsonStrategyComplex;
39
40     @Before
41     public void setup() {
42         try {
43             JSONObject pserver = new JSONObject();
44             pserver.put("hostname", "value1");
45             pserver.put("numberofCpus", 4);
46             jsonStrategy = new JSONStrategy(pserver, "pserver-type");
47
48             // The values of this object are arrays containing JSONObjects
49             JSONArray pservers = new JSONArray();
50             pservers.add(pserver);
51             JSONObject container = new JSONObject();
52             container.put("pservers", pservers);
53             jsonStrategyContainer = new JSONStrategy(container, "pservers-type");
54
55             // The values of this object are JSONObjects
56             JSONObject complex = new JSONObject();
57             complex.put("pserver", pserver);
58             jsonStrategyComplex = new JSONStrategy(complex, "pservers-type");
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 testGetMethods(){
74         Assert.assertEquals("pserver-type",jsonStrategy.getName());
75         Assert.assertEquals("pserver-type",jsonStrategy.getDbName());
76         Assert.assertEquals("",jsonStrategy.getGenericURI());
77         Assert.assertNull(jsonStrategy.getChildName());
78         Assert.assertEquals("key",jsonStrategy.preProcessKey("key"));
79     }
80     @Test
81     public void getPropertiesTest() {
82         Set<String> expected = new HashSet<>();
83         expected.add("hostname");
84         expected.add("numberofCpus");
85         Assert.assertEquals(expected, jsonStrategy.getProperties());
86     }
87
88     @Test
89     public void getGenericTypeTest() {
90         // If the values of this object are arrays, return the type within the array
91         Assert.assertEquals("class org.json.simple.JSONObject",
92                 jsonStrategyContainer.getGenericTypeClass("pservers").toString());
93     }
94
95     @Test
96     public void getJavaClassNameTest() {
97         Assert.assertEquals("org.json.simple.JSONObject", jsonStrategy.getJavaClassName());
98         Assert.assertEquals("org.json.simple.JSONObject", jsonStrategyContainer.getJavaClassName());
99     }
100
101     @Test
102     public void getTypeTest() {
103         Assert.assertEquals("java.lang.String", jsonStrategy.getType("hostname"));
104         Assert.assertEquals("java.lang.Integer", jsonStrategy.getType("numberofCpus"));
105     }
106
107     @Test
108     public void isContainerTest() {
109         Assert.assertTrue(jsonStrategyContainer.isContainer());
110     }
111
112     @Test
113     public void newInstanceOfPropertyTest() {
114         Assert.assertEquals("class org.json.simple.JSONArray",
115                 jsonStrategyContainer.newInstanceOfProperty("pservers").getClass().toString());
116     }
117
118     @Test(expected = NullPointerException.class)
119     public void newInvalidInstanceOfPropertyTest() {
120         Assert.assertEquals(null, jsonStrategyContainer.newInstanceOfProperty("invalid").getClass().toString());
121     }
122
123     @Test
124     public void newInstanceOfNestedPropertyTest() {
125         Assert.assertEquals("class org.json.simple.JSONObject",
126                 jsonStrategyContainer.newInstanceOfNestedProperty("pservers").getClass().toString());
127     }
128
129     @Test(expected = NullPointerException.class)
130     public void newInvalidInstanceOfNestedPropertyTest() {
131         jsonStrategyContainer.newInstanceOfNestedProperty("invalid").getClass().toString();
132     }
133
134     @Test
135     public void isComplexTypeTest() {
136         // Complex: The value of this key contains a JSONObject
137         Assert.assertTrue(jsonStrategyComplex.isComplexType("pserver"));
138         Assert.assertFalse(jsonStrategyContainer.isComplexType("pservers"));
139         Assert.assertFalse(jsonStrategy.isComplexType("hostname"));
140     }
141
142     @Test
143     public void isComplexGenericTypeTest() {
144         // Complex Generic: The value of this key contains an array of JSONObjects
145         Assert.assertTrue(jsonStrategyContainer.isComplexGenericType("pservers"));
146         Assert.assertFalse(jsonStrategyComplex.isComplexGenericType("pserver"));
147         Assert.assertFalse(jsonStrategy.isComplexGenericType("hostname"));
148     }
149 }