package com.miaxis.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.miaxis.JsjpApplication;
import com.miaxis.common.sms.MD5Utils;
import com.miaxis.feign.dto.Wa;
import com.miaxis.feign.dto.WaResult;
import com.miaxis.feign.service.IWaService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

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

@SpringBootTest(classes = JsjpApplication.class)
@RunWith(SpringRunner.class)
public class NormalTest4 {

    @Autowired
    private IWaService waService;

    private static String appid = "7629";
    private static String secret = "cce08fcab10ff96a863892bb6e396a9e";

    public static String getSign(Map<String, Object> params,String client_secret) {
        Map<String, Object> sortMap = new TreeMap<String, Object>();
        sortMap.putAll(params);
        // 以k1v1k2v2...方式拼接参数
        StringBuilder builder = new StringBuilder();
        for (Map.Entry<String, Object> s : sortMap.entrySet()) {
            String k = s.getKey();
            Object v = s.getValue();
            builder.append(k).append(v);
        }
//        if (!sortMap.isEmpty()) {
//            builder.deleteCharAt(builder.length() - 1);
//        }
        builder.append(client_secret);
//        builder.insert(0,client_secret);
        return builder.toString();
    }

    @Test
    public void test() throws Exception {
        long timeLong = System.currentTimeMillis();
        String timestamp = String.valueOf(timeLong/1000);
        Wa wa = new Wa();
        //公共请救参数
        wa.setAppid(appid);
        wa.setDomain("www.zzxcx.net");
        wa.setFormat("json");
        wa.setTime(timestamp);
        //sign 加密
        Map<String,Object> param = new HashMap<String,Object>();
        //公共参数
        param.put("appid",wa.getAppid());
        param.put("domain",wa.getDomain());
        param.put("format",wa.getFormat());
        param.put("time",wa.getTime());

        String sign = getSign(param,secret);
        System.out.println(sign);
        String md5 = MD5Utils.MD5Encode(sign);
        wa.setSign(md5);

        String str = waService.geWaInfo(wa);
        System.out.println(str);

        ObjectMapper objectMapper = new ObjectMapper();
        WaResult waResult = objectMapper.readValue(str, WaResult.class);
        System.out.println(waResult);
    }













}
