package com.ruoyi.web.modules.industryservice.research.controller;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
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 java.util.Collections;
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.ruoyi.web.modules.industryservice.domain.dto.OpenApiPageResult;
import com.ruoyi.web.modules.industryservice.research.domain.dto.ResearchTokenVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchPastureVo;
import com.ruoyi.web.modules.industryservice.research.service.IResearchOpenApiService;

@ExtendWith(MockitoExtension.class)
@DisplayName("科研开放接口 HTTP")
class ResearchOpenApiControllerApiTest
{
    @Mock
    private IResearchOpenApiService researchOpenApiService;

    @InjectMocks
    private ResearchOpenApiController controller;

    private MockMvc mockMvc;

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

    @Test
    @DisplayName("POST /auth/token 成功")
    void tokenOk() throws Exception
    {
        ResearchTokenVo token = new ResearchTokenVo();
        token.setAccessToken("test-token");
        token.setTokenType("Bearer");
        token.setExpiresIn(3600);
        when(researchOpenApiService.authenticate("demo-key", "demo-secret")).thenReturn(token);

        mockMvc.perform(post("/open-api/v1/research/auth/token")
                .contentType(MediaType.APPLICATION_JSON)
                .content("{\"appKey\":\"demo-key\",\"appSecret\":\"demo-secret\"}"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.accessToken").value("test-token"));
    }

    @Test
    @DisplayName("GET /pastures 成功")
    void listPastures() throws Exception
    {
        ResearchPastureVo row = new ResearchPastureVo();
        row.setPastureId(1L);
        row.setPastureName("测试牧场");
        OpenApiPageResult<ResearchPastureVo> page = new OpenApiPageResult<>();
        page.setPageNum(1);
        page.setPageSize(20);
        page.setTotal(1L);
        page.setRecords(Collections.singletonList(row));
        when(researchOpenApiService.listPastures(anyInt(), anyInt(), any(), any())).thenReturn(page);

        mockMvc.perform(get("/open-api/v1/research/pastures"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.records[0].pastureName").value("测试牧场"));
    }
}
