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.AppMedicationCompatibilityCheckVo;
import com.ruoyi.web.modules.architecture.service.IAppMedicationCompatibilityService;
import com.ruoyi.web.modules.architecture.service.IAppMedicationDrugService;

@ExtendWith(MockitoExtension.class)
@DisplayName("用药配伍移动端 HTTP MockMvc")
class BizAppMedicationCompatibilityControllerApiTest
{
    @Mock
    private IAppMedicationDrugService appMedicationDrugService;

    @Mock
    private IAppMedicationCompatibilityService appMedicationCompatibilityService;

    @InjectMocks
    private BizAppMedicationCompatibilityController controller;

    private MockMvc mockMvc;

    @BeforeEach
    void setUp()
    {
        mockMvc = MockMvcBuilders.standaloneSetup(controller)
                .setControllerAdvice(new GlobalExceptionHandler())
                .build();
        SysUser u = new SysUser();
        u.setUserId(1L);
        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/compatibility/check")
    void check() throws Exception
    {
        AppMedicationCompatibilityCheckVo vo = new AppMedicationCompatibilityCheckVo();
        vo.setDrugName1("青霉素");
        vo.setDrugName2("链霉素");
        vo.setHasIncompatibility(true);
        vo.setCompatResult("禁止合用");
        when(appMedicationCompatibilityService.checkCompatibility(eq("青霉素"), eq("链霉素"))).thenReturn(vo);

        mockMvc.perform(get("/app/medication/compatibility/check")
                .param("drugName1", "青霉素")
                .param("drugName2", "链霉素"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.hasIncompatibility").value(true))
                .andExpect(jsonPath("$.data.compatResult").value("禁止合用"));
    }
}
