Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 53 additions & 13 deletions src/main/java/com/mindee/parsing/BaseLocalResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,43 @@ public abstract class BaseLocalResponse {
protected final byte[] file;

/**
* Load from an {@link InputStream}.
* Load from a {@link String}.
*
* @param input Assumes UTF-8 encoding.
*/
public BaseLocalResponse(String input) {
if (input == null) {
throw new IllegalArgumentException("Input string cannot be null.");
}
this.file = this.readToCleanUtf8Bytes(input.lines());
}
Comment on lines 28 to 38

/**
* Load from a byte array.
*
* @param input will be decoded as UTF-8.
*/
public BaseLocalResponse(InputStream input) {
this.file = this
.getBytes(new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines());
public BaseLocalResponse(byte[] input) {
if (input == null) {
throw new IllegalArgumentException("Input byte array cannot be null.");
}
this.file = this.readToCleanUtf8Bytes(new String(input, StandardCharsets.UTF_8).lines());
}

/**
* Load from a {@link String}.
* Load from an {@link InputStream}.
* This method will not close the provided stream.
*
* @param input will be decoded as UTF-8.
*/
Comment thread
ianardee marked this conversation as resolved.
public BaseLocalResponse(String input) {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("Input string cannot be empty or null.");
public BaseLocalResponse(InputStream input) {
if (input == null) {
throw new IllegalArgumentException("Input stream cannot be null.");
}
this.file = input.getBytes(StandardCharsets.UTF_8);
this.file = this
.readToCleanUtf8Bytes(
new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines()
);
}

/**
Expand All @@ -53,7 +71,12 @@ public BaseLocalResponse(String input) {
* @param input will be decoded as UTF-8.
*/
public BaseLocalResponse(File input) throws IOException {
this.file = this.getBytes(Files.lines(input.toPath(), StandardCharsets.UTF_8));
if (input == null) {
throw new IllegalArgumentException("Input file cannot be null.");
}
try (var lines = Files.lines(input.toPath(), StandardCharsets.UTF_8)) {
this.file = this.readToCleanUtf8Bytes(lines);
}
}
Comment thread
ianardee marked this conversation as resolved.

/**
Expand All @@ -62,11 +85,20 @@ public BaseLocalResponse(File input) throws IOException {
* @param input will be decoded as UTF-8.
*/
public BaseLocalResponse(Path input) throws IOException {
this.file = this.getBytes(Files.lines(input, StandardCharsets.UTF_8));
if (input == null) {
throw new IllegalArgumentException("Input path cannot be null.");
}
try (var lines = Files.lines(input, StandardCharsets.UTF_8)) {
this.file = this.readToCleanUtf8Bytes(lines);
}
}

private byte[] getBytes(Stream<String> stream) {
return stream.collect(Collectors.joining("")).getBytes();
private byte[] readToCleanUtf8Bytes(Stream<String> stream) {
var cleanedString = stream.collect(Collectors.joining(""));
if (cleanedString.trim().isEmpty()) {
throw new IllegalArgumentException("Input cannot be empty or contain only whitespace.");
}
return cleanedString.getBytes(StandardCharsets.UTF_8);
}

/**
Expand Down Expand Up @@ -120,4 +152,12 @@ public boolean isValidHmacSignature(String secretKey, String signature) {

return MessageDigest.isEqual(expectedBytes, actualBytes);
}

/**
* Print the file as a UTF-8 string.
*/
@Override
public String toString() {
return new String(this.file, StandardCharsets.UTF_8);
}
}
8 changes: 6 additions & 2 deletions src/main/java/com/mindee/v1/parsing/LocalResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
*/
public class LocalResponse extends BaseLocalResponse {

public LocalResponse(InputStream input) {
public LocalResponse(String input) {
super(input);
}

public LocalResponse(String input) {
public LocalResponse(byte[] input) {
super(input);
}

public LocalResponse(InputStream input) {
super(input);
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/mindee/v2/parsing/LocalResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
*/
public class LocalResponse extends BaseLocalResponse {

public LocalResponse(InputStream input) {
public LocalResponse(String input) {
super(input);
}

public LocalResponse(String input) {
public LocalResponse(byte[] input) {
super(input);
}

public LocalResponse(InputStream input) {
super(input);
}

Expand Down
8 changes: 6 additions & 2 deletions src/test/java/com/mindee/TestingUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ public static Path getV1ResourcePath(String filePath) {
return Paths.get("src/test/resources/v1/" + filePath);
}

public static String getV1ResourcePathString(String filePath) {
return getV1ResourcePath(filePath).toString();
}

public static Path getV2ResourcePath(String filePath) {
return Paths.get("src/test/resources/v2/" + filePath);
}

public static String getV1ResourcePathString(String filePath) {
return getV1ResourcePath(filePath).toString();
public static Path getV2ProductPath(String filePath) {
return getV2ResourcePath("products/" + filePath);
}

public static void assertStringEqualsFile(String expected, String filePath) throws IOException {
Expand Down
10 changes: 4 additions & 6 deletions src/test/java/com/mindee/v2/MindeeClientIT.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mindee.v2;

import static com.mindee.TestingUtilities.getResourcePath;
import static com.mindee.TestingUtilities.getV2ResourcePath;
import static com.mindee.TestingUtilities.getV2ProductPath;
import static org.junit.jupiter.api.Assertions.*;

import com.mindee.input.LocalInputSource;
Expand Down Expand Up @@ -88,7 +88,7 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep
@DisplayName("Filled, single-page image – enqueue & parse must succeed")
void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedException {
var source = new LocalInputSource(
getV2ResourcePath("products/extraction/financial_document/default_sample.jpg")
getV2ProductPath("extraction/financial_document/default_sample.jpg")
);

var params = ExtractionParameters
Expand Down Expand Up @@ -137,16 +137,14 @@ void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedExc
@DisplayName("Data Schema Replace – enqueue & parse must succeed")
void parseFile_dataSchemaReplace_mustSucceed() throws IOException, InterruptedException {
var source = new LocalInputSource(
getV2ResourcePath("products/extraction/financial_document/default_sample.jpg")
getV2ProductPath("extraction/financial_document/default_sample.jpg")
);

var params = ExtractionParameters
.builder(modelId)
.rag(false)
.alias("java-integration-test_data-schema-replace")
.dataSchema(
Files.readString(getV2ResourcePath("products/extraction/data_schema_replace_param.json"))
)
.dataSchema(Files.readString(getV2ProductPath("extraction/data_schema_replace_param.json")))
.build();

var response = mindeeClient.enqueueAndGetResult(ExtractionResponse.class, source, params);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/mindee/v2/MindeeClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public <TResponse extends CommonResponse> TResponse reqGetResultByUrl(
class Enqueue {
@Test
@DisplayName("sends exactly one HTTP call and yields a non-null response")
void enqueue_post_async() throws IOException {
void enqueue_post() throws IOException {
var mindeeClient = new MindeeClient(new FakeMindeeApiV2(new JobResponse(), null));

var input = new LocalInputSource(getResourcePath("file_types/pdf/blank_1.pdf"));
Expand All @@ -115,7 +115,7 @@ void enqueue_post_async() throws IOException {
class GetJob {
@Test
@DisplayName("hits the HTTP endpoint once and returns a non-null response")
void document_getJob_async() throws JsonProcessingException {
void document_getJob() throws JsonProcessingException {
String json = "{\"job\": {\"id\": \"dummy-id\", \"status\": \"Processing\"}}";
var mapper = new ObjectMapper();
mapper.findAndRegisterModules();
Expand All @@ -134,7 +134,7 @@ void document_getJob_async() throws JsonProcessingException {
class GetExtractionInference {
@Test
@DisplayName("hits the HTTP endpoint once and returns a non-null response")
void document_getResult_async() throws IOException {
void document_getResult() throws IOException {
String json = Files
.readString(getResourcePath("v2/products/extraction/financial_document/complete.json"));

Expand Down Expand Up @@ -171,7 +171,7 @@ void document_getResult_async() throws IOException {
class GetResultFromUrl {
@Test
@DisplayName("hits the HTTP endpoint once and returns a non-null response")
void document_getResultFromUrl_async() throws IOException {
void document_getResultFromUrl() throws IOException {
String json = Files
.readString(getResourcePath("v2/products/extraction/financial_document/complete.json"));

Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/mindee/v2/fileoperations/CropTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static com.mindee.TestingUtilities.deleteRecursively;
import static com.mindee.TestingUtilities.getResourcePath;
import static com.mindee.TestingUtilities.getV2ResourcePath;
import static com.mindee.TestingUtilities.getV2ProductPath;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -26,8 +26,8 @@ public static void setup() throws IOException {

@Test
void singlePageCrop_cropsCorrectly() throws Exception {
var inputSample = new LocalInputSource(getV2ResourcePath("products/crop/default_sample.jpg"));
var localResponse = new LocalResponse(getV2ResourcePath("products/crop/default_sample.json"));
var inputSample = new LocalInputSource(getV2ProductPath("crop/default_sample.jpg"));
var localResponse = new LocalResponse(getV2ProductPath("crop/default_sample.json"));
var doc = localResponse.deserializeResponse(CropResponse.class);

var extractedCrops = new Crop(inputSample)
Expand Down Expand Up @@ -56,8 +56,8 @@ void singlePageCrop_cropsCorrectly() throws Exception {

@Test
void multiPageCrop_cropsCorrectly() throws Exception {
var inputSample = new LocalInputSource(getV2ResourcePath("products/crop/multipage_sample.pdf"));
var localResponse = new LocalResponse(getV2ResourcePath("products/crop/multipage_sample.json"));
var inputSample = new LocalInputSource(getV2ProductPath("crop/multipage_sample.pdf"));
var localResponse = new LocalResponse(getV2ProductPath("crop/multipage_sample.json"));
var doc = localResponse.deserializeResponse(CropResponse.class);

var extractedCrops = new Crop(inputSample)
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/mindee/v2/fileoperations/SplitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static com.mindee.TestingUtilities.deleteRecursively;
import static com.mindee.TestingUtilities.getResourcePath;
import static com.mindee.TestingUtilities.getV2ResourcePath;
import static com.mindee.TestingUtilities.getV2ProductPath;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -26,9 +26,9 @@ public static void setup() throws IOException {

@Test
void singlePage_splitsCorrectly() throws IOException {
var inputSample = new LocalInputSource(getV2ResourcePath("products/split/default_sample.pdf"));
var inputSample = new LocalInputSource(getV2ProductPath("split/default_sample.pdf"));
assertEquals(2, inputSample.getPageCount());
var localResponse = new LocalResponse(getV2ResourcePath("products/split/default_sample.json"));
var localResponse = new LocalResponse(getV2ProductPath("split/default_sample.json"));
var doc = localResponse.deserializeResponse(SplitResponse.class);

var extractedSplit = new Split(inputSample)
Expand All @@ -42,9 +42,9 @@ void singlePage_splitsCorrectly() throws IOException {

@Test
void multiplePages_splitsCorrectly() throws IOException {
var inputSample = new LocalInputSource(getV2ResourcePath("products/split/default_sample.pdf"));
var inputSample = new LocalInputSource(getV2ProductPath("split/default_sample.pdf"));
assertEquals(2, inputSample.getPageCount());
var localResponse = new LocalResponse(getV2ResourcePath("products/split/default_sample.json"));
var localResponse = new LocalResponse(getV2ProductPath("split/default_sample.json"));
var doc = localResponse.deserializeResponse(SplitResponse.class);

var extractedSplits = new Split(inputSample)
Expand Down
Loading
Loading