91 lines
3.2 KiB
Java
91 lines
3.2 KiB
Java
package com.ruoyi.lsfx.client;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
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.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.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 {
|
|
|
|
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
|
|
void shouldDeserializeCreditParseResponse() throws Exception {
|
|
String json = """
|
|
{
|
|
"message": "成功",
|
|
"status_code": "0",
|
|
"payload": {
|
|
"lx_header": {"query_cert_no": "3301"},
|
|
"lx_debt": {"uncle_bank_house_bal": "12.00"},
|
|
"lx_publictype": {"civil_cnt": 1}
|
|
}
|
|
}
|
|
""";
|
|
|
|
CreditParseResponse response = objectMapper.readValue(json, CreditParseResponse.class);
|
|
|
|
assertEquals("0", response.getStatusCode());
|
|
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")));
|
|
}
|
|
}
|