|
3 | 3 | import com.marklogic.hub.AbstractHubCoreTest; |
4 | 4 | import com.marklogic.hub.HubClient; |
5 | 5 | import com.marklogic.hub.job.JobStatus; |
| 6 | +import com.marklogic.hub.step.RunStepResponse; |
6 | 7 | import com.marklogic.hub.test.ReferenceModelProject; |
7 | 8 | import org.junit.jupiter.api.BeforeEach; |
8 | 9 | import org.junit.jupiter.api.Test; |
9 | 10 |
|
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
10 | 14 | import static org.junit.jupiter.api.Assertions.*; |
11 | 15 |
|
12 | 16 | public class RunStepWithCustomHookTest extends AbstractHubCoreTest { |
13 | 17 |
|
14 | 18 | private final static String URI_CREATED_BY_HOOK = "/insertedByHook/customer1.json"; |
15 | 19 |
|
16 | 20 | HubClient client; |
| 21 | + ReferenceModelProject project; |
17 | 22 |
|
18 | 23 | @BeforeEach |
19 | 24 | void beforeEach() { |
20 | | - ReferenceModelProject project = installReferenceModelProject(); |
| 25 | + project = installReferenceModelProject(); |
21 | 26 | runAsDataHubOperator(); |
22 | 27 | client = getHubClient(); |
23 | 28 | project.createRawCustomer(1, "Jane"); |
@@ -46,4 +51,61 @@ void afterHook() { |
46 | 51 | assertNull(client.getStagingClient().newDocumentManager().exists(URI_CREATED_BY_HOOK)); |
47 | 52 | assertNotNull(client.getFinalClient().newDocumentManager().exists(URI_CREATED_BY_HOOK)); |
48 | 53 | } |
| 54 | + |
| 55 | + @Test |
| 56 | + void beforeHookThrowsErrorAndStopOnErrorIsTrue() { |
| 57 | + project.createRawCustomer(2, "Janet"); |
| 58 | + |
| 59 | + Map<String, Object> options = new HashMap<>(); |
| 60 | + options.put("stopOnError", "true"); |
| 61 | + options.put("throwErrorForStepNumber", "1"); |
| 62 | + RunFlowResponse response = runFlow(new FlowInputs("customHookFlow", "1", "2").withOptions(options)); |
| 63 | + |
| 64 | + assertEquals(JobStatus.STOP_ON_ERROR.toString(), response.getJobStatus()); |
| 65 | + assertEquals("1", response.getLastAttemptedStep()); |
| 66 | + assertEquals("0", response.getLastCompletedStep()); |
| 67 | + assertEquals(1, response.getStepResponses().keySet().size(), |
| 68 | + "The second step should not have been run since stopOnError=true"); |
| 69 | + |
| 70 | + RunStepResponse stepResponse = response.getStepResponses().get("1"); |
| 71 | + assertEquals("canceled step 1", stepResponse.getStatus()); |
| 72 | + assertEquals(2, stepResponse.getTotalEvents()); |
| 73 | + assertEquals(0, stepResponse.getSuccessfulEvents()); |
| 74 | + assertEquals(2, stepResponse.getFailedEvents(), "Both items are considered to have failed since the " + |
| 75 | + "entire batch failed due to a custom hook error"); |
| 76 | + assertEquals(0, stepResponse.getSuccessfulBatches()); |
| 77 | + assertEquals(1, stepResponse.getFailedBatches()); |
| 78 | + assertEquals(1, stepResponse.getStepOutput().size()); |
| 79 | + assertTrue(stepResponse.getStepOutput().get(0).contains("Throwing error on purpose for step number"), |
| 80 | + "Unexpected step error: " + stepResponse.getStepOutput().get(0)); |
| 81 | + assertFalse(stepResponse.isSuccess()); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void afterHookThrowsErrorAndStopOnErrorIsTrue() { |
| 86 | + project.createRawCustomer(2, "Janet"); |
| 87 | + |
| 88 | + Map<String, Object> options = new HashMap<>(); |
| 89 | + options.put("stopOnError", "true"); |
| 90 | + options.put("throwErrorForStepNumber", "2"); |
| 91 | + RunFlowResponse response = runFlow(new FlowInputs("customHookFlow", "2", "1").withOptions(options)); |
| 92 | + |
| 93 | + assertEquals(JobStatus.STOP_ON_ERROR.toString(), response.getJobStatus()); |
| 94 | + assertEquals("2", response.getLastAttemptedStep()); |
| 95 | + assertEquals("0", response.getLastCompletedStep()); |
| 96 | + assertEquals(1, response.getStepResponses().keySet().size(), |
| 97 | + "The second step (step 1 in this scenario) should not have been run since stopOnError=true"); |
| 98 | + |
| 99 | + RunStepResponse stepResponse = response.getStepResponses().get("2"); |
| 100 | + assertEquals("canceled step 2", stepResponse.getStatus()); |
| 101 | + assertEquals(2, stepResponse.getTotalEvents()); |
| 102 | + assertEquals(0, stepResponse.getSuccessfulEvents()); |
| 103 | + assertEquals(2, stepResponse.getFailedEvents()); |
| 104 | + assertEquals(0, stepResponse.getSuccessfulBatches()); |
| 105 | + assertEquals(1, stepResponse.getFailedBatches()); |
| 106 | + assertEquals(1, stepResponse.getStepOutput().size()); |
| 107 | + assertTrue(stepResponse.getStepOutput().get(0).contains("Throwing error on purpose for step number"), |
| 108 | + "Unexpected step error: " + stepResponse.getStepOutput().get(0)); |
| 109 | + assertFalse(stepResponse.isSuccess()); |
| 110 | + } |
49 | 111 | } |
0 commit comments