use of org.apache.synapse.commons.emulator.http.dsl.dto.consumer.IncomingMessage in project wso2-synapse by wso2.
the class MockServiceCreator method routeThroughResourceMethod.
/**
* Start service for given parameters using emulator.
*
* @param resource mock service resource data
* @param emulator HttpConsumerContext emulator object
*/
private static void routeThroughResourceMethod(ServiceResource resource, HttpConsumerContext emulator) {
int serviceResponseStatusCode = resource.getStatusCode();
HttpResponseStatus responseStatus = HttpResponseStatus.valueOf(serviceResponseStatusCode);
String serviceMethod = resource.getMethod();
String serviceSubContext = resource.getSubContext();
Map.Entry<String, Map<String, String>> checkQueryParamEntry = splitQueryParams(serviceSubContext);
serviceSubContext = checkQueryParamEntry.getKey();
Map<String, String> queryParams = checkQueryParamEntry.getValue();
String serviceRequestPayload = "";
if (resource.getRequestPayload() != null) {
serviceRequestPayload = RequestProcessor.trimStrings(resource.getRequestPayload());
}
String serviceResponsePayload = resource.getResponsePayload();
List<Map.Entry<String, String>> requestHeaders = new ArrayList<>();
List<Map.Entry<String, String>> responseHeaders = new ArrayList<>();
if (resource.getRequestHeaders() != null) {
requestHeaders = resource.getRequestHeaders();
}
if (resource.getResponseHeaders() != null) {
responseHeaders = resource.getResponseHeaders();
}
switch(serviceMethod.toUpperCase()) {
case GET_METHOD:
// adding headers of request
IncomingMessage incomingMessage = request().withMethod(HttpMethod.GET).withPath(serviceSubContext);
for (Map.Entry<String, String> queryParam : queryParams.entrySet()) {
incomingMessage.withQueryParameter(queryParam.getKey(), queryParam.getValue());
}
for (Map.Entry<String, String> header : requestHeaders) {
incomingMessage.withHeader(header.getKey(), header.getValue());
}
emulator.when(incomingMessage);
// adding headers of response
OutgoingMessage outGoingMessage = response().withBody(serviceResponsePayload).withStatusCode(responseStatus);
for (Map.Entry<String, String> header : responseHeaders) {
outGoingMessage.withHeader(header.getKey(), header.getValue());
}
emulator.respond(outGoingMessage);
break;
case POST_METHOD:
// adding headers of request
incomingMessage = request().withMethod(HttpMethod.POST).withBody(serviceRequestPayload).withPath(serviceSubContext);
for (Map.Entry<String, String> queryParam : queryParams.entrySet()) {
incomingMessage.withQueryParameter(queryParam.getKey(), queryParam.getValue());
}
for (Map.Entry<String, String> header : requestHeaders) {
incomingMessage.withHeader(header.getKey(), header.getValue());
}
emulator.when(incomingMessage);
// adding headers of response
outGoingMessage = response().withBody(serviceResponsePayload).withStatusCode(responseStatus);
for (Map.Entry<String, String> header : responseHeaders) {
outGoingMessage.withHeader(header.getKey(), header.getValue());
}
emulator.respond(outGoingMessage);
break;
default:
// adding headers of request
incomingMessage = request().withMethod(HttpMethod.GET).withPath(serviceSubContext);
for (Map.Entry<String, String> queryParam : queryParams.entrySet()) {
incomingMessage.withQueryParameter(queryParam.getKey(), queryParam.getValue());
}
for (Map.Entry<String, String> header : requestHeaders) {
incomingMessage.withHeader(header.getKey(), header.getValue());
}
emulator.when(incomingMessage);
// adding headers of response
outGoingMessage = response().withBody(serviceResponsePayload).withStatusCode(responseStatus);
for (Map.Entry<String, String> header : responseHeaders) {
outGoingMessage.withHeader(header.getKey(), header.getValue());
}
emulator.respond(outGoingMessage);
break;
}
}
Aggregations