Newer
Older
RestfulChecker / src / restfulchecker / RestChecker.java
package restfulchecker;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

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();

	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 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;
		}
		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());
			receives.add(account.doReceive());
			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");
		} 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();
		results.add(account.doReceive());
	}

	public void jsonAcquisition(String path) {
		ObjectMapper mapper = new ObjectMapper();
		try {
			BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
			String line = null;
			StringBuilder builder = new StringBuilder();
			while ((line = reader.readLine()) != null) {
				builder.append(line);
			}
			jstr = builder.toString();
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public NecessaryParam getGetnesParam() {
		return getnesParam;
	}

	public enum Method {
		GET, POST, PUT, DELETE
	};

}