11package com .mindee .workflow ;
22
3+ import static org .mockito .Mockito .when ;
4+
5+ import com .fasterxml .jackson .databind .ObjectMapper ;
36import com .mindee .MindeeClient ;
4- import com .mindee .http .Endpoint ;
57import com .mindee .http .MindeeApi ;
68import com .mindee .input .LocalInputSource ;
7- import com .mindee .parsing .common .Document ;
89import com .mindee .parsing .common .Execution ;
9- import com .mindee .parsing .common .PredictResponse ;
1010import com .mindee .parsing .common .WorkflowResponse ;
1111import com .mindee .pdf .PdfOperation ;
12- import com .mindee .product .custom .CustomV1 ;
1312import com .mindee .product .generated .GeneratedV1 ;
1413import java .io .File ;
1514import java .io .IOException ;
1615import org .junit .jupiter .api .Assertions ;
1716import org .junit .jupiter .api .BeforeEach ;
1817import org .junit .jupiter .api .Test ;
1918import org .junit .jupiter .api .extension .ExtendWith ;
19+ import org .mockito .Mock ;
2020import org .mockito .Mockito ;
21+ import org .mockito .MockitoAnnotations ;
2122import org .mockito .junit .jupiter .MockitoExtension ;
2223
2324@ ExtendWith (MockitoExtension .class )
2425public class WorkflowTest {
2526 MindeeClient client ;
27+ @ Mock
28+ MindeeClient mockedClient ;
2629 MindeeApi mindeeApi ;
2730 PdfOperation pdfOperation ;
2831
32+ private ObjectMapper objectMapper ;
2933 @ BeforeEach
3034 public void setUp () {
3135 mindeeApi = Mockito .mock (MindeeApi .class );
3236 pdfOperation = Mockito .mock (PdfOperation .class );
3337 client = new MindeeClient (pdfOperation , mindeeApi );
38+
39+ MockitoAnnotations .openMocks (this );
40+ objectMapper = new ObjectMapper ();
3441 }
3542
3643 @ Test
@@ -39,23 +46,62 @@ void givenAWorkflowMockFileShouldReturnAValidWorkflowObject()
3946
4047 File file = new File ("src/test/resources/file_types/pdf/blank_1.pdf" );
4148
42- WorkflowResponse predictResponse = new WorkflowResponse ();
43- predictResponse .setExecution (new Execution ());
44- predictResponse .setApiRequest (null );
45- Mockito . when (
49+ WorkflowResponse workflowResponse = new WorkflowResponse ();
50+ workflowResponse .setExecution (new Execution ());
51+ workflowResponse .setApiRequest (null );
52+ when (
4653 mindeeApi .executeWorkflowPost (
47- GeneratedV1 . class ,
54+ Mockito . any () ,
4855 Mockito .any (),
4956 Mockito .any ()))
50- .thenReturn (predictResponse );
57+ .thenReturn (workflowResponse );
5158
52- WorkflowResponse <GeneratedV1 > workflowResponse = client .executeWorkflow (
59+ WorkflowResponse <GeneratedV1 > execution = client .executeWorkflow (
5360 "" ,
5461 new LocalInputSource (file )
5562 );
5663
57- Assertions .assertNotNull (workflowResponse );
64+ Assertions .assertNotNull (execution );
5865 Mockito .verify (mindeeApi , Mockito .times (1 ))
59- .predictPost (Mockito .any (), Mockito .any (), Mockito .any ());
66+ .executeWorkflowPost (Mockito .any (), Mockito .any (), Mockito .any ());
67+ }
68+
69+ @ Test
70+ void sendingADocumentToAnExecutionShouldDeserializeResponseCorrectly () throws IOException {
71+ File jsonFile = new File ("src/test/resources/workflows/success.json" );
72+ WorkflowResponse .Default mockResponse =
73+ objectMapper .readValue (jsonFile , WorkflowResponse .Default .class );
74+
75+ // Mock the executeWorkflow method
76+ when (mockedClient .executeWorkflow (Mockito .anyString (), Mockito .any (LocalInputSource .class )))
77+ .thenReturn (mockResponse );
78+
79+ // Test execution
80+ String workflowId = "workflow-id" ;
81+ String filePath = "src/test/resources/file_types/pdf/blank_1.pdf" ;
82+ LocalInputSource inputSource = new LocalInputSource (filePath );
83+
84+ WorkflowResponse <GeneratedV1 > response = mockedClient .executeWorkflow (workflowId , inputSource );
85+
86+ // Assertions
87+ Assertions .assertNotNull (response );
88+ Assertions .assertNotNull (response .getApiRequest ());
89+ Assertions .assertNull (response .getExecution ().getBatchName ());
90+ Assertions .assertNull (response .getExecution ().getCreatedAt ());
91+ Assertions .assertNull (response .getExecution ().getFile ().getAlias ());
92+ Assertions .assertEquals ("default_sample.jpg" , response .getExecution ().getFile ().getName ());
93+ Assertions .assertEquals ("8c75c035-e083-4e77-ba3b-7c3598bd1d8a" , response .getExecution ().getId ());
94+ Assertions .assertNull (response .getExecution ().getInference ());
95+ Assertions .assertEquals ("medium" , response .getExecution ().getPriority ());
96+ Assertions .assertNull (response .getExecution ().getReviewedAt ());
97+ Assertions .assertNull (response .getExecution ().getReviewedPrediction ());
98+ Assertions .assertEquals ("processing" , response .getExecution ().getStatus ());
99+ Assertions .assertEquals ("manual" , response .getExecution ().getType ());
100+ Assertions .assertEquals ("2024-11-13T13:02:31.699190" , response .getExecution ().getUploadedAt ().toString ());
101+ Assertions .assertEquals ("07ebf237-ff27-4eee-b6a2-425df4a5cca6" , response .getExecution ().getWorkflowId ());
102+
103+ // Verify that executeWorkflow was called with the correct parameters
104+ Mockito .verify (mockedClient ).executeWorkflow (workflowId , inputSource );
60105 }
106+
61107}
0 commit comments