package com.ruoyi.lsfx.controller; import com.fasterxml.jackson.databind.ObjectMapper; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.lsfx.client.CreditParseClient; import com.ruoyi.lsfx.domain.response.CreditParseInvokeResponse; import com.ruoyi.lsfx.exception.LsfxApiException; import com.ruoyi.lsfx.util.HttpUtil; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.util.ReflectionTestUtils; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class CreditParseControllerTest { @Mock private CreditParseClient client; @InjectMocks private CreditParseController controller; @Test void parse_shouldRejectBlankRemotePath() { AjaxResult result = controller.parse(null, null); assertEquals(500, result.get("code")); } @Test void shouldUseDefaultModelWhenMissing() { CreditParseInvokeResponse response = new CreditParseInvokeResponse(); response.setSuccess(true); response.setCode(1000); String remotePath = "http://127.0.0.1:62318/profile/credit-html/a.html"; when(client.parse(eq("LXCUSTALL"), eq(remotePath))).thenReturn(response); AjaxResult result = controller.parse(remotePath, null); assertEquals(200, result.get("code")); assertSame(response, result.get("data")); } @Test void shouldReturnAjaxErrorWhenClientThrows() { when(client.parse(anyString(), anyString())) .thenThrow(new LsfxApiException("超时")); AjaxResult result = controller.parse("http://127.0.0.1:62318/profile/credit-html/a.html", null); assertEquals(500, result.get("code")); } @Test @SuppressWarnings({"unchecked", "rawtypes"}) void creditParseClient_shouldParseSuccessResponseWithStringPayload() throws Exception { HttpUtil httpUtil = mock(HttpUtil.class); CreditParseClient parseClient = new CreditParseClient(); ObjectMapper objectMapper = new ObjectMapper(); ReflectionTestUtils.setField(parseClient, "httpUtil", httpUtil); ReflectionTestUtils.setField(parseClient, "creditParseUrl", "http://tz/api/service/interface/invokeService/xfeature"); ReflectionTestUtils.setField(parseClient, "orgCode", "902000"); ReflectionTestUtils.setField(parseClient, "runType", "1"); ReflectionTestUtils.setField(parseClient, "defaultModel", "LXCUSTALL"); ReflectionTestUtils.setField(parseClient, "objectMapper", objectMapper); String payload = "{\"lx_header\":{\"query_cert_no\":\"330101199001010011\",\"query_cust_name\":\"张三\",\"report_time\":\"2026-03-24\"},\"lx_debt\":{\"uncle_bank_house_bal\":\"1\"},\"lx_publictype\":{\"civil_cnt\":1}}"; when(httpUtil.postUrlEncodedFormForString( eq("http://tz/api/service/interface/invokeService/xfeature"), org.mockito.ArgumentMatchers.>any(), isNull() )).thenReturn("{\"success\":true,\"code\":10000,\"data\":{\"mappingOutputFields\":{\"message\":\"\",\"status_code\":\"0\",\"payload\":" + objectMapper.writeValueAsString(payload) + "}}}"); String remotePath = "http://127.0.0.1:62318/profile/credit-html/a.html"; CreditParseInvokeResponse actual = parseClient.parse(remotePath); assertEquals(true, actual.getSuccess()); assertEquals(10000, actual.getCode()); assertEquals("330101199001010011", actual.getData().getMappingOutputFields() .getPayload().getLxHeader().get("query_cert_no")); ArgumentCaptor> paramsCaptor = ArgumentCaptor.forClass((Class) Map.class); verify(httpUtil).postUrlEncodedFormForString( eq("http://tz/api/service/interface/invokeService/xfeature"), paramsCaptor.capture(), isNull() ); Map params = paramsCaptor.getValue(); assertNotNull(params.get("serialNum")); assertTrue(params.get("serialNum").toString().startsWith("CCDI_CREDIT_")); assertEquals("902000", params.get("orgCode")); assertEquals("1", params.get("runType")); assertEquals(remotePath, params.get("remotePath")); assertEquals("LXCUSTALL", params.get("model")); } }