View | Details | Raw Unified | Return to bug 65299
Collapse All | Expand All

(-)a/src/components/src/main/java/org/apache/jmeter/assertions/JSONPathAssertion.java (-2 / +5 lines)
Lines 20-25 package org.apache.jmeter.assertions; Link Here
20
import java.io.Serializable;
20
import java.io.Serializable;
21
import java.text.DecimalFormat;
21
import java.text.DecimalFormat;
22
import java.util.Map;
22
import java.util.Map;
23
import java.util.Objects;
23
24
24
import org.apache.jmeter.samplers.SampleResult;
25
import org.apache.jmeter.samplers.SampleResult;
25
import org.apache.jmeter.testelement.AbstractTestElement;
26
import org.apache.jmeter.testelement.AbstractTestElement;
Lines 31-36 import org.slf4j.LoggerFactory; Link Here
31
32
32
import net.minidev.json.JSONArray;
33
import net.minidev.json.JSONArray;
33
import net.minidev.json.JSONObject;
34
import net.minidev.json.JSONObject;
35
import net.minidev.json.JSONValue;
34
36
35
import com.jayway.jsonpath.JsonPath;
37
import com.jayway.jsonpath.JsonPath;
36
38
Lines 153-164 public class JSONPathAssertion extends AbstractTestElement implements Serializab Link Here
153
    }
155
    }
154
156
155
    private boolean isEquals(Object subj) {
157
    private boolean isEquals(Object subj) {
156
        String str = objectToString(subj);
157
        if (isUseRegex()) {
158
        if (isUseRegex()) {
159
            String str = objectToString(subj);
158
            Pattern pattern = JMeterUtils.getPatternCache().getPattern(getExpectedValue());
160
            Pattern pattern = JMeterUtils.getPatternCache().getPattern(getExpectedValue());
159
            return JMeterUtils.getMatcher().matches(str, pattern);
161
            return JMeterUtils.getMatcher().matches(str, pattern);
160
        } else {
162
        } else {
161
            return str.equals(getExpectedValue());
163
            Object expected = JSONValue.parse(getExpectedValue());
164
            return Objects.equals(expected, subj);
162
        }
165
        }
163
    }
166
    }
164
167
(-)a/src/components/src/main/java/org/apache/jmeter/assertions/jmespath/JMESPathAssertion.java (-1 / +10 lines)
Lines 18-23 Link Here
18
package org.apache.jmeter.assertions.jmespath;
18
package org.apache.jmeter.assertions.jmespath;
19
19
20
import java.io.Serializable;
20
import java.io.Serializable;
21
import java.util.Objects;
21
22
22
import org.apache.jmeter.assertions.Assertion;
23
import org.apache.jmeter.assertions.Assertion;
23
import org.apache.jmeter.assertions.AssertionResult;
24
import org.apache.jmeter.assertions.AssertionResult;
Lines 179-185 public class JMESPathAssertion extends AbstractTestElement implements Serializab Link Here
179
            Pattern pattern = JMeterUtils.getPatternCache().getPattern(getExpectedValue());
180
            Pattern pattern = JMeterUtils.getPatternCache().getPattern(getExpectedValue());
180
            return JMeterUtils.getMatcher().matches(str, pattern);
181
            return JMeterUtils.getMatcher().matches(str, pattern);
181
        } else {
182
        } else {
182
            return str.equals(getExpectedValue());
183
            String expectedValueString = getExpectedValue();
184
            // first try to match as a string value, as
185
            // we did in the old days
186
            if (str.equals(expectedValueString)) {
187
                return true;
188
            }
189
            // now try harder and compare it as an JSON object
190
            JsonNode expected = OBJECT_MAPPER.readValue(expectedValueString, JsonNode.class);
191
            return Objects.equals(expected, jsonNode);
183
        }
192
        }
184
    }
193
    }
