package com.ruoyi.web.modules.trading.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.math.BigDecimal;
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.trading.domain.vo.AppYakMarketYesterdayVo;
import com.ruoyi.web.modules.trading.service.IAppYakMarketService;

@ExtendWith(MockitoExtension.class)
@DisplayName("牦牛行情移动端 HTTP MockMvc")
class BizAppYakMarketControllerApiTest
{
    @Mock
    private IAppYakMarketService appYakMarketService;

    @InjectMocks
    private BizAppYakMarketController 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/yakMarket/yesterday")
    void yesterday() throws Exception
    {
        AppYakMarketYesterdayVo vo = new AppYakMarketYesterdayVo();
        vo.setAvgPrice(new BigDecimal("18.68"));
        vo.setPriceUnitName("元/斤");
        when(appYakMarketService.selectYesterdayQuote()).thenReturn(vo);

        mockMvc.perform(get("/app/yakMarket/yesterday"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.avgPrice").value(18.68))
                .andExpect(jsonPath("$.data.priceUnitName").value("元/斤"));
    }

    @Test
    @DisplayName("GET /app/yakMarket/trend?days=30")
    void trend30() throws Exception
    {
        when(appYakMarketService.selectTrend(eq(30))).thenReturn(Collections.emptyList());

        mockMvc.perform(get("/app/yakMarket/trend").param("days", "30"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }
}
