package com.ruoyi.web.modules.app.controller;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.framework.web.exception.GlobalExceptionHandler;
import com.ruoyi.web.modules.app.domain.AppUserRegisterBody;
import com.ruoyi.web.modules.app.service.IAppUserRegisterService;

@ExtendWith(MockitoExtension.class)
@DisplayName("小程序认证 HTTP MockMvc")
class AppAuthControllerApiTest
{

    @Mock
    private IAppUserRegisterService appUserRegisterService;

    @InjectMocks
    private AppAuthController controller;

    private MockMvc mockMvc;

    private final ObjectMapper objectMapper = new ObjectMapper();

    @BeforeEach
    void setUp()
    {
        mockMvc = MockMvcBuilders.standaloneSetup(controller)
                .setControllerAdvice(new GlobalExceptionHandler())
                .build();
    }

    @Test
    @DisplayName("GET usernameAvailable 可用")
    void usernameAvailable() throws Exception
    {
        when(appUserRegisterService.isUsernameAvailable("new01")).thenReturn(true);
        mockMvc.perform(get("/app/auth/usernameAvailable").param("userName", "new01"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.available").value(true))
                .andExpect(jsonPath("$.data.userName").value("new01"));
    }

    @Test
    @DisplayName("POST register 成功")
    void registerOk() throws Exception
    {
        AppUserRegisterBody body = new AppUserRegisterBody();
        body.setNickName("牧民");
        body.setUserName("herder01");
        body.setPassword("abc12345");
        when(appUserRegisterService.register(any())).thenReturn(100L);

        mockMvc.perform(post("/app/auth/register")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(body)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.userId").value(100))
                .andExpect(jsonPath("$.data.userName").value("herder01"));
    }

    @Test
    @DisplayName("POST register 参数校验失败")
    void registerValidationFail() throws Exception
    {
        AppUserRegisterBody body = new AppUserRegisterBody();
        body.setNickName("");
        body.setUserName("a");
        body.setPassword("12");

        mockMvc.perform(post("/app/auth/register")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(body)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(500));
    }

    @Test
    @DisplayName("GET usernameAvailable 缺参")
    void usernameMissing() throws Exception
    {
        mockMvc.perform(get("/app/auth/usernameAvailable"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(500));
    }
}
