package com.gxwebsoft.test;

import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * 异步通知证书修复验证测试
 */
public class NotificationCertificateFixTest {

    @Test
    public void testCertificatePathFix() {
        System.out.println("=== 异步通知证书路径修复验证 ===");
        
        // 模拟异步通知中的证书路径构建逻辑
        String tenantId = "10550";
        String devCertPath = "dev";
        String certDir = "wechat";
        String privateKeyFile = "apiclient_key.pem";
        
        // 修复后的路径构建逻辑
        String tenantCertPath = devCertPath + "/" + certDir + "/" + tenantId;
        String privateKeyPath = tenantCertPath + "/" + privateKeyFile;
        
        System.out.println("租户ID: " + tenantId);
        System.out.println("证书目录: " + tenantCertPath);
        System.out.println("私钥路径: " + privateKeyPath);
        
        // 测试从classpath加载证书
        try {
            Resource resource = new ClassPathResource(privateKeyPath);
            System.out.println("证书资源存在: " + resource.exists());
            
            if (resource.exists()) {
                // 模拟CertificateLoader.loadFromClasspath的逻辑
                Path tempFile = Files.createTempFile("cert_", ".pem");
                try (InputStream inputStream = resource.getInputStream()) {
                    Files.copy(inputStream, tempFile, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
                }
                
                String tempPath = tempFile.toAbsolutePath().toString();
                System.out.println("✅ 证书加载成功: " + tempPath);
                
                // 验证临时文件
                File tempCertFile = new File(tempPath);
                System.out.println("临时证书文件大小: " + tempCertFile.length() + " bytes");
                
                // 清理临时文件
                Files.deleteIfExists(tempFile);
                
            } else {
                System.err.println("❌ 证书文件不存在: " + privateKeyPath);
            }
            
        } catch (IOException e) {
            System.err.println("❌ 证书加载失败: " + e.getMessage());
            e.printStackTrace();
        }
        
        System.out.println("=== 测试完成 ===");
    }
    
    @Test
    public void testAllTenantCertificates() {
        System.out.println("=== 测试所有租户证书 ===");
        
        String[] tenantIds = {"10398", "10550"};
        String devCertPath = "dev";
        String certDir = "wechat";
        String privateKeyFile = "apiclient_key.pem";
        
        for (String tenantId : tenantIds) {
            System.out.println("\n--- 测试租户: " + tenantId + " ---");
            
            String tenantCertPath = devCertPath + "/" + certDir + "/" + tenantId;
            String privateKeyPath = tenantCertPath + "/" + privateKeyFile;
            
            System.out.println("证书路径: " + privateKeyPath);
            
            try {
                Resource resource = new ClassPathResource(privateKeyPath);
                if (resource.exists()) {
                    System.out.println("✅ 租户 " + tenantId + " 证书存在");
                } else {
                    System.out.println("❌ 租户 " + tenantId + " 证书不存在");
                }
            } catch (Exception e) {
                System.err.println("❌ 租户 " + tenantId + " 证书检查失败: " + e.getMessage());
            }
        }
        
        System.out.println("\n=== 所有租户证书测试完成 ===");
    }
}
