Skip to content

Commit fb67e50

Browse files
committed
MODWRKFLOW-72: Improve test coverage and handle NULL cases.
1 parent 938024f commit fb67e50

3 files changed

Lines changed: 107 additions & 32 deletions

File tree

service/src/main/java/org/folio/rest/workflow/service/WorkflowEngineService.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ public void delete(String workflowId, String tenant, String token)
105105
final ResponseEntity<ArrayNode> response = fetchDeploymentDefinitions(id, version, tenant, token);
106106

107107
if (response.getStatusCode() == HttpStatus.OK || response.getStatusCode() == HttpStatus.NOT_FOUND) {
108-
final ArrayNode definitions = !response.hasBody()
109-
? null
110-
: response.getBody();
108+
final ArrayNode definitions = response.hasBody()
109+
? response.getBody()
110+
: null;
111111

112112
if (definitions != null && response.getStatusCode() != HttpStatus.NOT_FOUND && !definitions.isEmpty()) {
113113
try {
@@ -197,10 +197,13 @@ public JsonNode history(String workflowId, String tenant, String token)
197197

198198
while (iter.hasNext()) {
199199
JsonNode instance = iter.next();
200-
String processInstanceId = instance.get("id").asString();
201200

202-
((ObjectNode) instance).withArray("incidents")
203-
.addAll(fetchIncidentsHistory(processInstanceId, tenant, token));
201+
if (instance.has("id")) {
202+
String processInstanceId = instance.get("id").asString();
203+
204+
((ObjectNode) instance).withArray("incidents")
205+
.addAll(fetchIncidentsHistory(processInstanceId, tenant, token));
206+
}
204207
}
205208

206209
return instances;
@@ -226,9 +229,9 @@ private JsonNode fetchFirstDeploymentDefinition(String workflowId, String deploy
226229
final ResponseEntity<ArrayNode> response = fetchDeploymentDefinitions(deploymentId, version, tenant, token);
227230

228231
if (response.getStatusCode() == HttpStatus.OK || response.getStatusCode() == HttpStatus.NOT_FOUND) {
229-
final ArrayNode definitions = !response.hasBody()
230-
? null
231-
: response.getBody();
232+
final ArrayNode definitions = response.hasBody()
233+
? response.getBody()
234+
: null;
232235

233236
if (definitions == null || response.getStatusCode() == HttpStatus.NOT_FOUND || definitions.isEmpty() || definitions.get(0) == null) {
234237
throw new WorkflowDeploymentNotFound(String.format("Workflow with Deployment ID '%s' for Workflow ID '%s' is not found.", deploymentId, workflowId));
@@ -281,8 +284,11 @@ private ArrayNode fetchProcessInstanceHistory(String processDefinitionId, String
281284
try {
282285
ResponseEntity<ArrayNode> response = exchange(url, HttpMethod.GET, httpEntity, ArrayNode.class, params);
283286

284-
ArrayNode definitions = response.getBody();
285-
if (response.getStatusCode() == HttpStatus.OK && definitions != null) {
287+
final ArrayNode definitions = response.hasBody()
288+
? response.getBody()
289+
: null;
290+
291+
if (definitions != null && response.getStatusCode() == HttpStatus.OK) {
286292
return definitions;
287293
}
288294

@@ -302,8 +308,11 @@ private ArrayNode fetchIncidentsHistory(String processInstanceId, String tenant,
302308
try {
303309
ResponseEntity<ArrayNode> response = exchange(url, HttpMethod.GET, httpEntity, ArrayNode.class, params);
304310

305-
ArrayNode incidents = response.getBody();
306-
if (response.getStatusCode() != HttpStatus.OK || incidents == null) {
311+
ArrayNode incidents = response.hasBody()
312+
? response.getBody()
313+
: null;
314+
315+
if (incidents == null || response.getStatusCode() != HttpStatus.OK) {
307316
LOG.debug("Unable to get workflow incidents history from workflow engine!");
308317

309318
incidents = mapper.createArrayNode();

service/src/test/java/org/folio/rest/workflow/controller/advice/WorkflowControllerAdviceTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.regex.Pattern;
1111
import java.util.stream.Stream;
1212
import org.folio.rest.workflow.exception.WorkflowAlreadyActiveException;
13+
import org.folio.rest.workflow.exception.WorkflowCreateAlreadyExistsException;
1314
import org.folio.rest.workflow.exception.WorkflowDeploymentException;
1415
import org.folio.rest.workflow.exception.WorkflowDeploymentNotFound;
1516
import org.folio.rest.workflow.exception.WorkflowEngineServiceException;
@@ -37,6 +38,8 @@ class WorkflowControllerAdviceTest {
3738

3839
private static final EntityNotFoundException ENF_EXC = new EntityNotFoundException(VALUE);
3940

41+
private static final WorkflowCreateAlreadyExistsException WCAE_EXC = new WorkflowCreateAlreadyExistsException(VALUE, VALUE);
42+
4043
private static final WorkflowNotFoundException WNF_EXC = new WorkflowNotFoundException(VALUE);
4144

4245
private static final WorkflowAlreadyActiveException WAA_EXC = new WorkflowAlreadyActiveException(VALUE);
@@ -77,6 +80,20 @@ void handleEntityNotFoundExceptionTest() {
7780
assertTrue(matchBody(response, simpleName));
7881
}
7982

83+
@Test
84+
void handleWorkflowCreateAlreadyExistsExceptionTest() {
85+
86+
final String simpleName = WorkflowCreateAlreadyExistsException.class.getSimpleName();
87+
final ResponseEntity<String> response = advice.handleWorkflowCreateAlreadyExistsException(WCAE_EXC);
88+
89+
assertNotNull(response);
90+
assertNotNull(response.getBody());
91+
92+
assertEquals(HttpStatus.CONFLICT, response.getStatusCode());
93+
assertEquals(MediaType.APPLICATION_JSON, response.getHeaders().getContentType());
94+
assertTrue(matchBody(response, simpleName));
95+
}
96+
8097
@Test
8198
void handleWorkflowNotFoundExceptionTest() {
8299

service/src/test/java/org/folio/rest/workflow/service/WorkflowEngineServiceTest.java

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class WorkflowEngineServiceTest {
5454
private static final RestClientException RC_EXC = new RestClientException("Trigger Failure");
5555

5656
private static final String DEACTIVATE = "workflow-engine/workflows/deactivate";
57+
private static final String HISTORY_INCIDENT = "history/incident";
5758
private static final String HISTORY_INSTANCE = "history/process-instance";
5859
private static final String PROCESS_DEFINITION = "process-definition";
5960

@@ -485,30 +486,74 @@ void startThrowsExceptionWorkflowNotFoundTest() {
485486
});
486487
}
487488

489+
@Test
490+
void startThrowsExceptionWorkflowNotFoundViaViewTest() {
491+
492+
final JsonNode context = JsonNodeFactory.instance.objectNode();
493+
494+
when(workflowRepo.getViewById(anyString(), eq(WorkflowOperationalDto.class))).thenReturn(null);
495+
496+
assertThrows(WorkflowNotFoundException.class, () -> {
497+
workflowEngineService.start(UUID, OKAPI_TENANT, OKAPI_TOKEN, context);
498+
});
499+
}
500+
501+
@Test
502+
void startThrowsExceptionWorkflowDeploymentNotFoundViaViewTest() {
503+
504+
final ObjectNode objectNode = JsonNodeFactory.instance.objectNode();
505+
objectNode.put("id", UUID);
506+
507+
final ArrayNode arrayNodeSingle = JsonNodeFactory.instance.arrayNode();
508+
509+
final ResponseEntity<ArrayNode> processEntity = new ResponseEntity<>(arrayNodeSingle, HttpStatus.OK);
510+
final JsonNode context = JsonNodeFactory.instance.objectNode();
511+
512+
when(workflowRepo.getViewById(anyString(), eq(WorkflowOperationalDto.class))).thenReturn(workflowOperational);
513+
514+
when(restTemplate.exchange(contains(PROCESS_DEFINITION), eq(HttpMethod.GET), any(HttpEntity.class), eq(ArrayNode.class), anyMap()))
515+
.thenReturn(processEntity);
516+
517+
assertThrows(WorkflowDeploymentNotFound.class, () -> {
518+
workflowEngineService.start(UUID, OKAPI_TENANT, OKAPI_TOKEN, context);
519+
});
520+
}
521+
488522
@Test
489523
void historyWorksTest() throws WorkflowDeploymentNotFound, WorkflowEngineServiceException {
490524
WorkflowOperationalDto workflowOperationalDto = (WorkflowOperationalDto) workflowOperational;
491-
ResponseEntity<ArrayNode> responseEntity = new ResponseEntity<>(HttpStatus.OK);
492-
ResponseEntity<ArrayNode> historyResponseEntity = new ResponseEntity<>(HttpStatus.OK);
525+
ResponseEntity<ArrayNode> processEntity = new ResponseEntity<>(HttpStatus.OK);
526+
ResponseEntity<ArrayNode> historyEntity = new ResponseEntity<>(HttpStatus.OK);
527+
ResponseEntity<ArrayNode> incidentEntity = new ResponseEntity<>(HttpStatus.OK);
528+
493529
ObjectNode objectNode = mapper.createObjectNode();
494530
objectNode.put("id", UUID);
495531

496532
ObjectNode historyNode = mapper.createObjectNode();
497533
historyNode.put("id", UUID);
498534
historyNode.put("history", VALUE);
499535

500-
ArrayNode arrayNode = mapper.createArrayNode();
501-
arrayNode.add(objectNode);
502-
setField(responseEntity, "body", arrayNode);
536+
ObjectNode incidentNode = mapper.createObjectNode();
537+
incidentNode.put("id", UUID);
538+
539+
ArrayNode processNode = mapper.createArrayNode();
540+
processNode.add(objectNode);
541+
setField(processEntity, "body", processNode);
503542

504543
ArrayNode historyArrayNode = mapper.createArrayNode();
505544
historyArrayNode.add(historyNode);
506-
setField(historyResponseEntity, "body", historyArrayNode);
545+
setField(historyEntity, "body", historyArrayNode);
507546

508547
when(workflowRepo.getViewById(anyString(), eq(WorkflowOperationalDto.class))).thenReturn(workflowOperationalDto);
509548

510-
when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class), eq(ArrayNode.class), anyMap()))
511-
.thenReturn(responseEntity, historyResponseEntity);
549+
when(restTemplate.exchange(contains(PROCESS_DEFINITION), eq(HttpMethod.GET), any(HttpEntity.class), eq(ArrayNode.class), anyMap()))
550+
.thenReturn(processEntity);
551+
552+
when(restTemplate.exchange(contains(HISTORY_INSTANCE), eq(HttpMethod.GET), any(HttpEntity.class), eq(ArrayNode.class), anyMap()))
553+
.thenReturn(historyEntity);
554+
555+
when(restTemplate.exchange(contains(HISTORY_INCIDENT), eq(HttpMethod.GET), any(HttpEntity.class), eq(ArrayNode.class), anyMap()))
556+
.thenReturn(incidentEntity);
512557

513558
JsonNode response = workflowEngineService.history(UUID, OKAPI_TENANT, OKAPI_TOKEN);
514559
assertEquals(historyArrayNode, response);
@@ -552,21 +597,25 @@ void historyWorksWithoutOkFetchingIncidentsHistoryTest() throws WorkflowDeployme
552597
@Test
553598
void historyThrowsExceptionWithNotOkHttpStatusForProcessTest() {
554599
WorkflowOperationalDto workflowOperationalDto = (WorkflowOperationalDto) workflowOperational;
555-
ResponseEntity<ArrayNode> responseEntity = new ResponseEntity<>(HttpStatus.OK);
556-
ResponseEntity<ArrayNode> historyResponseEntity = new ResponseEntity<>(HttpStatus.RESET_CONTENT);
600+
ResponseEntity<ArrayNode> processEntity = new ResponseEntity<>(HttpStatus.OK);
601+
ResponseEntity<ArrayNode> historyEntity = new ResponseEntity<>(HttpStatus.RESET_CONTENT);
602+
557603
ObjectNode objectNode = mapper.createObjectNode();
558604
objectNode.put("id", UUID);
559605

560-
ArrayNode arrayNode = mapper.createArrayNode();
561-
arrayNode.add(objectNode);
562-
setField(responseEntity, "body", arrayNode);
606+
ArrayNode processNode = mapper.createArrayNode();
607+
processNode.add(objectNode);
608+
setField(processEntity, "body", processNode);
563609

564-
setField(historyResponseEntity, "body", null);
610+
setField(historyEntity, "body", null);
565611

566612
when(workflowRepo.getViewById(anyString(), eq(WorkflowOperationalDto.class))).thenReturn(workflowOperationalDto);
567613

568-
when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class), eq(ArrayNode.class), anyMap()))
569-
.thenReturn(responseEntity, historyResponseEntity);
614+
when(restTemplate.exchange(contains(PROCESS_DEFINITION), eq(HttpMethod.GET), any(HttpEntity.class), eq(ArrayNode.class), anyMap()))
615+
.thenReturn(processEntity);
616+
617+
when(restTemplate.exchange(contains(HISTORY_INSTANCE), eq(HttpMethod.GET), any(HttpEntity.class), eq(ArrayNode.class), anyMap()))
618+
.thenReturn(historyEntity);
570619

571620
assertThrows(WorkflowEngineServiceException.class, () -> {
572621
workflowEngineService.history(UUID, OKAPI_TENANT, OKAPI_TOKEN);
@@ -591,10 +640,10 @@ void historyThrowsExceptionWithNullIncidentsForProcessTest() {
591640
when(workflowRepo.getViewById(anyString(), eq(WorkflowOperationalDto.class))).thenReturn(workflowOperationalDto);
592641

593642
when(restTemplate.exchange(contains(PROCESS_DEFINITION), eq(HttpMethod.GET), any(HttpEntity.class), eq(ArrayNode.class), anyMap()))
594-
.thenReturn(processEntity);
643+
.thenReturn(processEntity);
595644

596-
when(restTemplate.exchange(contains(HISTORY_INSTANCE), eq(HttpMethod.GET), any(HttpEntity.class), eq(ArrayNode.class), anyMap()))
597-
.thenReturn(historyEntity);
645+
when(restTemplate.exchange(contains(HISTORY_INSTANCE), eq(HttpMethod.GET), any(HttpEntity.class), eq(ArrayNode.class), anyMap()))
646+
.thenReturn(historyEntity);
598647

599648
assertThrows(WorkflowEngineServiceException.class, () -> {
600649
workflowEngineService.history(UUID, OKAPI_TENANT, OKAPI_TOKEN);

0 commit comments

Comments
 (0)