柑橘模拟器 |提取有效载荷
Citrus Simulator | Extract Payload
我正在尝试应用基于 Xpath 场景的条件,但遗憾的是无法做出决定,因为我无法获取请求负载。
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>
我想为每个请求检索 PropertyCode 的 Xpath 值,并且会有更多的 Xpath。
//HotelOperation/Form/Search/@PropertyCode
//HotelOperation/Form/RateSearch/@PropertyCode
//HotelOperation/Form/Book/@PropertyCode
//HotelOperation/Form/Cancel/@PropertyCode
我只创建了一个模拟器,想为每个请求提供响应。每个请求的响应都不同,我还需要从请求中获取值并填充到响应中。
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");
}
}
是否有可能从模拟器场景中获取请求负载,或者是否有办法为每个操作(搜索、评分、预订、取消)应用条件 XPath。
public void run(ScenarioRunner scenario) {
DefaultMessageStore messageStore = (DefaultMessageStore)
scenario.getTestContext().getMessageStore();
String rqXML = (String) messageStore.getMessage("receive(<scenario name>)").getPayload();
}
我正在尝试应用基于 Xpath 场景的条件,但遗憾的是无法做出决定,因为我无法获取请求负载。
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>
我想为每个请求检索 PropertyCode 的 Xpath 值,并且会有更多的 Xpath。
//HotelOperation/Form/Search/@PropertyCode
//HotelOperation/Form/RateSearch/@PropertyCode
//HotelOperation/Form/Book/@PropertyCode
//HotelOperation/Form/Cancel/@PropertyCode
我只创建了一个模拟器,想为每个请求提供响应。每个请求的响应都不同,我还需要从请求中获取值并填充到响应中。
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");
}
}
是否有可能从模拟器场景中获取请求负载,或者是否有办法为每个操作(搜索、评分、预订、取消)应用条件 XPath。
public void run(ScenarioRunner scenario) {
DefaultMessageStore messageStore = (DefaultMessageStore)
scenario.getTestContext().getMessageStore();
String rqXML = (String) messageStore.getMessage("receive(<scenario name>)").getPayload();
}