package cn.novalon.gym.manage.db.converter;

import cn.novalon.gym.manage.sys.core.domain.SysExceptionLog;
import cn.novalon.gym.manage.db.entity.SysExceptionLogEntity;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.LocalDateTime;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(MockitoExtension.class)
class SysExceptionLogConverterTest {

    private SysExceptionLogConverter converter;
    private SysExceptionLogEntity testEntity;
    private SysExceptionLog testDomain;

    @BeforeEach
    void setUp() {
        converter = new SysExceptionLogConverter();
        
        testEntity = new SysExceptionLogEntity();
        testEntity.setId(1L);
        testEntity.setUsername("admin");
        testEntity.setTitle("系统异常");
        testEntity.setExceptionName("NullPointerException");
        testEntity.setMethodName("getUserById");
        testEntity.setMethodParams("{\"id\":1}");
        testEntity.setExceptionMsg("空指针异常");
        testEntity.setExceptionStack("java.lang.NullPointerException\n\tat...");
        testEntity.setIp("127.0.0.1");
        testEntity.setCreateTime(LocalDateTime.now());
        
        testDomain = new SysExceptionLog();
        testDomain.setId(1L);
        testDomain.setUsername("admin");
        testDomain.setTitle("系统异常");
        testDomain.setExceptionName("NullPointerException");
        testDomain.setMethodName("getUserById");
        testDomain.setMethodParams("{\"id\":1}");
        testDomain.setExceptionMsg("空指针异常");
        testDomain.setExceptionStack("java.lang.NullPointerException\n\tat...");
        testDomain.setIp("127.0.0.1");
        testDomain.setCreateTime(LocalDateTime.now());
    }

    @Test
    void testToDomain() {
        SysExceptionLog result = converter.toDomain(testEntity);
        
        assertThat(result).isNotNull();
        assertThat(result.getId()).isEqualTo(testEntity.getId());
        assertThat(result.getUsername()).isEqualTo(testEntity.getUsername());
        assertThat(result.getTitle()).isEqualTo(testEntity.getTitle());
        assertThat(result.getExceptionName()).isEqualTo(testEntity.getExceptionName());
        assertThat(result.getMethodName()).isEqualTo(testEntity.getMethodName());
        assertThat(result.getMethodParams()).isEqualTo(testEntity.getMethodParams());
        assertThat(result.getExceptionMsg()).isEqualTo(testEntity.getExceptionMsg());
        assertThat(result.getExceptionStack()).isEqualTo(testEntity.getExceptionStack());
        assertThat(result.getIp()).isEqualTo(testEntity.getIp());
        assertThat(result.getCreateTime()).isEqualTo(testEntity.getCreateTime());
    }

    @Test
    void testToEntity() {
        SysExceptionLogEntity result = converter.toEntity(testDomain);
        
        assertThat(result).isNotNull();
        assertThat(result.getId()).isEqualTo(testDomain.getId());
        assertThat(result.getUsername()).isEqualTo(testDomain.getUsername());
        assertThat(result.getTitle()).isEqualTo(testDomain.getTitle());
        assertThat(result.getExceptionName()).isEqualTo(testDomain.getExceptionName());
        assertThat(result.getMethodName()).isEqualTo(testDomain.getMethodName());
        assertThat(result.getMethodParams()).isEqualTo(testDomain.getMethodParams());
        assertThat(result.getExceptionMsg()).isEqualTo(testDomain.getExceptionMsg());
        assertThat(result.getExceptionStack()).isEqualTo(testDomain.getExceptionStack());
        assertThat(result.getIp()).isEqualTo(testDomain.getIp());
        assertThat(result.getCreateTime()).isEqualTo(testDomain.getCreateTime());
    }

    @Test
    void testToDomainWithNull() {
        SysExceptionLog result = converter.toDomain(null);
        assertThat(result).isNull();
    }

    @Test
    void testToEntityWithNull() {
        SysExceptionLogEntity result = converter.toEntity(null);
        assertThat(result).isNull();
    }
}