新增征信解析客户端
This commit is contained in:
@@ -0,0 +1,47 @@
|
|||||||
|
package com.ruoyi.lsfx.client;
|
||||||
|
|
||||||
|
import com.ruoyi.lsfx.domain.response.CreditParseResponse;
|
||||||
|
import com.ruoyi.lsfx.exception.LsfxApiException;
|
||||||
|
import com.ruoyi.lsfx.util.HttpUtil;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class CreditParseClient {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private HttpUtil httpUtil;
|
||||||
|
|
||||||
|
@Value("${credit-parse.api.url}")
|
||||||
|
private String creditParseUrl;
|
||||||
|
|
||||||
|
public CreditParseResponse parse(String model, String hType, File file) {
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
log.info("【征信解析】开始调用: fileName={}, model={}, hType={}", file.getName(), model, hType);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("model", model);
|
||||||
|
params.put("hType", hType);
|
||||||
|
params.put("file", file);
|
||||||
|
|
||||||
|
CreditParseResponse response = httpUtil.uploadFile(creditParseUrl, params, null, CreditParseResponse.class);
|
||||||
|
|
||||||
|
long elapsed = System.currentTimeMillis() - startTime;
|
||||||
|
log.info("【征信解析】调用完成: statusCode={}, cost={}ms",
|
||||||
|
response != null ? response.getStatusCode() : null, elapsed);
|
||||||
|
return response;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("【征信解析】调用失败: fileName={}, model={}, hType={}, error={}",
|
||||||
|
file.getName(), model, hType, e.getMessage(), e);
|
||||||
|
throw new LsfxApiException("征信解析调用失败: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,14 +2,44 @@ package com.ruoyi.lsfx.client;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.ruoyi.lsfx.domain.response.CreditParseResponse;
|
import com.ruoyi.lsfx.domain.response.CreditParseResponse;
|
||||||
|
import com.ruoyi.lsfx.exception.LsfxApiException;
|
||||||
|
import com.ruoyi.lsfx.util.HttpUtil;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
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.util.ReflectionTestUtils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyMap;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.ArgumentMatchers.isNull;
|
||||||
|
import static org.mockito.Mockito.argThat;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
class CreditParseClientTest {
|
class CreditParseClientTest {
|
||||||
|
|
||||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private HttpUtil httpUtil;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private CreditParseClient client;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
ReflectionTestUtils.setField(client, "creditParseUrl", "http://credit-host/xfeature-mngs/conversation/htmlEval");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldDeserializeCreditParseResponse() throws Exception {
|
void shouldDeserializeCreditParseResponse() throws Exception {
|
||||||
String json = """
|
String json = """
|
||||||
@@ -29,4 +59,32 @@ class CreditParseClientTest {
|
|||||||
assertEquals("0", response.getStatusCode());
|
assertEquals("0", response.getStatusCode());
|
||||||
assertEquals("3301", response.getPayload().getLxHeader().get("query_cert_no"));
|
assertEquals("3301", response.getPayload().getLxHeader().get("query_cert_no"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldCallConfiguredUrlWithMultipartParams() {
|
||||||
|
File file = new File("sample.html");
|
||||||
|
CreditParseResponse response = new CreditParseResponse();
|
||||||
|
response.setStatusCode("0");
|
||||||
|
|
||||||
|
when(httpUtil.uploadFile(eq("http://credit-host/xfeature-mngs/conversation/htmlEval"), anyMap(), isNull(), eq(CreditParseResponse.class)))
|
||||||
|
.thenReturn(response);
|
||||||
|
|
||||||
|
CreditParseResponse actual = client.parse("LXCUSTALL", "PERSON", file);
|
||||||
|
|
||||||
|
assertEquals("0", actual.getStatusCode());
|
||||||
|
verify(httpUtil).uploadFile(eq("http://credit-host/xfeature-mngs/conversation/htmlEval"), argThat(params ->
|
||||||
|
"LXCUSTALL".equals(params.get("model"))
|
||||||
|
&& "PERSON".equals(params.get("hType"))
|
||||||
|
&& file.equals(params.get("file"))
|
||||||
|
), isNull(), eq(CreditParseResponse.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldWrapHttpErrorsAsLsfxApiException() {
|
||||||
|
when(httpUtil.uploadFile(anyString(), anyMap(), isNull(), eq(CreditParseResponse.class)))
|
||||||
|
.thenThrow(new LsfxApiException("网络失败"));
|
||||||
|
|
||||||
|
assertThrows(LsfxApiException.class,
|
||||||
|
() -> client.parse("LXCUSTALL", "PERSON", new File("sample.html")));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -141,4 +141,8 @@ lsfx:
|
|||||||
# 连接池配置
|
# 连接池配置
|
||||||
pool:
|
pool:
|
||||||
max-total: 100 # 最大连接数
|
max-total: 100 # 最大连接数
|
||||||
default-max-per-route: 20 # 每个路由最大连接数
|
default-max-per-route: 20 # 每个路由最大连接数
|
||||||
|
|
||||||
|
credit-parse:
|
||||||
|
api:
|
||||||
|
url: http://64.202.94.120:8081/xfeature-mngs/conversation/htmlEval
|
||||||
|
|||||||
@@ -141,4 +141,8 @@ lsfx:
|
|||||||
# 连接池配置
|
# 连接池配置
|
||||||
pool:
|
pool:
|
||||||
max-total: 100 # 最大连接数
|
max-total: 100 # 最大连接数
|
||||||
default-max-per-route: 20 # 每个路由最大连接数
|
default-max-per-route: 20 # 每个路由最大连接数
|
||||||
|
|
||||||
|
credit-parse:
|
||||||
|
api:
|
||||||
|
url: http://64.202.94.120:8081/xfeature-mngs/conversation/htmlEval
|
||||||
|
|||||||
Reference in New Issue
Block a user