package cn.novalon.manage.sys.core.service.impl;

import cn.novalon.manage.sys.core.domain.SysDictType;
import cn.novalon.manage.sys.core.repository.ISysDictTypeRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.time.LocalDateTime;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class SysDictTypeServiceTest {

    @Mock
    private ISysDictTypeRepository repository;

    private SysDictTypeService dictTypeService;
    private SysDictType testDictType;

    @BeforeEach
    void setUp() {
        dictTypeService = new SysDictTypeService(repository);
        
        testDictType = new SysDictType();
        testDictType.setId(1L);
        testDictType.setDictName("系统状态");
        testDictType.setDictType("sys_status");
        testDictType.setStatus("0");
        testDictType.setRemark("系统状态字典");
        testDictType.setCreatedAt(LocalDateTime.now());
    }

    @Test
    void testFindAll() {
        when(repository.findByDeletedAtIsNull()).thenReturn(Flux.just(testDictType));
        
        Flux<SysDictType> result = dictTypeService.findAll();
        
        StepVerifier.create(result)
                .expectNext(testDictType)
                .verifyComplete();
        
        verify(repository).findByDeletedAtIsNull();
    }

    @Test
    void testFindById() {
        when(repository.findById(1L)).thenReturn(Mono.just(testDictType));
        
        Mono<SysDictType> result = dictTypeService.findById(1L);
        
        StepVerifier.create(result)
                .expectNext(testDictType)
                .verifyComplete();
        
        verify(repository).findById(1L);
    }

    @Test
    void testFindByDictType() {
        when(repository.findByDictTypeAndDeletedAtIsNull("sys_status")).thenReturn(Mono.just(testDictType));
        
        Mono<SysDictType> result = dictTypeService.findByDictType("sys_status");
        
        StepVerifier.create(result)
                .expectNext(testDictType)
                .verifyComplete();
        
        verify(repository).findByDictTypeAndDeletedAtIsNull("sys_status");
    }

    @Test
    void testSave() {
        when(repository.save(any(SysDictType.class))).thenReturn(Mono.just(testDictType));
        
        Mono<SysDictType> result = dictTypeService.save(testDictType);
        
        StepVerifier.create(result)
                .expectNext(testDictType)
                .verifyComplete();
        
        verify(repository).save(any(SysDictType.class));
    }

    @Test
    void testDeleteById() {
        when(repository.deleteByIdAndDeletedAtIsNull(1L)).thenReturn(Mono.empty());
        
        Mono<Void> result = dictTypeService.deleteById(1L);
        
        StepVerifier.create(result)
                .verifyComplete();
        
        verify(repository).deleteByIdAndDeletedAtIsNull(1L);
    }
}