QrCodeTest.java 3.3 KB
package com.xkl;

import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;

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

/**
 * Created by zhaoyue on 2017/3/11.
 */
public class QrCodeTest {
    private static final String URL_USER = Constants.URL_PREFIX + "/userInfo";


    public static final String URL_GETQR_WITHACCPWD = Constants.URL_PREFIX + "/qrcode/getQrWithAccPwd";
    public static final String URL_GETQR_WITHTOKEN = Constants.URL_PREFIX + "/qrcode/getQrWithToken";
    public static final String URL_GETQR_WITHOPNEID = Constants.URL_PREFIX + "/qrcode/getQrWithOpenId";
    public static final String URL_GETUSRINFO_WITHQR = Constants.URL_PREFIX + "/qrcode/getUsrInfoWithQr";




    /**
     * 使用token得到qrcode
     */
    @Test
    public void testGetQrWithToken() {
        String token = TokenTest.loginAndGetToken(Constants.USR_ACC, Constants.USR_PWD);
        Map<String, String> params = new HashMap<String, String>();
        params.put("t", HttpTools.getNow());
        params.put("type", Constants.KEY_ID);
        params.put("sign", HttpTools.getSign(params));
        String response = HttpTools.requestByMapWithToken(URL_GETQR_WITHTOKEN, "GET", params, token);
        System.out.println(response);
        Assert.assertTrue(hasQrCode(response));
    }

    /**
     * 使用openid得到qrcode
     */
    @Test
    public void testGetQrWithOpenid() {
        String token = TokenTest.loginAndGetToken(Constants.USR_ACC, Constants.USR_PWD);
        Map<String, String> params = new HashMap<String, String>();
        params.put("openId", Constants.USR_OPENID);
        params.put("openIdType", Constants.USR_OPENID_TYPE);
        params.put("t", HttpTools.getNow());
        params.put("type", Constants.KEY_ID);
        params.put("sign", HttpTools.getSign(params));
        String response = HttpTools.requestByMap(URL_GETQR_WITHOPNEID, "GET", params);
        System.out.println(response);
        Assert.assertTrue(hasQrCode(response));
    }



    private boolean hasQrCode(String responseStr) {
        try {
            JSONObject res = new JSONObject(responseStr);
            String qrcode = res.getJSONObject("content").getString("qrcode");
            Assert.assertNotEquals("qrcode", "");
            Assert.assertNotEquals("qrcode", null);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    private boolean hasSuccCode(String responseStr) {
        try {
            JSONObject res = new JSONObject(responseStr);
            int code = res.getInt("code");
            Assert.assertEquals(code, 100);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    private String getQrCode() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("username", Constants.USR_ACC);
        params.put("password", EncodeTools.encode("MD5", Constants.USR_PWD));
        params.put("t", HttpTools.getNow());
        String response = HttpTools.requestByMap(URL_GETQR_WITHACCPWD, "GET", params);
        String qrcode = null;
        try {
            JSONObject res = new JSONObject(response);
            qrcode = res.getJSONObject("content").getString("qrcode");
        } catch (Exception e) {
            return null;
        }
        return qrcode;
    }


}