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

import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
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 NotifyMessageDO} 的单元测试
 *
 * @author Gqr
 * @since 2025/12/15 11:29
 */
class NotifyMessageDOTest {
    @Test
    void testJavaSerialization() throws Exception {
        // given
        Map<String, Object> params = new HashMap<>();
        params.put("orderId", 123L);
        params.put("amount", 99.9);

        NotifyMessageDO original = NotifyMessageDO.builder()
                .id(1L)
                .userId(100L)
                .userType(1)
                .templateId(10L)
                .templateCode("ORDER_PAY")
                .templateType(1)
                .templateNickname("系统通知")
                .templateContent("订单已支付")
                .templateParams(params)
                .build();

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

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

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

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

        // 验证关键字段
        assertNotNull(deserialized);
        assertEquals(original.getId(), deserialized.getId());
        assertEquals(original.getUserId(), deserialized.getUserId());
        assertEquals(original.getTemplateCode(), deserialized.getTemplateCode());
        assertEquals(original.getTemplateContent(), deserialized.getTemplateContent());
        assertEquals(original.getTemplateParams(), deserialized.getTemplateParams());
    }
}
