介面自動化通用驗證類
- 2019 年 10 月 8 日
- 筆記
在做自動化的過程中,一定會遇到很多驗證的點,但是有些驗證功能是通用的,所以我封裝了一個通用的驗證類,來解決重複驗證的問題,之前也寫過一個,現在這個增加了一下數組的驗證,還有一些隱藏bug的修復。話不多說,分享程式碼,供大家參考:
package com.fun.frame; import com.fun.base.bean.RequestInfo; import com.fun.base.interfaces.IBase; import com.fun.frame.httpclient.FanLibrary; import com.fun.utils.Regex; import net.sf.json.JSONException; import net.sf.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * 通用驗證方法封裝 */ public class Verify extends SourceCode { private static Logger logger = LoggerFactory.getLogger(Verify.class); /** * 斷言的json對象 */ private JSONObject verifyJson; /** * 斷言的code碼 */ private int code; /** * 斷言的json對象分行解析 */ private List<String> lines = new ArrayList<>(); public Verify(JSONObject jsonObject) { this.verifyJson = jsonObject; this.lines = parseJsonLines(jsonObject); } /** * 獲取 code *<p>這裡的requestinfo主要的目的是為了攔截一些不必要的checkcode驗證的,主要有black_host名單提供,在使用時,注意requestinfo的非空校驗</p> * * @return */ public int getCode() { return FanLibrary.getiBase().checkCode(verifyJson, null); } /** * 校驗code碼是否正確,==0 * * @return */ public boolean isRight() { return 0 == this.getCode(); } /** * 獲取節點值 * * @param key 節點 * @return 返回節點值 */ public String getValue(String key) { int size = lines.size(); for (int i = 0; i < size; i++) { String line = lines.get(i); if (line.startsWith(key + ":")) return line.replaceFirst(key + ":", EMPTY); } return EMPTY; } /** * 校驗是否包含文本 * * @param text 需要校驗的文本 * @return 返回 Boolean 值 */ public boolean isContains(String... text) { boolean result = true; String content = verifyJson.toString(); int length = text.length; for (int i = 0; i < length; i++) { if (!result) break; result = content.contains(text[i]) & result; } return result; } /** * 校驗節點值為數字 * * @param value 節點名 * @return 返回 Boolean 值 */ public boolean isNum(String... value) { boolean result = true; int length = value.length; for (int i = 0; i < length; i++) { String key = value[i] + ":"; if (!verifyJson.toString().contains(value[i]) || !result) return false; for (int k = 0; k < lines.size(); k++) { String line = lines.get(k); if (line.startsWith(key)) { String lineValue = line.replaceFirst(key, EMPTY); result = isNumber(lineValue) & result; } } } return result; } /** * 校驗節點值不為空 * * @param keys 節點名 * @return 返回 Boolean 值,為空返回false,不為空返回true */ public boolean notNull(String... keys) { boolean result = true; for (int i = 0; i < keys.length; i++) { String key = keys[i] + ":"; if (!verifyJson.toString().contains(keys[i]) || !result) return false; for (int k = 0; k < lines.size(); k++) { String line = lines.get(k); if (line.startsWith(key)) { String lineValue = line.replaceFirst(key, EMPTY); result = lineValue != null & !lineValue.isEmpty() & result; } } } return result; } /** * 驗證是否為列表,根據欄位後面的符號是否是[ * * @param key 返回體的欄位值 * @return */ public boolean isArray(String key) { String json = verifyJson.toString(); int index = json.indexOf(key); char a = json.charAt(index + key.length() + 2); return a == '['; } /** * 驗證是否是json,根據後面跟的符號是否是{ * * @param key 返回體的欄位值 * @return */ public boolean isJson(String key) { String json = verifyJson.toString(); int index = json.indexOf(key); char a = json.charAt(index + key.length() + 2); if (a == '{') return true; return false; } /** * 是否是Boolean值 * * @return */ public boolean isBoolean(String... value) { boolean result = true; int length = value.length; for (int i = 0; i < length; i++) { String key = value[i] + ":"; if (!verifyJson.toString().contains(value[i]) || !result) return false; for (int k = 0; k < lines.size(); k++) { String line = lines.get(k); if (line.startsWith(key)) { String lineValue = line.replaceFirst(key, EMPTY); result = Regex.isRegex(lineValue, "^(false)|(true)$") & result; } } } return result; } /** * 驗證正則匹配結果 * * @param regex * @return */ public boolean isRegex(String regex) { String text = verifyJson.toString(); return Regex.isRegex(text, regex); } /** * 解析json資訊 * * @param response json格式的響應實體 * @return json每個欄位和值,key:value形式 */ public static List<String> parseJsonLines(JSONObject response) { String jsonStr = response.toString();// 先將json對象轉化為string對象 jsonStr = jsonStr.replaceAll(",", LINE); jsonStr = jsonStr.replaceAll(""", EMPTY); jsonStr = jsonStr.replaceAll("\\/", OR); jsonStr = jsonStr.replaceAll("\{", LINE); jsonStr = jsonStr.replaceAll("\[", LINE); jsonStr = jsonStr.replaceAll("}", LINE); jsonStr = jsonStr.replaceAll("]", LINE); List<String> jsonLines = Arrays.asList(jsonStr.split(LINE)); return jsonLines; } }