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

(-)a/src/components/src/main/java/org/apache/jmeter/extractor/json/jmespath/JMESPathExtractor.java (-18 / +30 lines)
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 {
(-)a/src/components/src/main/java/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessor.java (-11 / +17 lines)
Lines 18-27 Link Here
18
package org.apache.jmeter.extractor.json.jsonpath;
18
package org.apache.jmeter.extractor.json.jsonpath;
19
19
20
import java.io.Serializable;
20
import java.io.Serializable;
21
import java.util.ArrayList;
21
import java.util.Arrays;
22
import java.util.Arrays;
23
import java.util.Collections;
22
import java.util.List;
24
import java.util.List;
25
import java.util.stream.Collectors;
23
26
24
import org.apache.commons.lang3.StringUtils;
25
import org.apache.jmeter.processor.PostProcessor;
27
import org.apache.jmeter.processor.PostProcessor;
26
import org.apache.jmeter.samplers.SampleResult;
28
import org.apache.jmeter.samplers.SampleResult;
27
import org.apache.jmeter.testelement.AbstractScopedTestElement;
29
import org.apache.jmeter.testelement.AbstractScopedTestElement;
Lines 62-68 public class JSONPostProcessor Link Here
62
    public void process() {
64
    public void process() {
63
        JMeterContext context = getThreadContext();
65
        JMeterContext context = getThreadContext();
64
        JMeterVariables vars = context.getVariables();
66
        JMeterVariables vars = context.getVariables();
65
        String jsonResponse = extractJsonResponse(context, vars);
67
        List<String> jsonResponses = extractJsonResponse(context, vars);
66
        String[] refNames = getRefNames().split(SEPARATOR);
68
        String[] refNames = getRefNames().split(SEPARATOR);
67
        String[] jsonPathExpressions = getJsonPathExpressions().split(SEPARATOR);
69
        String[] jsonPathExpressions = getJsonPathExpressions().split(SEPARATOR);
68
        String[] defaultValues = getDefaultValues().split(SEPARATOR);
70
        String[] defaultValues = getDefaultValues().split(SEPARATOR);
Lines 76-86 public class JSONPostProcessor Link Here
76
            String currentJsonPath = jsonPathExpressions[i].trim();
78
            String currentJsonPath = jsonPathExpressions[i].trim();
77
            clearOldRefVars(vars, currentRefName);
79
            clearOldRefVars(vars, currentRefName);
78
            try {
80
            try {
79
                if (StringUtils.isEmpty(jsonResponse)) {
81
                if (jsonResponses.isEmpty()) {
80
                    handleEmptyResponse(vars, defaultValues, i, currentRefName);
82
                    handleEmptyResponse(vars, defaultValues, i, currentRefName);
81
                } else {
83
                } else {
82
                    List<Object> extractedValues = localMatcher.get()
84
                    List<Object> extractedValues = new ArrayList<>();
83
                            .extractWithJsonPath(jsonResponse, currentJsonPath);
85
                    for (String jsonResponse: jsonResponses) {
86
                        extractedValues.addAll(localMatcher.get().extractWithJsonPath(jsonResponse, currentJsonPath));
87
                    }
84
                    // if no values extracted, default value added
88
                    // if no values extracted, default value added
85
                    if (extractedValues.isEmpty()) {
89
                    if (extractedValues.isEmpty()) {
86
                        handleEmptyResult(vars, defaultValues, i, matchNumber, currentRefName);
90
                        handleEmptyResult(vars, defaultValues, i, matchNumber, currentRefName);
Lines 198-220 public class JSONPostProcessor Link Here
198
        vars.put(currentRefName, defaultValues[i]);
202
        vars.put(currentRefName, defaultValues[i]);
199
    }
203
    }
200
204
201
    private String extractJsonResponse(JMeterContext context, JMeterVariables vars) {
205
    private List<String> extractJsonResponse(JMeterContext context, JMeterVariables vars) {
202
        String jsonResponse = "";
206
        String jsonResponse = "";
203
        if (isScopeVariable()) {
207
        if (isScopeVariable()) {
204
            jsonResponse = vars.get(getVariableName());
205
            if (log.isDebugEnabled()) {
208
            if (log.isDebugEnabled()) {
206
                log.debug("JSON Extractor is using variable: {}, which content is: {}", getVariableName(), jsonResponse);
209
                log.debug("JSON Extractor is using variable: {}, which content is: {}", getVariableName(), jsonResponse);
207
            }
210
            }
211
            return Arrays.asList(vars.get(getVariableName()));
208
        } else {
212
        } else {
209
            SampleResult previousResult = context.getPreviousResult();
213
            SampleResult previousResult = context.getPreviousResult();
210
            if (previousResult != null) {
214
            if (previousResult != null) {
211
                jsonResponse = previousResult.getResponseDataAsString();
215
                List<String> results = getSampleList(previousResult).stream()
216
                        .map(SampleResult::getResponseDataAsString)
217
                        .collect(Collectors.toList());
212
                if (log.isDebugEnabled()) {
218
                if (log.isDebugEnabled()) {
213
                    log.debug("JSON Extractor {} working on Response: {}", getName(), jsonResponse);
219
                    log.debug("JSON Extractor {} working on Responses: {}", getName(), results);
214
                }
220
                }
221
                return results;
215
            }
222
            }
216
        }
223
        }
217
        return jsonResponse;
224
        return Collections.emptyList();
218
    }
225
    }
219
226
220
    private void clearOldRefVars(JMeterVariables vars, String refName) {
227
    private void clearOldRefVars(JMeterVariables vars, String refName) {
221
- 

Return to bug 65269