@Test public void testProcess() { System.out.println("process"); JMeterContext context = JMeterContextService.getContext(); SampleResult res = new SampleResult(); res.setResponseData(json.getBytes()); context.setPreviousResult(res); JSONPathExtractor instance = new JSONPathExtractor(); instance.setDefaultValue("DEFAULT"); instance.setVar("test"); instance.setJsonPath("$.store.book[*].author"); instance.process(); JMeterVariables vars = context.getVariables(); assertEquals("[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]", vars.get("test")); }
@Override public void process() { JMeterContext context = getThreadContext(); JMeterVariables vars = context.getVariables(); SampleResult previousResult = context.getPreviousResult(); String responseData; if (getSubject().equals(SUBJECT_VARIABLE)) { responseData = vars.get(getSrcVariableName()); } else { responseData = previousResult.getResponseDataAsString(); } try { if (INPUT_YAML.equals(getInputFormat())) { responseData = YAMLToJSONConverter.convert(responseData); } Object jsonPathResult = JsonPath.read(responseData, getJsonPath()); if (jsonPathResult instanceof JSONArray) { Object[] arr = ((JSONArray) jsonPathResult).toArray(); if (arr.length == 0) { throw new PathNotFoundException("Extracted array is empty"); } vars.put(this.getVar(), objectToString(jsonPathResult)); vars.put(this.getVar() + "_matchNr", objectToString(arr.length)); int k = 1; while (vars.get(this.getVar() + "_" + k) != null) { vars.remove(this.getVar() + "_" + k); k++; } for (int n = 0; n < arr.length; n++) { vars.put(this.getVar() + "_" + (n + 1), objectToString(arr[n])); } } else { vars.put(this.getVar(), objectToString(jsonPathResult)); } } catch (Exception e) { log.debug("Extract failed", e); vars.put(this.getVar(), getDefaultValue()); vars.put(this.getVar() + "_matchNr", "0"); int k = 1; while (vars.get(this.getVar() + "_" + k) != null) { vars.remove(this.getVar() + "_" + k); k++; } } }
JSONPathExtractor extends AbstractTestElement implements PostProcessor { @Override public void process() { JMeterContext context = getThreadContext(); JMeterVariables vars = context.getVariables(); SampleResult previousResult = context.getPreviousResult(); String responseData; if (getSubject().equals(SUBJECT_VARIABLE)) { responseData = vars.get(getSrcVariableName()); } else { responseData = previousResult.getResponseDataAsString(); } try { if (INPUT_YAML.equals(getInputFormat())) { responseData = YAMLToJSONConverter.convert(responseData); } Object jsonPathResult = JsonPath.read(responseData, getJsonPath()); if (jsonPathResult instanceof JSONArray) { Object[] arr = ((JSONArray) jsonPathResult).toArray(); if (arr.length == 0) { throw new PathNotFoundException("Extracted array is empty"); } vars.put(this.getVar(), objectToString(jsonPathResult)); vars.put(this.getVar() + "_matchNr", objectToString(arr.length)); int k = 1; while (vars.get(this.getVar() + "_" + k) != null) { vars.remove(this.getVar() + "_" + k); k++; } for (int n = 0; n < arr.length; n++) { vars.put(this.getVar() + "_" + (n + 1), objectToString(arr[n])); } } else { vars.put(this.getVar(), objectToString(jsonPathResult)); } } catch (Exception e) { log.debug("Extract failed", e); vars.put(this.getVar(), getDefaultValue()); vars.put(this.getVar() + "_matchNr", "0"); int k = 1; while (vars.get(this.getVar() + "_" + k) != null) { vars.remove(this.getVar() + "_" + k); k++; } } } }
JSONPathExtractor extends AbstractTestElement implements PostProcessor { @Override public void process() { JMeterContext context = getThreadContext(); JMeterVariables vars = context.getVariables(); SampleResult previousResult = context.getPreviousResult(); String responseData; if (getSubject().equals(SUBJECT_VARIABLE)) { responseData = vars.get(getSrcVariableName()); } else { responseData = previousResult.getResponseDataAsString(); } try { if (INPUT_YAML.equals(getInputFormat())) { responseData = YAMLToJSONConverter.convert(responseData); } Object jsonPathResult = JsonPath.read(responseData, getJsonPath()); if (jsonPathResult instanceof JSONArray) { Object[] arr = ((JSONArray) jsonPathResult).toArray(); if (arr.length == 0) { throw new PathNotFoundException("Extracted array is empty"); } vars.put(this.getVar(), objectToString(jsonPathResult)); vars.put(this.getVar() + "_matchNr", objectToString(arr.length)); int k = 1; while (vars.get(this.getVar() + "_" + k) != null) { vars.remove(this.getVar() + "_" + k); k++; } for (int n = 0; n < arr.length; n++) { vars.put(this.getVar() + "_" + (n + 1), objectToString(arr[n])); } } else { vars.put(this.getVar(), objectToString(jsonPathResult)); } } catch (Exception e) { log.debug("Extract failed", e); vars.put(this.getVar(), getDefaultValue()); vars.put(this.getVar() + "_matchNr", "0"); int k = 1; while (vars.get(this.getVar() + "_" + k) != null) { vars.remove(this.getVar() + "_" + k); k++; } } } JSONPathExtractor(); }
JSONPathExtractor extends AbstractTestElement implements PostProcessor { @Override public void process() { JMeterContext context = getThreadContext(); JMeterVariables vars = context.getVariables(); SampleResult previousResult = context.getPreviousResult(); String responseData; if (getSubject().equals(SUBJECT_VARIABLE)) { responseData = vars.get(getSrcVariableName()); } else { responseData = previousResult.getResponseDataAsString(); } try { if (INPUT_YAML.equals(getInputFormat())) { responseData = YAMLToJSONConverter.convert(responseData); } Object jsonPathResult = JsonPath.read(responseData, getJsonPath()); if (jsonPathResult instanceof JSONArray) { Object[] arr = ((JSONArray) jsonPathResult).toArray(); if (arr.length == 0) { throw new PathNotFoundException("Extracted array is empty"); } vars.put(this.getVar(), objectToString(jsonPathResult)); vars.put(this.getVar() + "_matchNr", objectToString(arr.length)); int k = 1; while (vars.get(this.getVar() + "_" + k) != null) { vars.remove(this.getVar() + "_" + k); k++; } for (int n = 0; n < arr.length; n++) { vars.put(this.getVar() + "_" + (n + 1), objectToString(arr[n])); } } else { vars.put(this.getVar(), objectToString(jsonPathResult)); } } catch (Exception e) { log.debug("Extract failed", e); vars.put(this.getVar(), getDefaultValue()); vars.put(this.getVar() + "_matchNr", "0"); int k = 1; while (vars.get(this.getVar() + "_" + k) != null) { vars.remove(this.getVar() + "_" + k); k++; } } } JSONPathExtractor(); void setInputFormat(String inputFormat); String getInputFormat(); String getJsonPath(); void setJsonPath(String jsonPath); String getVar(); void setVar(String var); void setDefaultValue(String defaultValue); String getDefaultValue(); void setSrcVariableName(String defaultValue); String getSrcVariableName(); void setSubject(String defaultValue); String getSubject(); @Override void process(); static String objectToString(Object subj); }
JSONPathExtractor extends AbstractTestElement implements PostProcessor { @Override public void process() { JMeterContext context = getThreadContext(); JMeterVariables vars = context.getVariables(); SampleResult previousResult = context.getPreviousResult(); String responseData; if (getSubject().equals(SUBJECT_VARIABLE)) { responseData = vars.get(getSrcVariableName()); } else { responseData = previousResult.getResponseDataAsString(); } try { if (INPUT_YAML.equals(getInputFormat())) { responseData = YAMLToJSONConverter.convert(responseData); } Object jsonPathResult = JsonPath.read(responseData, getJsonPath()); if (jsonPathResult instanceof JSONArray) { Object[] arr = ((JSONArray) jsonPathResult).toArray(); if (arr.length == 0) { throw new PathNotFoundException("Extracted array is empty"); } vars.put(this.getVar(), objectToString(jsonPathResult)); vars.put(this.getVar() + "_matchNr", objectToString(arr.length)); int k = 1; while (vars.get(this.getVar() + "_" + k) != null) { vars.remove(this.getVar() + "_" + k); k++; } for (int n = 0; n < arr.length; n++) { vars.put(this.getVar() + "_" + (n + 1), objectToString(arr[n])); } } else { vars.put(this.getVar(), objectToString(jsonPathResult)); } } catch (Exception e) { log.debug("Extract failed", e); vars.put(this.getVar(), getDefaultValue()); vars.put(this.getVar() + "_matchNr", "0"); int k = 1; while (vars.get(this.getVar() + "_" + k) != null) { vars.remove(this.getVar() + "_" + k); k++; } } } JSONPathExtractor(); void setInputFormat(String inputFormat); String getInputFormat(); String getJsonPath(); void setJsonPath(String jsonPath); String getVar(); void setVar(String var); void setDefaultValue(String defaultValue); String getDefaultValue(); void setSrcVariableName(String defaultValue); String getSrcVariableName(); void setSubject(String defaultValue); String getSubject(); @Override void process(); static String objectToString(Object subj); static final String INPUT_JSON; static final String INPUT_YAML; static final String INPUT_FORMAT; static final String JSONPATH; static final String VAR; static final String DEFAULT; static final String SUBJECT; static final String SRC_VARNAME; static final String SUBJECT_BODY; static final String SUBJECT_VARIABLE; static final DecimalFormat decimalFormatter; }