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

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Collections;
import org.junit.jupiter.api.AfterEach;
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.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.exception.GlobalExceptionHandler;
import com.ruoyi.web.modules.architecture.domain.AppMedicationDrugQueryVo;
import com.ruoyi.web.modules.architecture.service.IAppMedicationDrugService;

@ExtendWith(MockitoExtension.class)
@DisplayName("用药工具移动端 HTTP MockMvc")
class BizAppMedicationDrugControllerApiTest
{
    @Mock
    private IAppMedicationDrugService appMedicationDrugService;

    @InjectMocks
    private BizAppMedicationDrugController controller;

    private MockMvc mockMvc;

    @BeforeEach
    void setUp()
    {
        mockMvc = MockMvcBuilders.standaloneSetup(controller)
                .setControllerAdvice(new GlobalExceptionHandler())
                .build();
        SysUser u = new SysUser();
        u.setUserId(1L);
        u.setUserName("herder");
        LoginUser lu = new LoginUser(1L, 200L, u, Collections.emptySet());
        SecurityContextHolder.getContext().setAuthentication(
                new UsernamePasswordAuthenticationToken(lu, "", lu.getAuthorities()));
    }

    @AfterEach
    void tearDown()
    {
        SecurityContextHolder.clearContext();
    }

    @Test
    @DisplayName("GET /app/medication/drug/names")
    void drugNames() throws Exception
    {
        when(appMedicationDrugService.selectDrugNameOptions(eq("青"))).thenReturn(Collections.singletonList("青霉素"));
        mockMvc.perform(get("/app/medication/drug/names").param("keyword", "青"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data[0]").value("青霉素"));
    }

    @Test
    @DisplayName("GET /app/medication/drug/query")
    void query() throws Exception
    {
        AppMedicationDrugQueryVo vo = new AppMedicationDrugQueryVo();
        vo.setDrugName("青霉素");
        vo.setDrugType(1);
        vo.setDrugTypeName("抗生素");
        vo.setWithdrawalDays(7);
        vo.setIncompatibilityCompanions("链霉素");
        when(appMedicationDrugService.queryByDrugName(eq("青霉素"))).thenReturn(vo);

        mockMvc.perform(get("/app/medication/drug/query").param("drugName", "青霉素"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.drugTypeName").value("抗生素"))
                .andExpect(jsonPath("$.data.incompatibilityCompanions").value("链霉素"));
    }
}
