From f0c4c7acf9082fea9e5a018ee0ef7078d3f5bf9c Mon Sep 17 00:00:00 2001 From: gabriel-farache Date: Mon, 3 Jul 2023 14:55:55 +0200 Subject: [PATCH] Add test and changed list of works to sorted set of works --- workflow-service-sdk/api/openapi.yaml | 15 ++- .../docs/WorkDefinitionResponseDTO.md | 2 + .../sdk/model/WorkDefinitionResponseDTO.java | 72 +++++++++++++- .../model/WorkFlowDefinitionResponseDTO.java | 48 +++++----- .../generated/openapi/openapi.json | 8 ++ .../dto/WorkDefinitionResponseDTO.java | 25 +++-- .../WorkFlowTaskDefinitionDTOConverter.java | 9 +- .../WorkFlowDefinitionServiceImpl.java | 41 ++++---- .../workflow/WorkFlowDelegateTest.java | 17 ++-- .../WorkFlowDefinitionServiceImplTest.java | 96 ++++++++++++++++++- 10 files changed, 258 insertions(+), 75 deletions(-) diff --git a/workflow-service-sdk/api/openapi.yaml b/workflow-service-sdk/api/openapi.yaml index 75b725138..84282ff4d 100644 --- a/workflow-service-sdk/api/openapi.yaml +++ b/workflow-service-sdk/api/openapi.yaml @@ -1242,10 +1242,12 @@ components: type: object WorkDefinitionResponseDTO: example: + cronExpression: cronExpression outputs: - EXCEPTION - EXCEPTION processingType: SEQUENTIAL + workFlowCheckerMappingDefinitionId: 046b6c7f-0b8a-43b9-b35d-6489e6daee91 works: - null - null @@ -1259,6 +1261,8 @@ components: properties: author: type: string + cronExpression: + type: string id: format: uuid type: string @@ -1285,6 +1289,9 @@ components: - PARALLEL - OTHER type: string + workFlowCheckerMappingDefinitionId: + format: uuid + type: string workType: enum: - TASK @@ -1420,10 +1427,12 @@ components: cronExpression: cronExpression processingType: SEQUENTIAL works: - - outputs: + - cronExpression: cronExpression + outputs: - EXCEPTION - EXCEPTION processingType: SEQUENTIAL + workFlowCheckerMappingDefinitionId: 046b6c7f-0b8a-43b9-b35d-6489e6daee91 works: - null - null @@ -1434,10 +1443,12 @@ components: parameters: key: key: "{}" - - outputs: + - cronExpression: cronExpression + outputs: - EXCEPTION - EXCEPTION processingType: SEQUENTIAL + workFlowCheckerMappingDefinitionId: 046b6c7f-0b8a-43b9-b35d-6489e6daee91 works: - null - null diff --git a/workflow-service-sdk/docs/WorkDefinitionResponseDTO.md b/workflow-service-sdk/docs/WorkDefinitionResponseDTO.md index 3faa8d2df..0c450c173 100644 --- a/workflow-service-sdk/docs/WorkDefinitionResponseDTO.md +++ b/workflow-service-sdk/docs/WorkDefinitionResponseDTO.md @@ -8,11 +8,13 @@ | Name | Type | Description | Notes | |------------ | ------------- | ------------- | -------------| |**author** | **String** | | [optional] | +|**cronExpression** | **String** | | [optional] | |**id** | **UUID** | | [optional] | |**name** | **String** | | [optional] | |**outputs** | [**List<OutputsEnum>**](#List<OutputsEnum>) | | [optional] | |**parameters** | **Map<String, Map<String, Object>>** | | [optional] | |**processingType** | [**ProcessingTypeEnum**](#ProcessingTypeEnum) | | [optional] | +|**workFlowCheckerMappingDefinitionId** | **UUID** | | [optional] | |**workType** | [**WorkTypeEnum**](#WorkTypeEnum) | | [optional] | |**works** | [**Set<WorkDefinitionResponseDTO>**](WorkDefinitionResponseDTO.md) | | [optional] | diff --git a/workflow-service-sdk/src/main/java/com/redhat/parodos/sdk/model/WorkDefinitionResponseDTO.java b/workflow-service-sdk/src/main/java/com/redhat/parodos/sdk/model/WorkDefinitionResponseDTO.java index e33a05d7d..49506d146 100644 --- a/workflow-service-sdk/src/main/java/com/redhat/parodos/sdk/model/WorkDefinitionResponseDTO.java +++ b/workflow-service-sdk/src/main/java/com/redhat/parodos/sdk/model/WorkDefinitionResponseDTO.java @@ -48,6 +48,11 @@ public class WorkDefinitionResponseDTO { @SerializedName(SERIALIZED_NAME_AUTHOR) private String author; + public static final String SERIALIZED_NAME_CRON_EXPRESSION = "cronExpression"; + + @SerializedName(SERIALIZED_NAME_CRON_EXPRESSION) + private String cronExpression; + public static final String SERIALIZED_NAME_ID = "id"; @SerializedName(SERIALIZED_NAME_ID) @@ -181,6 +186,11 @@ public ProcessingTypeEnum read(final JsonReader jsonReader) throws IOException { @SerializedName(SERIALIZED_NAME_PROCESSING_TYPE) private ProcessingTypeEnum processingType; + public static final String SERIALIZED_NAME_WORK_FLOW_CHECKER_MAPPING_DEFINITION_ID = "workFlowCheckerMappingDefinitionId"; + + @SerializedName(SERIALIZED_NAME_WORK_FLOW_CHECKER_MAPPING_DEFINITION_ID) + private UUID workFlowCheckerMappingDefinitionId; + /** * Gets or Sets workType */ @@ -266,6 +276,25 @@ public void setAuthor(String author) { this.author = author; } + public WorkDefinitionResponseDTO cronExpression(String cronExpression) { + + this.cronExpression = cronExpression; + return this; + } + + /** + * Get cronExpression + * @return cronExpression + **/ + @javax.annotation.Nullable + public String getCronExpression() { + return cronExpression; + } + + public void setCronExpression(String cronExpression) { + this.cronExpression = cronExpression; + } + public WorkDefinitionResponseDTO id(UUID id) { this.id = id; @@ -377,6 +406,25 @@ public void setProcessingType(ProcessingTypeEnum processingType) { this.processingType = processingType; } + public WorkDefinitionResponseDTO workFlowCheckerMappingDefinitionId(UUID workFlowCheckerMappingDefinitionId) { + + this.workFlowCheckerMappingDefinitionId = workFlowCheckerMappingDefinitionId; + return this; + } + + /** + * Get workFlowCheckerMappingDefinitionId + * @return workFlowCheckerMappingDefinitionId + **/ + @javax.annotation.Nullable + public UUID getWorkFlowCheckerMappingDefinitionId() { + return workFlowCheckerMappingDefinitionId; + } + + public void setWorkFlowCheckerMappingDefinitionId(UUID workFlowCheckerMappingDefinitionId) { + this.workFlowCheckerMappingDefinitionId = workFlowCheckerMappingDefinitionId; + } + public WorkDefinitionResponseDTO workType(WorkTypeEnum workType) { this.workType = workType; @@ -433,18 +481,22 @@ public boolean equals(Object o) { } WorkDefinitionResponseDTO workDefinitionResponseDTO = (WorkDefinitionResponseDTO) o; return Objects.equals(this.author, workDefinitionResponseDTO.author) + && Objects.equals(this.cronExpression, workDefinitionResponseDTO.cronExpression) && Objects.equals(this.id, workDefinitionResponseDTO.id) && Objects.equals(this.name, workDefinitionResponseDTO.name) && Objects.equals(this.outputs, workDefinitionResponseDTO.outputs) && Objects.equals(this.parameters, workDefinitionResponseDTO.parameters) && Objects.equals(this.processingType, workDefinitionResponseDTO.processingType) + && Objects.equals(this.workFlowCheckerMappingDefinitionId, + workDefinitionResponseDTO.workFlowCheckerMappingDefinitionId) && Objects.equals(this.workType, workDefinitionResponseDTO.workType) && Objects.equals(this.works, workDefinitionResponseDTO.works); } @Override public int hashCode() { - return Objects.hash(author, id, name, outputs, parameters, processingType, workType, works); + return Objects.hash(author, cronExpression, id, name, outputs, parameters, processingType, + workFlowCheckerMappingDefinitionId, workType, works); } @Override @@ -452,11 +504,14 @@ public String toString() { StringBuilder sb = new StringBuilder(); sb.append("class WorkDefinitionResponseDTO {\n"); sb.append(" author: ").append(toIndentedString(author)).append("\n"); + sb.append(" cronExpression: ").append(toIndentedString(cronExpression)).append("\n"); sb.append(" id: ").append(toIndentedString(id)).append("\n"); sb.append(" name: ").append(toIndentedString(name)).append("\n"); sb.append(" outputs: ").append(toIndentedString(outputs)).append("\n"); sb.append(" parameters: ").append(toIndentedString(parameters)).append("\n"); sb.append(" processingType: ").append(toIndentedString(processingType)).append("\n"); + sb.append(" workFlowCheckerMappingDefinitionId: ") + .append(toIndentedString(workFlowCheckerMappingDefinitionId)).append("\n"); sb.append(" workType: ").append(toIndentedString(workType)).append("\n"); sb.append(" works: ").append(toIndentedString(works)).append("\n"); sb.append("}"); @@ -482,11 +537,13 @@ private String toIndentedString(Object o) { // a set of all properties/fields (JSON key names) openapiFields = new HashSet(); openapiFields.add("author"); + openapiFields.add("cronExpression"); openapiFields.add("id"); openapiFields.add("name"); openapiFields.add("outputs"); openapiFields.add("parameters"); openapiFields.add("processingType"); + openapiFields.add("workFlowCheckerMappingDefinitionId"); openapiFields.add("workType"); openapiFields.add("works"); @@ -530,6 +587,12 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException { String.format("Expected the field `author` to be a primitive type in the JSON string but got `%s`", jsonObj.get("author").toString())); } + if ((jsonObj.get("cronExpression") != null && !jsonObj.get("cronExpression").isJsonNull()) + && !jsonObj.get("cronExpression").isJsonPrimitive()) { + throw new IllegalArgumentException(String.format( + "Expected the field `cronExpression` to be a primitive type in the JSON string but got `%s`", + jsonObj.get("cronExpression").toString())); + } if ((jsonObj.get("id") != null && !jsonObj.get("id").isJsonNull()) && !jsonObj.get("id").isJsonPrimitive()) { throw new IllegalArgumentException( String.format("Expected the field `id` to be a primitive type in the JSON string but got `%s`", @@ -553,6 +616,13 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException { "Expected the field `processingType` to be a primitive type in the JSON string but got `%s`", jsonObj.get("processingType").toString())); } + if ((jsonObj.get("workFlowCheckerMappingDefinitionId") != null + && !jsonObj.get("workFlowCheckerMappingDefinitionId").isJsonNull()) + && !jsonObj.get("workFlowCheckerMappingDefinitionId").isJsonPrimitive()) { + throw new IllegalArgumentException(String.format( + "Expected the field `workFlowCheckerMappingDefinitionId` to be a primitive type in the JSON string but got `%s`", + jsonObj.get("workFlowCheckerMappingDefinitionId").toString())); + } if ((jsonObj.get("workType") != null && !jsonObj.get("workType").isJsonNull()) && !jsonObj.get("workType").isJsonPrimitive()) { throw new IllegalArgumentException(String.format( diff --git a/workflow-service-sdk/src/main/java/com/redhat/parodos/sdk/model/WorkFlowDefinitionResponseDTO.java b/workflow-service-sdk/src/main/java/com/redhat/parodos/sdk/model/WorkFlowDefinitionResponseDTO.java index ff2947213..6464fac1b 100644 --- a/workflow-service-sdk/src/main/java/com/redhat/parodos/sdk/model/WorkFlowDefinitionResponseDTO.java +++ b/workflow-service-sdk/src/main/java/com/redhat/parodos/sdk/model/WorkFlowDefinitionResponseDTO.java @@ -52,16 +52,16 @@ public class WorkFlowDefinitionResponseDTO { @SerializedName(SERIALIZED_NAME_CREATE_DATE) private Date createDate; - public static final String SERIALIZED_NAME_FALLBACK_WORKFLOW = "fallbackWorkflow"; - - @SerializedName(SERIALIZED_NAME_FALLBACK_WORKFLOW) - private String fallbackWorkflow; - public static final String SERIALIZED_NAME_CRON_EXPRESSION = "cronExpression"; @SerializedName(SERIALIZED_NAME_CRON_EXPRESSION) private String cronExpression; + public static final String SERIALIZED_NAME_FALLBACK_WORKFLOW = "fallbackWorkflow"; + + @SerializedName(SERIALIZED_NAME_FALLBACK_WORKFLOW) + private String fallbackWorkflow; + public static final String SERIALIZED_NAME_ID = "id"; @SerializedName(SERIALIZED_NAME_ID) @@ -251,42 +251,42 @@ public void setCreateDate(Date createDate) { this.createDate = createDate; } - public WorkFlowDefinitionResponseDTO fallbackWorkflow(String fallbackWorkflow) { + public WorkFlowDefinitionResponseDTO cronExpression(String cronExpression) { - this.fallbackWorkflow = fallbackWorkflow; + this.cronExpression = cronExpression; return this; } /** - * Get fallbackWorkflow - * @return fallbackWorkflow + * Get cronExpression + * @return cronExpression **/ @javax.annotation.Nullable - public String getFallbackWorkflow() { - return fallbackWorkflow; + public String getCronExpression() { + return cronExpression; } - public void setFallbackWorkflow(String fallbackWorkflow) { - this.fallbackWorkflow = fallbackWorkflow; + public void setCronExpression(String cronExpression) { + this.cronExpression = cronExpression; } - public WorkFlowDefinitionResponseDTO cronExpression(String cronExpression) { + public WorkFlowDefinitionResponseDTO fallbackWorkflow(String fallbackWorkflow) { - this.cronExpression = cronExpression; + this.fallbackWorkflow = fallbackWorkflow; return this; } /** - * Get cronExpression - * @return cronExpression + * Get fallbackWorkflow + * @return fallbackWorkflow **/ @javax.annotation.Nullable - public String getCronExpression() { - return cronExpression; + public String getFallbackWorkflow() { + return fallbackWorkflow; } - public void setCronExpression(String cronExpression) { - this.cronExpression = cronExpression; + public void setFallbackWorkflow(String fallbackWorkflow) { + this.fallbackWorkflow = fallbackWorkflow; } public WorkFlowDefinitionResponseDTO id(UUID id) { @@ -482,8 +482,8 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(author, createDate, cronExpression, id, modifyDate, name, parameters, processingType, - properties, rollbackWorkflow, type, works); + return Objects.hash(author, createDate, cronExpression, fallbackWorkflow, id, modifyDate, name, parameters, + processingType, properties, type, works); } @Override @@ -492,8 +492,8 @@ public String toString() { sb.append("class WorkFlowDefinitionResponseDTO {\n"); sb.append(" author: ").append(toIndentedString(author)).append("\n"); sb.append(" createDate: ").append(toIndentedString(createDate)).append("\n"); - sb.append(" fallbackWorkflow: ").append(toIndentedString(fallbackWorkflow)).append("\n"); sb.append(" cronExpression: ").append(toIndentedString(cronExpression)).append("\n"); + sb.append(" fallbackWorkflow: ").append(toIndentedString(fallbackWorkflow)).append("\n"); sb.append(" id: ").append(toIndentedString(id)).append("\n"); sb.append(" modifyDate: ").append(toIndentedString(modifyDate)).append("\n"); sb.append(" name: ").append(toIndentedString(name)).append("\n"); diff --git a/workflow-service/generated/openapi/openapi.json b/workflow-service/generated/openapi/openapi.json index a66c8641f..82af2aac8 100644 --- a/workflow-service/generated/openapi/openapi.json +++ b/workflow-service/generated/openapi/openapi.json @@ -1659,6 +1659,9 @@ "author" : { "type" : "string" }, + "cronExpression" : { + "type" : "string" + }, "id" : { "type" : "string", "format" : "uuid" @@ -1686,11 +1689,16 @@ "type" : "string", "enum" : [ "SEQUENTIAL", "PARALLEL", "OTHER" ] }, + "workFlowCheckerMappingDefinitionId" : { + "type" : "string", + "format" : "uuid" + }, "workType" : { "type" : "string", "enum" : [ "TASK", "WORKFLOW", "CHECKER" ] }, "works" : { + "uniqueItems" : true, "type" : "array", "items" : { "$ref" : "#/components/schemas/WorkDefinitionResponseDTO" diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/dto/WorkDefinitionResponseDTO.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/dto/WorkDefinitionResponseDTO.java index 5b635e459..978ce5b3c 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/dto/WorkDefinitionResponseDTO.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/dto/WorkDefinitionResponseDTO.java @@ -15,10 +15,9 @@ */ package com.redhat.parodos.workflow.definition.dto; -import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.UUID; import com.fasterxml.jackson.annotation.JsonIgnore; @@ -63,14 +62,17 @@ public class WorkDefinitionResponseDTO { private String author; - private Set works; + private LinkedHashSet works; private Map> parameters; private List outputs; - @JsonIgnore - private UUID checkerWorkId; + @JsonInclude(JsonInclude.Include.NON_EMPTY) + private UUID workFlowCheckerMappingDefinitionId; + + @JsonInclude(JsonInclude.Include.NON_EMPTY) + private String cronExpression; @JsonIgnore private Integer numberOfWorkUnits; @@ -92,8 +94,8 @@ public WorkDefinitionResponseDTOBuilder parameterFromString(String parameters) { public static WorkDefinitionResponseDTO fromWorkFlowDefinitionEntity(WorkFlowDefinition wd, List dependencies) { return WorkDefinitionResponseDTO.builder().id(wd.getId()).workType(WorkType.WORKFLOW).name(wd.getName()) - .parameterFromString(wd.getParameters()).processingType(wd.getProcessingType()).works(new HashSet<>()) - .numberOfWorkUnits(dependencies.size()).build(); + .parameterFromString(wd.getParameters()).processingType(wd.getProcessingType()) + .works(new LinkedHashSet<>()).numberOfWorkUnits(dependencies.size()).build(); } public static WorkDefinitionResponseDTO fromWorkFlowTaskDefinition(WorkFlowTaskDefinition wdt) { @@ -103,15 +105,18 @@ public static WorkDefinitionResponseDTO fromWorkFlowTaskDefinition(WorkFlowTaskD }, List.of())).numberOfWorkUnits(0); if (wdt.getWorkFlowCheckerMappingDefinition() != null) { - builder = builder.checkerWorkId(wdt.getWorkFlowCheckerMappingDefinition().getId()); + builder = builder.workFlowCheckerMappingDefinitionId(wdt.getWorkFlowCheckerMappingDefinition().getId()); } return builder.build(); } public static WorkDefinitionResponseDTO fromWorkFlowCheckerMappingDefinition(WorkFlowCheckerMappingDefinition wcd) { - return WorkDefinitionResponseDTO.builder().workType(WorkType.CHECKER).numberOfWorkUnits(wcd.getTasks().size()) - .checkerWorkId(wcd.getCheckWorkFlow().getCheckerWorkFlowDefinition().getId()).build(); + return WorkDefinitionResponseDTO.builder().id(wcd.getCheckWorkFlow().getId()) + .name(wcd.getCheckWorkFlow().getName()).parameterFromString(wcd.getCheckWorkFlow().getParameters()) + .processingType(wcd.getCheckWorkFlow().getProcessingType()).workType(WorkType.CHECKER) + .numberOfWorkUnits(wcd.getCheckWorkFlow().getNumberOfWorks()).cronExpression(wcd.getCronExpression()) + .build(); } } diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/dto/converter/WorkFlowTaskDefinitionDTOConverter.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/dto/converter/WorkFlowTaskDefinitionDTOConverter.java index 5d2b4aa88..76f55839c 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/dto/converter/WorkFlowTaskDefinitionDTOConverter.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/dto/converter/WorkFlowTaskDefinitionDTOConverter.java @@ -16,6 +16,7 @@ package com.redhat.parodos.workflow.definition.dto.converter; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import com.fasterxml.jackson.core.JsonProcessingException; @@ -35,11 +36,11 @@ */ public class WorkFlowTaskDefinitionDTOConverter - implements Converter, List> { + implements Converter, Set> { @Override - public List convert( - MappingContext, List> context) { + public Set convert( + MappingContext, Set> context) { ObjectMapper objectMapper = new ObjectMapper(); List source = context.getSource(); return source.stream().map(workFlowTaskDefinition -> { @@ -52,7 +53,7 @@ public List convert( catch (JsonProcessingException e) { throw new WorkflowDefinitionException(e); } - }).collect(Collectors.toList()); + }).collect(Collectors.toSet()); } } diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/service/WorkFlowDefinitionServiceImpl.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/service/WorkFlowDefinitionServiceImpl.java index 9a9b8944a..16dbdd982 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/service/WorkFlowDefinitionServiceImpl.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/service/WorkFlowDefinitionServiceImpl.java @@ -19,7 +19,7 @@ import java.util.Comparator; import java.util.Date; import java.util.HashMap; -import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -269,7 +269,8 @@ public WorkFlowDefinition getParentWorkFlowByWorkName(String workName) { private void getWorksFromWorkDefinition(WorkDefinitionResponseDTO workflow, List workFlowWorkDefinitions, Queue responseDTOs) { if (workflow.getWorks() == null) { - workflow.setWorks(new HashSet<>()); + // LinkedHashSet to keep insertion order + workflow.setWorks(new LinkedHashSet<>()); } workFlowWorkDefinitions.forEach(workFlowWorkDefinition -> { @@ -342,13 +343,11 @@ private WorkDefinitionResponseDTO buildRootWorkFlow(WorkFlowDefinition workFlowD List workFlowWorkDefinitions, Queue workDefinitionResponseDTOs) { WorkDefinitionResponseDTO rootWorkFlow = WorkDefinitionResponseDTO.builder().id(workFlowDefinition.getId()) - .workType(workFlowDefinition.getType() == WorkFlowType.CHECKER ? WorkType.CHECKER : WorkType.WORKFLOW) - .name(workFlowDefinition.getName()).parameterFromString(workFlowDefinition.getParameters()) - .processingType(workFlowDefinition.getProcessingType()).works(new HashSet<>()) + .workType(WorkType.WORKFLOW).name(workFlowDefinition.getName()) + .parameterFromString(workFlowDefinition.getParameters()) + .processingType(workFlowDefinition.getProcessingType()).works(new LinkedHashSet<>()) .numberOfWorkUnits(workFlowWorkDefinitions.size()).build(); workDefinitionResponseDTOs.add(rootWorkFlow); - // add workflowWorkUnits - this.getWorksFromWorkDefinition(rootWorkFlow, workFlowWorkDefinitions, workDefinitionResponseDTOs); return rootWorkFlow; } @@ -369,20 +368,22 @@ private void populateWorkFlowWorksRecursive(Queue wor private void getCheckerWorkFromWorkDefinition(WorkDefinitionResponseDTO workflow, Queue workDefinitionResponseDTOs) { - if (workflow.getCheckerWorkId() != null) { - WorkFlowDefinition checkerWorkflowDefinition = workFlowCheckerMappingDefinitionRepository - .findById(workflow.getCheckerWorkId()).get().getCheckWorkFlow(); - WorkDefinitionResponseDTO checkerWorkflowDefinitionDTO = WorkDefinitionResponseDTO.builder() - .id(checkerWorkflowDefinition.getId()).workType(WorkType.CHECKER) - .name(checkerWorkflowDefinition.getName()) - .parameterFromString(checkerWorkflowDefinition.getParameters()) - .processingType(checkerWorkflowDefinition.getProcessingType()).works(new HashSet<>()) - .numberOfWorkUnits(checkerWorkflowDefinition.getNumberOfWorks()).build(); - workDefinitionResponseDTOs.add(checkerWorkflowDefinitionDTO); - if (workflow.getWorks() == null) { - workflow.setWorks(new HashSet<>()); + if (workflow.getWorkFlowCheckerMappingDefinitionId() != null) { + Optional checkerMappingDefinition = workFlowCheckerMappingDefinitionRepository + .findById(workflow.getWorkFlowCheckerMappingDefinitionId()); + if (checkerMappingDefinition.isPresent()) { + WorkDefinitionResponseDTO checkerWorkflowDefinitionDTO = WorkDefinitionResponseDTO + .fromWorkFlowCheckerMappingDefinition(checkerMappingDefinition.get()); + workDefinitionResponseDTOs.add(checkerWorkflowDefinitionDTO); + if (workflow.getWorks() == null) { + workflow.setWorks(new LinkedHashSet<>()); + } + workflow.getWorks().add(checkerWorkflowDefinitionDTO); + } + else { + log.error("WorkFlowCheckerMappingDefinition %s associated with WorkDefinition %s not found" + .formatted(workflow.getWorkFlowCheckerMappingDefinitionId(), workflow.getId())); } - workflow.getWorks().add(checkerWorkflowDefinitionDTO); } } diff --git a/workflow-service/src/test/java/com/redhat/parodos/workflow/WorkFlowDelegateTest.java b/workflow-service/src/test/java/com/redhat/parodos/workflow/WorkFlowDelegateTest.java index 41f9099f2..ccfae3d4a 100644 --- a/workflow-service/src/test/java/com/redhat/parodos/workflow/WorkFlowDelegateTest.java +++ b/workflow-service/src/test/java/com/redhat/parodos/workflow/WorkFlowDelegateTest.java @@ -16,8 +16,8 @@ package com.redhat.parodos.workflow; import java.util.HashMap; +import java.util.LinkedHashSet; import java.util.List; -import java.util.Set; import java.util.UUID; import com.fasterxml.jackson.core.type.TypeReference; @@ -113,14 +113,13 @@ void initWorkFlowContext_when_workflowParameterIsFound_thenReturn_success() { } private WorkFlowDefinitionResponseDTO sampleWorkflowDefinitionResponse() { - return WorkFlowDefinitionResponseDTO - .builder().name( - TEST_WORKFLOW_NAME) - .works(Set.of(WorkDefinitionResponseDTO.builder().name(TEST_SUB_WORKFLOW_NAME) - .workType(WorkType.WORKFLOW).works(Set.of(WorkDefinitionResponseDTO.builder() - .name(TEST_TASK_NAME).workType(WorkType.TASK).build())) - .build())) - .build(); + LinkedHashSet workFlowWorks = new LinkedHashSet<>(); + LinkedHashSet taskWorks = new LinkedHashSet<>(); + + taskWorks.add(WorkDefinitionResponseDTO.builder().name(TEST_TASK_NAME).workType(WorkType.TASK).build()); + workFlowWorks.add(WorkDefinitionResponseDTO.builder().name(TEST_SUB_WORKFLOW_NAME).workType(WorkType.WORKFLOW) + .works(taskWorks).build()); + return WorkFlowDefinitionResponseDTO.builder().name(TEST_WORKFLOW_NAME).works(workFlowWorks).build(); } } diff --git a/workflow-service/src/test/java/com/redhat/parodos/workflow/definition/service/WorkFlowDefinitionServiceImplTest.java b/workflow-service/src/test/java/com/redhat/parodos/workflow/definition/service/WorkFlowDefinitionServiceImplTest.java index 18665dc6a..f1e5688e2 100644 --- a/workflow-service/src/test/java/com/redhat/parodos/workflow/definition/service/WorkFlowDefinitionServiceImplTest.java +++ b/workflow-service/src/test/java/com/redhat/parodos/workflow/definition/service/WorkFlowDefinitionServiceImplTest.java @@ -18,6 +18,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.Optional; @@ -51,6 +52,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -313,9 +315,39 @@ public void getWorkFlowDefinitionWithoutData() { @Test public void getWorkFlowDefinitionsWithData() { // given - when(this.workFlowDefinitionRepository.findByTypeIsNot(WorkFlowType.CHECKER)).thenReturn( - Arrays.asList(sampleWorkFlowDefinition("workFLowOne"), sampleWorkFlowDefinition("workFLowTwo"))); + WorkFlowDefinition workFlowOne = sampleWorkFlowDefinition("workFLowOne"); + WorkFlowDefinition checker = sampleCheckerWorkFlowDefinition("checker"); + WorkParameter workParameter = WorkParameter.builder().key("key").description("the key").optional(false) + .type(WorkParameterType.URI).build(); + WorkFlowCheckerMappingDefinition workFlowCheckerMappingDefinition = WorkFlowCheckerMappingDefinition.builder() + .checkWorkFlow(checker).cronExpression("* * * * *") + .tasks(Collections.singletonList(sampleWorkFlowTaskDefinition(checker, "taskOfChecker", workParameter))) + .build(); + workFlowCheckerMappingDefinition.setId(UUID.randomUUID()); + WorkFlowTaskDefinition task01 = sampleWorkFlowTaskDefinitionWithChecker(workFlowOne, "task01", workParameter, + workFlowCheckerMappingDefinition); + WorkFlowTaskDefinition task02 = sampleWorkFlowTaskDefinition(workFlowOne, "task02", workParameter); + + WorkFlowWorkDefinition taskDefinition = WorkFlowWorkDefinition.builder().workFlowDefinition(workFlowOne) + .workDefinitionId(UUID.randomUUID()).workDefinitionType(WorkType.TASK).createDate(new Date()).build(); + taskDefinition.setId(UUID.randomUUID()); + WorkFlowWorkDefinition taskDefinition02 = WorkFlowWorkDefinition.builder().workFlowDefinition(workFlowOne) + .workDefinitionId(UUID.randomUUID()).workDefinitionType(WorkType.TASK).createDate(new Date()).build(); + taskDefinition02.setId(UUID.randomUUID()); + + workFlowOne.setNumberOfWorks(1); + + when(this.workFlowCheckerMappingDefinitionRepository.findById(workFlowCheckerMappingDefinition.getId())) + .thenReturn(Optional.of(workFlowCheckerMappingDefinition)); + when(this.workFlowTaskDefinitionRepository.findById(taskDefinition.getWorkDefinitionId())) + .thenReturn(Optional.of(task01)); + when(this.workFlowTaskDefinitionRepository.findById(taskDefinition02.getWorkDefinitionId())) + .thenReturn(Optional.of(task02)); + when(this.workFlowDefinitionRepository.findByTypeIsNot(WorkFlowType.CHECKER)) + .thenReturn(Arrays.asList(workFlowOne, sampleWorkFlowDefinition("workFLowTwo"))); + when(this.workFlowWorkRepository.findByWorkFlowDefinitionIdOrderByCreateDateAsc(workFlowOne.getId())) + .thenReturn(List.of(taskDefinition, taskDefinition02)); // when List workFlowDefinitionResponseDTOs = this.workFlowDefinitionService .getWorkFlowDefinitions(); @@ -323,8 +355,36 @@ public void getWorkFlowDefinitionsWithData() { // then assertNotNull(workFlowDefinitionResponseDTOs); assertEquals(workFlowDefinitionResponseDTOs.size(), 2); - assertEquals(workFlowDefinitionResponseDTOs.get(0).getName(), "workFLowOne"); - assertEquals(workFlowDefinitionResponseDTOs.get(0).getProperties().getVersion(), "1.0.0"); + + // check root WorkFlow + WorkFlowDefinitionResponseDTO workFlowOneResponseDTO = workFlowDefinitionResponseDTOs.get(0); + assertEquals(workFlowOneResponseDTO.getName(), "workFLowOne"); + assertEquals(workFlowOneResponseDTO.getType(), workFlowOne.getType()); + assertEquals(workFlowOneResponseDTO.getProcessingType(), workFlowOne.getProcessingType()); + assertNull(workFlowOneResponseDTO.getCronExpression()); + assertEquals(workFlowDefinitionResponseDTOs.get(1).getName(), "workFLowTwo"); + assertEquals(workFlowOneResponseDTO.getProperties().getVersion(), "1.0.0"); + assertFalse(workFlowOneResponseDTO.getWorks().isEmpty()); + + // check associated tasks + ArrayList tasks = new ArrayList<>(workFlowOneResponseDTO.getWorks()); + WorkDefinitionResponseDTO taskWorkUnit = tasks.get(0); + assertEquals(taskWorkUnit.getWorkFlowCheckerMappingDefinitionId(), workFlowCheckerMappingDefinition.getId()); + assertEquals(taskWorkUnit.getParameters().get(workParameter.getKey()), workParameter.getAsJsonSchema()); + assertEquals(taskWorkUnit.getName(), task01.getName()); + WorkDefinitionResponseDTO taskWorkUnit02 = tasks.get(1); + assertEquals(taskWorkUnit02.getName(), task02.getName()); + assertNull(taskWorkUnit02.getWorkFlowCheckerMappingDefinitionId()); + assertEquals(taskWorkUnit.getParameters().get(workParameter.getKey()), workParameter.getAsJsonSchema()); + + // check checker of task01 + WorkDefinitionResponseDTO checkerWorkUnit = taskWorkUnit.getWorks().stream().findFirst().get(); + assertEquals(checkerWorkUnit.getWorkType(), WorkType.CHECKER); + assertEquals(checkerWorkUnit.getName(), checker.getName()); + assertEquals(checkerWorkUnit.getProcessingType(), checker.getProcessingType()); + assertEquals(checkerWorkUnit.getCronExpression(), workFlowCheckerMappingDefinition.getCronExpression()); + assertEquals(checkerWorkUnit.getId(), checker.getId()); + verify(this.workFlowDefinitionRepository, times(1)).findByTypeIsNot(WorkFlowType.CHECKER); } @@ -453,13 +513,39 @@ private WorkFlowDefinition sampleWorkFlowDefinition(String name) { return workFlowDefinition; } + private WorkFlowDefinition sampleCheckerWorkFlowDefinition(String name) { + com.redhat.parodos.workflow.parameter.WorkParameter workParameter = com.redhat.parodos.workflow.parameter.WorkParameter + .builder().key(KEY).description(KEY_DESCRIPTION).optional(false) + .type(com.redhat.parodos.workflow.parameter.WorkParameterType.TEXT).build(); + + WorkFlowDefinition workFlowDefinition = WorkFlowDefinition.builder().name(name).type(WorkFlowType.CHECKER) + .properties(WorkFlowPropertiesDefinition.builder().version("1.0.0").build()) + .processingType(WorkFlowProcessingType.SEQUENTIAL) + .parameters(WorkFlowDTOUtil + .writeObjectValueAsString(Map.of(workParameter.getKey(), workParameter.getAsJsonSchema()))) + .numberOfWorks(1).build(); + workFlowDefinition.setId(UUID.randomUUID()); + return workFlowDefinition; + } + private WorkFlowTaskDefinition sampleWorkFlowTaskDefinition(WorkFlowDefinition workFlowDefinition, String workFlowTaskName, WorkParameter workParameter) { WorkFlowTaskDefinition workFlowTaskDefinition = WorkFlowTaskDefinition.builder() .workFlowDefinition(workFlowDefinition).name(workFlowTaskName) .parameters(WorkFlowDTOUtil .writeObjectValueAsString(Map.of(workParameter.getKey(), workParameter.getAsJsonSchema()))) - .build(); + .outputs("[]").build(); + workFlowTaskDefinition.setId(UUID.randomUUID()); + return workFlowTaskDefinition; + } + + private WorkFlowTaskDefinition sampleWorkFlowTaskDefinitionWithChecker(WorkFlowDefinition workFlowDefinition, + String workFlowTaskName, WorkParameter workParameter, WorkFlowCheckerMappingDefinition checker) { + WorkFlowTaskDefinition workFlowTaskDefinition = WorkFlowTaskDefinition.builder() + .workFlowDefinition(workFlowDefinition).name(workFlowTaskName) + .parameters(WorkFlowDTOUtil + .writeObjectValueAsString(Map.of(workParameter.getKey(), workParameter.getAsJsonSchema()))) + .workFlowCheckerMappingDefinition(checker).outputs("[]").build(); workFlowTaskDefinition.setId(UUID.randomUUID()); return workFlowTaskDefinition; }