OpenIdTest.java 3.17 KB
package com.xkl.partner;

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;

/**
 * Created by win7 on 2017/1/2.
 */
public class OpenIdTest {
    public static final String URL_OPEN_ID = Constants.URL_PREFIX + "/openId";
    public static final String URL_LOGIN = Constants.URL_PREFIX + "/openId/login";

    /**
     * OpenId绑定接口
     */
    @Test
    public void testBind() {
        System.out.println(bindWithOpenIdAndUser());
    }

    public static String bindWithOpenIdAndUser() {
        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("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_OPEN_ID, "POST", params);
        Assert.assertTrue(RtnCodeTools.verifyCode(response, Constants.OPENID_BIND_SUCCESS));
        return response;
    }

    /**
     * OpenId解除绑定接口
     */
    @Test
    public void testUnBind() {
        String token = OpenIdTest.bindWithOpenIdAndLoginAndGetToken(Constants.USR_OPENID, Constants.USR_OPENID_TYPE);
        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.requestByMapWithToken(URL_OPEN_ID, "DELETE", params, token);
        System.out.println(response);
        Assert.assertTrue(RtnCodeTools.verifyCode(response, Constants.OPENID_UNBIND_SUCESS));

        // 解除绑定后,又重新绑定好以便下次测试
        testBind();
    }

    /**
     * OpenId登陆接口
     */
    @Test
    public void testLogin() {
        System.out.println(bindWithOpenIdAndLoginAndGetToken(Constants.USR_OPENID, Constants.USR_OPENID_TYPE));
    }

    public static String bindWithOpenIdAndLoginAndGetToken(String openId, String openIdType) {
        bindWithOpenIdAndUser();
        Map<String, String> params = new HashMap<String, String>();
        params.put("openId", openId);
        params.put("openIdType", openIdType);

        params.put("t", HttpTools.getNow());
        params.put("type", Constants.KEY_ID);
        params.put("sign", HttpTools.getSign(params));
        String response = HttpTools.requestByMap(URL_LOGIN, "POST", params);
        Assert.assertTrue(RtnCodeTools.verifyCode(response, Constants.CODE_SUCC));

        System.out.println("response:" + response);
        JSONObject jsonObject = new JSONObject(response);
        JSONObject content = jsonObject.getJSONObject("content");
        return content.getString("userId") + "_" + content.getString("token");
    }
}