package cn.novalon.manage.db.converter;

import cn.novalon.manage.sys.core.domain.OperationLog;
import cn.novalon.manage.db.entity.OperationLogEntity;
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 OperationLogConverterTest {

    private OperationLogConverter converter;
    private OperationLogEntity testEntity;
    private OperationLog testDomain;

    @BeforeEach
    void setUp() {
        converter = new OperationLogConverter();
        
        testEntity = new OperationLogEntity();
        testEntity.setId(1L);
        testEntity.setUsername("admin");
        testEntity.setOperation("用户登录");
        testEntity.setMethod("login");
        testEntity.setParams("{\"username\":\"admin\"}");
        testEntity.setResult("success");
        testEntity.setIp("127.0.0.1");
        testEntity.setDuration(100L);
        testEntity.setStatus("0");
        testEntity.setErrorMsg(null);
        testEntity.setCreatedAt(LocalDateTime.now());
        testEntity.setUpdatedAt(LocalDateTime.now());
        
        testDomain = new OperationLog();
        testDomain.setId(1L);
        testDomain.setUsername("admin");
        testDomain.setOperation("用户登录");
        testDomain.setMethod("login");
        testDomain.setParams("{\"username\":\"admin\"}");
        testDomain.setResult("success");
        testDomain.setIp("127.0.0.1");
        testDomain.setDuration(100L);
        testDomain.setStatus("0");
        testDomain.setErrorMsg(null);
        testDomain.setCreatedAt(LocalDateTime.now());
        testDomain.setUpdatedAt(LocalDateTime.now());
    }

    @Test
    void testToDomain() {
        OperationLog result = converter.toDomain(testEntity);
        
        assertThat(result).isNotNull();
        assertThat(result.getId()).isEqualTo(testEntity.getId());
        assertThat(result.getUsername()).isEqualTo(testEntity.getUsername());
        assertThat(result.getOperation()).isEqualTo(testEntity.getOperation());
        assertThat(result.getMethod()).isEqualTo(testEntity.getMethod());
        assertThat(result.getParams()).isEqualTo(testEntity.getParams());
        assertThat(result.getResult()).isEqualTo(testEntity.getResult());
        assertThat(result.getIp()).isEqualTo(testEntity.getIp());
        assertThat(result.getDuration()).isEqualTo(testEntity.getDuration());
        assertThat(result.getStatus()).isEqualTo(testEntity.getStatus());
        assertThat(result.getErrorMsg()).isEqualTo(testEntity.getErrorMsg());
    }

    @Test
    void testToEntity() {
        OperationLogEntity result = converter.toEntity(testDomain);
        
        assertThat(result).isNotNull();
        assertThat(result.getId()).isEqualTo(testDomain.getId());
        assertThat(result.getUsername()).isEqualTo(testDomain.getUsername());
        assertThat(result.getOperation()).isEqualTo(testDomain.getOperation());
        assertThat(result.getMethod()).isEqualTo(testDomain.getMethod());
        assertThat(result.getParams()).isEqualTo(testDomain.getParams());
        assertThat(result.getResult()).isEqualTo(testDomain.getResult());
        assertThat(result.getIp()).isEqualTo(testDomain.getIp());
        assertThat(result.getDuration()).isEqualTo(testDomain.getDuration());
        assertThat(result.getStatus()).isEqualTo(testDomain.getStatus());
        assertThat(result.getErrorMsg()).isEqualTo(testDomain.getErrorMsg());
    }

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

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