Newer
Older
RestfulChecker / src / restfulchecker / RestChecker.java
y-ota on 3 Dec 2018 3 KB testcaseをMainに追加
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 code;
	private String judgeResult;
	private String method;

	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 String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	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;
		// this.code = code;
	}

	public void doRestTest() {
		if (type == null) {
			System.out.println("not Selected Method");
			return;
		}
		RestAccount first = new RestAccount(accounts.get(0).getUrl());
		first.doGet();
		addParam(first);
		first.doAnything();
		beforeResults.add(first.doReceive());
		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.setCode(code);
			account.doAnything();
			responses.add(account.getResponseCode());
			receives.add(account.doReceive());
			RestAccount second = new RestAccount(accounts.get(0).getUrl());
			second.doGet();
			addParam(second);
			second.doAnything();
			results.add(second.doReceive());
			first = new RestAccount(accounts.get(0).getUrl());
			first.doGet();
			addParam(first);
			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) {
			judgeResult = "PUT or DELETE";
			// if (results.get(0).indexOf(receives.get(0)) == -1) {
			// judgeResult = "DELETE";
			// } else {
			// judgeResult = "PUT";
			// }
		} else {
			judgeResult = "POST";
		}
		if (judgeResult.indexOf(method) != -1) {
			System.out.println("OK");
		} else {
			System.out.println("WARNING!! Perhaps:" + judgeResult);
		}
	}

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

}