Search in sources :

Example 26 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext 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"));
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) JSONPostProcessor(org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor) Test(org.junit.Test)

Example 27 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext 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()));
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) JSONPostProcessor(org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor) Test(org.junit.Test)

Example 28 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext 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"));
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) JSONPostProcessor(org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor) SampleResult(org.apache.jmeter.samplers.SampleResult) Test(org.junit.Test)

Example 29 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class TestSwitchController method testFunction.

/*
         * N.B. Requires ApacheJMeter_functions.jar to be on the classpath,
         * otherwise the function cannot be resolved.
        */
@Test
public void testFunction() throws Exception {
    JMeterContext jmctx = JMeterContextService.getContext();
    Map<String, String> variables = new HashMap<>();
    ReplaceStringWithFunctions transformer = new ReplaceStringWithFunctions(new CompoundVariable(), variables);
    jmctx.setVariables(new JMeterVariables());
    JMeterVariables jmvars = jmctx.getVariables();
    jmvars.put("VAR", "100");
    StringProperty prop = new StringProperty(SwitchController.SWITCH_VALUE, "${__counter(TRUE,VAR)}");
    JMeterProperty newProp = transformer.transformValue(prop);
    newProp.setRunningVersion(true);
    GenericController controller = new GenericController();
    SwitchController switch_cont = new SwitchController();
    switch_cont.setProperty(newProp);
    controller.addTestElement(new TestSampler("before"));
    controller.addTestElement(switch_cont);
    switch_cont.addTestElement(new TestSampler("0"));
    switch_cont.addTestElement(new TestSampler("1"));
    switch_cont.addTestElement(new TestSampler("2"));
    switch_cont.addTestElement(new TestSampler("3"));
    controller.addTestElement(new TestSampler("after"));
    controller.initialize();
    assertEquals("100", jmvars.get("VAR"));
    for (int i = 1; i <= 3; i++) {
        assertEquals("Loop " + i, "before", nextName(controller));
        assertEquals("Loop " + i, "" + i, nextName(controller));
        assertEquals("Loop " + i, "" + i, jmvars.get("VAR"));
        assertEquals("Loop " + i, "after", nextName(controller));
        assertNull(nextName(controller));
    }
    int i = 4;
    assertEquals("Loop " + i, "before", nextName(controller));
    assertEquals("Loop " + i, "0", nextName(controller));
    assertEquals("Loop " + i, "" + i, jmvars.get("VAR"));
    assertEquals("Loop " + i, "after", nextName(controller));
    assertNull(nextName(controller));
    assertEquals("4", jmvars.get("VAR"));
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) ReplaceStringWithFunctions(org.apache.jmeter.engine.util.ReplaceStringWithFunctions) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) HashMap(java.util.HashMap) StringProperty(org.apache.jmeter.testelement.property.StringProperty) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Test(org.junit.Test)

Example 30 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext 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"));
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) JSONPostProcessor(org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor) SampleResult(org.apache.jmeter.samplers.SampleResult) Test(org.junit.Test)

Aggregations

JMeterContext (org.apache.jmeter.threads.JMeterContext)47 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)33 SampleResult (org.apache.jmeter.samplers.SampleResult)21 Test (org.junit.Test)11 Sampler (org.apache.jmeter.samplers.Sampler)9 JSONPostProcessor (org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor)8 Before (org.junit.Before)8 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)7 Properties (java.util.Properties)3 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)3 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)3 JMeterException (org.apache.jorphan.util.JMeterException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ReplaceStringWithFunctions (org.apache.jmeter.engine.util.ReplaceStringWithFunctions)2 HTTPSamplerBase (org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)2 StringProperty (org.apache.jmeter.testelement.property.StringProperty)2 BeanShellInterpreter (org.apache.jmeter.util.BeanShellInterpreter)2 MatchResult (org.apache.oro.text.regex.MatchResult)2