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.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; 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 ArrayList<String> keys = new ArrayList<>(); private ArrayList<String> values = new ArrayList<>(); private ResourceParam resourceParam = new ResourceParam(); private ArrayList<RemoveJsonParam> removeParam = new ArrayList<>(); private String readJson; private boolean isRemove = false; public static final int ACCOUNT_NUM = 3; 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(RemoveJsonParam 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 ResourceParam getResourceParam() { return resourceParam; } 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(readJson); 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 (RemoveJsonParam remove : removeParam) { if (receive != null) { jsonRemove(receive, remove); isRemove = true; } } receives.add(receive); ReadResource(results); if (accounts.iterator().hasNext()) 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<>(); boolean isResponse = true; for (int res : responses) { if (res / 100 != 2) isResponse = false; } for (int i = 0; i < results.size(); i++) { identically.add(results.get(i).equals(beforeResults.get(i))); } 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 (!isResponse && type == Method.POST) { judgeResult = "POST"; } else { judgeResult = "PUT or DELETE"; } } else { judgeResult = "POST"; } if (judgeResult.indexOf(method) != -1) { System.out.println("OK"); if (isRemove) { System.out.println("It`s Quasi " + judgeResult); } if (!isResponse && type == Method.POST) { System.out.println("It`s POST Once Exactry"); } } else { System.out.println("WARNING!! Possibly:" + judgeResult); } } private void ReadResource(ArrayList<String> results) { RestAccount account = new RestAccount(accounts.get(0).getUrl()); account.doGet(); if (resourceParam.getQueryParams() != null) for (RestParam param : resourceParam.getQueryParams()) { account.addQueryParam(param.key, param.value); } if (resourceParam.getPathParams() != null) for (String param : resourceParam.getPathParams()) { account.addPathParam(param); } for (int i = 0; i < resourceParam.getKeys().size(); i++) { account.setRequestPropertie(resourceParam.getKeys().get(i), resourceParam.getValues().get(i)); } account.doAnything(); String receive = account.doReceive(); for (RemoveJsonParam 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, RemoveJsonParam param) { try { JsonNode json = new ObjectMapper().readTree(JsonString); if (json == null) { return ""; } if (json.getClass() != ArrayNode.class) { jsonRemove(json, param); } else { for (JsonNode js : json) { removeParam(js, param); } } return json.toString(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public String jsonRemove(JsonNode Json, RemoveJsonParam param) { JsonNode current = Json; for (String node : param.getNode()) { current = current.get(node); } ObjectNode obj = (ObjectNode) current; obj.remove(param.getRemoveParam()); return Json.toString(); } private void removeParam(JsonNode json, RemoveJsonParam param) { JsonNode current = json; for (String node : param.getNode()) { current = json.get(node); } ObjectNode obj = (ObjectNode) current; obj.remove(param.getRemoveParam()); } public enum Method { GET, POST, PUT, DELETE }; }