package com.cf.imes.module.system.dal.dataobject.mail;

import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
 * {@link MailLogDO} 的单元测试
 *
 * @author Gqr
 * @since 2025/12/15 11:32
 */
class MailLogDOTest {
    @Test
    void testJavaSerialization() throws Exception {
        // given
        Map<String, Object> params = new HashMap<>();
        params.put("code", "RESET_PWD");
        params.put("expire", 30);

        MailLogDO original = new MailLogDO();
        original.setId(1L);
        original.setUserId(100L);
        original.setUserType(1);
        original.setToMail("test@example.com");
        original.setAccountId(10L);
        original.setFromMail("noreply@example.com");
        original.setTemplateId(20L);
        original.setTemplateCode("RESET_PWD");
        original.setTemplateNickname("系统邮件");
        original.setTemplateTitle("重置密码");
        original.setTemplateContent("您的验证码是 ${code}");
        original.setTemplateParams(params);
        original.setSendStatus(1);
        original.setSendTime(LocalDateTime.now());
        original.setSendMessageId("msg-123");
        original.setSendException(null);

        // when：序列化
        byte[] bytes;
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(bos)) {

            oos.writeObject(original);
            bytes = bos.toByteArray();
        }

        // then：反序列化
        MailLogDO deserialized;
        try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
             ObjectInputStream ois = new ObjectInputStream(bis)) {

            deserialized = (MailLogDO) ois.readObject();
        }

        // 基本断言（确保 read/writeObject 被执行）
        assertNotNull(deserialized);
        assertEquals(original.getId(), deserialized.getId());
        assertEquals(original.getUserId(), deserialized.getUserId());
        assertEquals(original.getToMail(), deserialized.getToMail());
        assertEquals(original.getTemplateCode(), deserialized.getTemplateCode());
        assertEquals(original.getTemplateParams(), deserialized.getTemplateParams());
        assertEquals(original.getSendMessageId(), deserialized.getSendMessageId());
    }
}
