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

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 OperateLogDO} 的单元测试
 *
 * @author Gqr
 * @since 2025/12/15 11:34
 */
class OperateLogDOTest {
    @Test
    void testJavaSerialization() throws Exception {
        // given
        Map<String, Object> exts = new HashMap<>();
        exts.put("orderId", 10001L);
        exts.put("status", "PAID");

        OperateLogDO original = new OperateLogDO();
        original.setId(1L);
        original.setTraceId("trace-123");
        original.setUserId(100L);
        original.setUserType(1);
        original.setModule("order");
        original.setName("createOrder");
        original.setType(1);
        original.setContent("创建订单");
        original.setExts(exts);
        original.setRequestMethod("POST");
        original.setRequestUrl("/api/order/create");
        original.setUserIp("127.0.0.1");
        original.setUserAgent("Mozilla/5.0");
        original.setJavaMethod("OrderController#create");
        original.setJavaMethodArgs("{\"productId\":1}");
        original.setStartTime(LocalDateTime.now());
        original.setDuration(120);
        original.setResultCode(0);
        original.setResultMsg("success");
        original.setResultData("{\"orderId\":10001}");

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

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

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

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

        // 核心断言（确保 read/writeObject 被调用）
        assertNotNull(deserialized);
        assertEquals(original.getId(), deserialized.getId());
        assertEquals(original.getTraceId(), deserialized.getTraceId());
        assertEquals(original.getUserId(), deserialized.getUserId());
        assertEquals(original.getModule(), deserialized.getModule());
        assertEquals(original.getName(), deserialized.getName());
        assertEquals(original.getExts(), deserialized.getExts());
        assertEquals(original.getResultCode(), deserialized.getResultCode());
        assertEquals(original.getResultData(), deserialized.getResultData());
    }
}
