Skip to content

Commit 13351ff

Browse files
add micro-test
1 parent 8f623b6 commit 13351ff

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import com.mindee.MindeeClient;
2+
import com.mindee.input.LocalInputSource;
3+
import com.mindee.parsing.common.WorkflowResponse;
4+
import com.mindee.product.generated.GeneratedV1;
5+
import com.mindee.http.MindeeHttpException;
6+
import java.io.IOException;
7+
8+
public class SimpleMindeeClient {
9+
10+
public static void main(String[] args) throws IOException {
11+
String apiKey = "my-api-key";
12+
String workflowId = "workflow-id";
13+
String filePath = "/path/to/the/file.ext";
14+
15+
// Init a new client
16+
MindeeClient mindeeClient = new MindeeClient(apiKey);
17+
18+
// Load a file from disk
19+
LocalInputSource inputSource = new LocalInputSource(filePath);
20+
21+
// Parse the file
22+
WorkflowResponse.Default response = mindeeClient.executeWorkflow(
23+
workflowId,
24+
inputSource
25+
System.out.println(response.toString());
26+
27+
}
28+
29+
}

src/main/java/com/mindee/MindeeClient.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.mindee.pdf.SplitQuery;
1919
import com.mindee.product.custom.CustomV1;
2020
import com.mindee.product.generated.GeneratedV1;
21+
import com.mindee.parsing.common.WorkflowResponse;
2122
import java.io.IOException;
2223
import java.net.URL;
2324

@@ -323,6 +324,40 @@ private <T extends Inference> AsyncPredictResponse<T> enqueueAndParse(
323324
throw new RuntimeException("Max retries exceeded. Failed to get the document.");
324325
}
325326

327+
/**
328+
* Send a local file to a workflow execution.
329+
*/
330+
public <T extends Inference> WorkflowResponse<T> executeWorkflow(
331+
Class<T> type,
332+
String workflowId,
333+
LocalInputSource localInputSource
334+
335+
) throws IOException {
336+
return this.mindeeApi.executeWorkflowPost(
337+
type,
338+
workflowId,
339+
RequestParameters.builder()
340+
.file(localInputSource.getFile())
341+
.fileName(localInputSource.getFilename())
342+
.build()
343+
);
344+
}
345+
346+
public WorkflowResponse<GeneratedV1> executeWorkflow(
347+
String workflowId,
348+
LocalInputSource localInputSource
349+
350+
) throws IOException {
351+
return this.mindeeApi.executeWorkflowPost(
352+
GeneratedV1.class,
353+
workflowId,
354+
RequestParameters.builder()
355+
.file(localInputSource.getFile())
356+
.fileName(localInputSource.getFilename())
357+
.build()
358+
);
359+
}
360+
326361
/**
327362
* Send a local file to a Standard prediction API and parse the results.
328363
*/

src/main/java/com/mindee/parsing/common/WorkflowResponse.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.mindee.product.generated.GeneratedV1;
56
import lombok.Data;
67
import lombok.EqualsAndHashCode;
78

@@ -18,4 +19,7 @@ public class WorkflowResponse<DocT extends Inference> extends ApiResponse {
1819
*/
1920
@JsonProperty("execution")
2021
Execution<DocT> execution;
22+
23+
// Type alias for default type parameter
24+
public static class Default extends WorkflowResponse<GeneratedV1> {}
2125
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.mindee.workflow;
2+
3+
import com.mindee.MindeeClient;
4+
import com.mindee.http.Endpoint;
5+
import com.mindee.http.MindeeApi;
6+
import com.mindee.input.LocalInputSource;
7+
import com.mindee.parsing.common.Document;
8+
import com.mindee.parsing.common.Execution;
9+
import com.mindee.parsing.common.PredictResponse;
10+
import com.mindee.parsing.common.WorkflowResponse;
11+
import com.mindee.pdf.PdfOperation;
12+
import com.mindee.product.custom.CustomV1;
13+
import com.mindee.product.generated.GeneratedV1;
14+
import java.io.File;
15+
import java.io.IOException;
16+
import org.junit.jupiter.api.Assertions;
17+
import org.junit.jupiter.api.BeforeEach;
18+
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.extension.ExtendWith;
20+
import org.mockito.Mockito;
21+
import org.mockito.junit.jupiter.MockitoExtension;
22+
23+
@ExtendWith(MockitoExtension.class)
24+
public class WorkflowTest {
25+
MindeeClient client;
26+
MindeeApi mindeeApi;
27+
PdfOperation pdfOperation;
28+
29+
@BeforeEach
30+
public void setUp() {
31+
mindeeApi = Mockito.mock(MindeeApi.class);
32+
pdfOperation = Mockito.mock(PdfOperation.class);
33+
client = new MindeeClient(pdfOperation, mindeeApi);
34+
}
35+
36+
@Test
37+
void givenAWorkflowMockFileShouldReturnAValidWorkflowObject()
38+
throws IOException {
39+
40+
File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");
41+
42+
WorkflowResponse predictResponse = new WorkflowResponse();
43+
predictResponse.setExecution(new Execution());
44+
predictResponse.setApiRequest(null);
45+
Mockito.when(
46+
mindeeApi.executeWorkflowPost(
47+
GeneratedV1.class,
48+
Mockito.any(),
49+
Mockito.any()))
50+
.thenReturn(predictResponse);
51+
52+
WorkflowResponse<GeneratedV1> workflowResponse = client.executeWorkflow(
53+
"",
54+
new LocalInputSource(file)
55+
);
56+
57+
Assertions.assertNotNull(workflowResponse);
58+
Mockito.verify(mindeeApi, Mockito.times(1))
59+
.predictPost(Mockito.any(), Mockito.any(), Mockito.any());
60+
}
61+
}

tests/test_code_samples.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ OUTPUT_FILE='SimpleMindeeClient.java'
55
ACCOUNT=$1
66
ENDPOINT=$2
77
API_KEY=$3
8+
WORKFLOW_ID=$4
89

910
if [ -z "${ACCOUNT}" ]; then echo "ACCOUNT is required"; exit 1; fi
1011
if [ -z "${ENDPOINT}" ]; then echo "ENDPOINT is required"; exit 1; fi
@@ -41,6 +42,11 @@ do
4142
sed -i "s/my-version/1/" $OUTPUT_FILE
4243
fi
4344

45+
if echo "${f}" | grep -q "workflow_execution.txt"
46+
then
47+
sed -i "s/workflow-id/$WORKFLOW_ID/" $OUTPUT_FILE
48+
fi
49+
4450
sed -i "s/my-api-key/$API_KEY/" $OUTPUT_FILE
4551
sed -i "s/\/path\/to\/the\/file.ext/src\/test\/resources\/file_types\/pdf\/blank_1.pdf/" $OUTPUT_FILE
4652

0 commit comments

Comments
 (0)