package com.ruoyi.web.modules.region.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.region.service.IRegionService;
import com.ruoyi.web.modules.region.vo.RegionTreeVO;

@ExtendWith(MockitoExtension.class)
class RegionControllerTest
{
    @Mock
    private IRegionService regionService;

    @InjectMocks
    private RegionController controller;

    private MockMvc mockMvc;

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

    @Test
    void tree_returns200() throws Exception
    {
        RegionTreeVO province = new RegionTreeVO();
        province.setId(1);
        province.setName("北京市");
        province.setType(1);
        province.setCode(110000);
        when(regionService.getRegionTree()).thenReturn(Collections.singletonList(province));

        mockMvc.perform(get("/agri/region/tree"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data[0].name").value("北京市"));
    }
}
