Newer
Older
RestfulChecker / src / restfulchecker / RestChecker.java
s1571024 on 9 Nov 2018 3 KB RestCheckerに変更
package restfulchecker;

import java.util.ArrayList;
import java.util.Iterator;

public class RestChecker {
	private ArrayList<RestAccount> accounts = 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());
			String receive = account.doReceive();
			results.add(receive);
			beforeResults.add(receive);
		}
		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<>();
		Iterator<String> current = beforeResults.iterator();
		Boolean first = null;
		boolean judge = false;
		for (String result : results) {
			String beforeResult = current.next();
			identically.add(result.equals(beforeResult));
		}
		for (boolean idente : identically) {
			if (first == null) {
				first = idente;
			} else {
				if (!idente) {
					judge = false;
				} else {
					judge = true;
				}
			}
		}
		if (first && judge) {
			judgeResult = "GET";
		} else if (!first && judge) {
			judgeResult = "PUT or DELETE";
		} else {
			judgeResult = "POST";
		}
	}

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

}