2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.common.test;
24 import java.util.Comparator;
25 import java.util.Iterator;
26 import org.json.JSONArray;
27 import org.json.JSONException;
28 import org.json.JSONObject;
30 public class JSONAssert {
33 * nonstrict comparison means that json array items can be in different orders
35 private static Comparator<JSONObject> nonStrictComarator = new Comparator<JSONObject>() {
38 public int compare(JSONObject o1, JSONObject o2) {
42 Iterator<?> keys = o1.keys();
43 while (keys.hasNext()) {
44 String key = String.valueOf(keys.next());
45 int x = this.test(o1.get(key), o2.get(key));
53 private int test(Object o1, Object o2) {
55 if ((o1 instanceof Double) && (o2 instanceof Double)) {
57 return (((Double) o1).doubleValue() - ((Double) o2).doubleValue()) < 0 ? -1 : 1;
58 } else if ((o1 instanceof Boolean) && (o2 instanceof Boolean)) {
59 return ((Boolean) o1).booleanValue() == ((Boolean) o2).booleanValue() ? 0 : -1;
61 } else if ((o1 instanceof String) && (o2 instanceof String)) {
63 return ((String) o1).equals(((String) o2)) ? 0 : -1;
64 } else if ((o1 instanceof JSONObject) && (o2 instanceof JSONObject)) {
65 if (((JSONObject) o1).length() != ((JSONObject) o2).length()) {
66 return ((JSONObject) o1).length() - ((JSONObject) o2).length() < 0 ? -1 : 1;
68 Iterator<?> keys = ((JSONObject) o1).keys();
69 while (keys.hasNext()) {
70 String key = String.valueOf(keys.next());
71 if (!((JSONObject) o2).has(key)) {
74 x = this.test(((JSONObject) o1).get(key), ((JSONObject) o2).get(key));
79 } else if ((o1 instanceof JSONArray) && (o2 instanceof JSONArray)) {
80 if (((JSONArray) o1).length() != ((JSONArray) o2).length()) {
81 return ((JSONArray) o1).length() - ((JSONArray) o2).length() < 0 ? -1 : 1;
83 for (int i = 0; i < ((JSONArray) o1).length(); i++) {
84 x = this.findInArray(((JSONArray) o1).get(i), (JSONArray) o2);
89 for (int i = 0; i < ((JSONArray) o2).length(); i++) {
90 x = this.findInArray(((JSONArray) o2).get(i), (JSONArray) o1);
100 private int findInArray(Object node, JSONArray o) {
101 for (int i = 0; i < o.length(); i++) {
102 if (this.test(o.get(i), node) == 0) {
110 * strict comparison means that json array items have to be in the same order
112 private static Comparator<JSONObject> strictComarator = new Comparator<JSONObject>() {
115 public int compare(JSONObject o1, JSONObject o2) {
119 Iterator<?> keys = o1.keys();
120 while (keys.hasNext()) {
121 String key = String.valueOf(keys.next());
122 int x = this.test(o1.get(key), o2.get(key));
130 private int test(Object o1, Object o2) {
132 if ((o1 instanceof Double) && (o2 instanceof Double)) {
134 return (((Double) o1).doubleValue() - ((Double) o2).doubleValue()) < 0 ? -1 : 1;
135 } else if ((o1 instanceof Boolean) && (o2 instanceof Boolean)) {
136 return ((Boolean) o1).booleanValue() == ((Boolean) o2).booleanValue() ? 0 : -1;
138 } else if ((o1 instanceof String) && (o2 instanceof String)) {
140 return ((String) o1).equals(((String) o2)) ? 0 : -1;
141 } else if ((o1 instanceof JSONObject) && (o2 instanceof JSONObject)) {
142 if (((JSONObject) o1).length() == 0 && ((JSONObject) o2).length() == 0) {
145 Iterator<?> keys = ((JSONObject) o1).keys();
146 while (keys.hasNext()) {
147 String key = String.valueOf(keys.next());
148 if (!((JSONObject) o2).has(key)) {
151 x = this.test(((JSONObject) o1).get(key), ((JSONObject) o2).get(key));
156 } else if ((o1 instanceof JSONArray) && (o2 instanceof JSONArray)) {
157 if (((JSONArray) o1).length() != ((JSONArray) o2).length()) {
158 return ((JSONArray) o1).length() - ((JSONArray) o2).length() < 0 ? -1 : 1;
160 for (int i = 0; i < ((JSONArray) o1).length(); i++) {
161 x = this.test(((JSONArray) o1).get(i), ((JSONArray) o2).get(i));
172 public static void assertEquals(String def, String toTest, boolean strict) throws JSONException {
173 assertEquals(null, def, toTest, strict);
176 public static void assertEquals(String message, String def, String toTest, boolean strict) throws JSONException {
178 assertEqualsStrict(message, def, toTest);
180 assertEqualsNonStrict(message, def, toTest);
184 private static void assertEqualsNonStrict(String message, String def, String toTest) throws JSONException {
186 JSONObject d1 = new JSONObject(def);
187 JSONObject d2 = new JSONObject(toTest);
188 if (nonStrictComarator.compare(d1, d2) != 0) {
189 throw new AssertionError(message);
193 private static void assertEqualsStrict(String message, String def, String toTest) throws JSONException {
194 JSONObject d1 = new JSONObject(def);
195 JSONObject d2 = new JSONObject(toTest);
196 if (strictComarator.compare(d1, d2) != 0) {
197 throw new AssertionError(message);