package com.ruoyi.info.collection.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.info.collection.domain.dto.CcdiCreditInfoQueryDTO;
import com.ruoyi.info.collection.domain.vo.CreditInfoListVO;
import com.ruoyi.info.collection.service.ICcdiCreditInfoService;
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.mock.web.MockHttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class CcdiCreditInfoControllerTest {

    @InjectMocks
    private CcdiCreditInfoController controller;

    @Mock
    private ICcdiCreditInfoService service;

    @Test
    void list_shouldDelegateWithPageRequest() {
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.addParameter("pageNum", "1");
        request.addParameter("pageSize", "10");
        RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
        try {
            when(service.selectCreditInfoPage(any(), any())).thenReturn(new Page<CreditInfoListVO>(1, 10, 0));

            TableDataInfo result = controller.list(new CcdiCreditInfoQueryDTO());

            assertEquals(0L, result.getTotal());
        } finally {
            RequestContextHolder.resetRequestAttributes();
        }
    }

    @Test
    void remove_shouldCallDeleteByPersonId() {
        when(service.deleteByPersonId("330101199001010011")).thenReturn(1);

        AjaxResult result = controller.remove("330101199001010011");

        assertEquals(200, result.get("code"));
    }
}
