citrus-framework

Citrus Simulator | Extract Payload


I am trying to apply condition based on the Xpath scenario, but unfortunately unable to make decision since I am unable to fetch the request payload.

RQ1- 
<HotelOperation>
 <Head Function="Search">
 </Head>
 <Form>
   <Search PropertyCode="Hotel1">
     ...
   </Search>
 </Form>
</HotelOperation>

RQ2- 
<HotelOperation>
 <Head Function="Rate">
 </Head>
 <Form>
   <RateSearch PropertyCode="Hotel1">
     ...
   </RateSearch>
 </Form>
</HotelOperation>

RQ3- 
<HotelOperation>
 <Head Function="Book">
 </Head>
 <Form>
   <Book PropertyCode="Hotel1">
     ...
   </Book>
 </Form>
</HotelOperation>


RQ4 -
<HotelOperation>
 <Head Function="Cancel">
 </Head>
 <Form>
   <Cancel PropertyCode="Hotel1">
     ...
   </Cancel>
 </Form>
</HotelOperation>

I want to retrieve the Xpath value for PropertyCode for each request and there will be more Xpaths.

//HotelOperation/Form/Search/@PropertyCode
//HotelOperation/Form/RateSearch/@PropertyCode
//HotelOperation/Form/Book/@PropertyCode
//HotelOperation/Form/Cancel/@PropertyCode

I have created only one simulator and want to serve the response for each request. The responses will be different for each request and also I need to fetch the values from the request and populate to the response.

package com.dhisco.learnings.simulator.scenario;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.xml.HasXPath.hasXPath;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.consol.citrus.context.TestContext;
import com.consol.citrus.endpoint.adapter.mapping.XPathPayloadMappingKeyExtractor;
import com.consol.citrus.message.Message;
import com.consol.citrus.simulator.scenario.AbstractSimulatorScenario;
import com.consol.citrus.simulator.scenario.Scenario;
import com.consol.citrus.simulator.scenario.ScenarioDesigner;
import com.consol.citrus.simulator.scenario.ScenarioRunner;
import com.dhisco.learnings.simulator.variables.UDOperations;

@Scenario("HotelOperationScenario")
@RequestMapping(value = "/services/rest/simulator/hoteloperation", method = RequestMethod.POST)
public class CtripUltraDirectScenario extends AbstractSimulatorScenario {

    @Override
    public void run(ScenarioRunner scenario) {
    }

    @Override
    public void run(ScenarioDesigner scenario) {
        scenario.correlation().start().withHandler(this);

        scenario.http().receive().post().
                .extractFromPayload("//HotelOperation/Form/Search/@PropertyCode", "propCode")
                .extractFromPayload("//HotelOperation/Head/@Function", "operation");

        scenario.conditional().when("${operation}", equalTo("Search"))                .actions(scenario.http().send().response(HttpStatus.OK).contentType(MediaType.APPLICATION_XML_VALUE)
                        .payload("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                        + "<HotelOperation>"
                        + "<Property Code=\"${propCode}\">"
                        + "</Property>" + "</HotelOperation>"));
        scenario.conditional().when("${operation}", equalTo("RateSearch"))              .actions(scenario.http().send().response(HttpStatus.OK).contentType(MediaType.APPLICATION_XML_VALUE)
                        .payload("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                        + "<HotelOperation>"
                        + "<Rate Code=\"${propCode}\">"
                        + "</Rate>" + "</HotelOperation>"));

    }

    @Override
    public boolean isHandlerFor(Message message, TestContext context) {
        return new XPathPayloadMappingKeyExtractor().getMappingKey(message).equals("HotelOperation");
    }
}


Is there a possibility to fetch the request payload from the simulator scenario or is there a way to apply conditional XPath for each operation (Search, Rate, Book, Cancel).


Solution

  • public void run(ScenarioRunner scenario) {
     DefaultMessageStore messageStore = (DefaultMessageStore) 
     scenario.getTestContext().getMessageStore();
     String rqXML = (String) messageStore.getMessage("receive(<scenario name>)").getPayload();
    }