BaseControllerTest.java 3.96 KB
//package cn.com.glsx.controller;
//
//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.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.DefaultUriTemplateHandler;
//import org.springframework.web.util.UriComponents;
//import org.springframework.web.util.UriComponentsBuilder;
//import cn.com.glsx.IpConfiguration;
//import com.rop.utils.RopUtils;
//
///**
// * @Title: BaseControllerTest.java
// * @Description:
// * @author Alvin
// * @date 2019年9月3日 上午9:32:12
// * @version V1.0
// * @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.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");
//	}
//
//	String get(MultiValueMap<String, String> paramsMap) throws Exception{
//		return get(paramsMap, String.class);
//	}
//
//	<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();
//		rest.setUriTemplateHandler(uriTemplateHandler);
//		return rest.getForObject(url, responseType, params.toSingleValueMap());
//	}
//
//	String post(MultiValueMap<String, String> paramsMap) throws Exception{
//		return post(paramsMap, String.class);
//	}
//
//	<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);
//	}
//
//	MultiValueMap<String, String> getSysParams(){
//		MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(16);
//		params.add("appKey", appKey);
//		params.add("format", "json");
//		params.add("locale", "zh_CN");
//		return params;
//	}
//
//	class RopUriTemplateHandler extends DefaultUriTemplateHandler{
//		@Override
//		protected UriComponents expandAndEncode(UriComponentsBuilder builder,
//				Map<String, ?> uriVariables) {
//			if(uriVariables != null && !uriVariables.isEmpty()){
//				MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
//				for(Entry<String, ?> entry:uriVariables.entrySet()){
//					params.add(entry.getKey(), entry.getValue().toString());
//				}
//				builder.queryParams(params);
//			}
//			return super.expandAndEncode(builder, uriVariables);
//		}
//	}
//}