Details
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Profile.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Profile.java
index 3a2e8ae..69f2de8 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Profile.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Profile.java
@@ -27,8 +27,12 @@ public class Profile {
this.urlHasRequestPlan = urlHasRequestPlan;
Map<String, RequestPlan> rootsReferences = new HashMap<>(this.urlHasRequestPlan);
this.urlHasRequestPlan.values().forEach(requestPlan -> {
- requestPlan.linksReferences().forEach(linkReference -> {
- rootsReferences.remove(linkReference);
+ if (requestPlan.links().count() == 0) {
+ rootsReferences.remove(requestPlan.getReference());
+ return;
+ }
+ requestPlan.links().forEach(linkReference -> {
+ rootsReferences.remove(linkReference.getReference());
});
});
this.roots = rootsReferences.values();
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/ProfileReader.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/ProfileReader.java
index 676c68c..e7af81d 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/ProfileReader.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/ProfileReader.java
@@ -7,13 +7,18 @@ package br.ufrgs.inf.prosoft.requestssimulator;
import br.ufrgs.inf.prosoft.requestssimulator.requests.RequestPlan;
import com.google.gson.Gson;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
+import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
@@ -36,20 +41,45 @@ public class ProfileReader {
Gson gson = new Gson();
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(fileContent).getAsJsonObject();
- jsonObject.entrySet().forEach((entry) -> {
+ jsonObject.entrySet().forEach(entry -> {
RequestPlan requestPlan = gson.fromJson(entry.getValue(), RequestPlan.class);
urlHasRequestPlan.put(entry.getKey(), requestPlan);
});
- Map<String, RequestPlan> rootsReferences = new HashMap<>(urlHasRequestPlan);
- urlHasRequestPlan.values().forEach(requestPlan -> {
- requestPlan.linksReferences().forEach(linkReference -> {
- RequestPlan reference = urlHasRequestPlan.get(linkReference);
- if (reference == null) {
- throw new RuntimeException("link not declared");
- }
- requestPlan.addLink(reference);
- rootsReferences.remove(linkReference);
- });
+ jsonObject.entrySet().forEach(entry -> {
+ RequestPlan requestPlan = urlHasRequestPlan.get(entry.getKey());
+ Collection<String> linksReferences = new ArrayList<>();
+ JsonObject jsonRequestPlan = entry.getValue().getAsJsonObject();
+ JsonElement linksReferencesElement = jsonRequestPlan.get("linksReferences");
+ if (linksReferencesElement != null) {
+ linksReferencesElement.getAsJsonArray().forEach(array -> {
+ linksReferences.add(array.getAsString());
+ });
+ linksReferences.forEach(new Consumer<String>() {
+ @Override
+ public void accept(String linkReference) {
+ RequestPlan link;
+ if (linkReference.charAt(0) == '*') {
+ link = urlHasRequestPlan.get(linkReference.substring(1));
+ link.links().forEach(referenceToCopy -> {
+ accept(referenceToCopy.getReference());
+ });
+ } else {
+ link = urlHasRequestPlan.get(linkReference);
+ if (link == null) {
+ throw new RuntimeException("link not declared");
+ }
+ requestPlan.addLink(link);
+ }
+ }
+ });
+ }
+ JsonElement requirementsReferences = jsonRequestPlan.get("requirementsReferences");
+ if (requirementsReferences != null) {
+ requirementsReferences.getAsJsonArray().forEach(requirementReference -> {
+ RequestPlan link = urlHasRequestPlan.get(requirementReference.getAsString());
+ requestPlan.addRequirement(link);
+ });
+ }
});
Profile profile = new Profile(urlHasRequestPlan);
return profile;
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/DeleteRequest.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/DeleteRequest.java
new file mode 100644
index 0000000..00486d7
--- /dev/null
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/DeleteRequest.java
@@ -0,0 +1,68 @@
+/*
+ * 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.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+
+/**
+ *
+ * @author romulo
+ */
+public class DeleteRequest extends Request {
+
+ protected DeleteRequest(RequestPlan requestPlan, Session session, String URL) {
+ super(requestPlan, session, URL);
+ }
+
+ protected DeleteRequest(RequestPlan requestPlan, Session session, String URL, String headers) {
+ super(requestPlan, session, URL, headers);
+ }
+
+ @Override
+ public void fireRequest() {
+ HttpURLConnection httpURLConnection = null;
+ try {
+ String stringURL = fillPropertyVariable(getURL());
+ URL url = new URL(stringURL);
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ httpURLConnection = connection;
+ connection.setRequestMethod("DELETE");
+ headers().forEach(entry -> {
+ String value = fillPropertyVariable(entry.getValue());
+ connection.setRequestProperty(entry.getKey(), value);
+ });
+ int responseCode = connection.getResponseCode();
+ if (responseCode < 400) {
+ Logger.getGlobal().log(Level.INFO, "{0} on {1} {2}", new Object[]{responseCode, getMethod(), stringURL});
+ try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
+ String response = bufferedReader.lines().collect(Collectors.joining());
+ processResponse(response);
+ }
+ } else {
+ Logger.getGlobal().log(Level.SEVERE, "error {0} on {1} {2}", new Object[]{responseCode, getMethod(), stringURL});
+ }
+ } catch (MalformedURLException ex) {
+ Logger.getGlobal().log(Level.SEVERE, "Malormed URL");
+ } catch (IOException ex) {
+ Logger.getGlobal().log(Level.SEVERE, "IOException");
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ } finally {
+ if (httpURLConnection != null) {
+ httpURLConnection.disconnect();
+ }
+ }
+ }
+}
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/GetRequest.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/GetRequest.java
index ce1a787..48b47d8 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/GetRequest.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/GetRequest.java
@@ -31,26 +31,27 @@ public class GetRequest extends Request {
}
@Override
- public void fire() {
+ public void fireRequest() {
HttpURLConnection httpURLConnection = null;
try {
- URL url = new URL(getURL());
+ String stringURL = fillPropertyVariable(getURL());
+ URL url = new URL(stringURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
httpURLConnection = connection;
connection.setRequestMethod("GET");
- forEachHeader((key, value) -> {
- value = fillPropertyVariable(value);
- connection.setRequestProperty(key, value);
+ headers().forEach(entry -> {
+ String value = fillPropertyVariable(entry.getValue());
+ connection.setRequestProperty(entry.getKey(), value);
});
int responseCode = connection.getResponseCode();
if (responseCode < 400) {
- Logger.getGlobal().log(Level.INFO, "{0} {1}", new Object[]{getMethod(), getURL()});
+ Logger.getGlobal().log(Level.INFO, "{0} on {1} {2}", new Object[]{responseCode, getMethod(), stringURL});
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String response = bufferedReader.lines().collect(Collectors.joining());
processResponse(response);
}
} else {
- Logger.getGlobal().log(Level.SEVERE, "error {0} on {1} {2}", new Object[]{responseCode, getMethod(), getURL()});
+ Logger.getGlobal().log(Level.SEVERE, "error {0} on {1} {2}", new Object[]{responseCode, getMethod(), stringURL});
}
} catch (MalformedURLException ex) {
Logger.getGlobal().log(Level.SEVERE, "Malormed URL");
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/MultipartRequest.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/MultipartRequest.java
index 0118c71..ad1357e 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/MultipartRequest.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/MultipartRequest.java
@@ -12,10 +12,10 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
-import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
@@ -45,7 +45,7 @@ public class MultipartRequest extends Request {
return this.forms;
}
- public void forEachForm(BiConsumer<? super String, ? super String> forEach) {
+ public Stream<Map.Entry<String, String>> forms() {
Map<String, String> forms = new HashMap<>();
if (this.forms != null) {
String[] formsPairs = this.forms.split("; ");
@@ -54,38 +54,39 @@ public class MultipartRequest extends Request {
forms.put(pair[0], pair[1]);
}
}
- forms.forEach(forEach);
+ return forms.entrySet().stream();
}
@Override
- public void fire() {
+ public void fireRequest() {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
- HttpPost httpPost = new HttpPost(getURL());
+ String stringURL = fillPropertyVariable(getURL());
+ HttpPost httpPost = new HttpPost(stringURL);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
- forEachForm((key, value) -> {
- if (key.equals("file")) {
- File file = new File(value);
- multipartEntityBuilder.addBinaryBody(key, file);
+ forms().forEach(entry -> {
+ if (entry.getKey().equals("file")) {
+ File file = new File(entry.getValue());
+ multipartEntityBuilder.addBinaryBody(entry.getKey(), file);
} else {
- multipartEntityBuilder.addTextBody(key, value);
+ multipartEntityBuilder.addTextBody(entry.getKey(), entry.getValue());
}
});
HttpEntity httpEntity = multipartEntityBuilder.build();
httpPost.setEntity(httpEntity);
- forEachHeader((key, value) -> {
- value = fillPropertyVariable(value);
- httpPost.setHeader(key, value);
+ headers().forEach(entry -> {
+ String value = fillPropertyVariable(entry.getValue());
+ httpPost.setHeader(entry.getKey(), value);
});
try (CloseableHttpResponse closeableHttpResponse = httpClient.execute(httpPost)) {
int responseCode = closeableHttpResponse.getStatusLine().getStatusCode();
if (responseCode < 400) {
- Logger.getGlobal().log(Level.INFO, "{0} {1}", new Object[]{getMethod(), getURL()});
+ Logger.getGlobal().log(Level.INFO, "{0} on {1} {2}", new Object[]{responseCode, getMethod(), stringURL});
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(closeableHttpResponse.getEntity().getContent()))) {
String response = bufferedReader.lines().collect(Collectors.joining());
processResponse(response);
}
} else {
- Logger.getGlobal().log(Level.SEVERE, "error {0} on {1} {2}", new Object[]{responseCode, getMethod(), getURL()});
+ Logger.getGlobal().log(Level.SEVERE, "error {0} on {1} {2}", new Object[]{responseCode, getMethod(), stringURL});
}
}
} catch (IOException e) {
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/PostRequest.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/PostRequest.java
index 2af7b03..43bb371 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/PostRequest.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/PostRequest.java
@@ -40,16 +40,17 @@ public class PostRequest extends Request {
}
@Override
- public void fire() {
+ protected void fireRequest() {
HttpURLConnection httpURLConnection = null;
try {
- URL url = new URL(getURL());
+ String stringURL = fillPropertyVariable(getURL());
+ URL url = new URL(stringURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
httpURLConnection = connection;
connection.setRequestMethod("POST");
- forEachHeader((key, value) -> {
- value = fillPropertyVariable(value);
- connection.setRequestProperty(key, value);
+ headers().forEach(entry -> {
+ String value = fillPropertyVariable(entry.getValue());
+ connection.setRequestProperty(entry.getKey(), value);
});
connection.setDoOutput(true);
try (OutputStream outputStream = connection.getOutputStream()) {
@@ -57,13 +58,13 @@ public class PostRequest extends Request {
}
int responseCode = connection.getResponseCode();
if (responseCode < 400) {
- Logger.getGlobal().log(Level.INFO, "{0} {1}", new Object[]{getMethod(), getURL()});
+ Logger.getGlobal().log(Level.INFO, "{0} on {1} {2}", new Object[]{responseCode, getMethod(), stringURL});
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String response = bufferedReader.lines().collect(Collectors.joining());
processResponse(response);
}
} else {
- Logger.getGlobal().log(Level.SEVERE, "error {0} on {1} {2}", new Object[]{responseCode, getMethod(), getURL()});
+ Logger.getGlobal().log(Level.SEVERE, "error {0} on {1} {2}", new Object[]{responseCode, getMethod(), stringURL});
}
} catch (MalformedURLException ex) {
Logger.getGlobal().log(Level.SEVERE, "Malormed URL");
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/Request.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/Request.java
index 38ba8ec..c936610 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/Request.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/Request.java
@@ -11,10 +11,10 @@ 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;
import java.util.logging.Level;
import java.util.logging.Logger;
+import java.util.stream.Stream;
/**
*
@@ -53,7 +53,7 @@ public abstract class Request {
return this.headers;
}
- public void forEachHeader(BiConsumer<? super String, ? super String> forEach) {
+ public Stream<Map.Entry<String, String>> headers() {
Map<String, String> headers = new HashMap<>();
if (this.headers != null && !this.headers.isEmpty()) {
String[] headerPairs = this.headers.split("; ");
@@ -66,7 +66,7 @@ public abstract class Request {
}
}
}
- headers.forEach(forEach);
+ return headers.entrySet().stream();
}
public Request pickNextRequest(Session session) {
@@ -78,25 +78,36 @@ public abstract class Request {
Logger.getGlobal().log(Level.SEVERE, "empty response");
return;
}
- if (!this.requestPlan.storeFields()) {
- return;
- }
- this.requestPlan.forEachStoreField(new Consumer<String>() {
+ this.requestPlan.storeFields().forEach(new Consumer<String>() {
private final JsonParser jsonParser;
private final JsonObject jsonObject;
{
this.jsonParser = new JsonParser();
- this.jsonObject = this.jsonParser.parse(response).getAsJsonObject();
+ JsonElement jsonElement = this.jsonParser.parse(response);
+ if (jsonElement.isJsonObject()) {
+ this.jsonObject = this.jsonParser.parse(response).getAsJsonObject();
+ } else {
+ this.jsonObject = null;
+ }
}
@Override
public void accept(String storeField) {
- JsonElement jsonElement = this.jsonObject.get(storeField);
- if (jsonElement == null) {
- Logger.getGlobal().log(Level.SEVERE, "field {0} not present", storeField);
- return;
+ String[] storeSequence = storeField.split("#");
+ JsonObject jsonObject = this.jsonObject;
+ JsonElement jsonElement = null;
+
+ for (String storeLevel : storeSequence) {
+ jsonElement = jsonObject.get(storeLevel);
+ if (jsonElement == null) {
+ Logger.getGlobal().log(Level.SEVERE, "field {0} not present", storeField);
+ return;
+ }
+ if (jsonElement.isJsonObject()) {
+ jsonObject = jsonElement.getAsJsonObject();
+ }
}
String storedValue = jsonElement.getAsString();
storeValue(storeField, storedValue);
@@ -123,7 +134,18 @@ public abstract class Request {
return filledProperty;
}
- public abstract void fire();
+ public void fire() {
+ this.requestPlan.requirements().forEach(requirement -> {
+ boolean unfired = requirement.storeFields()
+ .anyMatch(field -> this.session.getStoredValue(field) == null);
+ if (unfired) {
+ requirement.build(this.session).fire();
+ }
+ });
+ fireRequest();
+ }
+
+ protected abstract void fireRequest();
public final JsonObject toJsonObject() {
JsonObject jsonObject = new JsonObject();
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/RequestPlan.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/RequestPlan.java
index 2b37d44..e36c1b6 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/RequestPlan.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/RequestPlan.java
@@ -11,7 +11,6 @@ import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Random;
-import java.util.function.Consumer;
import java.util.stream.Stream;
/**
@@ -23,10 +22,10 @@ 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 Collection<RequestPlan> requirements;
private String headers;
private String forms;
@@ -92,11 +91,17 @@ public class RequestPlan {
return this;
}
- public Stream<String> linksReferences() {
- if (this.linksReferences == null) {
+ public Stream<RequestPlan> links() {
+ if (this.getLinks == null && this.postLinks == null) {
return Stream.empty();
}
- return this.linksReferences.stream();
+ 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) {
@@ -119,12 +124,26 @@ public class RequestPlan {
return this;
}
- protected boolean storeFields() {
- return this.storeFields != null && !this.storeFields.isEmpty();
+ 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 void forEachStoreField(Consumer<? super String> forEach) {
- this.storeFields.forEach(forEach);
+ protected Stream<String> storeFields() {
+ if (this.storeFields == null || this.storeFields.isEmpty()) {
+ return Stream.empty();
+ }
+ return this.storeFields.stream();
}
public Request pickNextRequest(Session session) {
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Session.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Session.java
index 6a623c8..c7a363f 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Session.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Session.java
@@ -62,7 +62,12 @@ public class Session {
throw new RuntimeException("root within cycle");
}
if (this.roots.size() == 1) {
- return this.roots.stream().findAny().get().pickNextRequest(this);
+ RequestPlan requestPlan = this.roots.stream().findAny().get();
+ try {
+ return requestPlan.build(this);
+ } catch (RuntimeException ex) {
+ return requestPlan.pickNextRequest(this);
+ }
}
currentRequest = RequestPlan.get("root")
.addLinks(this.roots)