package restfulchecker;
import java.util.ArrayList;
public class RestChecker {
private ArrayList<RestAccount> accounts = new ArrayList<>();
private ArrayList<String> receives = new ArrayList<>();
private ArrayList<String> results = new ArrayList<>();
private ArrayList<String> beforeResults = new ArrayList<>();
private ArrayList<Integer> responses = new ArrayList<>();
private Method type;
private ArrayList<RestParam> queryParam;
private ArrayList<RestParam> formParam;
private ArrayList<String> pathParam;
private String judgeResult;
public static final int ACCOUNT_NUM = 5;
public void setAccount(String url) {
for (int i = 0; i < ACCOUNT_NUM; i++)
accounts.add(new RestAccount(url));
}
public ArrayList<RestAccount> getAccounts() {
return accounts;
}
public ArrayList<String> getResults() {
return results;
}
public ArrayList<String> getBeforeResults() {
return beforeResults;
}
public String getJudgeResult() {
return judgeResult;
}
public void setupRestTest(Method method, ArrayList<RestParam> queryParam, ArrayList<RestParam> formParam,
ArrayList<String> pathParam) {
this.type = method;
this.queryParam = queryParam;
this.formParam = formParam;
this.pathParam = pathParam;
}
public void doRestTest() {
if (type == null) {
System.out.println("not Selected Method");
return;
}
RestAccount first = new RestAccount(accounts.get(0).getUrl());
first.doGet();
first.doAnything();
beforeResults.add(first.doReceive());
for (RestAccount account : accounts) {
switch (type) {
case GET:
account.doGet();
break;
case POST:
account.doPost();
break;
case PUT:
account.doPut();
break;
case DELETE:
account.doDelete();
break;
}
addParam(account);
account.doAnything();
responses.add(account.getResponseCode());
receives.add(account.doReceive());
RestAccount second = new RestAccount(accounts.get(0).getUrl());
second.doGet();
second.doAnything();
results.add(second.doReceive());
first = new RestAccount(accounts.get(0).getUrl());
first.doGet();
first.doAnything();
beforeResults.add(first.doReceive());
}
judgeMethod();
}
private void addParam(RestAccount account) {
if (queryParam != null)
for (RestParam param : queryParam) {
account.addQueryParam(param.key, param.value);
}
if (formParam != null && type != Method.GET)
for (RestParam param : formParam) {
account.addFormParam(param.key, param.value);
}
if (pathParam != null)
for (String param : pathParam) {
account.addPathParam(param);
}
}
private void judgeMethod() {
ArrayList<Boolean> identically = new ArrayList<>();
for (int i = 0; i < results.size(); i++) {
boolean isResponse = false;
if (responses.get(i) / 100 == 2) {
isResponse = results.get(i).equals(beforeResults.get(i));
}
identically.add(isResponse);
}
boolean isSafety = identically.get(0);
boolean isIdempotency = false;
for (int i = 1; i < identically.size(); i++) {
isIdempotency = identically.get(i);
}
if (isSafety && isIdempotency) {
judgeResult = "GET";
} else if (!isSafety && isIdempotency) {
if (receives.get(0).indexOf(results.get(0)) == -1) {
judgeResult = "DELETE";
} else {
judgeResult = "PUT";
}
} else {
judgeResult = "POST";
}
}
public enum Method {
GET, POST, PUT, DELETE
};
}