All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.visionarts.powerjambda.testing.ActionRequestBuilder Maven / Gradle / Ivy

/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.visionarts.powerjambda.testing;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.HashMap;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.visionarts.powerjambda.ApiGatewayRequestContext;
import com.visionarts.powerjambda.AwsProxyRequest;
import com.visionarts.powerjambda.http.HttpMethod;
import com.visionarts.powerjambda.models.ActionRequest;
import com.visionarts.powerjambda.utils.Utils;

/**
 *  for unittest
 *
 */
public class ActionRequestBuilder {

    private AwsProxyRequest request;
    private T body;

    private final ObjectMapper om = Utils.getObjectMapper();

    public ActionRequestBuilder() {
        request = new AwsProxyRequest();
        request.setHeaders(new HashMap<>());
        request.setPathParameters(new HashMap<>());
        request.setQueryStringParameters(new HashMap<>());
        request.setStageVariables(new HashMap<>());
        request.setRequestContext(new ApiGatewayRequestContext());
    }

    public ActionRequestBuilder(AwsProxyRequest request) {
        this.request = request;
    }

    public ActionRequestBuilder method(HttpMethod method) {
        request.setHttpMethod(method.name());
        return this;
    }

    public ActionRequestBuilder stage(String stage) {
        request.getRequestContext().setStage(stage);
        return this;
    }

    public ActionRequestBuilder path(String path) {
        request.setPath(path);
        return this;
    }

    public ActionRequestBuilder header(String key, String value) {
        request.getHeaders().put(key, value);
        return this;
    }

    public ActionRequestBuilder pathParameter(String key, String value) {
        request.getPathParameters().put(key, value);
        return this;
    }

    public ActionRequestBuilder queryStringParameter(String key, String value) {
        request.getQueryStringParameters().put(key, value);
        return this;
    }

    public ActionRequestBuilder stageVariable(String key, String value) {
        request.getStageVariables().put(key, value);
        return this;
    }

    public ActionRequestBuilder bodyAsString(String body, Class bodyClazz) {
        try {
            this.body = om.readValue(body, bodyClazz);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
        request.setBody(body);
        return this;
    }

    public ActionRequestBuilder body(T body) {
        this.body = body;
        return this;
    }

    public ActionRequest build() {
        ActionRequest newRequest = new ActionRequest<>(request);
        newRequest.setBody(body);
        return newRequest;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy