aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/test/JSONAssert.java
blob: f95dfe0b2749f276e12c8432b676bd8784177888 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk features
 * ================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */
package org.onap.ccsdk.features.sdnr.wt.common.test;

import java.util.Comparator;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONAssert {

    /**
     * nonstrict comparison means that json array items can be in different orders
     */
    private static Comparator<JSONObject> nonStrictComarator = new Comparator<JSONObject>() {

        @Override
        public int compare(JSONObject o1, JSONObject o2) {
            if (o1.equals(o2)) {
                return 0;
            }
            Iterator<?> keys = o1.keys();
            while (keys.hasNext()) {
                String key = String.valueOf(keys.next());
                int x = this.test(o1.get(key), o2.get(key));
                if (x != 0) {
                    return x;
                }
            }
            return 0;
        }

        private int test(Object o1, Object o2) {
            int x;
            if ((o1 instanceof Double) && (o2 instanceof Double)) {

                return (((Double) o1).doubleValue() - ((Double) o2).doubleValue()) < 0 ? -1 : 1;
            } else if ((o1 instanceof Boolean) && (o2 instanceof Boolean)) {
                return ((Boolean) o1).booleanValue() == ((Boolean) o2).booleanValue() ? 0 : -1;

            } else if ((o1 instanceof String) && (o2 instanceof String)) {

                return ((String) o1).equals(((String) o2)) ? 0 : -1;
            } else if ((o1 instanceof JSONObject) && (o2 instanceof JSONObject)) {
                if (((JSONObject) o1).length() != ((JSONObject) o2).length()) {
                    return ((JSONObject) o1).length() - ((JSONObject) o2).length() < 0 ? -1 : 1;
                }
                Iterator<?> keys = ((JSONObject) o1).keys();
                while (keys.hasNext()) {
                    String key = String.valueOf(keys.next());
                    if (!((JSONObject) o2).has(key)) {
                        return -1;
                    }
                    x = this.test(((JSONObject) o1).get(key), ((JSONObject) o2).get(key));
                    if (x != 0) {
                        return x;
                    }
                }
            } else if ((o1 instanceof JSONArray) && (o2 instanceof JSONArray)) {
                if (((JSONArray) o1).length() != ((JSONArray) o2).length()) {
                    return ((JSONArray) o1).length() - ((JSONArray) o2).length() < 0 ? -1 : 1;
                }
                for (int i = 0; i < ((JSONArray) o1).length(); i++) {
                    x = this.findInArray(((JSONArray) o1).get(i), (JSONArray) o2);
                    if (x != 0) {
                        return x;
                    }
                }
                for (int i = 0; i < ((JSONArray) o2).length(); i++) {
                    x = this.findInArray(((JSONArray) o2).get(i), (JSONArray) o1);
                    if (x != 0) {
                        return x;
                    }
                }
            }
            return 0;

        }

        private int findInArray(Object node, JSONArray o) {
            for (int i = 0; i < o.length(); i++) {
                if (this.test(o.get(i), node) == 0) {
                    return 0;
                }
            }
            return -1;
        }
    };
    /**
     * strict comparison means that json array items have to be in the same order
     */
    private static Comparator<JSONObject> strictComarator = new Comparator<JSONObject>() {

        @Override
        public int compare(JSONObject o1, JSONObject o2) {
            if (o1.equals(o2)) {
                return 0;
            }
            Iterator<?> keys = o1.keys();
            while (keys.hasNext()) {
                String key = String.valueOf(keys.next());
                int x = this.test(o1.get(key), o2.get(key));
                if (x != 0) {
                    return x;
                }
            }
            return 0;
        }

        private int test(Object o1, Object o2) {
            int x;
            if ((o1 instanceof Double) && (o2 instanceof Double)) {

                return (((Double) o1).doubleValue() - ((Double) o2).doubleValue()) < 0 ? -1 : 1;
            } else if ((o1 instanceof Boolean) && (o2 instanceof Boolean)) {
                return ((Boolean) o1).booleanValue() == ((Boolean) o2).booleanValue() ? 0 : -1;

            } else if ((o1 instanceof String) && (o2 instanceof String)) {

                return ((String) o1).equals(((String) o2)) ? 0 : -1;
            } else if ((o1 instanceof JSONObject) && (o2 instanceof JSONObject)) {
                if (((JSONObject) o1).length() == 0 && ((JSONObject) o2).length() == 0) {
                    return 0;
                }
                Iterator<?> keys = ((JSONObject) o1).keys();
                while (keys.hasNext()) {
                    String key = String.valueOf(keys.next());
                    if (!((JSONObject) o2).has(key)) {
                        return -1;
                    }
                    x = this.test(((JSONObject) o1).get(key), ((JSONObject) o2).get(key));
                    if (x != 0) {
                        return x;
                    }
                }
            } else if ((o1 instanceof JSONArray) && (o2 instanceof JSONArray)) {
                if (((JSONArray) o1).length() != ((JSONArray) o2).length()) {
                    return ((JSONArray) o1).length() - ((JSONArray) o2).length() < 0 ? -1 : 1;
                }
                for (int i = 0; i < ((JSONArray) o1).length(); i++) {
                    x = this.test(((JSONArray) o1).get(i), ((JSONArray) o2).get(i));
                    if (x != 0) {
                        return x;
                    }
                }
            }
            return 0;

        }
    };

    public static void assertEquals(String def, String toTest, boolean strict) throws JSONException {
        assertEquals(null, def, toTest, strict);
    }

    public static void assertEquals(String message, String def, String toTest, boolean strict) throws JSONException {
        if (strict) {
            assertEqualsStrict(message, def, toTest);
        } else {
            assertEqualsNonStrict(message, def, toTest);
        }
    }

    private static void assertEqualsNonStrict(String message, String def, String toTest) throws JSONException {

        JSONObject d1 = new JSONObject(def);
        JSONObject d2 = new JSONObject(toTest);
        if (nonStrictComarator.compare(d1, d2) != 0) {
            throw new AssertionError(message);
        }
    }

    private static void assertEqualsStrict(String message, String def, String toTest) throws JSONException {
        JSONObject d1 = new JSONObject(def);
        JSONObject d2 = new JSONObject(toTest);
        if (strictComarator.compare(d1, d2) != 0) {
            throw new AssertionError(message);
        }
    }

}