Request.java

106 lines | 3.1 kB Blame History Raw Download
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrgs.inf.prosoft.requestssimulator.requests;

import br.ufrgs.inf.prosoft.requestssimulator.Session;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 *
 * @author romulo
 */
public abstract class Request {

    private final RequestPlan requestPlan;
    private final String URL;
    private final String headers;
    private final Session session;

    protected Request(RequestPlan requestPlan, Session session, String URL) {
        this.requestPlan = requestPlan;
        this.session = session;
        this.URL = URL;
        this.headers = null;
    }

    protected Request(RequestPlan requestPlan, Session session, String URL, String headers) {
        this.requestPlan = requestPlan;
        this.session = session;
        this.URL = URL;
        this.headers = headers;
    }

    public String getMethod() {
        return this.requestPlan.getMethod();
    }

    public String getURL() {
        return this.URL;
    }

    protected String getHeaders() {
        return this.headers;
    }

    public void forEachHeader(BiConsumer<? super String, ? super String> forEach) {
        Map<String, String> headers = new HashMap<>();
        if (this.headers != null) {
            String[] headerPairs = this.headers.split("; ");
            for (String headerPair : headerPairs) {
                String[] pair = headerPair.split(": ");
                headers.put(pair[0], pair[1]);
            }
        }
        headers.forEach(forEach);
    }

    public Request pickNextRequest(Session session) {
        return this.requestPlan.pickNextRequest(session);
    }
    
    protected void processResponse(String response) {
        this.requestPlan.forEachStoreField(new Consumer<String>() {

            private final JsonParser jsonParser;
            private final JsonObject jsonObject;

            {
                this.jsonParser = new JsonParser();
                this.jsonObject = jsonParser.parse(response).getAsJsonObject();
            }

            @Override
            public void accept(String storeField) {
                String storedValue = jsonObject.get(storeField).getAsString();
                storeValue(storeField, storedValue);
            }
        });
    }

    protected Request storeValue(String field, String value) {
        this.session.storeValue(field, value);
        return this;
    }


    protected String fillPropertyVariable(String property) {
        String filledProperty = property;
        if (filledProperty.contains("#")) {
            String storedField = filledProperty.substring(filledProperty.indexOf("#") + 1);
            String storedValue = this.session.getStoredValue(storedField);
            filledProperty = filledProperty.replace("#" + storedField, storedValue);
        }
        return filledProperty;
    }

    public abstract void fire();
}