package cn.novalon.manage.sys.handler.dictionary;

import cn.novalon.manage.sys.core.domain.Dictionary;
import cn.novalon.manage.sys.core.service.IDictionaryService;
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 org.springframework.http.HttpStatus;
import org.springframework.mock.web.reactive.function.server.MockServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

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

/**
 * 字典处理器单元测试类
 * 
 * @author 张翔
 * @date 2026-03-14
 */
@ExtendWith(MockitoExtension.class)
class DictionaryHandlerTest {

    @Mock
    private IDictionaryService service;

    private DictionaryHandler handler;

    @BeforeEach
    void setUp() {
        handler = new DictionaryHandler(service);
    }

    @Test
    void testGetAllDictionaries() {
        Dictionary dict = new Dictionary();
        dict.setId(1L);
        dict.setType("type1");

        when(service.findAll()).thenReturn(Flux.just(dict));

        Mono<ServerResponse> responseMono = handler.getAllDictionaries(null);

        StepVerifier.create(responseMono)
                .expectNextMatches(response -> response.statusCode().equals(HttpStatus.OK))
                .verifyComplete();

        verify(service).findAll();
    }

    @Test
    void testGetDictionaryById() {
        Dictionary dict = new Dictionary();
        dict.setId(1L);
        dict.setType("type1");

        when(service.findById(1L)).thenReturn(Mono.just(dict));

        MockServerRequest request = MockServerRequest.builder()
                .pathVariable("id", "1")
                .build();

        Mono<ServerResponse> responseMono = handler.getDictionaryById(request);

        StepVerifier.create(responseMono)
                .expectNextMatches(response -> response.statusCode().equals(HttpStatus.OK))
                .verifyComplete();

        verify(service).findById(1L);
    }

    @Test
    void testCreateDictionary() {
        Dictionary dict = new Dictionary();
        dict.setId(1L);
        dict.setType("type1");

        when(service.save(any())).thenReturn(Mono.just(dict));

        MockServerRequest request = MockServerRequest.builder()
                .body(Mono.just(dict));

        Mono<ServerResponse> responseMono = handler.createDictionary(request);

        StepVerifier.create(responseMono)
                .expectNextMatches(response -> response.statusCode().equals(HttpStatus.CREATED))
                .verifyComplete();

        verify(service).save(any());
    }
}