package ${domain};

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import ${domain}.frame.base.SortType;
import ${domain}.frame.client.ApiClient;
import ${domain}.frame.utils.MapperUtil;
<#list apiList as item>
import ${domain}.module.${module}.req.${item.request};
</#list>
<#list apiList as item>
import ${domain}.module.${module}.rsp.${item.response};
</#list>
import ${domain}.module.system.req.UserLoginRequest;
import ${domain}.module.system.rsp.UserLoginResponse;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * ApiTest - 测试用例
 *
 * @author ${author?default("")}
 * @version 0.0.1
 * @since ${.now?string["yyyy-MM-dd"]}
 */
public class ${className} {

    private ApiClient client;

    @Before
    public void before() {
        //实例化API请求客户端
        client = ApiClient.getInstance("http://localhost:8080/api", "app_key", "01234567890123456789012345678901");
        client.setDebug(true);

        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        //设置发送网络请求前的统一操作
        client.setCallBefore(request -> {
            System.out.println("before-->request = " + MapperUtil.toJson(request));
            System.out.println("before-->path    = " + request.path());
        });
        //设置网络请求完成后的统一操作
        client.setCallAfter((request, response) -> {
            System.out.println("after-->response = " + MapperUtil.toJson(response));
            Date end = new Date();
            System.out.println("after-->time     = " + simpleDateFormat.format(end));
            if (response.hasError()) {
                System.err.println(MapperUtil.toJson(response));
            }
        });

        UserLoginRequest request = new UserLoginRequest();
        request.setUsername("test");
        request.setPassword("test123");
        UserLoginResponse response = client.execute(request);
        Assert.assertTrue(!response.hasError());
        client.setToken(response.getToken());
    }
<#list apiList as item>

    @Test
    public void test${item.request}() {
        ${item.request} request = new ${item.request}();
        <#if item.requestFather == 'BaseFindRequest'>
        request.setPageNumber(1);
        request.setPageSize(10);
        request.setSortKey("CREATE_TIME");
        request.setSortType(SortType.DESC);
        </#if>
        ${item.response} response = client.execute(request);
        Assert.assertTrue(!response.hasError());
    }
</#list>
}
