java做个自动化注册检验,接口自动化通用验证类

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 lines = new ArrayList<>();

public Verify(JSONObject jsonObject) {

this.verifyJson = jsonObject;

this.lines = parseJsonLines(jsonObject);

}

/**

* 获取 code

*

这里的requestinfo主要的目的是为了拦截一些不必要的checkcode验证的,主要有black_host名单提供,在使用时,注意requestinfo的非空校验

*

* @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 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 jsonLines = Arrays.asList(jsonStr.split(LINE));

return jsonLines;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值