package com.unionpaysmart.data.sdk.info.api.mock;

import com.unionpaysmart.data.sdk.api.DataApi;
import com.unionpaysmart.data.sdk.constant.Constant;
import com.unionpaysmart.data.sdk.model.info.request.EnterpriseInfoRequest;
import com.unionpaysmart.data.sdk.model.info.response.EnterpriseInfoResponse;
import com.unionpaysmart.data.sdk.util.SignUtil;
import com.unionpaysmart.data.sdk.util.http.RequestUtil;
import com.unionpaysmart.data.sdk.valid.exception.ValidationException;
import org.apache.log4j.PropertyConfigurator;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.modules.junit4.PowerMockRunner;

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

import static junit.framework.Assert.assertEquals;
import static org.easymock.EasyMock.expect;
import static org.powermock.api.easymock.PowerMock.mockStatic;
import static org.powermock.api.easymock.PowerMock.replay;

/**
 * 企业基本信息查询<br/>
 * 使用测试环境
 * 
 * @author Nie Xiongding
 * @since 2015年3月6日 下午3:40:20
 */
@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor("upsmart.data.sdk.util.http.RequestUtil")
@PrepareForTest({RequestUtil.class,DataApi.class})
public class EnterpriseInfoMockApi {
    @BeforeClass
    public static void setUp() {
        String log4jPath = Thread.currentThread().getContextClassLoader().getResource(".").getPath()
                + "/properties/log4j.properties";
        PropertyConfigurator.configure(log4jPath);
    }

    /**
     * 测试没有设置帐号的时候 提示ValidationException 异常
     */
    @Test(expected = ValidationException.class)
    public void testWithoutAccount() {
    	DataApi.account = null;
        DataApi.privateKey = null;
        
        String _response_data = "{\n" +
                "  \"resCode\" : \"1005\",\n" +
                "  \"resMsg\" : \"参数为空或格式错误 [字段 mvalue 不能为空]\"\n" +
                "}";
        
        Map<String, Object> params = new HashMap<String, Object>();
        
        mockStatic(RequestUtil.class);
        expect(RequestUtil.doGet("http://192.168.88.103:9999/info/enterpriseInfo",params)).andReturn(_response_data);
        replay(RequestUtil.class);

        EnterpriseInfoResponse response = DataApi.callEnterpriseInfo(new EnterpriseInfoRequest());
            assertEquals("resCode","1005",response.getResCode());
            assertEquals("resMsg","参数为空或格式错误 [字段 mvalue 不能为空]",response.getResMsg());
    }
    
    /**
     * 不填写必要参数时测试
     */
    @Test
    public void testWithoutNeededParams() {
        String _response_data = "{\n" +
                "  \"resCode\" : \"1005\",\n" +
                "  \"resMsg\" : \"参数为空或格式错误 [字段 mvalue 不能为空]\"\n" +
                "}";
        
        Map<String, Object> params = new HashMap<String, Object>();
        
        mockStatic(RequestUtil.class);
        expect(RequestUtil.doGet("http://192.168.88.103:9999/info/enterpriseInfo",params)).andReturn(_response_data);
        replay(RequestUtil.class);

        EnterpriseInfoRequest request = new EnterpriseInfoRequest();

        EnterpriseInfoResponse response =  DataApi.callEnterpriseInfo(request);
            assertEquals("resCode","1005",response.getResCode());
            assertEquals("resMsg","参数为空或格式错误 [字段 mvalue 不能为空]",response.getResMsg());
    }
    
