package cn.hztech.sdk;

import cn.hztech.sdk.client.SdkHttpClient;
import cn.hztech.sdk.crypto.envelope.GmPacketBuilder;
import cn.hztech.sdk.crypto.envelope.GmPacketParser;
import cn.hztech.sdk.crypto.sm2.SM2Util;
import cn.hztech.sdk.crypto.sm4.SM4Util;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
 * 简化的服务器联调测试
 */
class SimpleServerTest {

    private SdkHttpClient httpClient;
    private SM2Util sm2Util;
    private SM4Util sm4Util;

    @BeforeEach
    void setUp() {
        sm2Util = new SM2Util();
        sm4Util = new SM4Util();

        // 使用与模拟服务器匹配的密钥
        String subAppPrivateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgjyuCLesqPpe3vKIZyxcRc0kIHAjNV1COx1ivB2yll3igCgYIKoZIzj0DAQehRANCAATYuayAIafF/4a9vAXPTpGMkVeQO/Sifa6qgkzVX8QOlQTnBA3I1GmG+zenZZgKlK9ZkKjIglAanX9on794pNCn";
        String platformPublicKey = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEZ9kLrwbjeI8YynMckN1fNepjnkWc7oUFb5r33WgXHQx569sjMbYbmJFW/IoZJWcVBWpIF0D73nOmZF24ZbLyEw==";

        String gatewayUrl = "http://localhost:8081";

        GmPacketBuilder packetBuilder = new GmPacketBuilder(
            "SUB_APP_001", subAppPrivateKey, platformPublicKey, sm2Util, sm4Util
        );

        GmPacketParser packetParser = new GmPacketParser(
            subAppPrivateKey, platformPublicKey, sm2Util, sm4Util, 60000L
        );

        httpClient = new SdkHttpClient(
            gatewayUrl, 10000, 30000, 30000,
            packetBuilder, packetParser,
            true, false, 0, 0
        );

        System.out.println("HTTP客户端初始化完成");
    }

    @Test
    void testConnection() {
        System.out.println("\n=== 测试服务器连接 ===");
        try {
            String result = httpClient.post("/hztech-application/user/detail",
                "{\"id\":\"USER_001\"}", String.class);
            System.out.println("✓ 服务器连接成功");
            System.out.println("  响应: " + result);
        } catch (Exception e) {
            System.err.println("✗ 服务器连接失败");
            System.err.println("  错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
