use of org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor in project jmeter by apache.
the class TestJSONPostProcessor method testProcessAllElementsOneMatch.
@Test
public void testProcessAllElementsOneMatch() {
JMeterContext context = JMeterContextService.getContext();
JSONPostProcessor processor = setupProcessor(context, "-1", true);
JMeterVariables vars = new JMeterVariables();
processor.setDefaultValues("NONE");
processor.setJsonPathExpressions("$[*]");
processor.setRefNames("varname");
processor.setScopeVariable("contentvar");
context.setVariables(vars);
vars.put("contentvar", "[\"one\"]");
processor.process();
assertThat(vars.get("varname"), CoreMatchers.is(CoreMatchers.nullValue()));
assertThat(vars.get("varname_1"), CoreMatchers.is("one"));
assertThat(vars.get("varname_matchNr"), CoreMatchers.is("1"));
}
use of org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor in project jmeter by apache.
the class TestJSONPostProcessor method testPR235CaseMatchOneWithZero.
@Test
public void testPR235CaseMatchOneWithZero() {
JMeterContext context = JMeterContextService.getContext();
JSONPostProcessor processor = setupProcessor(context, "-1", true);
JMeterVariables vars = new JMeterVariables();
processor.setDefaultValues("NONE");
processor.setJsonPathExpressions("$[*]");
processor.setRefNames("varname");
processor.setScopeVariable("contentvar");
context.setVariables(vars);
vars.put("contentvar", "[\"one\", \"two\"]");
processor.process();
assertThat(vars.get("varname_1"), CoreMatchers.is("one"));
assertThat(vars.get("varname_2"), CoreMatchers.is("two"));
assertThat(vars.get("varname_matchNr"), CoreMatchers.is("2"));
vars.put("contentvar", "[\"A\", \"B\"]");
processor.setMatchNumbers("0");
processor.process();
assertThat(vars.get("varname"), CoreMatchers.is(CoreMatchers.anyOf(CoreMatchers.is("A"), CoreMatchers.is("B"))));
assertThat(vars.get("varname_matchNr"), CoreMatchers.is(CoreMatchers.nullValue()));
assertThat(vars.get("varname_1"), CoreMatchers.is(CoreMatchers.nullValue()));
assertThat(vars.get("varname_2"), CoreMatchers.is(CoreMatchers.nullValue()));
}
use of org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor in project jmeter by apache.
the class TestJSONPostProcessor method setupProcessor.
private JSONPostProcessor setupProcessor(JMeterContext context, String matchNumbers, boolean computeConcatenation) {
JSONPostProcessor processor = new JSONPostProcessor();
processor.setThreadContext(context);
processor.setRefNames(VAR_NAME);
processor.setMatchNumbers(matchNumbers);
processor.setComputeConcatenation(computeConcatenation);
return processor;
}
use of org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor in project jmeter by apache.
the class TestJSONPostProcessor method testExtractComplexElements.
@Test
public void testExtractComplexElements() {
JMeterContext context = JMeterContextService.getContext();
JSONPostProcessor processor = setupProcessor(context, "-1");
String data = "[{\"a\":[1,{\"d\":2},3]},[\"b\",{\"h\":23}],3]";
SampleResult result = new SampleResult();
result.setResponseData(data.getBytes(StandardCharsets.UTF_8));
JMeterVariables vars = new JMeterVariables();
context.setVariables(vars);
context.setPreviousResult(result);
processor.setJsonPathExpressions("$[*]");
processor.process();
String jsonWithoutOuterParens = data.substring(1, data.length() - 1);
Assert.assertEquals(jsonWithoutOuterParens, vars.get(VAR_NAME + "_ALL"));
Assert.assertEquals("{\"a\":[1,{\"d\":2},3]}", vars.get(VAR_NAME + "_1"));
Assert.assertEquals("[\"b\",{\"h\":23}]", vars.get(VAR_NAME + "_2"));
Assert.assertEquals("3", vars.get(VAR_NAME + "_3"));
Assert.assertEquals("3", vars.get(VAR_NAME + "_matchNr"));
}
use of org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor in project jmeter by apache.
the class TestJSONPostProcessor method testExtractSimpleArrayElements.
@Test
public void testExtractSimpleArrayElements() {
JMeterContext context = JMeterContextService.getContext();
JSONPostProcessor processor = setupProcessor(context, "-1");
String data = "[1,2,3]";
SampleResult result = new SampleResult();
result.setResponseData(data.getBytes(StandardCharsets.UTF_8));
JMeterVariables vars = new JMeterVariables();
context.setVariables(vars);
context.setPreviousResult(result);
processor.setJsonPathExpressions("$[*]");
processor.process();
Assert.assertEquals("1,2,3", vars.get(VAR_NAME + "_ALL"));
for (int i = 1; i <= 3; i++) {
String v = Integer.toString(i);
Assert.assertEquals(v, vars.get(VAR_NAME + "_" + v));
}
Assert.assertEquals("3", vars.get(VAR_NAME + "_matchNr"));
}
Aggregations