Ambuj Jauhari
Ambuj Jauhari

Reputation: 1299

Extracting post variable in citrus simulator

I am trying out citrus simulator and trying out a rest example. Below is the code for simulator and rest client. Key thing here is i am not using @CitrusTest annotation.

@Scenario("MyRestServiceScenario")
@RequestMapping(value = "/services/rest/simulator/hello", method = RequestMethod.POST)
public class MyRestServiceSimulator extends AbstractSimulatorScenario {
    @Override
    public void run(ScenarioDesigner scenario) {

        scenario
                .http()
                .receive()
                .post()
                .payload("{\"Hello\":[\"JS01\"]}")
                .extractFromPayload("$Hello", "greeting").getActor().;

        //String gr = scenario.getTestContext().getVariable("greeting");
        scenario.echo("Received greeting: ${greeting}");

        scenario
                .http()
                .send()
                .response(HttpStatus.OK)
                .payload("<HelloResponse xmlns=\"http://citrusframework.org/schemas/hello\">" +
                        "Hi there!" +
                        "</HelloResponse>");
    }
}

Main Class

public class CitrusRest {
    public static void main(String... args) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        RestTemplate rt = new RestTemplate();

        MultiValueMap<String, String>  vars = new LinkedMultiValueMap<String, String>();
        vars.add("Hello", "JS01");

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(vars, headers);
        String uri = "http://" + "localhost" + ":8080//services/rest/simulator/hello";

        rt.postForEntity(uri, request, String.class);
    }
}

In debugger mode i do see the payload arriving in run method and i am expecting that post .extractFromPayload("$Hello", "greeting"). I should receive JS01 in greeting variable. but the variable is never initialized.

Can someone please help here ?

Upvotes: 0

Views: 661

Answers (1)

Christoph Deppisch
Christoph Deppisch

Reputation: 2216

$Hello is not a valid JsonPath expression. You should also see some error for that in the logs:

com.jayway.jsonpath.InvalidPathException: Illegal character at position 1 expected '.' or '[

The correct expression would be $.Hello

Upvotes: 1

Related Questions