package ${domain};

import ${domain}.request.*;
import ${domain}.response.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

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

/**
 * ApiTest - 测试用例
 *
 * @author author
 * @version 0.0.1
 * @since 2019-06-11
 */
public class ApiTest {

    private ApiClient apiClient;

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

        // 以下为对称加密公钥，对重要关键请求加密，可不设
        String cryptPublicKeyBase64 = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCTrwfsrJjCF+pP4S3A/wrD4U1txg53EuBC1mPt" +
                "3vGXvSK2U0YNRVR3Q65ooHnPKmk4LwI8v+7+ATTxUg3qkuRiDuzBa5zLkYKM50LOgEWSdOKzbnbx" +
                "a5FnE7IXawNt1p8+MVN1TTI7J/fZy6g1x0WBy1odE5Osru4WfZNOqQtjHwIDAQAB";
        client.setPublicKey(RSAUtil.parsePublicKey(cryptPublicKeyBase64));

        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        //设置发送网络请求前的统一操作
        client.setBefore(new ApiClient.Before() {
            public void call(ApiRequest request) {
                System.out.println("请求参数" + MapperUtil.toJson(request));
                System.out.println("请求方法" + request.apiMethod());
            }
        });
        //设置网络请求完成后的统一操作
        client.setAfter(new ApiClient.After() {
            public void call(ApiRequest request, ApiResponse response) {
                System.out.println("响应参数" + MapperUtil.toJson(response));
                Date end = new Date();
                System.out.println(simpleDateFormat.format(end));
            }
        });

    }

    @Test
    public void testFileUpload() {
        {//无进度显示
            final Date start = new Date();

            //文件上传请求
            FileUploadRequest fileUploadRequest = new FileUploadRequest();
            fileUploadRequest.setFile(new File("D:\\1.PNG"));
            apiClient.fileUpload(fileUploadRequest, new ApiClient.Callback<FileUploadResponse>() {
                public void call(FileUploadResponse response) {
                    Date end = new Date();
                    System.out.println("无进度显示" + (end.getTime() - start.getTime()));
                }
            });
        }

        {//有进度显示
            final Date start = new Date();

            //文件上传请求
            FileUploadRequest fileUploadRequest = new FileUploadRequest();
            fileUploadRequest.setFile(new File("D:\\1.PNG"));
            apiClient.fileUpload(fileUploadRequest, new ApiClient.Callback<FileUploadResponse>() {
                public void call(FileUploadResponse response) {
                    Date end = new Date();
                    System.out.println("有进度显示" + (end.getTime() - start.getTime()));
                }
            }, new ProgressRequestBody.ProgressListener() {
                public void onProgress(long totalBytes, long remainingBytes, boolean done) {
                    System.out.println((totalBytes - remainingBytes) * 100 / totalBytes + "%");
                }
            });
        }

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
<#list apiList as item>

    @Test
    public void test${item.targetRequest}() {
        ${item.targetRequest} request = new ${item.targetRequest}();
        ${item.targetResponse} response = apiClient.execute(request);
        Assert.assertTrue(!response.hasError());
    }
</#list>
}
