ReportTest.java 2.39 KB
package com.xkl.upsoft;

import com.xkl.Constants;
import com.xkl.EncodeTools;
import com.xkl.HttpTools;
import com.xkl.RtnCodeTools;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * 报告上传与删除接口测试。
 * zhaoyue
 * 2017-1-14
 */

public class ReportTest {
    private static final String URL_REPORT = Constants.URL_PREFIX + "/upsoft/report";
    private static final String URL_LOGIN = Constants.URL_PREFIX + "/upsoft/account";

    public static String loginAndGetToken(String user, String pass) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("account", user);
        params.put("password", EncodeTools.encode("MD5", pass));
        params.put("t", HttpTools.getNow());
        String response = HttpTools.requestByMap(URL_LOGIN, "POST", params);
        System.out.println("login success:" + response);
        JSONObject jsonObject = new JSONObject(response);
        JSONObject content = jsonObject.getJSONObject("content");
        return content.getString("userId") + "_" + content.getString("token");
    }


    @Test
    public void testAddReport() {
        // 程序自动生成json报告。
        String report = Data.AMP_REPORT;
//         从本地加载json报告。
//        String report = Data.readTxtFile("C:\\Users\\zhaoyue\\Desktop\\report.txt");
        System.out.print(report);
        JSONObject rpJson = new JSONObject(report.trim());
//        System.out.print(report);
        String token = loginAndGetToken(Constants.ADMIN_ACCOUNT, Constants.ADMIN_PWD);
        Map<String, String> params = new HashMap<String, String>();


        params.put("json_report", report);
        String response = HttpTools.requestByMapWithToken(URL_REPORT, "POST", params, token);
        Assert.assertTrue(RtnCodeTools.verifyCode(response, Constants.CODE_SUCC));
        System.out.println("add report success:" + response);
    }

    @Test
    public void testDelReport() {
        String token = loginAndGetToken(Constants.ADMIN_ACCOUNT, Constants.ADMIN_PWD);
        Map<String, String> params = new HashMap<String, String>();
        params.put("report_id", "143");
        String response = HttpTools.requestByMapWithToken(URL_REPORT, "DELETE", params, token);
        System.out.println("delete report:" + response);
        Assert.assertTrue(response.contains("message"));
    }
}