DrZoidberg
DrZoidberg

Reputation: 31

How do I access a citrus http receive message body in Java?

I'm using cucumber and citrus together, and in my @Then definition, I have the citrus HTTP response:

@CitrusResource TestRunner runner;

runner.http(builder -> {
    final HttpClientResponseActionBuilder rab = 
        builder.client("citrusEndpointAPI").receive()
        .response(HttpStatus.OK).messageType(MessageType.JSON)
        .contentType(MediaType.APPLICATION_JSON_VALUE);

Is there a way to store the returned JSON message body into a java JSON object?

Upvotes: 2

Views: 1826

Answers (1)

Christoph Deppisch
Christoph Deppisch

Reputation: 2216

You can use the local message store. Each message should be saved to that local in memory storage during the test. You can access the stored messages later in that test case via its name:

receive(action -> action.endpoint("sampleEndpoint")
    .name("sampleMessage")
    .payload("..."));

echo("citrus:message(sampleMessage.payload())");

Please note that we named the received message sampleMessage. You can access the message store via test context in custom test actions, too.

context.getMessageStore().getMessage("sampleMessage");

Besides that you could also use a custom message validation callback in Java DSL. Here you have full access to the received message content.

receive(action -> action.endpoint("sampleEndpoint")
    .validationCallback((message, context) -> {
        //Do something with message content
        message.getPayload(String.class);
    }));

Upvotes: 2

Related Questions