Newer
Older
RestfulChecker / src / main / RestApi.java
y-ota on 6 Nov 2018 1 KB rest
package main;

import java.util.ArrayList;

public class RestApi {
	private ArrayList<RestAccount> accounts = new ArrayList<>();
	private ArrayList<String> results = 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 = 0;

	public void setAccount(String url) {
		for(int i=0;i<ACCOUNT_NUM;i++)accounts.add(new RestAccount(url));
	}

	public RestAccount getAccount(int num) {
		return accounts.get(num);
	}

	public String getResult(int num) {
		return results.get(num);
	}

	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)
			return;
		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();
			results.add(account.doReceive());
		}
		if(results.get(0).equals(results.get(1))) {
			judgeResult = "GET";
		}else {
			
		}
	}

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

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

}