185
194
(-)a/src/components/src/test/java/org/apache/jmeter/assertions/TestJSONPathAssertion.java (-29 / +44 lines)
Lines 17-35 Link Here
17
17
18
package org.apache.jmeter.assertions;
18
package org.apache.jmeter.assertions;
19
19
20
import static org.junit.Assert.assertEquals;
20
21
import static org.junit.Assert.assertFalse;
21
import static org.junit.jupiter.api.Assertions.assertEquals;
22
import static org.junit.Assert.assertTrue;
22
import static org.junit.jupiter.api.Assertions.assertFalse;
23
import static org.junit.jupiter.api.Assertions.assertTrue;
23
24
24
import java.util.Locale;
25
import java.util.Locale;
25
26
26
import org.apache.jmeter.samplers.SampleResult;
27
import org.apache.jmeter.samplers.SampleResult;
27
import org.junit.jupiter.api.Test;
28
import org.junit.jupiter.api.Test;
28
29
29
public class TestJSONPathAssertion {
30
class TestJSONPathAssertion {
30
31
31
    @Test
32
    @Test
32
    public void testGetJsonPath() {
33
    void testGetJsonPath() {
33
        JSONPathAssertion instance = new JSONPathAssertion();
34
        JSONPathAssertion instance = new JSONPathAssertion();
34
        String expResult = "";
35
        String expResult = "";
35
        String result = instance.getJsonPath();
36
        String result = instance.getJsonPath();
Lines 37-50 public class TestJSONPathAssertion { Link Here
37
    }
38
    }
38
39
39
    @Test
40
    @Test
40
    public void testSetJsonPath() {
41
    void testSetJsonPath() {
41
        String jsonPath = "";
42
        String jsonPath = "";
42
        JSONPathAssertion instance = new JSONPathAssertion();
43
        JSONPathAssertion instance = new JSONPathAssertion();
43
        instance.setJsonPath(jsonPath);
44
        instance.setJsonPath(jsonPath);
44
    }
45
    }
45
46
46
    @Test
47
    @Test
47
    public void testGetExpectedValue() {
48
    void testGetExpectedValue() {
48
        JSONPathAssertion instance = new JSONPathAssertion();
49
        JSONPathAssertion instance = new JSONPathAssertion();
49
        String expResult = "";
50
        String expResult = "";
50
        String result = instance.getExpectedValue();
51
        String result = instance.getExpectedValue();
Lines 52-78 public class TestJSONPathAssertion { Link Here
52
    }
53
    }
53
54
54
    @Test
55
    @Test
55
    public void testSetExpectedValue() {
56
    void testSetExpectedValue() {
56
        String expectedValue = "";
57
        String expectedValue = "";
57
        JSONPathAssertion instance = new JSONPathAssertion();
58
        JSONPathAssertion instance = new JSONPathAssertion();
58
        instance.setExpectedValue(expectedValue);
59
        instance.setExpectedValue(expectedValue);
59
    }
60
    }
60
61
61
    @Test
62
    @Test
62
    public void testSetJsonValidationBool() {
63
    void testSetJsonValidationBool() {
63
        JSONPathAssertion instance = new JSONPathAssertion();
64
        JSONPathAssertion instance = new JSONPathAssertion();
64
        instance.setJsonValidationBool(false);
65
        instance.setJsonValidationBool(false);
65
    }
66
    }
66
67
67
    @Test
68
    @Test
68
    public void testIsJsonValidationBool() {
69
    void testIsJsonValidationBool() {
69
        JSONPathAssertion instance = new JSONPathAssertion();
70
        JSONPathAssertion instance = new JSONPathAssertion();
70
        boolean result = instance.isJsonValidationBool();
71
        boolean result = instance.isJsonValidationBool();
71
        assertFalse(result);
72
        assertFalse(result);
72
    }
73
    }
73
74
74
    @Test
75
    @Test
75
    public void testGetResult_positive() {
76
    void testGetResult_positive() {
76
        SampleResult samplerResult = new SampleResult();
77
        SampleResult samplerResult = new SampleResult();
77
        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
78
        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
78
79
Lines 87-93 public class TestJSONPathAssertion { Link Here
87
    }
88
    }
88
89
89
    @Test
90
    @Test
90
    public void testGetResult_positive_regexp() {
91
    void testGetResult_positive_regexp() {
91
        SampleResult samplerResult = new SampleResult();
92
        SampleResult samplerResult = new SampleResult();
92
        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
93
        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
93
94
Lines 106-112 public class TestJSONPathAssertion { Link Here
106
    }
107
    }
107
108
108
    @Test
109
    @Test
109
    public void testGetResult_positive_invert() {
110
    void testGetResult_positive_invert() {
110
        SampleResult samplerResult = new SampleResult();
111
        SampleResult samplerResult = new SampleResult();
111
        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
112
        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
112
113
Lines 122-128 public class TestJSONPathAssertion { Link Here
122
    }
123
    }
123
124
124
    @Test
125
    @Test
125
    public void testGetResult_not_regexp() {
126
    void testGetResult_not_regexp() {
126
        SampleResult samplerResult = new SampleResult();
127
        SampleResult samplerResult = new SampleResult();
127
        samplerResult.setResponseData("{\"myval\": \"some complicated value\"}".getBytes());
128
        samplerResult.setResponseData("{\"myval\": \"some complicated value\"}".getBytes());
128
129
Lines 139-145 public class TestJSONPathAssertion { Link Here
139
    }
140
    }
140
141
141
    @Test
142
    @Test
142
    public void testGetResult_negative() {
143
    void testGetResult_complex_map() {
144
        SampleResult samplerResult = new SampleResult();
145
        samplerResult.setResponseData("{\"myval\": { \"a\": 23, \"b\": 42, \"c\": \"something\" } }".getBytes());
146
147
        JSONPathAssertion instance = new JSONPathAssertion();
148
        instance.setJsonPath("$.myval");
149
        instance.setJsonValidationBool(true);
150
        instance.setIsRegex(false);
151
        instance.setExpectedValue("{\n\t\"a\": 23,\n\"b\": 42,\n\t\"c\": \"something\"\n}");
152
        AssertionResult result = instance.getResult(samplerResult);
153
        assertFalse(result.isFailure());
154
    }
155
156
    @Test
157
    void testGetResult_negative() {
143
        SampleResult samplerResult = new SampleResult();
158
        SampleResult samplerResult = new SampleResult();
144
        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
159
        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
145
160
Lines 154-160 public class TestJSONPathAssertion { Link Here
154
    }
169
    }
155
170
156
    @Test
171
    @Test
157
    public void testGetResult_negative_invert() {
172
    void testGetResult_negative_invert() {
158
        SampleResult samplerResult = new SampleResult();
173
        SampleResult samplerResult = new SampleResult();
159
        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
174
        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
160
175
Lines 170-176 public class TestJSONPathAssertion { Link Here
170
    }
185
    }
171
186
172
    @Test
187
    @Test
173
    public void testGetResult_null() {
188
    void testGetResult_null() {
174
        SampleResult samplerResult = new SampleResult();
189
        SampleResult samplerResult = new SampleResult();
175
        samplerResult.setResponseData("{\"myval\": null}".getBytes());
190
        samplerResult.setResponseData("{\"myval\": null}".getBytes());
176
191
Lines 185-191 public class TestJSONPathAssertion { Link Here
185
    }
200
    }
186
201
187
    @Test
202
    @Test
188
    public void testGetResult_null_not_found() {
203
    void testGetResult_null_not_found() {
189
        SampleResult samplerResult = new SampleResult();
204
        SampleResult samplerResult = new SampleResult();
190
        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
205
        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
191
206
Lines 200-206 public class TestJSONPathAssertion { Link Here
200
    }
215
    }
201
216
202
    @Test
217
    @Test
203
    public void testGetResult_null_novalidate() {
218
    void testGetResult_null_novalidate() {
204
        SampleResult samplerResult = new SampleResult();
219
        SampleResult samplerResult = new SampleResult();
205
        samplerResult.setResponseData("{\"myval\": null}".getBytes());
220
        samplerResult.setResponseData("{\"myval\": null}".getBytes());
206
221
Lines 214-220 public class TestJSONPathAssertion { Link Here
214
    }
229
    }
215
230
216
    @Test
231
    @Test
217
    public void testGetResult_no_such_path() {
232
    void testGetResult_no_such_path() {
218
        SampleResult samplerResult = new SampleResult();
233
        SampleResult samplerResult = new SampleResult();
219
        samplerResult.setResponseData("{\"myval\": null}".getBytes());
234
        samplerResult.setResponseData("{\"myval\": null}".getBytes());
220
235
Lines 228-234 public class TestJSONPathAssertion { Link Here
228
    }
243
    }
229
244
230
    @Test
245
    @Test
231
    public void testGetResult_list_val() {
246
    void testGetResult_list_val() {
232
        SampleResult samplerResult = new SampleResult();
247
        SampleResult samplerResult = new SampleResult();
233
        samplerResult.setResponseData("{\"myval\": [{\"test\":1},{\"test\":2},{\"test\":3}]}".getBytes());
248
        samplerResult.setResponseData("{\"myval\": [{\"test\":1},{\"test\":2},{\"test\":3}]}".getBytes());
234
249
Lines 243-249 public class TestJSONPathAssertion { Link Here
243
    }
258
    }
244
259
245
    @Test
260
    @Test
246
    public void testGetResult_list_negative() {
261
    void testGetResult_list_negative() {
247
        SampleResult samplerResult = new SampleResult();
262
        SampleResult samplerResult = new SampleResult();
248
        samplerResult.setResponseData("{\"myval\": [{\"test\":1},{\"test\":2},{\"test\":3}]}".getBytes());
263
        samplerResult.setResponseData("{\"myval\": [{\"test\":1},{\"test\":2},{\"test\":3}]}".getBytes());
249
264
Lines 258-264 public class TestJSONPathAssertion { Link Here
258
    }
273
    }
259
274
260
    @Test
275
    @Test
261
    public void testGetResult_list_empty_novalidate() {
276
    void testGetResult_list_empty_novalidate() {
262
        SampleResult samplerResult = new SampleResult();
277
        SampleResult samplerResult = new SampleResult();
263
        samplerResult.setResponseData("{\"myval\": []}".getBytes());
278
        samplerResult.setResponseData("{\"myval\": []}".getBytes());
264
279
Lines 272-278 public class TestJSONPathAssertion { Link Here
272
    }
287
    }
273
288
274
    @Test
289
    @Test
275
    public void testGetResult_list_empty_validate() {
290
    void testGetResult_list_empty_validate() {
276
        SampleResult samplerResult = new SampleResult();
291
        SampleResult samplerResult = new SampleResult();
277
        samplerResult.setResponseData("{\"myval\": []}".getBytes());
292
        samplerResult.setResponseData("{\"myval\": []}".getBytes());
278
293
Lines 287-293 public class TestJSONPathAssertion { Link Here
287
    }
302
    }
288
303
289
    @Test
304
    @Test
290
    public void testGetResult_dict() {
305
    void testGetResult_dict() {
291
        SampleResult samplerResult = new SampleResult();
306
        SampleResult samplerResult = new SampleResult();
292
        samplerResult.setResponseData("{\"myval\": {\"key\": \"val\"}}".getBytes());
307
        samplerResult.setResponseData("{\"myval\": {\"key\": \"val\"}}".getBytes());
293
308
Lines 302-308 public class TestJSONPathAssertion { Link Here
302
    }
317
    }
303
318
304
    @Test
319
    @Test
305
    public void testGetResult_inverted_null() {
320
    void testGetResult_inverted_null() {
306
        SampleResult samplerResult = new SampleResult();
321
        SampleResult samplerResult = new SampleResult();
307
        samplerResult.setResponseData("{\"myval\": [{\"key\": null}]}".getBytes());
322
        samplerResult.setResponseData("{\"myval\": [{\"key\": null}]}".getBytes());
308
323
Lines 318-324 public class TestJSONPathAssertion { Link Here
318
    }
333
    }
319
334
320
    @Test
335
    @Test
321
    public void testGetResult_match_msg_problem() {
336
    void testGetResult_match_msg_problem() {
322
        SampleResult samplerResult = new SampleResult();
337
        SampleResult samplerResult = new SampleResult();
323
        String str = "{\"execution\":[{\"scenario\":{\"requests\":[{\"headers\":{\"headerkey\":\"header value\"}}]}}]}";
338
        String str = "{\"execution\":[{\"scenario\":{\"requests\":[{\"headers\":{\"headerkey\":\"header value\"}}]}}]}";
324
        samplerResult.setResponseData(str.getBytes());
339
        samplerResult.setResponseData(str.getBytes());
Lines 339-345 public class TestJSONPathAssertion { Link Here
339
    }
354
    }
340
355
341
    @Test
356
    @Test
342
    public void testGetResult_match_msg_problem2() {
357
    void testGetResult_match_msg_problem2() {
343
        SampleResult samplerResult = new SampleResult();
358
        SampleResult samplerResult = new SampleResult();
344
        String str = "{\n" +
359
        String str = "{\n" +
345
                " \"code\":200,\n" +
360
                " \"code\":200,\n" +
Lines 370-376 public class TestJSONPathAssertion { Link Here
370
    }
385
    }
371
386
372
    @Test
387
    @Test
373
    public void testGetResultFloat() {
388
    void testGetResultFloat() {
374
        Locale prevLocale = Locale.getDefault();
389
        Locale prevLocale = Locale.getDefault();
375
        try {
390
        try {
376
            // 0.0000123456789 is locale-dependent
391
            // 0.0000123456789 is locale-dependent
(-)a/src/components/src/test/java/org/apache/jmeter/assertions/jmespath/TestJMESPathAssertion.java (-2 / +5 lines)
Lines 143-149 public class TestJMESPathAssertion { Link Here
143
                                    + "      ]\n" + "    }\n" + "  ]\n" + "}",
143
                                    + "      ]\n" + "    }\n" + "  ]\n" + "}",
144
                            "reservations[*].instances[*].state", ValidationType.USE_VALIDATION,
144
                            "reservations[*].instances[*].state", ValidationType.USE_VALIDATION,
145
                            ComparisonType.USE_NO_REXEG, ResultNullity.EXPECT_NOT_NULL,
145
                            ComparisonType.USE_NO_REXEG, ResultNullity.EXPECT_NOT_NULL,
146
                            "[[\"running\",\"stopped\"],[\"terminated\",\"running\"]]", ResultType.SUCCESS, "" } });
146
                            "[[\"running\",\"stopped\"],[\"terminated\",\"running\"]]", ResultType.SUCCESS, "" },
147
                    { InvertType.USE_NO_INVERT, "{\"x\": {\"a\": 23, \"b\": 42, \"c\": \"something\"}}", "x",
148
                            ValidationType.USE_VALIDATION, ComparisonType.USE_NO_REXEG, ResultNullity.EXPECT_NOT_NULL,
149
                            "{\n\t\"a\": 23,\n\t\"b\": 42,\n\t\"c\": \"something\"\n}", ResultType.SUCCESS,
150
                            "" } });
147
        }
151
        }
148
152
149
153
150
- 

Return to bug 65299