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 br.ufrgs.inf.prosoft.requestssimulator.Session;
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 List<RequestPlan> getLinks;
private List<RequestPlan> postLinks;
private Collection<String> storeFields;
private Collection<RequestPlan> requirements;
private String headers;
private String forms;
private RequestPlan(String URL) {
this.method = "GET";
this.URL = URL;
this.data = null;
}
private RequestPlan(String URL, String data) {
this.method = "POST";
this.URL = URL;
this.data = data;
}
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 this.method;
}
public String getReference() {
return this.method + "@" + this.URL;
}
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<RequestPlan> links() {
if (this.getLinks == null && this.postLinks == null) {
return Stream.empty();
}
if (this.getLinks != null && this.postLinks != null) {
return Stream.concat(this.getLinks.stream(), this.postLinks.stream());
}
if (this.getLinks != null) {
return this.getLinks.stream();
}
return this.postLinks.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 addRequirement(RequestPlan requestPlan) {
if (this.requirements == null) {
this.requirements = new ArrayList<>();
}
this.requirements.add(requestPlan);
return this;
}
protected Stream<RequestPlan> requirements() {
if (this.requirements == null || this.requirements.isEmpty()) {
return Stream.empty();
}
return this.requirements.stream();
}
protected Stream<String> storeFields() {
if (this.storeFields == null || this.storeFields.isEmpty()) {
return Stream.empty();
}
return this.storeFields.stream();
}
public Request pickNextRequest(Session session) {
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(session);
}
public Request build(Session session) {
Random random = new Random();
int randomInt = random.nextInt() & Integer.MAX_VALUE;
if (this.URL == null || this.URL.isEmpty()) {
throw new RuntimeException("URL is empty");
}
String URL = this.URL.replace("$", String.valueOf(randomInt));
String headers = null;
if (this.headers != null) {
headers = this.headers.replace("$", String.valueOf(randomInt));
if (this.headers.contains("multipart")) {
String forms = this.forms;
return new MultipartRequest(this, session, URL, headers, forms);
}
}
if (this.method.equals("GET")) {
return new GetRequest(this, session, URL, headers);
}
String data = this.data.replace("$", String.valueOf(randomInt));;
return new PostRequest(this, session, URL, data, headers);
}
public Request bind(Request request, Session session) {
if (request instanceof GetRequest) {
return new GetRequest(this, session, request.getURL(), request.getHeaders());
}
if (request instanceof PostRequest) {
PostRequest postRequest = (PostRequest) request;
return new PostRequest(this, session, postRequest.getURL(), postRequest.getData(), postRequest.getHeaders());
}
MultipartRequest multipartRequest = (MultipartRequest) request;
return new MultipartRequest(this, session, multipartRequest.getURL(), multipartRequest.getHeaders(), multipartRequest.getForms());
}
@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);
}
}