    /**
     * 提交失败0002
     */
    @Test
    public void testSubmitFail0002() {
        String _response_data = "{\n" +
                "  \"resCode\" : \"0002\",\n" +
                "  \"resMsg\" : \"提交失败\"\n" +
                "}";
        
        Map<String, Object> params = new HashMap<String, Object>();
	        params.put(Constant.ACCOUNT, "100000");
//	        params.put(Constant.PRIVATE_KEY, "99999999qqqqqqqq99999999qqqqqqqq");
	        params.put("mvalue", "银联");
	        params.put("type", 1);
	        params.put(Constant.SIGN_PARAMS_KEY, SignUtil.generateSign(params));
        
        mockStatic(RequestUtil.class);
        expect(RequestUtil.doGet("http://192.168.88.103:9999/info/enterpriseInfo",params)).andReturn(_response_data);
        replay(RequestUtil.class);
        
        EnterpriseInfoRequest request = new EnterpriseInfoRequest();
        request.setMvalue("银联");
        request.setType(1);
        
        EnterpriseInfoResponse response = DataApi.callEnterpriseInfo(request);
	        assertEquals("resCode", "0002", response.getResCode());
        	assertEquals("resMsg", "提交失败", response.getResMsg());
    }
    
    /**
     * 提交失败1000
     */
    @Test
    public void testSubmitFail1000() {
        String _response_data = "{\n" +
                "  \"resCode\" : \"1000\",\n" +
                "  \"resMsg\" : \"账号不存在或被禁用\"\n" +
                "}";
        
        Map<String, Object> params = new HashMap<String, Object>();
	        params.put(Constant.ACCOUNT, "100000");
	        params.put("mvalue", "银联");
	        params.put("type", 1);
	        params.put(Constant.SIGN_PARAMS_KEY, SignUtil.generateSign(params));
        
        mockStatic(RequestUtil.class);
        expect(RequestUtil.doGet("http://192.168.88.103:9999/info/enterpriseInfo",params)).andReturn(_response_data);
        replay(RequestUtil.class);
        
        EnterpriseInfoRequest request = new EnterpriseInfoRequest();
        request.setMvalue("银联");
        request.setType(1);
        
        EnterpriseInfoResponse response = DataApi.callEnterpriseInfo(request);
	        assertEquals("resCode", "1000", response.getResCode());
        	assertEquals("resMsg", "账号不存在或被禁用", response.getResMsg());
    }

    /**
     * 提交失败1001
     */
    @Test
    public void testSubmitFail1001() {
        String _response_data = "{\n" +
                "  \"resCode\" : \"1001\",\n" +
                "  \"resMsg\" : \"没有此接口访问权限\"\n" +
                "}";
        
        Map<String, Object> params = new HashMap<String, Object>();
	        params.put(Constant.ACCOUNT, "100000");
	        params.put("mvalue", "银联");
	        params.put("type", 1);
	        params.put(Constant.SIGN_PARAMS_KEY, SignUtil.generateSign(params));
        
        mockStatic(RequestUtil.class);
        expect(RequestUtil.doGet("http://192.168.88.103:9999/info/enterpriseInfo",params)).andReturn(_response_data);
        replay(RequestUtil.class);
        
        EnterpriseInfoRequest request = new EnterpriseInfoRequest();
        request.setMvalue("银联");
        request.setType(1);
        
        EnterpriseInfoResponse response = DataApi.callEnterpriseInfo(request);
	        assertEquals("resCode", "1001", response.getResCode());
        	assertEquals("resMsg", "没有此接口访问权限", response.getResMsg());
    }

    /**
     * 提交失败1004
     */
    @Test
    public void testSubmitFail1004() {
        String _response_data = "{\n" +
                "  \"resCode\" : \"1004\",\n" +
                "  \"resMsg\" : \"请求的资源不存在\"\n" +
                "}";
        
        Map<String, Object> params = new HashMap<String, Object>();
	        params.put(Constant.ACCOUNT, "100000");
	        params.put("mvalue", "银联");
	        params.put("type", 1);
	        params.put(Constant.SIGN_PARAMS_KEY, SignUtil.generateSign(params));
        
        mockStatic(RequestUtil.class);
        expect(RequestUtil.doGet("http://192.168.88.103:9999/info/enterpriseInfo",params)).andReturn(_response_data);
        replay(RequestUtil.class);
        
        EnterpriseInfoRequest request = new EnterpriseInfoRequest();
        request.setMvalue("银联");
        request.setType(1);
        
        EnterpriseInfoResponse response = DataApi.callEnterpriseInfo(request);
	        assertEquals("resCode", "1004", response.getResCode());
        	assertEquals("resMsg", "请求的资源不存在", response.getResMsg());
    }

