Reformat sdnr common to ONAP code style
[ccsdk/features.git] / sdnr / wt / common / src / main / java / org / onap / ccsdk / features / sdnr / wt / common / test / JSONAssert.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.common.test;
23
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;
29
30 public class JSONAssert {
31
32     /**
33      * nonstrict comparison means that json array items can be in different orders
34      */
35     private static Comparator<JSONObject> nonStrictComarator = new Comparator<JSONObject>() {
36
37         @Override
38         public int compare(JSONObject o1, JSONObject o2) {
39             if (o1.equals(o2)) {
40                 return 0;
41             }
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));
46                 if (x != 0) {
47                     return x;
48                 }
49             }
50             return 0;
51         }
52
53         private int test(Object o1, Object o2) {
54             int x;
55             if ((o1 instanceof Double) && (o2 instanceof Double)) {
56
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;
60
61             } else if ((o1 instanceof String) && (o2 instanceof String)) {
62
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;
67                 }
68                 Iterator<?> keys = ((JSONObject) o1).keys();
69                 while (keys.hasNext()) {
70                     String key = String.valueOf(keys.next());
71                     if (!((JSONObject) o2).has(key)) {
72                         return -1;
73                     }
74                     x = this.test(((JSONObject) o1).get(key), ((JSONObject) o2).get(key));
75                     if (x != 0) {
76                         return x;
77                     }
78                 }
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;
82                 }
83                 for (int i = 0; i < ((JSONArray) o1).length(); i++) {
84                     x = this.findInArray(((JSONArray) o1).get(i), (JSONArray) o2);
85                     if (x != 0) {
86                         return x;
87                     }
88                 }
89                 for (int i = 0; i < ((JSONArray) o2).length(); i++) {
90                     x = this.findInArray(((JSONArray) o2).get(i), (JSONArray) o1);
91                     if (x != 0) {
92                         return x;
93                     }
94                 }
95             }
96             return 0;
97
98         }
99
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) {
103                     return 0;
104                 }
105             }
106             return -1;
107         }
108     };
109     /**
110      * strict comparison means that json array items have to be in the same order
111      */
112     private static Comparator<JSONObject> strictComarator = new Comparator<JSONObject>() {
113
114         @Override
115         public int compare(JSONObject o1, JSONObject o2) {
116             if (o1.equals(o2)) {
117                 return 0;
118             }
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));
123                 if (x != 0) {
124                     return x;
125                 }
126             }
127             return 0;
128         }
129
130         private int test(Object o1, Object o2) {
131             int x;
132             if ((o1 instanceof Double) && (o2 instanceof Double)) {
133
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;
137
138             } else if ((o1 instanceof String) && (o2 instanceof String)) {
139
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) {
143                     return 0;
144                 }
145                 Iterator<?> keys = ((JSONObject) o1).keys();
146                 while (keys.hasNext()) {
147                     String key = String.valueOf(keys.next());
148                     if (!((JSONObject) o2).has(key)) {
149                         return -1;
150                     }
151                     x = this.test(((JSONObject) o1).get(key), ((JSONObject) o2).get(key));
152                     if (x != 0) {
153                         return x;
154                     }
155                 }
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;
159                 }
160                 for (int i = 0; i < ((JSONArray) o1).length(); i++) {
161                     x = this.test(((JSONArray) o1).get(i), ((JSONArray) o2).get(i));
162                     if (x != 0) {
163                         return x;
164                     }
165                 }
166             }
167             return 0;
168
169         }
170     };
171
172     public static void assertEquals(String def, String toTest, boolean strict) throws JSONException {
173         assertEquals(null, def, toTest, strict);
174     }
175
176     public static void assertEquals(String message, String def, String toTest, boolean strict) throws JSONException {
177         if (strict) {
178             assertEqualsStrict(message, def, toTest);
179         } else {
180             assertEqualsNonStrict(message, def, toTest);
181         }
182     }
183
184     private static void assertEqualsNonStrict(String message, String def, String toTest) throws JSONException {
185
186         JSONObject d1 = new JSONObject(def);
187         JSONObject d2 = new JSONObject(toTest);
188         if (nonStrictComarator.compare(d1, d2) != 0) {
189             throw new AssertionError(message);
190         }
191     }
192
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);
198         }
199     }
200
201 }