mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(textract): retain async results until expiry (#3965)
This commit is contained in:
1 parent
8ccc89df41
commit
4618d633e0
4 files changed
+203
-28
No files matched your search
+34
@@ -114,6 +114,40 @@ class TextractIntegrationTest {
|
||||
.body("Blocks", hasSize(3));
|
||||
}
|
||||
@Test
|
||||
void asyncResultCanBeReadMoreThanOnceWithStableBlocks() {
|
||||
String jobId = given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "Textract.StartDocumentTextDetection")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("{\"DocumentLocation\":{\"S3Object\":{\"Bucket\":\"my-bucket\",\"Name\":\"test.pdf\"}}}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("JobId");
|
||||
String firstBlockId = given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "Textract.GetDocumentTextDetection")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("{\"JobId\":\"" + jobId + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("Blocks[0].Id");
|
||||
String secondBlockId = given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "Textract.GetDocumentTextDetection")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("{\"JobId\":\"" + jobId + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("Blocks[0].Id");
|
||||
assertThat(secondBlockId, equalTo(firstBlockId));
|
||||
}
|
||||
@Test
|
||||
void getDocumentTextDetection_unknownJobId_returns400() {
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package io.github.hectorvent.floci.services.textract;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AiMockConfigLoader;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
class TextractServiceTest {
|
||||
|
||||
private static final Instant START = Instant.parse("2026-01-01T00:00:00Z");
|
||||
|
||||
@Test
|
||||
void repeatedGetReturnsTheSameStableResult() {
|
||||
TextractService service = serviceAt(START);
|
||||
String jobId = startJob(service, "TEXT_DETECTION");
|
||||
|
||||
ObjectNode first = (ObjectNode) service.getDocumentTextDetection(jobId).getEntity();
|
||||
ObjectNode second = (ObjectNode) service.getDocumentTextDetection(jobId).getEntity();
|
||||
|
||||
assertEquals(first, second);
|
||||
assertEquals("SUCCEEDED", second.get("JobStatus").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getBeforeExpiryStillReturnsTheResult() {
|
||||
MutableClock clock = new MutableClock(START);
|
||||
TextractService service = new TextractService(new ObjectMapper(), mock(AiMockConfigLoader.class), clock);
|
||||
String jobId = startJob(service, "DOCUMENT_ANALYSIS");
|
||||
|
||||
clock.advance(TextractService.RESULT_RETENTION.minusSeconds(1));
|
||||
|
||||
assertEquals(200, service.getDocumentAnalysis(jobId).getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void expiredJobReadsAsInvalid() {
|
||||
MutableClock clock = new MutableClock(START);
|
||||
TextractService service = new TextractService(new ObjectMapper(), mock(AiMockConfigLoader.class), clock);
|
||||
String jobId = startJob(service, "TEXT_DETECTION");
|
||||
|
||||
clock.advance(TextractService.RESULT_RETENTION.plusSeconds(1));
|
||||
|
||||
AwsException exception = assertThrows(AwsException.class,
|
||||
() -> service.getDocumentTextDetection(jobId));
|
||||
assertEquals("InvalidJobIdException", exception.getErrorCode());
|
||||
assertEquals(400, exception.getHttpStatus());
|
||||
}
|
||||
|
||||
private static String startJob(TextractService service, String jobType) {
|
||||
ObjectNode response = jobType.equals("DOCUMENT_ANALYSIS")
|
||||
? (ObjectNode) service.startDocumentAnalysis().getEntity()
|
||||
: (ObjectNode) service.startDocumentTextDetection().getEntity();
|
||||
return response.get("JobId").asText();
|
||||
}
|
||||
|
||||
private static TextractService serviceAt(Instant instant) {
|
||||
return new TextractService(new ObjectMapper(), mock(AiMockConfigLoader.class), new MutableClock(instant));
|
||||
}
|
||||
|
||||
private static final class MutableClock extends Clock {
|
||||
|
||||
private Instant instant;
|
||||
|
||||
private MutableClock(Instant instant) {
|
||||
this.instant = instant;
|
||||
}
|
||||
|
||||
private void advance(Duration duration) {
|
||||
instant = instant.plus(duration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZoneId getZone() {
|
||||
return ZoneOffset.UTC;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Clock withZone(ZoneId zone) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Instant instant() {
|
||||
return instant;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user