Link Here
|
|
|
1 |
/* |
2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
* contributor license agreements. See the NOTICE file distributed with |
4 |
* this work for additional information regarding copyright ownership. |
5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
* (the "License"); you may not use this file except in compliance with |
7 |
* the License. You may obtain a copy of the License at |
8 |
* |
9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
10 |
* |
11 |
* Unless required by applicable law or agreed to in writing, software |
12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
* See the License for the specific language governing permissions and |
15 |
* limitations under the License. |
16 |
* |
17 |
*/ |
18 |
|
19 |
package org.apache.jmeter.extractor; |
20 |
|
21 |
import java.nio.charset.StandardCharsets; |
22 |
|
23 |
import net.minidev.json.parser.JSONParser; |
24 |
import net.minidev.json.parser.ParseException; |
25 |
|
26 |
import org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor; |
27 |
import org.apache.jmeter.samplers.SampleResult; |
28 |
import org.apache.jmeter.threads.JMeterContext; |
29 |
import org.apache.jmeter.threads.JMeterContextService; |
30 |
import org.apache.jmeter.threads.JMeterVariables; |
31 |
import org.junit.Assert; |
32 |
import org.junit.Test; |
33 |
|
34 |
public class TestJSONPostProcessor { |
35 |
|
36 |
private static final String VAR_NAME = "varName"; |
37 |
|
38 |
@Test |
39 |
public void testBug59609() throws ParseException { |
40 |
JMeterContext context = JMeterContextService.getContext(); |
41 |
JSONPostProcessor processor = setupProcessor(context, "0", false); |
42 |
|
43 |
String innerValue = "{\"a\":\"one\",\"b\":\"two\"}"; |
44 |
String data = "{\"context\":" + innerValue + "}"; |
45 |
SampleResult result = new SampleResult(); |
46 |
result.setResponseData(data.getBytes(StandardCharsets.UTF_8)); |
47 |
|
48 |
JMeterVariables vars = new JMeterVariables(); |
49 |
context.setVariables(vars); |
50 |
context.setPreviousResult(result); |
51 |
|
52 |
processor.setJsonPathExpressions("$.context"); |
53 |
processor.process(); |
54 |
|
55 |
JSONParser parser = new JSONParser(0); |
56 |
Object expectedValue = parser.parse(innerValue); |
57 |
Assert.assertEquals(expectedValue, parser.parse(vars.get(VAR_NAME))); |
58 |
Assert.assertEquals("1", vars.get(VAR_NAME + "_matchNr")); |
59 |
} |
60 |
|
61 |
@Test |
62 |
public void testExtractSimpleArrayElements() { |
63 |
JMeterContext context = JMeterContextService.getContext(); |
64 |
JSONPostProcessor processor = setupProcessor(context, "-1"); |
65 |
String data = "[1,2,3]"; |
66 |
SampleResult result = new SampleResult(); |
67 |
result.setResponseData(data.getBytes(StandardCharsets.UTF_8)); |
68 |
JMeterVariables vars = new JMeterVariables(); |
69 |
context.setVariables(vars); |
70 |
context.setPreviousResult(result); |
71 |
|
72 |
processor.setJsonPathExpressions("$[*]"); |
73 |
processor.process(); |
74 |
|
75 |
Assert.assertEquals("1,2,3", vars.get(VAR_NAME+ "_ALL")); |
76 |
for (int i = 1; i <= 3; i++) { |
77 |
String v = Integer.toString(i); |
78 |
Assert.assertEquals(v, vars.get(VAR_NAME + "_" + v)); |
79 |
} |
80 |
|
81 |
Assert.assertEquals("3", vars.get(VAR_NAME + "_matchNr")); |
82 |
} |
83 |
|
84 |
@Test |
85 |
public void testExtractComplexElements() { |
86 |
JMeterContext context = JMeterContextService.getContext(); |
87 |
JSONPostProcessor processor = setupProcessor(context, "-1"); |
88 |
String data = "[{\"a\":[1,{\"d\":2},3]},[\"b\"],3]"; |
89 |
SampleResult result = new SampleResult(); |
90 |
result.setResponseData(data.getBytes(StandardCharsets.UTF_8)); |
91 |
JMeterVariables vars = new JMeterVariables(); |
92 |
context.setVariables(vars); |
93 |
context.setPreviousResult(result); |
94 |
|
95 |
processor.setJsonPathExpressions("$[*]"); |
96 |
processor.process(); |
97 |
|
98 |
String jsonWithoutOuterParens = data.substring(1, data.length() - 1); |
99 |
Assert.assertEquals(jsonWithoutOuterParens, vars.get(VAR_NAME + "_ALL")); |
100 |
|
101 |
Assert.assertEquals("{\"a\":[1,{\"d\":2},3]}", vars.get(VAR_NAME + "_1")); |
102 |
Assert.assertEquals("[\"b\"]", vars.get(VAR_NAME + "_2")); |
103 |
Assert.assertEquals("3", vars.get(VAR_NAME + "_3")); |
104 |
|
105 |
Assert.assertEquals("3", vars.get(VAR_NAME + "_matchNr")); |
106 |
} |
107 |
|
108 |
private JSONPostProcessor setupProcessor(JMeterContext context, |
109 |
String matchNumbers) { |
110 |
return setupProcessor(context, matchNumbers, true); |
111 |
} |
112 |
|
113 |
private JSONPostProcessor setupProcessor(JMeterContext context, |
114 |
String matchNumbers, boolean computeConcatenation) { |
115 |
JSONPostProcessor processor = new JSONPostProcessor(); |
116 |
processor.setThreadContext(context); |
117 |
processor.setRefNames(VAR_NAME); |
118 |
processor.setMatchNumbers(matchNumbers); |
119 |
processor.setComputeConcatenation(computeConcatenation); |
120 |
return processor; |
121 |
} |
122 |
|
123 |
} |