RequestPlan.java
Home
/
src /
main /
java /
br /
ufrgs /
inf /
prosoft /
requestssimulator /
requests /
RequestPlan.java
/*
* 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 java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Stream;
/**
*
* @author romulo
*/
public class RequestPlan {
private final String method;
private final String URL;
private final String data;
private Collection<String> linksReferences;
private List<RequestPlan> getLinks;
private List<RequestPlan> postLinks;
private Collection<String> storeFields;
private String contentType;
private String headers;
private String forms;
private RequestPlan(String URL) {
this.method = "GET";
this.URL = URL;
this.data = null;
resetFields();
}
private RequestPlan(String URL, String data) {
this.method = "POST";
this.URL = URL;
this.data = data;
resetFields();
}
private void resetFields() {
this.contentType = "application/json";
this.headers = "";
this.forms = "";
}
public static RequestPlan get(String URL) {
return new RequestPlan(URL);
}
public static RequestPlan post(String URL, String data) {
return new RequestPlan(URL, data);
}
public String getMethod() {
return method;
}
public RequestPlan addHeader(String header) {
if (this.headers == null || this.headers.isEmpty()) {
this.headers = header;
} else {
this.headers += "; " + header;
}
return this;
}
public RequestPlan setHeaders(String headers) {
this.headers = headers;
return this;
}
public RequestPlan addForm(String form) {
if (this.method.equals("GET")) {
throw new RuntimeException("Adding form to GET request");
}
if (this.forms == null) {
this.forms = form;
} else {
this.forms += "; " + form;
}
return this;
}
public RequestPlan setForms(String forms) {
if (this.method.equals("GET")) {
throw new RuntimeException("Adding form to GET request");
}
this.forms = forms;
return this;
}
public Stream<String> linksReferences() {
if (this.linksReferences == null) {
return Stream.empty();
}
return this.linksReferences.stream();
}
public RequestPlan addLink(RequestPlan requestPlan) {
if (requestPlan.getMethod().equals("GET")) {
if (this.getLinks == null) {
this.getLinks = new ArrayList<>();
}
this.getLinks.add(requestPlan);
} else {
if (this.postLinks == null) {
this.postLinks = new ArrayList<>();
}
this.postLinks.add(requestPlan);
}
return this;
}
public RequestPlan addLinks(Collection<RequestPlan> requestPlans) {
requestPlans.forEach(requestPlan -> addLink(requestPlan));
return this;
}
public RequestPlan setContentType(String contentType) {
this.contentType = contentType;
return this;
}
public Request pickNextRequest() {
Random random = new Random();
int probability = random.nextInt(100);
if ((this.getLinks == null || this.getLinks.isEmpty()) && (this.postLinks == null || this.postLinks.isEmpty())) {
throw new RuntimeException("GET and POST links empty");
}
RequestPlan chosen;
if (this.getLinks == null || this.getLinks.isEmpty()) {
probability = 100;
} else if (this.postLinks == null || this.postLinks.isEmpty()) {
probability = 0;
}
if (probability < 80) {
int chosenIndex = random.nextInt(this.getLinks.size());
chosen = this.getLinks.get(chosenIndex);
} else {
int chosenIndex = random.nextInt(this.postLinks.size());
chosen = this.postLinks.get(chosenIndex);
}
return chosen.build();
}
public Request build() {
Random random = new Random();
int randomInt = random.nextInt();
if (this.URL == null || this.URL.isEmpty()) {
throw new RuntimeException("URL is empty");
}
String URL = this.URL.replace("$", String.valueOf(randomInt));
String headers = this.headers;
if (this.contentType == null) {
this.contentType = "application/json";
}
if (this.contentType.startsWith("multipart")) {
String forms = this.forms;
return new MultipartRequest(this, URL, headers, forms);
}
if (this.method.equals("GET")) {
return new GetRequest(this, URL, headers);
}
String data = this.data;
return new PostRequest(this, URL, data, headers);
}
@Override
public int hashCode() {
return Objects.hash(this.method, this.URL);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof RequestPlan)) {
return false;
}
RequestPlan other = (RequestPlan) obj;
return this.method.equals(other.method) && this.URL.equals(other.URL);
}
}