BaseControllerTest.java 6.91 KB
package cn.com.glsx.merchants.controller;

import java.net.URI;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriBuilder;

import cn.com.glsx.merchants.IpConfiguration;

import com.rop.utils.RopUtils;

/**
 * @author Alvin
 * @version V1.0
 * @Title: BaseControllerTest.java
 * @Description:
 * @date 2019年9月3日 上午9:32:12
 * @Company: didihu.com.cn
 * @Copyright Copyright (c) 2018
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class BaseControllerTest {

    String url = "http://localhost:";

    @Value("${server.servlet.context-path}")
    String contextPath;

    @Autowired
    private IpConfiguration ip;

    final String appKey = "48e5e13229b82c1b4e6e8c96151f0637";

    final String secret = "c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b";

    @Before
    public void setUp() {
        url = url.concat(String.valueOf(ip.getPort())).concat(contextPath).concat("/router");
    }

    public String get(MultiValueMap<String, String> paramsMap) throws Exception {
        return get(paramsMap, String.class);
    }

    public <T> T get(MultiValueMap<String, String> paramsMap, Class<T> responseType) throws Exception {
        MultiValueMap<String, String> params = getSysParams();
        params.putAll(paramsMap);
        for (Entry<String, String> entry : params.toSingleValueMap().entrySet()) {
            if (StringUtils.isEmpty(entry.getValue())) {
                params.remove(entry.getKey());
            }
        }
        String sign = RopUtils.sign(params.toSingleValueMap(), secret);
        params.add("sign", sign);
        RestTemplate rest = new RestTemplate();
        RopUriTemplateHandler uriTemplateHandler = new RopUriTemplateHandler(url);
        uriTemplateHandler.setDefaultUriVariables(params.toSingleValueMap());
        rest.setUriTemplateHandler(uriTemplateHandler);
        return rest.getForObject(url, responseType, params.toSingleValueMap());
    }

    public String getByHeader(MultiValueMap<String, String> paramsMap) throws Exception {
        MultiValueMap<String, String> params = getSysParams();
        params.putAll(paramsMap);
        for (Entry<String, String> entry : params.toSingleValueMap().entrySet()) {
            if (StringUtils.isEmpty(entry.getValue())) {
                params.remove(entry.getKey());
            }
        }
        String sign = RopUtils.sign(params.toSingleValueMap(), secret);
        RestTemplate rest = new RestTemplate();
        RopUriTemplateHandler uriTemplateHandler = new RopUriTemplateHandler(url);
        uriTemplateHandler.setDefaultUriVariables(params.toSingleValueMap());
        rest.setUriTemplateHandler(uriTemplateHandler);
        url=url+"?"+getUrlParamsByMap(params);
        HttpHeaders headers = new HttpHeaders();
        headers.add("TGT", "1aaf2f5b-bbdf-42b3-83bb-fd3992f0e7e7");
        String s = rest.postForObject(url, new HttpEntity<String>(headers), String.class);
        return s;
    }

    public static String getUrlParamsByMap(MultiValueMap<String, String> map) {
        if (map == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (Entry<String, String> entry : map.toSingleValueMap().entrySet()) {
            sb.append(entry.getKey() + "=" + entry.getValue());
            sb.append("&");
        }
        String s = sb.toString();
        if (s.endsWith("&")) {
            s = s.substring(0, s.length() - 1);
        }
        return s;
    }

    public String post(MultiValueMap<String, String> paramsMap) throws Exception {
        return post(paramsMap, String.class);
    }

    public <T> T post(MultiValueMap<String, String> paramsMap, Class<T> responseType) throws Exception {
        MultiValueMap<String, String> params = getSysParams();
        params.putAll(paramsMap);
        for (Entry<String, String> entry : params.toSingleValueMap().entrySet()) {
            if (StringUtils.isEmpty(entry.getValue())) {
                params.remove(entry.getKey());
            }
        }
        String sign = RopUtils.sign(params.toSingleValueMap(), secret);
        params.add("sign", sign);
        return new RestTemplate().postForObject(url, params, responseType);
    }

    public String upload(MultiValueMap<String, String> paramsMap, MultiValueMap<String, String> fileParamsMap) throws Exception {
        return upload(paramsMap, fileParamsMap, String.class);
    }

    public <T> T upload(MultiValueMap<String, String> paramsMap, MultiValueMap<String, String> fileParamsMap, Class<T> responseType) throws Exception {
        MultiValueMap<String, String> params = getSysParams();
        params.putAll(paramsMap);
        for (Entry<String, String> entry : params.toSingleValueMap().entrySet()) {
            if (StringUtils.isEmpty(entry.getValue())) {
                params.remove(entry.getKey());
            }
        }
        String sign = RopUtils.sign(params.toSingleValueMap(), secret);
        params.add("sign", sign);
        params.putAll(fileParamsMap);
        return new RestTemplate().postForObject(url, params, responseType);
    }

    public MultiValueMap<String, String> getSysParams() {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(16);
        params.add("appKey", appKey);
        params.add("format", "json");
        return params;
    }

    class RopUriTemplateHandler extends DefaultUriBuilderFactory {

        public RopUriTemplateHandler(String baseUriTemplate) {
            super(baseUriTemplate);
        }

        @Override
        public URI expand(String uriTemplate, Map<String, ?> uriVars) {
            UriBuilder builder = uriString(uriTemplate);
            if (uriVars != null && !uriVars.isEmpty()) {
                MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
                for (Entry<String, ?> entry : uriVars.entrySet()) {
                    params.add(entry.getKey(), entry.getValue().toString());
                }
                builder.queryParams(params);
            }
            return builder.build(uriVars);
        }
    }
}