Java:接口自动化

97 阅读9分钟

import com.huahua.esys.control.ActivityAction;
import com.huahua.esys.control.Jhl1484Action;
import com.huahua.esys.control.LiveUtils;
import com.huahua.esys.control.virtualImage.CommonUtil;
import com.huahua.network.common.Environment;
import com.huahua.network.common.LogUtils;
import com.huahua.network.net.BvtHelper;
import com.huahua.network.utility.Utilly;
import net.sf.json.JSONObject;
import org.apache.commons.lang.ObjectUtils;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;


import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class PopWindow1617Test {
    Environment env = Environment.TEST_PEPPER_ACTIVITY;//action里host要改,https改成http
//        Environment env = Environment.PEPPER_ACTIVITY;  // 线上环境
    BvtHelper bvthelper = new BvtHelper();
    Utilly utils = new Utilly();
    Jhl1484Action jhl1484Action = new Jhl1484Action();
    ActivityAction activityAction = new ActivityAction();
    LiveUtils liveUtils = new LiveUtils();
    PopWindow1617Action popWindow1617Action = new PopWindow1617Action();

    String testIp = "popwindow1617.activity.test.huahua.com";
    Object uid = 43008476; //45864006;//45863987;//45755656;//43872265;//45755591
    Object author = 45755591; // 主播ID
    Object uidToken = "";
    Object liveid = liveUtils.getCurrentLiveid(author);


    @BeforeClass
    public void beforeClass() {
        if (env == Environment.PEPPER_ACTIVITY) {
            testIp = "popwindow1617.activity.huahua.com";  // 线上环境地址
            uid = 252860815; // 252105928  252608592
            author = 220652118;
        }
        uidToken = utils.getTokenV2(uid);
    }

    @DataProvider // 直播间内外弹窗规范-重置弹窗次数
    public Object[][] data_resetPop() {
        int scene = 1; // 1直播间外 2 直播间内
        String appName = "huahua";
        return new Object[][]{
//                new Object[]{0, "重置-45864010", utils.getTokenV2(45865164), 45865164, scene, appName, 0},
//                new Object[]{1, "重置-45864010", utils.getTokenV2(45865164), 45865164, 2, appName, 0},
        };
    }

    @Test(dataProvider = "data_resetPop")
    public void resetPop(int id, String casename, String token, Object uid, Object scene, String appName, int exp) {
        LogUtils.printTitle(id, casename);
        JSONObject result = JSONObject.fromObject(popWindow1617Action.resetPop(testIp, token, uid, scene, appName));
        Assert.assertEquals(result.getInt("errno"), exp);
    }

    @DataProvider // 按活动重置次数
    public Object[][] data_resetUnique() {
        int uniqueId = 1520; // 活动ID
        String appName = "huahua";  // 252105928  252608592  251522530
        return new Object[][]{
//                new Object[]{0, "重置-45864010", utils.getTokenV2(45865164), 45865164, uniqueId, appName, 0},
//                new Object[]{0, "重置-45865263", utils.getTokenV2(45865263), 45865263, 3333, appName, 0},
        };
    }

    @Test(dataProvider = "data_resetUnique")
    public void resetUnique(int id, String casename, String token, Object uid, Object uniqueId, String appName, int exp) {
        LogUtils.printTitle(id, casename);
        JSONObject result = JSONObject.fromObject(popWindow1617Action.resetUnique(testIp, token, uid, uniqueId, appName));
        Assert.assertEquals(result.getInt("errno"), exp);
    }


    @DataProvider // 启动app调用
    public Object[][] data_loginPop() {
        String appName = "huahua";  // 252105928  252608592  251522530
        return new Object[][]{
                new Object[]{0, "启动-252860815", utils.getTokenV2(45864027), appName, 0},
//                new Object[]{1, "启动-45864075", utils.getTokenV2(45864075), appName, 0},
//                new Object[]{2, "启动-45864010", utils.getTokenV2(45864010), appName, 0},
        };
    }

    @DataProvider // 进入直播间调用
    public Object[][] data_joinRoomPop() {
        String appName = "huahua"; // 252105928  252608592
        return new Object[][]{
//                new Object[]{0, "直播间调用-252860815", utils.getTokenV2(252860815), appName, 0},
//                new Object[]{1, "直播间调用-45864010", utils.getTokenV2(45864010), appName, 0},
//                new Object[]{2, "直播间调用-45865333", utils.getTokenV2(45865333), appName, 0},
        };
    }


    @DataProvider // 业务侧接入调用pushPopMessage-单个用户
    public Object[][] data_pushPopMessage() {
        String appName = "huahua";
        String uniqueId = "1200";
        int scene = 1; // 1直播间外,2直播间内
        int sequence = 1; // 启动时序
        int priority = 12; // 优先级,值越高越大
        String slot = "1"; // 坑位slotId中的排列,20220707_1_1_1  后三位分别代表_内外_时序_1
        String url1 = "https://web.test.huahua.com/jimu/222/index.html"; // 1200 12 1
        String url2 = "https://activity.test.huahua.com/web/share/banner/2022/luckyBag/popup.html"; // 3333
        return new Object[][]{
//                new Object[]{0, "消息外-45864010", utils.getTokenV2(45864010), 45864010, scene, sequence, priority, slot, uniqueId, appName, url1, 0},
//                new Object[]{1, "消息外-45864010", utils.getTokenV2(45864010), 45864010, scene, 2, 333, slot, "3333", appName, url2, 0},
        };
    }

    @DataProvider // 获取key
    public Object[][] data_getRedisKey() {
        String key0 = "buildKey:value:PopWindow:1617:0:uid:login:count:252105928:20220713:huahua";
        return new Object[][]{
//                new Object[]{0, "查询-252105928", utils.getTokenV2(252105928), key0, 0},
//                new Object[]{0, "重置-252608592", utils.getTokenV2(252608592), key2, 0},
//                new Object[]{1, "重置-251522530", utils.getTokenV2(251522530), scene, 0},
        };
    }

    @Test(dataProvider = "data_getRedisKey")
    public void getRedisKey(int id, String casename, String token, String key, int exp) {
        LogUtils.printTitle(id, casename);
        JSONObject result = JSONObject.fromObject(popWindow1617Action.getRedisKey(testIp, token, key));
        Assert.assertEquals(result.getInt("errno"), exp);
    }

    @DataProvider // 修改key
    public Object[][] data_setRedisKey() {
        String key3 = "buildKey:value:PopWindow:1617:0:uid:pop:window:close:day:1319:252608592:huahua";
        String key_3 = "buildKey:value:PopWindow:1617:0:uid:close:pop:window:count:1319:252608592:huahua";
        String value1 = "20220510"; // 时间 20220515
        String value_1 = "0"; // 次数
        return new Object[][]{
//                new Object[]{0, "修改key-252105928", utils.getTokenV2(252608592), key3, value1, 0},
//                new Object[]{0, "修改key-252105928", utils.getTokenV2(252105928), key_1, value_1, 0},
        };
    }


    @Test(dataProvider = "data_setRedisKey")
    public void setRedisKey(int id, String casename, String token, String key, String value, int exp) {
        LogUtils.printTitle(id, casename);
        JSONObject result = JSONObject.fromObject(popWindow1617Action.setRedisKey(testIp, token, key, value));
        Assert.assertEquals(result.getInt("errno"), exp);
    }




    @DataProvider // 是否关闭页面
    public Object[][] data_isClose() {
        String appName = "huahua";
        String uniqueId = "1520"; // 活动ID 1319  2222
        String slotId = "20220628_2_1_1"; // 坑位
        int close = 1; // 0 不关闭,1 关闭    // 252105928  252608592  251522530
        return new Object[][]{
//                new Object[]{0, "关闭-45865333", utils.getTokenV2(45865333), appName, uniqueId, slotId, close, 0},
//                new Object[]{2, "关闭-45865332", utils.getTokenV2(45865332), appName, uniqueId, slotId, close, 0},
        };
    }

    @DataProvider // 补救消息(增加/更改/删除)-全站用户
    public Object[][] data_rescueMessage() {
        int scene = 2; // 1直播间外 2直播间内
        int sequence = 1;
        String uniqueId = "1520";  // 252105928  252608592
        String action = "update"; // delete -删除 update -更新 add -增加
        String appName = "huahua";
        String slot = "1"; // 编号 20220511_2_1_1  后三位分别代表_内外_时序_编号
        return new Object[][]{
//                new Object[]{0, "补救-45864066", utils.getTokenV2(45865390), scene, sequence, uniqueId, action, appName, slot, 0},
//                new Object[]{1, "补救消息-删除", utils.getTokenV2(45864062), scene, sequence, uniqueId, "delete", appName, slot, 0},
//                new Object[]{1, "补救消息-更新", utils.getTokenV2(45864075), scene, sequence, uniqueId, "update", appName, slot, 0},
        };
    }


    @DataProvider // 弹窗通知服务端--
    public Object[][] data_pop() {
        String appName = "huahua";
        String uniqueId = "1600";
        String slotId = "20220517_1_1_2";  // 252105928  252608592
        return new Object[][]{
//                new Object[]{0, "弹窗通知服务端", utils.getTokenV2(45864066), appName, uniqueId, slotId, 0},
//                new Object[]{1, "弹窗通知服务端", utils.getTokenV2(45864062), appName, uniqueId, slotId, 0},
//                new Object[]{1, "弹窗通知服务端", utils.getTokenV2(45864075), appName, uniqueId, slotId, 0},
        };
    }

    @Test(dataProvider = "data_loginPop")
    public void loginPop(int id, String casename, String token, String appName, int exp) {
        LogUtils.printTitle(id, casename);
        JSONObject result = JSONObject.fromObject(popWindow1617Action.loginPop(testIp, token, appName));
        Assert.assertEquals(result.getInt("errno"), exp);
    }


    @Test(dataProvider = "data_isClose")
    public void isClose(int id, String casename, String token, String appName, String uniqueId, String slotId, Object close, int exp) {
        LogUtils.printTitle(id, casename);
        JSONObject result = JSONObject.fromObject(popWindow1617Action.isClose(testIp, token, appName, uniqueId, slotId, close));
        Assert.assertEquals(result.getInt("errno"), exp);
    }


    @Test(dataProvider = "data_pushPopMessage")
    public void pushPopMessage(int id, String casename, String token, Object uid, Object scene, Object sequence, Object priority, String slot, String uniqueId, String appName, Object url1, int exp) {
        LogUtils.printTitle(id, casename);
        JSONObject result = JSONObject.fromObject(popWindow1617Action.pushPopMessage(testIp, token, uid, scene, sequence, priority, slot, uniqueId, appName, url1));
        Assert.assertEquals(result.getInt("errno"), exp);
    }


    @Test(dataProvider = "data_rescueMessage")
    public void rescueMessage(int id, String casename, String token, Object scene, Object sequence, String uniqueId, String action, String appName, String slot, int exp) {
        LogUtils.printTitle(id, casename);
        JSONObject result = JSONObject.fromObject(popWindow1617Action.rescueMessage(testIp, token, scene, sequence, uniqueId, action, appName, slot));
        Assert.assertEquals(result.getInt("errno"), exp);
    }


    @Test(dataProvider = "data_pop")
    public void pop(int id, String casename, String token, String appName, String uniqueId, String slotId, int exp) {
        LogUtils.printTitle(id, casename);
        JSONObject result = JSONObject.fromObject(popWindow1617Action.pop(testIp, token, appName, uniqueId, slotId));
        Assert.assertEquals(result.getInt("errno"), exp);
    }

    @Test(dataProvider = "data_joinRoomPop")
    public void joinRoomPop(int id, String casename, String token, String appName, int exp) {
        LogUtils.printTitle(id, casename);
        JSONObject result = JSONObject.fromObject(popWindow1617Action.joinRoomPop(testIp, token, appName));
        Assert.assertEquals(result.getInt("errno"), exp);
    }


    @DataProvider // 直播间内外弹窗规范-是否允许弹窗
    public Object[][] data_isPermitPop() {
        String versionId = "8.3.8"; //系统版本号,大于这个版本号才会弹窗
        return new Object[][]{
//                new Object[]{0, "允许弹窗-高版本", utils.getTokenV2(252105928), 252105928, versionId, 0},
//                new Object[]{1, "不许弹窗-低版本", utils.getTokenV2(252608592), 252608592, "8.3.7", 0},
        };
    }

    @Test(dataProvider = "data_isPermitPop")
    public void isPermitPop(int id, String casename, String token, Object uid, String version, int exp) {
        LogUtils.printTitle(id, casename);
        JSONObject result = JSONObject.fromObject(popWindow1617Action.isPermitPop(testIp, token, uid, version));
        Assert.assertEquals(result.getInt("errno"), exp);
    }

    class PopWindow1617Action {
        // 直播间内外弹窗规范-启动app调用
        public Object loginPop(String ip, String token, String appName) {
            String uri = "PopWindow/loginPop";
            String url = String.format("%s://%s/%s%s", CommonUtil.replaceHost(ip), ip, uri, utils.buildClientGUID("ios","8.4.1"));
            Map<String, String> headers = new HashMap<>();
            headers.put("Host", ip.contains("test") ? "popwindow1617.activity.test.huahua.com" : "popwindow1617.activity.huahua.com");
            headers.put("Cookie", "token=" + token);
            Map<String, Object> params = new HashMap<String, Object>();
            if (appName != null) {
                params.put("appName", appName);
            }
            if (token != null) {
                params.put("token", token);
            }
            StringBuilder paramsa = bvthelper.builder(params);
            return bvthelper.sendandget(url, paramsa, headers, true, "form");
        }


        public Object joinRoomPop(String ip, String token, String appName) {
            String uri = "PopWindow/joinRoomPop";
            String url = String.format("%s://%s/%s%s", CommonUtil.replaceHost(ip), ip, uri, utils.buildClientGUID("ios","8.4.1"));
            Map<String, String> headers = new HashMap<>();
            headers.put("Host", ip.contains("test") ? "popwindow1617.activity.test.huahua.com" : "popwindow1617.activity.huahua.com");
            headers.put("Cookie", "token=" + token);
            Map<String, Object> params = new HashMap<String, Object>();
            if (appName != null) {
                params.put("appName", appName);
            }
            StringBuilder paramsa = bvthelper.builder(params);
            return bvthelper.sendandget(url, paramsa, headers, true, "form");
        }


        public Object isClose(String ip, String token, String appName, String uniqueId, String slotId, Object close) {
            String uri = "PopWindow/isClose";
            String url = String.format("%s://%s/%s%s", CommonUtil.replaceHost(ip), ip, uri, utils.buildClientGUID("ios","8.4.1"));
            Map<String, String> headers = new HashMap<>();
            headers.put("Host", ip.contains("test") ? "popwindow1617.activity.test.huahua.com" : "popwindow1617.activity.huahua.com");
            headers.put("Cookie", "token=" + token);
            Map<String, Object> params = new HashMap<String, Object>();
            if (appName != null) {
                params.put("appName", appName);
            }
            if (uniqueId != null) {
                params.put("uniqueId", uniqueId); // 活动唯一标识
            }
            if (slotId != null) {
                params.put("slotId", slotId); // 坑位
            }
            if (close != null) {
                params.put("close", close); // 1关闭 0 不关闭
            }
            StringBuilder paramsa = bvthelper.builder(params);
            return bvthelper.sendandget(url, paramsa, headers, true, "form");
        }


        public Object pop(String ip, String token, String appName, String uniqueId, String slotId) {
            String uri = "PopWindow/pop";
            String url = String.format("%s://%s/%s%s", CommonUtil.replaceHost(ip), ip, uri, utils.buildClientGUID("ios","8.4.1"));
            Map<String, String> headers = new HashMap<>();
            headers.put("Host", ip.contains("test") ? "popwindow1617.activity.test.huahua.com" : "popwindow1617.activity.huahua.com");
            headers.put("Cookie", "token=" + token);
            Map<String, Object> params = new HashMap<String, Object>();
            if (appName != null) {
                params.put("appName", appName);
            }
            if (uniqueId != null) {
                params.put("uniqueId", uniqueId); // 活动唯一标识
            }
            if (slotId != null) {
                params.put("slotId", slotId); // 坑位
            }
            StringBuilder paramsa = bvthelper.builder(params);
            return bvthelper.sendandget(url, paramsa, headers, true, "form");
        }


        // 直播间内外弹窗规范-业务侧接入调用pushPopMessage
        public Object pushPopMessage(String ip, String token, Object uid, Object scene, Object sequence, Object priority, String slot, String uniqueId, String appName, Object url1) {
            String uri = "PopWindow/pushPopMessage";
            String url = String.format("%s://%s/%s%s", CommonUtil.replaceHost(ip), ip, uri, utils.buildClientGUID("ios","8.4.1"));
            Map<String, String> headers = new HashMap<>();
            headers.put("Host", ip.contains("test") ? "popwindow1617.activity.test.huahua.com" : "popwindow1617.activity.huahua.com");
            headers.put("Cookie", "token=" + token);
            Map<String, Object> params = new HashMap<String, Object>();
            if (appName != null) {
                params.put("appName", appName);
            }
            if (uniqueId != null) {
                params.put("uniqueId", uniqueId);
            }
            if (uid != null) {
                params.put("uid", uid);
            }
            if (scene != null) {
                params.put("scene", scene);
            }
            if (sequence != null) {
                params.put("sequence", sequence);
            }
            if (priority != null) {
                params.put("priority", priority);
            }
            if (slot != null) {
                params.put("slot", slot);
            }
            if (url1 != null) {
                params.put("url", url1);
            }
            StringBuilder paramsa = bvthelper.builder(params);
            return bvthelper.sendandget(url, paramsa, headers, true, "form");
        }


        public Object isPermitPop(String ip, String token, Object uid, String versionId) {
            String uri = "PopWindow/isPermitPop";
            String url = String.format("%s://%s/%s%s", CommonUtil.replaceHost(ip), ip, uri, utils.buildClientGUID("ios","8.4.1"));
            Map<String, String> headers = new HashMap<>();
            headers.put("Host", ip.contains("test") ? "popwindow1617.activity.test.huahua.com" : "popwindow1617.activity.huahua.com");
            headers.put("Cookie", "token=" + token);
            Map<String, Object> params = new HashMap<String, Object>();
            if (uid != null) {
                params.put("uid", uid);
            }
            if (versionId != null) {
                params.put("versionId", versionId);
            }
            StringBuilder paramsa = bvthelper.builder(params);
            return bvthelper.sendandget(url, paramsa, headers, true, "form");
        }

        // 补救消息(更改/删除)
        public Object rescueMessage(String ip, String token, Object scene, Object sequence, String uniqueId, String action, String appName, String slot) {
            String uri = "PopWindow/rescueMessage";
            String url = String.format("%s://%s/%s%s", CommonUtil.replaceHost(ip), ip, uri, utils.buildClientGUID("ios","8.4.1"));
            Map<String, String> headers = new HashMap<>();
            headers.put("Host", ip.contains("test") ? "popwindow1617.activity.test.huahua.com" : "popwindow1617.activity.huahua.com");
            headers.put("Cookie", "token=" + token);
            Map<String, Object> params = new HashMap<String, Object>();
            if (scene != null) {
                params.put("scene", scene);
            }
            if (sequence != null) {
                params.put("sequence", sequence);
            }
            if (uniqueId != null) {
                params.put("uniqueId", uniqueId);
            }
            if (action != null) {
                params.put("action", action);
            }
            if (appName != null) {
                params.put("appName", appName);
            }
            if (slot != null) {
                params.put("slot", slot);
            }
            StringBuilder paramsa = bvthelper.builder(params);
            return bvthelper.sendandget(url, paramsa, headers, true, "form");
        }


        public Object resetPop(String ip, String token, Object uid, Object scene, String appName) {
            String uri = "PopWindow/resetPop";
            String url = String.format("%s://%s/%s%s", CommonUtil.replaceHost(ip), ip, uri, utils.buildClientGUID("ios","8.4.1"));
            Map<String, String> headers = new HashMap<>();
            headers.put("Host", ip.contains("test") ? "popwindow1617.activity.test.huahua.com" : "popwindow1617.activity.huahua.com");
            headers.put("Cookie", "token=" + token);
            Map<String, Object> params = new HashMap<String, Object>();
            if (uid != null) {
                params.put("uid", uid);
            }
            if (scene != null) {
                params.put("scene", scene);
            }
            if (appName != null) {
                params.put("appName", appName);
            }
            StringBuilder paramsa = bvthelper.builder(params);
            return bvthelper.sendandget(url, paramsa, headers, true, "form");
        }

        public Object resetUnique(String ip, String token, Object uid, Object uniqueId, String appName) {
            String uri = "PopWindow/resetUnique";
            String url = String.format("%s://%s/%s%s", CommonUtil.replaceHost(ip), ip, uri, utils.buildClientGUID("ios","8.4.1"));
            Map<String, String> headers = new HashMap<>();
            headers.put("Host", ip.contains("test") ? "popwindow1617.activity.test.huahua.com" : "popwindow1617.activity.huahua.com");
            headers.put("Cookie", "token=" + token);
            Map<String, Object> params = new HashMap<String, Object>();
            if (uid != null) {
                params.put("uid", uid);
            }
            if (uniqueId != null) {
                params.put("uniqueId", uniqueId);
            }
            if (appName != null) {
                params.put("appName", appName);
            }
            StringBuilder paramsa = bvthelper.builder(params);
            return bvthelper.sendandget(url, paramsa, headers, true, "form");
        }

        public Object getRedisKey(String ip, String token, String key) {
            String uri = "PopWindow/getRedisKey";
            String url = String.format("%s://%s/%s%s", CommonUtil.replaceHost(ip), ip, uri, utils.buildClientGUID("ios","8.4.1"));
            Map<String, String> headers = new HashMap<>();
            headers.put("Host", ip.contains("test") ? "popwindow1617.activity.test.huahua.com" : "popwindow1617.activity.huahua.com");
            headers.put("Cookie", "token=" + token);
            Map<String, Object> params = new HashMap<String, Object>();
            if (key != null) {
                params.put("key", key);
            }
            StringBuilder paramsa = bvthelper.builder(params);
            return bvthelper.sendandget(url, paramsa, headers, true, "form");
        }


        public Object setRedisKey(String ip, String token, String key, String value) {
            String uri = "PopWindow/setRedisKey";
            String url = String.format("%s://%s/%s%s", CommonUtil.replaceHost(ip), ip, uri, utils.buildClientGUID("ios","8.4.1"));
            Map<String, String> headers = new HashMap<>();
            headers.put("Host", ip.contains("test") ? "popwindow1617.activity.test.huahua.com" : "popwindow1617.activity.huahua.com");
            headers.put("Cookie", "token=" + token);
            Map<String, Object> params = new HashMap<String, Object>();
            if (key != null) {
                params.put("key", key);
            }
            if (value != null) {
                params.put("value", value);
            }
            StringBuilder paramsa = bvthelper.builder(params);
            return bvthelper.sendandget(url, paramsa, headers, true, "form");
        }


    }

}