package com.gxwebsoft.common.core.service;

import com.gxwebsoft.common.core.config.CertificateProperties;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import javax.annotation.Resource;
import java.util.Map;

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

/**
 * 证书服务测试类
 * 
 * @author 科技小王子
 * @since 2024-07-26
 */
@SpringBootTest
@ActiveProfiles("dev")
public class CertificateServiceTest {

    @Resource
    private CertificateService certificateService;
    
    @Resource
    private CertificateProperties certificateProperties;
    
    @Resource
    private CertificateHealthService certificateHealthService;

    @Test
    public void testCertificateServiceInitialization() {
        assertNotNull(certificateService);
        assertNotNull(certificateProperties);
        assertEquals(CertificateProperties.LoadMode.CLASSPATH, certificateProperties.getLoadMode());
    }

    @Test
    public void testWechatCertificateExists() {
        // 测试微信支付证书文件是否存在
        assertTrue(certificateService.certificateExists("wechat", "apiclient_key.pem"));
        assertTrue(certificateService.certificateExists("wechat", "apiclient_cert.pem"));
        assertTrue(certificateService.certificateExists("wechat", "wechatpay_cert.pem"));
    }

    @Test
    public void testAlipayCertificateExists() {
        // 测试支付宝证书文件是否存在
        assertTrue(certificateService.certificateExists("alipay", "appCertPublicKey.crt"));
        assertTrue(certificateService.certificateExists("alipay", "alipayCertPublicKey.crt"));
        assertTrue(certificateService.certificateExists("alipay", "alipayRootCert.crt"));
        assertTrue(certificateService.certificateExists("alipay", "app_private_key.pem"));
    }

    @Test
    public void testGetCertificatePaths() {
        // 测试获取证书路径
        String wechatKeyPath = certificateService.getWechatPayCertPath("apiclient_key.pem");
        assertNotNull(wechatKeyPath);
        assertTrue(wechatKeyPath.contains("wechat"));
        assertTrue(wechatKeyPath.contains("apiclient_key.pem"));

        String alipayAppCertPath = certificateService.getAlipayCertPath("appCertPublicKey.crt");
        assertNotNull(alipayAppCertPath);
        assertTrue(alipayAppCertPath.contains("alipay"));
        assertTrue(alipayAppCertPath.contains("appCertPublicKey.crt"));
    }

    @Test
    public void testGetAllCertificateStatus() {
        // 测试获取所有证书状态
        Map<String, Object> status = certificateService.getAllCertificateStatus();
        assertNotNull(status);
        assertTrue(status.containsKey("wechat"));
        assertTrue(status.containsKey("alipay"));
        assertTrue(status.containsKey("system"));

        @SuppressWarnings("unchecked")
        Map<String, Object> wechatStatus = (Map<String, Object>) status.get("wechat");
        assertTrue(wechatStatus.containsKey("privateKey"));
        assertTrue(wechatStatus.containsKey("apiclientCert"));
        assertTrue(wechatStatus.containsKey("wechatpayCert"));

        @SuppressWarnings("unchecked")
        Map<String, Object> alipayStatus = (Map<String, Object>) status.get("alipay");
        assertTrue(alipayStatus.containsKey("appPrivateKey"));
        assertTrue(alipayStatus.containsKey("appCertPublicKey"));
        assertTrue(alipayStatus.containsKey("alipayCertPublicKey"));
        assertTrue(alipayStatus.containsKey("alipayRootCert"));
    }

    @Test
    public void testCertificateHealthCheck() {
        // 测试证书健康检查
        assertNotNull(certificateHealthService);
        
        Map<String, Object> diagnostic = certificateHealthService.getDiagnosticInfo();
        assertNotNull(diagnostic);
        assertTrue(diagnostic.containsKey("loadMode"));
        assertTrue(diagnostic.containsKey("certificateStatus"));
        assertTrue(diagnostic.containsKey("healthStatus"));
    }

    @Test
    public void testValidateX509Certificate() {
        // 测试X509证书验证（对于.crt和.pem文件）
        CertificateService.CertificateInfo wechatCertInfo = 
            certificateService.validateX509Certificate("wechat", "apiclient_cert.pem");
        
        if (wechatCertInfo != null) {
            assertNotNull(wechatCertInfo.getSubject());
            assertNotNull(wechatCertInfo.getIssuer());
            assertNotNull(wechatCertInfo.getSerialNumber());
        }

        CertificateService.CertificateInfo alipayCertInfo = 
            certificateService.validateX509Certificate("alipay", "appCertPublicKey.crt");
        
        if (alipayCertInfo != null) {
            assertNotNull(alipayCertInfo.getSubject());
            assertNotNull(alipayCertInfo.getIssuer());
            assertNotNull(alipayCertInfo.getSerialNumber());
        }
    }

    @Test
    public void testNonExistentCertificate() {
        // 测试不存在的证书文件
        assertFalse(certificateService.certificateExists("wechat", "non_existent.pem"));
        assertFalse(certificateService.certificateExists("alipay", "non_existent.crt"));
    }

    @Test
    public void testCertificateConfiguration() {
        // 测试证书配置
        CertificateProperties.WechatPayConfig wechatConfig = certificateProperties.getWechatPay();
        assertNotNull(wechatConfig);
        assertEquals("wechat", wechatConfig.getCertDir());
        assertEquals("apiclient_key.pem", wechatConfig.getDev().getPrivateKeyFile());

        CertificateProperties.AlipayConfig alipayConfig = certificateProperties.getAlipay();
        assertNotNull(alipayConfig);
        assertEquals("alipay", alipayConfig.getCertDir());
        assertEquals("app_private_key.pem", alipayConfig.getAppPrivateKeyFile());
    }
}
