Files
ccdi/ccdi-lsfx/src/test/java/com/ruoyi/lsfx/controller/CreditParseControllerTest.java

111 lines
4.3 KiB
Java
Raw Normal View History

2026-03-23 15:54:50 +08:00
package com.ruoyi.lsfx.controller;
import com.ruoyi.common.core.domain.AjaxResult;
2026-03-23 16:01:46 +08:00
import com.ruoyi.lsfx.client.CreditParseClient;
import com.ruoyi.lsfx.domain.response.CreditParseInvokeResponse;
2026-03-23 16:01:46 +08:00
import com.ruoyi.lsfx.exception.LsfxApiException;
import com.ruoyi.lsfx.util.HttpUtil;
2026-03-23 15:54:50 +08:00
import org.junit.jupiter.api.Test;
2026-03-23 16:01:46 +08:00
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
2026-03-23 16:01:46 +08:00
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
2026-03-23 15:54:50 +08:00
import java.util.Map;
2026-03-23 15:54:50 +08:00
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
2026-03-23 16:01:46 +08:00
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
2026-03-23 16:01:46 +08:00
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;
2026-03-23 16:01:46 +08:00
import static org.mockito.Mockito.when;
2026-03-23 15:54:50 +08:00
2026-03-23 16:01:46 +08:00
@ExtendWith(MockitoExtension.class)
2026-03-23 15:54:50 +08:00
class CreditParseControllerTest {
2026-03-23 16:01:46 +08:00
@Mock
private CreditParseClient client;
@InjectMocks
private CreditParseController controller;
2026-03-23 15:54:50 +08:00
@Test
void parse_shouldRejectBlankRemotePath() {
AjaxResult result = controller.parse(null, null);
2026-03-23 15:54:50 +08:00
assertEquals(500, result.get("code"));
}
@Test
void shouldUseDefaultModelWhenMissing() {
CreditParseInvokeResponse response = new CreditParseInvokeResponse();
response.setSuccess(true);
response.setCode(1000);
2026-03-23 16:01:46 +08:00
String remotePath = "http://127.0.0.1:62318/profile/credit-html/a.html";
when(client.parse(eq("LXCUSTALL"), eq(remotePath))).thenReturn(response);
2026-03-23 16:01:46 +08:00
AjaxResult result = controller.parse(remotePath, null);
2026-03-23 16:01:46 +08:00
assertEquals(200, result.get("code"));
assertSame(response, result.get("data"));
}
@Test
void shouldReturnAjaxErrorWhenClientThrows() {
when(client.parse(anyString(), anyString()))
2026-03-23 16:01:46 +08:00
.thenThrow(new LsfxApiException("超时"));
AjaxResult result = controller.parse("http://127.0.0.1:62318/profile/credit-html/a.html", null);
2026-03-23 16:01:46 +08:00
assertEquals(500, result.get("code"));
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
void creditParseClient_shouldPostUrlEncodedRemotePathParameters() {
HttpUtil httpUtil = mock(HttpUtil.class);
CreditParseClient parseClient = new CreditParseClient();
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");
CreditParseInvokeResponse response = new CreditParseInvokeResponse();
response.setSuccess(true);
response.setCode(1000);
when(httpUtil.postUrlEncodedForm(
eq("http://tz/api/service/interface/invokeService/xfeature"),
org.mockito.ArgumentMatchers.<Map<String, Object>>any(),
isNull(),
eq(CreditParseInvokeResponse.class)
)).thenReturn(response);
String remotePath = "http://127.0.0.1:62318/profile/credit-html/a.html";
CreditParseInvokeResponse actual = parseClient.parse(remotePath);
assertSame(response, actual);
ArgumentCaptor<Map<String, Object>> paramsCaptor = ArgumentCaptor.forClass((Class) Map.class);
verify(httpUtil).postUrlEncodedForm(
eq("http://tz/api/service/interface/invokeService/xfeature"),
paramsCaptor.capture(),
isNull(),
eq(CreditParseInvokeResponse.class)
);
Map<String, Object> 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"));
}
2026-03-23 15:54:50 +08:00
}