Search in sources :

Example 1 with OutgoingMessage

use of org.apache.synapse.commons.emulator.http.dsl.dto.consumer.OutgoingMessage 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;
    }
}
Also used : OutgoingMessage(org.apache.synapse.commons.emulator.http.dsl.dto.consumer.OutgoingMessage) IncomingMessage(org.apache.synapse.commons.emulator.http.dsl.dto.consumer.IncomingMessage) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Aggregations

HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 IncomingMessage (org.apache.synapse.commons.emulator.http.dsl.dto.consumer.IncomingMessage)1 OutgoingMessage (org.apache.synapse.commons.emulator.http.dsl.dto.consumer.OutgoingMessage)1