Spring-boot 3.1 update
[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 java.util.HashSet;
24 import java.util.Set;
25
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.onap.aai.AAISetup;
31
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import com.fasterxml.jackson.databind.node.ArrayNode;
34 import com.fasterxml.jackson.databind.node.ObjectNode;
35
36 @Ignore("Not a used/flushed out feature")
37 // This has been converted from org.json to Jackson,
38 // but not in a way that tests are working
39 public class JSONStrategyTest extends AAISetup {
40     private JSONStrategy jsonStrategy;
41     private JSONStrategy jsonStrategyContainer;
42     private JSONStrategy jsonStrategyComplex;
43
44     @Before
45     public void setup() {
46         try {
47
48             ObjectMapper mapper = new ObjectMapper();
49             ObjectNode pserver = mapper.createObjectNode();
50
51             pserver.put("hostname", "value1");
52             pserver.put("numberofCpus", 4);
53             jsonStrategy = new JSONStrategy(pserver, "pserver-type");
54
55             // The values of this object are arrays containing JSONObjects
56             ArrayNode pservers = mapper.createArrayNode();
57             pservers.add(pserver);
58
59             ObjectNode container = mapper.createObjectNode();
60             container.set("pservers", pservers);
61             jsonStrategyContainer = new JSONStrategy(container, "pservers-type");
62
63             // The values of this object are JSONObjects
64             ObjectNode complex = mapper.createObjectNode();
65             complex.set("pserver", pserver);
66             jsonStrategyComplex = new JSONStrategy(complex, "pservers-type");
67         } catch (Exception e) {
68             System.out.println("error during setup: " + e.getMessage());
69         }
70     }
71
72     @Test
73     public void getSetTest() {
74         jsonStrategy.setValue("ramInMegabytes", 1024);
75         Assert.assertEquals("value1", jsonStrategy.getValue("hostname"));
76         Assert.assertEquals(4, jsonStrategy.getValue("numberofCpus"));
77         Assert.assertEquals(1024, jsonStrategy.getValue("ramInMegabytes"));
78
79     }
80
81     @Test
82     public void testGetMethods() {
83         Assert.assertEquals("pserver-type", jsonStrategy.getName());
84         Assert.assertEquals("pserver-type", jsonStrategy.getDbName());
85         Assert.assertEquals("", jsonStrategy.getGenericURI());
86         Assert.assertNull(jsonStrategy.getChildName());
87         Assert.assertEquals("key", jsonStrategy.preProcessKey("key"));
88     }
89
90     @Test
91     public void getPropertiesTest() {
92         Set<String> expected = new HashSet<>();
93         expected.add("hostname");
94         expected.add("numberofCpus");
95         Assert.assertEquals(expected, jsonStrategy.getProperties());
96     }
97
98     @Test
99     public void getGenericTypeTest() {
100         // If the values of this object are arrays, return the type within the array
101         Assert.assertEquals("class org.json.simple.JSONObject",
102                 jsonStrategyContainer.getGenericTypeClass("pservers").toString());
103     }
104
105     @Test
106     public void getJavaClassNameTest() {
107         Assert.assertEquals("com.fasterxml.jackson.databind.node.ObjectNode", jsonStrategy.getJavaClassName());
108         Assert.assertEquals("com.fasterxml.jackson.databind.node.ObjectNode", jsonStrategyContainer.getJavaClassName());
109     }
110
111     @Test
112     public void getTypeTest() {
113         Assert.assertEquals("java.lang.String", jsonStrategy.getType("hostname"));
114         Assert.assertEquals("java.lang.Integer", jsonStrategy.getType("numberofCpus"));
115     }
116
117     @Test
118     public void isContainerTest() {
119         Assert.assertTrue(jsonStrategyContainer.isContainer());
120     }
121
122     @Test
123     public void newInstanceOfPropertyTest() {
124         Assert.assertEquals("class org.json.simple.JSONArray",
125                 jsonStrategyContainer.newInstanceOfProperty("pservers").getClass().toString());
126     }
127
128     @Test(expected = NullPointerException.class)
129     public void newInvalidInstanceOfPropertyTest() {
130         Assert.assertEquals(null, jsonStrategyContainer.newInstanceOfProperty("invalid").getClass().toString());
131     }
132
133     @Test
134     public void newInstanceOfNestedPropertyTest() {
135         Assert.assertEquals("class org.json.simple.JSONObject",
136                 jsonStrategyContainer.newInstanceOfNestedProperty("pservers").getClass().toString());
137     }
138
139     @Test(expected = NullPointerException.class)
140     public void newInvalidInstanceOfNestedPropertyTest() {
141         jsonStrategyContainer.newInstanceOfNestedProperty("invalid").getClass().toString();
142     }
143
144     @Test
145     public void isComplexTypeTest() {
146         // Complex: The value of this key contains a JSONObject
147         Assert.assertTrue(jsonStrategyComplex.isComplexType("pserver"));
148         Assert.assertFalse(jsonStrategyContainer.isComplexType("pservers"));
149         Assert.assertFalse(jsonStrategy.isComplexType("hostname"));
150     }
151
152     @Test
153     public void isComplexGenericTypeTest() {
154         // Complex Generic: The value of this key contains an array of JSONObjects
155         Assert.assertTrue(jsonStrategyContainer.isComplexGenericType("pservers"));
156         Assert.assertFalse(jsonStrategyComplex.isComplexGenericType("pserver"));
157         Assert.assertFalse(jsonStrategy.isComplexGenericType("hostname"));
158     }
159 }