    /**
     * 提交失败1006
     */
    @Test
    public void testSubmitFail1006() {
        String _response_data = "{\n" +
                "  \"resCode\" : \"1006\",\n" +
                "  \"resMsg\" : \"验签失败\"\n" +
                "}";
        
        Map<String, Object> params = new HashMap<String, Object>();
	        params.put(Constant.ACCOUNT, "100000");
	        params.put("mvalue", "银联");
	        params.put("type", 1);
	        params.put(Constant.SIGN_PARAMS_KEY, SignUtil.generateSign(params));
        
        mockStatic(RequestUtil.class);
        expect(RequestUtil.doGet("http://192.168.88.103:9999/info/enterpriseInfo",params)).andReturn(_response_data);
        replay(RequestUtil.class);
        
        EnterpriseInfoRequest request = new EnterpriseInfoRequest();
        request.setMvalue("银联");
        request.setType(1);
        
        EnterpriseInfoResponse response = DataApi.callEnterpriseInfo(request);
	        assertEquals("resCode", "1006", response.getResCode());
        	assertEquals("resMsg", "验签失败", response.getResMsg());
    }

    /**
     * 提交失败9999
     */
    @Test
    public void testSubmitFail9999() {
        String _response_data = "{\n" +
                "  \"resCode\" : \"9999\",\n" +
                "  \"resMsg\" : \"系统错误\"\n" +
                "}";
        
        Map<String, Object> params = new HashMap<String, Object>();
	        params.put(Constant.ACCOUNT, "100000");
	        params.put("mvalue", "银联");
	        params.put("type", 1);
	        params.put(Constant.SIGN_PARAMS_KEY, SignUtil.generateSign(params));
        
        mockStatic(RequestUtil.class);
        expect(RequestUtil.doGet("http://192.168.88.103:9999/info/enterpriseInfo",params)).andReturn(_response_data);
        replay(RequestUtil.class);
        
        EnterpriseInfoRequest request = new EnterpriseInfoRequest();
        request.setMvalue("银联");
        request.setType(1);
        
        EnterpriseInfoResponse response = DataApi.callEnterpriseInfo(request);
	        assertEquals("resCode", "9999", response.getResCode());
        	assertEquals("resMsg", "系统错误", response.getResMsg());
    }

    /**
     * 正常调用接口测试
     */
    @Test
    public void testNormal() {
    	String _response_data = "{\n" +
                "  \"resCode\" : \"0000\",\n" +
                "  \"resMsg\" : \"提交成功\"\n" +
                "}";
        
        Map<String, Object> params = new HashMap<String, Object>();
	        params.put(Constant.ACCOUNT, "100000");
	        params.put("mvalue", "银联");
	        params.put("type", 1);
	        params.put(Constant.SIGN_PARAMS_KEY, SignUtil.generateSign(params));
        
        mockStatic(RequestUtil.class);
        expect(RequestUtil.doGet("http://192.168.88.103:9999/info/enterpriseInfo",params)).andReturn(_response_data);
        replay(RequestUtil.class);
        
        EnterpriseInfoRequest request = new EnterpriseInfoRequest();
        request.setMvalue("银联");
        request.setType(1);
        
        EnterpriseInfoResponse response = DataApi.callEnterpriseInfo(request);
	        assertEquals("resCode", "0000", response.getResCode());
        	assertEquals("resMsg", "提交成功", response.getResMsg());

    }

    /**
     * 初始化参数
     */
    @Before
    public void init() {
      DataApi.account = "100000";
      DataApi.privateKey = "99999999qqqqqqqq99999999qqqqqqqq";
      DataApi.TEST_URI="http://192.168.88.103:9999";
    }
}
