package restfulchecker; import java.io.File; import java.io.IOException; import java.util.ArrayList; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.TextNode; 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; private String method; private String jstr; private ArrayList<String> keys = new ArrayList<>(); private ArrayList<String> values = new ArrayList<>(); private NecessaryParam getnesParam = new NecessaryParam(); private ArrayList<String> removeParam = new ArrayList<>(); private String readJson; private boolean isRemove = false; public static final int ACCOUNT_NUM = 5; public void setProperty(String key, String value) { keys.add(key); values.add(value); } public void setAccount(String url) { for (int i = 0; i < ACCOUNT_NUM; i++) accounts.add(new RestAccount(url)); } public void setRemoveParam(String removeParam) { this.removeParam.add(removeParam); } public ArrayList<RestAccount> getAccounts() { return accounts; } public ArrayList<String> getResults() { return results; } public ArrayList<String> getBeforeResults() { return beforeResults; } public String getJudgeResult() { return judgeResult; } public NecessaryParam getGetnesParam() { return getnesParam; } public String getReadJson() { return readJson; } 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; } ReadResource(beforeResults); for (RestAccount account : accounts) { switch (type) { case GET: account.doGet(); method = "GET"; break; case POST: account.doPost(); method = "POST"; break; case PUT: account.doPut(); method = "PUT"; break; case DELETE: account.doDelete(); method = "DELETE"; break; } addParam(account); account.setJson(jstr); for (int i = 0; i < keys.size(); i++) { account.setRequestPropertie(keys.get(i), values.get(i)); } account.doAnything(); responses.add(account.getResponseCode()); String receive = account.doReceive(); for (String remove : removeParam) { jsonRemove(receive, remove); isRemove = true; } receives.add(receive); ReadResource(results); ReadResource(beforeResults); } 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) { judgeResult = "PUT or DELETE"; } else { judgeResult = "POST"; } if (judgeResult.indexOf(method) != -1) { System.out.println("OK"); if (isRemove) System.out.println("This is Quasi " + judgeResult); } else { System.out.println("WARNING!! Perhaps:" + judgeResult); } } private void ReadResource(ArrayList<String> results) { RestAccount account = new RestAccount(accounts.get(0).getUrl()); account.doGet(); if (getnesParam.getQueryParams() != null) for (RestParam param : getnesParam.getQueryParams()) { account.addQueryParam(param.key, param.value); } if (getnesParam.getPathParams() != null) for (String param : getnesParam.getPathParams()) { account.addPathParam(param); } account.doAnything(); String receive = account.doReceive(); for (String remove : removeParam) { receive = jsonRemove(receive, remove); isRemove = true; } results.add(receive); } public void jsonAcquisition(String path) { ObjectMapper json = new ObjectMapper(); try { readJson = json.readTree(new File(path)).toString(); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String jsonRemove(String JsonString, String fieldName) { try { JsonNode json = new ObjectMapper().readTree(JsonString); for(JsonNode js : json) { if(js.getClass() != TextNode.class) { ObjectNode obj = (ObjectNode) js; obj.remove(fieldName); }else { } } return json.toString(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public String jsonRemove(JsonNode Json, String fieldName) { ObjectNode obj = (ObjectNode) Json; obj.remove(fieldName); return obj.toString(); } public enum Method { GET, POST, PUT, DELETE }; }