jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fschumac...@apache.org
Subject [jmeter] 04/04: Simplify method by extracting parts of code
Date Sat, 28 Sep 2019 10:18:41 GMT
This is an automated email from the ASF dual-hosted git repository.

fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git

commit 598eb15b8ccb8ea0b1c9416367572884724cc969
Author: Felix Schumacher <felix.schumacher@internetallee.de>
AuthorDate: Sat Sep 28 12:17:56 2019 +0200

    Simplify method by extracting parts of code
---
 .../extractor/json/jmespath/JMESPathExtractor.java | 122 ++++++++++++---------
 1 file changed, 70 insertions(+), 52 deletions(-)

diff --git a/src/components/src/main/java/org/apache/jmeter/extractor/json/jmespath/JMESPathExtractor.java
b/src/components/src/main/java/org/apache/jmeter/extractor/json/jmespath/JMESPathExtractor.java
index bdaa43c..5c411c6 100644
--- a/src/components/src/main/java/org/apache/jmeter/extractor/json/jmespath/JMESPathExtractor.java
+++ b/src/components/src/main/java/org/apache/jmeter/extractor/json/jmespath/JMESPathExtractor.java
@@ -68,69 +68,87 @@ public class JMESPathExtractor extends AbstractScopedTestElement
         final String jsonPathExpression = getJmesPathExpression().trim();
         clearOldRefVars(vars, refName);
         if (StringUtils.isEmpty(jsonResponse)) {
+            handleEmptyResponse(vars, refName, defaultValue);
+            return;
+        }
+
+        try {
+            JsonNode result = null;
+            JsonNode actualObj = OBJECT_MAPPER.readValue(jsonResponse, JsonNode.class);
+            result = JMESPathCache.getInstance().get(jsonPathExpression).search(actualObj);
+            if (result.isNull()) {
+                handleNullResult(vars, refName, defaultValue, matchNumber);
+                return;
+            }
+            List<String> resultList = splitJson(result);
+            // if more than one value extracted, suffix with "_index"
+            if (!resultList.isEmpty()) {
+                handleListResult(vars, refName, defaultValue, matchNumber, resultList);
+            } else {
+                // else just one value extracted
+                handleSingleResult(vars, refName, matchNumber, resultList);
+            }
+            vars.put(refName + REF_MATCH_NR, Integer.toString(resultList.size()));
+        } catch (Exception e) {
+            // if something wrong, default value added
             if (log.isDebugEnabled()) {
-                log.debug("Response or source variable is null or empty for {}", getName());
+                log.debug("Error processing JSON content in {}, message: {}", getName(),
e.getLocalizedMessage(), e);
+            } else {
+                log.debug("Error processing JSON content in {}, message: {}", getName(),
e.getLocalizedMessage());
             }
             vars.put(refName, defaultValue);
+        }
+    }
+
+    private void handleSingleResult(JMeterVariables vars, String refName, int matchNumber,
List<String> resultList) {
+        String suffix = (matchNumber < 0) ? "_1" : "";
+        placeObjectIntoVars(vars, refName + suffix, resultList, 0);
+    }
+
+    private void handleListResult(JMeterVariables vars, String refName, String defaultValue,
int matchNumber,
+            List<String> resultList) {
+        if (matchNumber < 0) {
+            // Extract all
+            int index = 1;
+            for (String extractedString : resultList) {
+                vars.put(refName + "_" + index, extractedString); // $NON-NLS-1$
+                index++;
+            }
+        } else if (matchNumber == 0) {
+            // Random extraction
+            int matchSize = resultList.size();
+            int matchNr = JMeterUtils.getRandomInt(matchSize);
+            placeObjectIntoVars(vars, refName, resultList, matchNr);
         } else {
-            try {
-                JsonNode result = null;
-                JsonNode actualObj = OBJECT_MAPPER.readValue(jsonResponse, JsonNode.class);
-                result = JMESPathCache.getInstance().get(jsonPathExpression).search(actualObj);
-                if (result.isNull()) {
-                    vars.put(refName, defaultValue);
-                    vars.put(refName + REF_MATCH_NR, "0"); //$NON-NLS-1$
-                    if (matchNumber < 0) {
-                        log.debug("No value extracted, storing empty in: {}", refName);
-                    }
-                } else {
-                    List<String> resultList = splitJson(result);
-                    // if more than one value extracted, suffix with "_index"
-                    if (resultList.size() > 1) {
-                        if (matchNumber < 0) {
-                            // Extract all
-                            int index = 1;
-                            for (String extractedString : resultList) {
-                                vars.put(refName + "_" + index, extractedString); // $NON-NLS-1$
-                                index++;
-                            }
-                        } else if (matchNumber == 0) {
-                            // Random extraction
-                            int matchSize = resultList.size();
-                            int matchNr = JMeterUtils.getRandomInt(matchSize);
-                            placeObjectIntoVars(vars, refName, resultList, matchNr);
-                        } else {
-                            // extract at position
-                            if (matchNumber > resultList.size()) {
-                                if (log.isDebugEnabled()) {
-                                    log.debug(
-                                            "matchNumber({}) exceeds number of items found({}),
default value will be used",
-                                            matchNumber, resultList.size());
-                                }
-                                vars.put(refName, defaultValue);
-                            } else {
-                                placeObjectIntoVars(vars, refName, resultList, matchNumber
- 1);
-                            }
-                        }
-                    } else {
-                        // else just one value extracted
-                        String suffix = (matchNumber < 0) ? "_1" : "";
-                        placeObjectIntoVars(vars, refName + suffix, resultList, 0);
-                    }
-                    vars.put(refName + REF_MATCH_NR, Integer.toString(resultList.size()));
-                }
-            } catch (Exception e) {
-                // if something wrong, default value added
+            // extract at position
+            if (matchNumber > resultList.size()) {
                 if (log.isDebugEnabled()) {
-                    log.debug("Error processing JSON content in {}, message: {}", getName(),
e.getLocalizedMessage(), e);
-                } else {
-                    log.debug("Error processing JSON content in {}, message: {}", getName(),
e.getLocalizedMessage());
+                    log.debug(
+                            "matchNumber({}) exceeds number of items found({}), default value
will be used",
+                            Integer.valueOf(matchNumber), Integer.valueOf(resultList.size()));
                 }
                 vars.put(refName, defaultValue);
+            } else {
+                placeObjectIntoVars(vars, refName, resultList, matchNumber - 1);
             }
         }
     }
 
+    private void handleNullResult(JMeterVariables vars, String refName, String defaultValue,
int matchNumber) {
+        vars.put(refName, defaultValue);
+        vars.put(refName + REF_MATCH_NR, "0"); //$NON-NLS-1$
+        if (matchNumber < 0) {
+            log.debug("No value extracted, storing empty in: {}", refName);
+        }
+    }
+
+    private void handleEmptyResponse(JMeterVariables vars, String refName, String defaultValue)
{
+        if (log.isDebugEnabled()) {
+            log.debug("Response or source variable is null or empty for {}", getName());
+        }
+        vars.put(refName, defaultValue);
+    }
+
     private String getData(JMeterVariables vars, JMeterContext context) {
         String jsonResponse = null;
         if (isScopeVariable()) {


Mime
View raw message