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.json.simple.JSONArray;
27 import org.json.simple.JSONObject;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.aai.AAISetup;
33 public class JSONStrategyTest extends AAISetup {
34 private JSONStrategy jsonStrategy;
35 private JSONStrategy jsonStrategyContainer;
36 private JSONStrategy jsonStrategyComplex;
41 JSONObject pserver = new JSONObject();
42 pserver.put("hostname", "value1");
43 pserver.put("numberofCpus", 4);
44 jsonStrategy = new JSONStrategy(pserver, "pserver-type");
46 // The values of this object are arrays containing JSONObjects
47 JSONArray pservers = new JSONArray();
48 pservers.add(pserver);
49 JSONObject container = new JSONObject();
50 container.put("pservers", pservers);
51 jsonStrategyContainer = new JSONStrategy(container, "pservers-type");
53 // The values of this object are JSONObjects
54 JSONObject complex = new JSONObject();
55 complex.put("pserver", pserver);
56 jsonStrategyComplex = new JSONStrategy(complex, "pservers-type");
57 } catch (Exception e) {
58 System.out.println("error during setup: " + e.getMessage());
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"));
71 public void getPropertiesTest() {
72 Set<String> expected = new HashSet<>();
73 expected.add("hostname");
74 expected.add("numberofCpus");
75 Assert.assertEquals(expected, jsonStrategy.getProperties());
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",
82 jsonStrategyContainer.getGenericTypeClass("pservers").toString());
86 public void getJavaClassNameTest() {
87 Assert.assertEquals("org.json.simple.JSONObject", jsonStrategy.getJavaClassName());
88 Assert.assertEquals("org.json.simple.JSONObject", jsonStrategyContainer.getJavaClassName());
92 public void getTypeTest() {
93 Assert.assertEquals("java.lang.String", jsonStrategy.getType("hostname"));
94 Assert.assertEquals("java.lang.Integer", jsonStrategy.getType("numberofCpus"));
98 public void isContainerTest() {
99 Assert.assertTrue(jsonStrategyContainer.isContainer());
103 public void newInstanceOfPropertyTest() {
104 Assert.assertEquals("class org.json.simple.JSONArray",
105 jsonStrategyContainer.newInstanceOfProperty("pservers").getClass().toString());
108 @Test(expected = NullPointerException.class)
109 public void newInvalidInstanceOfPropertyTest() {
110 Assert.assertEquals(null, jsonStrategyContainer.newInstanceOfProperty("invalid").getClass().toString());
114 public void newInstanceOfNestedPropertyTest() {
115 Assert.assertEquals("class org.json.simple.JSONObject",
116 jsonStrategyContainer.newInstanceOfNestedProperty("pservers").getClass().toString());
119 @Test(expected = NullPointerException.class)
120 public void newInvalidInstanceOfNestedPropertyTest() {
121 jsonStrategyContainer.newInstanceOfNestedProperty("invalid").getClass().toString();
125 public void isComplexTypeTest() {
126 // Complex: The value of this key contains a JSONObject
127 Assert.assertTrue(jsonStrategyComplex.isComplexType("pserver"));
128 Assert.assertFalse(jsonStrategyContainer.isComplexType("pservers"));
129 Assert.assertFalse(jsonStrategy.isComplexType("hostname"));
133 public void isComplexGenericTypeTest() {
134 // Complex Generic: The value of this key contains an array of JSONObjects
135 Assert.assertTrue(jsonStrategyContainer.isComplexGenericType("pservers"));
136 Assert.assertFalse(jsonStrategyComplex.isComplexGenericType("pserver"));
137 Assert.assertFalse(jsonStrategy.isComplexGenericType("hostname"));