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

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.BeforeEach;
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.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.ruoyi.web.modules.content.service.IBannerService;
import com.ruoyi.web.modules.content.vo.HomeBannerAppVO;

/**
 * C 端首页 Banner 接口测试（BAN-API-016～）
 */
@ExtendWith(MockitoExtension.class)
class HomeBannerAppControllerTest
{
    @Mock
    private IBannerService bannerService;

    @InjectMocks
    private HomeBannerAppController controller;

    private MockMvc mockMvc;

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

    @Test
    void banners_returns200() throws Exception
    {
        HomeBannerAppVO vo = new HomeBannerAppVO();
        vo.setBannerId(1L);
        vo.setBannerImage("/profile/upload/b1.jpg");
        vo.setSortNo(0);
        when(bannerService.listBannersForApp()).thenReturn(Collections.singletonList(vo));

        mockMvc.perform(get("/api/home/banners"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data[0].bannerId").value(1))
                .andExpect(jsonPath("$.data[0].bannerImage").value("/profile/upload/b1.jpg"))
                .andExpect(jsonPath("$.data[0].sortNo").value(0));
    }

    @Test
    void banners_emptyList() throws Exception
    {
        when(bannerService.listBannersForApp()).thenReturn(Collections.emptyList());

        mockMvc.perform(get("/api/home/banners"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data").isArray())
                .andExpect(jsonPath("$.data").isEmpty());
    }
}
