Lines 20-26
package org.apache.jmeter.extractor.json.jmespath;
Link Here
|
20 |
import java.io.IOException; |
20 |
import java.io.IOException; |
21 |
import java.io.Serializable; |
21 |
import java.io.Serializable; |
22 |
import java.util.ArrayList; |
22 |
import java.util.ArrayList; |
|
|
23 |
import java.util.Arrays; |
24 |
import java.util.Collections; |
23 |
import java.util.List; |
25 |
import java.util.List; |
|
|
26 |
import java.util.stream.Collectors; |
24 |
|
27 |
|
25 |
import org.apache.commons.lang3.StringUtils; |
28 |
import org.apache.commons.lang3.StringUtils; |
26 |
import org.apache.jmeter.processor.PostProcessor; |
29 |
import org.apache.jmeter.processor.PostProcessor; |
Lines 38-43
import com.fasterxml.jackson.databind.JsonNode;
Link Here
|
38 |
import com.fasterxml.jackson.databind.ObjectMapper; |
41 |
import com.fasterxml.jackson.databind.ObjectMapper; |
39 |
import com.fasterxml.jackson.databind.node.ArrayNode; |
42 |
import com.fasterxml.jackson.databind.node.ArrayNode; |
40 |
|
43 |
|
|
|
44 |
import io.burt.jmespath.Expression; |
45 |
|
41 |
/** |
46 |
/** |
42 |
* JMESPATH based extractor |
47 |
* JMESPATH based extractor |
43 |
* |
48 |
* |
Lines 60-66
public class JMESPathExtractor extends AbstractScopedTestElement
Link Here
|
60 |
public void process() { |
65 |
public void process() { |
61 |
JMeterContext context = getThreadContext(); |
66 |
JMeterContext context = getThreadContext(); |
62 |
JMeterVariables vars = context.getVariables(); |
67 |
JMeterVariables vars = context.getVariables(); |
63 |
String jsonResponse = getData(vars, context); |
68 |
List<String> jsonResponse = getData(vars, context); |
64 |
String refName = getRefName(); |
69 |
String refName = getRefName(); |
65 |
String defaultValue = getDefaultValue(); |
70 |
String defaultValue = getDefaultValue(); |
66 |
int matchNumber; |
71 |
int matchNumber; |
Lines 71-91
public class JMESPathExtractor extends AbstractScopedTestElement
Link Here
|
71 |
} |
76 |
} |
72 |
final String jsonPathExpression = getJmesPathExpression().trim(); |
77 |
final String jsonPathExpression = getJmesPathExpression().trim(); |
73 |
clearOldRefVars(vars, refName); |
78 |
clearOldRefVars(vars, refName); |
74 |
if (StringUtils.isEmpty(jsonResponse)) { |
79 |
if (jsonResponse.isEmpty()) { |
75 |
handleEmptyResponse(vars, refName, defaultValue); |
80 |
handleEmptyResponse(vars, refName, defaultValue); |
76 |
return; |
81 |
return; |
77 |
} |
82 |
} |
78 |
|
83 |
|
79 |
try { |
84 |
try { |
80 |
JsonNode actualObj = OBJECT_MAPPER.readValue(jsonResponse, JsonNode.class); |
85 |
List<String> resultList = new ArrayList<>(); |
81 |
JsonNode result = JMESPathCache.getInstance().get(jsonPathExpression).search(actualObj); |
86 |
Expression<JsonNode> searchExpression = JMESPathCache.getInstance().get(jsonPathExpression); |
82 |
if (result.isNull()) { |
87 |
for (String response: jsonResponse) { |
83 |
handleNullResult(vars, refName, defaultValue, matchNumber); |
88 |
JsonNode actualObj = OBJECT_MAPPER.readValue(response, JsonNode.class); |
84 |
return; |
89 |
JsonNode result = searchExpression.search(actualObj); |
|
|
90 |
if (result.isNull()) { |
91 |
continue; |
92 |
} |
93 |
resultList.addAll(splitJson(result)); |
85 |
} |
94 |
} |
86 |
List<String> resultList = splitJson(result); |
|
|
87 |
// if more than one value extracted, suffix with "_index" |
95 |
// if more than one value extracted, suffix with "_index" |
88 |
if (resultList.size() > 1) { |
96 |
int size = resultList.size(); |
|
|
97 |
if (size > 1) { |
89 |
handleListResult(vars, refName, defaultValue, matchNumber, resultList); |
98 |
handleListResult(vars, refName, defaultValue, matchNumber, resultList); |
90 |
} else if (resultList.isEmpty()){ |
99 |
} else if (resultList.isEmpty()){ |
91 |
handleNullResult(vars, refName, defaultValue, matchNumber); |
100 |
handleNullResult(vars, refName, defaultValue, matchNumber); |
Lines 94-100
public class JMESPathExtractor extends AbstractScopedTestElement
Link Here
|
94 |
// else just one value extracted |
103 |
// else just one value extracted |
95 |
handleSingleResult(vars, refName, matchNumber, resultList); |
104 |
handleSingleResult(vars, refName, matchNumber, resultList); |
96 |
} |
105 |
} |
97 |
vars.put(refName + REF_MATCH_NR, Integer.toString(resultList.size())); |
106 |
vars.put(refName + REF_MATCH_NR, Integer.toString(size)); |
98 |
} catch (Exception e) { |
107 |
} catch (Exception e) { |
99 |
// if something wrong, default value added |
108 |
// if something wrong, default value added |
100 |
if (log.isDebugEnabled()) { |
109 |
if (log.isDebugEnabled()) { |
Lines 155-177
public class JMESPathExtractor extends AbstractScopedTestElement
Link Here
|
155 |
vars.put(refName, defaultValue); |
164 |
vars.put(refName, defaultValue); |
156 |
} |
165 |
} |
157 |
|
166 |
|
158 |
private String getData(JMeterVariables vars, JMeterContext context) { |
167 |
private List<String> getData(JMeterVariables vars, JMeterContext context) { |
159 |
String jsonResponse = null; |
|
|
160 |
if (isScopeVariable()) { |
168 |
if (isScopeVariable()) { |
161 |
jsonResponse = vars.get(getVariableName()); |
169 |
String jsonResponse = vars.get(getVariableName()); |
162 |
if (log.isDebugEnabled()) { |
170 |
if (log.isDebugEnabled()) { |
163 |
log.debug("JMESExtractor is using variable: {}, which content is: {}", getVariableName(), jsonResponse); |
171 |
log.debug("JMESExtractor is using variable: {}, which content is: {}", getVariableName(), jsonResponse); |
164 |
} |
172 |
} |
|
|
173 |
return Arrays.asList(jsonResponse); |
165 |
} else { |
174 |
} else { |
166 |
SampleResult previousResult = context.getPreviousResult(); |
175 |
SampleResult previousResult = context.getPreviousResult(); |
167 |
if (previousResult != null) { |
176 |
if (previousResult != null) { |
168 |
jsonResponse = previousResult.getResponseDataAsString(); |
177 |
List<String> results = getSampleList(previousResult).stream() |
169 |
} |
178 |
.map(SampleResult::getResponseDataAsString) |
170 |
if (log.isDebugEnabled()) { |
179 |
.collect(Collectors.toList()); |
171 |
log.debug("JMESExtractor {} working on Response: {}", getName(), jsonResponse); |
180 |
if (log.isDebugEnabled()) { |
|
|
181 |
log.debug("JMESExtractor {} working on Responses: {}", getName(), results); |
182 |
} |
183 |
return results; |
172 |
} |
184 |
} |
173 |
} |
185 |
} |
174 |
return jsonResponse; |
186 |
return Collections.emptyList(); |
175 |
} |
187 |
} |
176 |
|
188 |
|
177 |
public List<String> splitJson(JsonNode jsonNode) throws IOException { |
189 |
public List<String> splitJson(JsonNode jsonNode) throws IOException { |