This commit is contained in:
2026-09-15 20:52:02 +08:00
parent 4b671bb0ab
commit 8b19085bc4
24 changed files with 387 additions and 38 deletions
@@ -0,0 +1,60 @@
package com.baiyue.lastleg.inboundplan.pda.gateway;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestClient;
import org.springframework.web.server.ResponseStatusException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
class HttpDomesticLoadingEvidenceGatewayTest {
private HttpDomesticLoadingEvidenceGateway gateway;
private MockRestServiceServer server;
@BeforeEach
void setUp() {
RestClient.Builder builder = RestClient.builder();
server = MockRestServiceServer.bindTo(builder).build();
gateway = new HttpDomesticLoadingEvidenceGateway();
ReflectionTestUtils.setField(gateway, "client", builder.baseUrl("http://domestic-file").build());
ReflectionTestUtils.setField(gateway, "internalToken", "internal-token");
}
@Test
void downloadsImageFromDomesticFileService() {
server.expect(requestTo("http://domestic-file/internal/file/v1/loading-evidence/8/9/content"))
.andExpect(header("X-Internal-Service-Token", "internal-token"))
.andRespond(withSuccess("photo-bytes".getBytes(), MediaType.IMAGE_JPEG)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=seal.jpg"));
DomesticLoadingEvidenceGateway.DownloadedFile result = gateway.download(8L, 9L);
assertThat(result.content()).containsExactly("photo-bytes".getBytes());
assertThat(result.fileName()).isEqualTo("seal.jpg");
assertThat(result.contentType()).isEqualTo(MediaType.IMAGE_JPEG_VALUE);
server.verify();
}
@Test
void rejectsBusinessErrorEnvelopeReturnedWithHttp200() {
server.expect(requestTo("http://domestic-file/internal/file/v1/loading-evidence/8/9/content"))
.andRespond(withSuccess("{\"code\":\"S0500\",\"message\":\"Internal server error\"}",
MediaType.APPLICATION_JSON));
assertThatThrownBy(() -> gateway.download(8L, 9L))
.isInstanceOfSatisfying(ResponseStatusException.class, error -> {
assertThat(error.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
assertThat(error.getReason()).isEqualTo("国内装柜照片返回内容无效");
});
server.verify();
}
}