Citrus framework: How to make a soap response / citrus variable / citrus function return result available to java

天涯浪子 提交于 2020-04-16 05:49:33

问题


I'm using Citrus 2.7.8 with Cucumber 2.4.0. I'm making a soap call and want to get the response and do some advanced parsing on it to validate a graphql response has matching values. (I understand how to do validations when it's something that just has one element, but I need something able to handle when there could be one or many elements returned (for example, 1 vehicle or 4 vehicles)). To make my validation very dynamic and able to handle many different 'quotes', I want to store the response to a Citrus variable and then make it available to java to read in the file and do the advanced parsing and validation.

The TestContext injection doesn't appear to currently work with cucumber (see https://github.com/citrusframework/citrus/issues/657) so I'm using the workaround here: How to inject TestContext using TestRunner and cucumber to manually create the context. Without this I get a nullpointerexception on anything with the context.

I am able to use Citrus's message function to grab the soap response which is awesome. My echo statements in the console show that it successfully put the right value into the citrus variable. But I'm having problems making that available to java so that I can then open it up and parse through it.

I've scaled down my step definition file to just the pertinent code. My couple attempts are listed below along with the problems I encountered in their results.

Does anyone have any ideas on how I can successfully workaround the context issues and make my response available to java?

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import com.consol.citrus.Citrus;
import com.consol.citrus.annotations.CitrusFramework;
import com.consol.citrus.annotations.CitrusResource;
import com.consol.citrus.config.CitrusSpringConfig;
import com.consol.citrus.context.TestContext;

import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;
import com.consol.citrus.dsl.runner.TestRunner;
import com.consol.citrus.ws.client.WebServiceClient;

import cucumber.api.java.en.When;

@ContextConfiguration(classes = CitrusSpringConfig.class)
public class CitrusSteps extends JUnit4CitrusTestRunner {
    @CitrusFramework
    private Citrus citrus;

    @CitrusResource
    private TestRunner runner;

    @CitrusResource
    private TestContext context;

    @Autowired
    private WebServiceClient getQuote;

    @When("^I call getQuote with id \"([^\"]*)\"$")
    public void i_call_getquote_with_id(String quoteId) throws Throwable {
        context = citrus.createTestContext();

        String soappayload = "my payload (taken out for privacy purposes)";

        runner.soap(action -> action.client(getQuote)
                .send()
                .soapAction("getQuote")
                .payload(soappayload));

        runner.soap(action -> action.client(getQuote)
        .receive()
        .name("getQuoteResponseStoredMessage"));        

        //this bombs out on the context line with this: "com.consol.citrus.exceptions.CitrusRuntimeException: Unknown variable 'messageStoreGetQuoteResponse1'"
        runner.variable("messageStoreGetQuoteResponse1", "citrus:message(getQuoteResponseStoredMessage.payload())");
        runner.echo("First try: ${messageStoreGetQuoteResponse1}");
        String firstTry = context.getVariable("messageStoreGetQuoteResponse1");
        log.info("First Try java variable: " + firstTry);

        //this bombs out on the context line with this: "com.consol.citrus.exceptions.CitrusRuntimeException: Unknown variable 'messageStoreGetQuoteResponse2'"
        runner.createVariable("messageStoreGetQuoteResponse2", "citrus:message(getQuoteResponseStoredMessage.payload())");
        runner.echo("Second try: ${messageStoreGetQuoteResponse2}");
        String secondTry = context.getVariable("messageStoreGetQuoteResponse2");
        log.info("Second Try java variable: " + secondTry);

        //This stores the literal as the value - it doesn't store the message so it appears I can't use citrus functions within the context
        context.setVariable("messageStoreGetQuoteResponse3", "citrus:message(getQuoteResponseStoredMessage.payload())");
        String thirdTry = context.getVariable("messageStoreGetQuoteResponse3");
        log.info("Third Try java variable: " + thirdTry);

    }

}

回答1:


A smart co-worker figured out a workaround for the injection not working w/ cucumber.

I replaced these two lines:

@CitrusResource
private TestContext context;

with these lines instead:

TestContext testContext;
public TestContext getTestContext() {
    if (testContext == null) {
        runner.run(new AbstractTestAction() {
            @Override
            public void doExecute(TestContext context) {
                testContext = context;
            }
        });
    }
    return testContext;
}

Then within my step where I want the context, I can use the above method. In my case I wanted my message response, so I was able to use this and confirm that the response is now in my java variable:

String responseXML = getTestContext().getMessageStore().getMessage("getQuoteResponseStoredMessage").getPayload(String.class);
log.info("Show response XML: " + responseXML);


来源:https://stackoverflow.com/questions/60539128/citrus-framework-how-to-make-a-soap-response-citrus-variable-citrus-functio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!