2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.introspection;
23 import java.util.HashSet;
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;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import com.fasterxml.jackson.databind.node.ArrayNode;
34 import com.fasterxml.jackson.databind.node.ObjectNode;
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;
48 ObjectMapper mapper = new ObjectMapper();
49 ObjectNode pserver = mapper.createObjectNode();
51 pserver.put("hostname", "value1");
52 pserver.put("numberofCpus", 4);
53 jsonStrategy = new JSONStrategy(pserver, "pserver-type");
55 // The values of this object are arrays containing JSONObjects
56 ArrayNode pservers = mapper.createArrayNode();
57 pservers.add(pserver);
59 ObjectNode container = mapper.createObjectNode();
60 container.set("pservers", pservers);
61 jsonStrategyContainer = new JSONStrategy(container, "pservers-type");
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());
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"));
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"));
91 public void getPropertiesTest() {
92 Set<String> expected = new HashSet<>();
93 expected.add("hostname");
94 expected.add("numberofCpus");
95 Assert.assertEquals(expected, jsonStrategy.getProperties());
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());
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());
112 public void getTypeTest() {
113 Assert.assertEquals("java.lang.String", jsonStrategy.getType("hostname"));
114 Assert.assertEquals("java.lang.Integer", jsonStrategy.getType("numberofCpus"));
118 public void isContainerTest() {
119 Assert.assertTrue(jsonStrategyContainer.isContainer());
123 public void newInstanceOfPropertyTest() {
124 Assert.assertEquals("class org.json.simple.JSONArray",
125 jsonStrategyContainer.newInstanceOfProperty("pservers").getClass().toString());
128 @Test(expected = NullPointerException.class)
129 public void newInvalidInstanceOfPropertyTest() {
130 Assert.assertEquals(null, jsonStrategyContainer.newInstanceOfProperty("invalid").getClass().toString());
134 public void newInstanceOfNestedPropertyTest() {
135 Assert.assertEquals("class org.json.simple.JSONObject",
136 jsonStrategyContainer.newInstanceOfNestedProperty("pservers").getClass().toString());
139 @Test(expected = NullPointerException.class)
140 public void newInvalidInstanceOfNestedPropertyTest() {
141 jsonStrategyContainer.newInstanceOfNestedProperty("invalid").getClass().toString();
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"));
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"));