From 6427db07898612772b12214566f8d25c39986c30 Mon Sep 17 00:00:00 2001 From: richardwang98 Date: Mon, 1 May 2023 00:13:31 -0400 Subject: [PATCH] propertyPath in update parameter response --- .../parameter/WorkParameterValueResponse.java | 2 +- .../ComplexWorkParameterValueProvider.java | 7 +- .../complex/task/AdGroupsWorkFlowTask.java | 4 +- .../dto/WorkParameterValueResponseDTO.java | 8 +++ .../entity/WorkFlowWorkDefinition.java | 1 + .../parameter/WorkParameterServiceImpl.java | 36 ++++++++-- .../repository/WorkFlowWorkRepository.java | 2 +- .../service/WorkFlowDefinitionService.java | 6 ++ .../WorkFlowDefinitionServiceImpl.java | 34 ++++++++-- .../service/WorkFlowServiceImpl.java | 2 +- .../registry/BeanWorkFlowRegistryImpl.java | 22 ++---- .../workflow/registry/WorkFlowRegistry.java | 6 +- .../registry/WorkFlowRegistryDelegate.java | 33 +++++++++ .../WorkParameterServiceImplTest.java | 41 +++++++++++- .../WorkFlowDefinitionServiceImplTest.java | 67 ++++++++++++++++++- .../aspect/WorkFlowExecutionAspectTest.java | 4 +- .../service/WorkFlowServiceImplTest.java | 8 +-- 17 files changed, 234 insertions(+), 49 deletions(-) create mode 100644 workflow-service/src/main/java/com/redhat/parodos/workflow/registry/WorkFlowRegistryDelegate.java diff --git a/parodos-model-api/src/main/java/com/redhat/parodos/workflow/parameter/WorkParameterValueResponse.java b/parodos-model-api/src/main/java/com/redhat/parodos/workflow/parameter/WorkParameterValueResponse.java index 5f8cf3406..612a7386a 100644 --- a/parodos-model-api/src/main/java/com/redhat/parodos/workflow/parameter/WorkParameterValueResponse.java +++ b/parodos-model-api/src/main/java/com/redhat/parodos/workflow/parameter/WorkParameterValueResponse.java @@ -35,7 +35,7 @@ public class WorkParameterValueResponse { private String key; - private String propertyPath; + private String workName; private List options; diff --git a/workflow-examples/src/main/java/com/redhat/parodos/examples/complex/parameter/ComplexWorkParameterValueProvider.java b/workflow-examples/src/main/java/com/redhat/parodos/examples/complex/parameter/ComplexWorkParameterValueProvider.java index 47638f4e3..e4d07a926 100644 --- a/workflow-examples/src/main/java/com/redhat/parodos/examples/complex/parameter/ComplexWorkParameterValueProvider.java +++ b/workflow-examples/src/main/java/com/redhat/parodos/examples/complex/parameter/ComplexWorkParameterValueProvider.java @@ -41,8 +41,11 @@ public List getValues(List getWorkFlowTaskParameters() { WorkParameter.builder().key("adGroups").description("The ad groups").type(WorkParameterType.TEXT) .optional(false).build(), WorkParameter.builder().key("userId").description("The user id").type(WorkParameterType.TEXT) - .optional(false).build()); + .optional(false).build(), + WorkParameter.builder().key("dynamic-options").description("Dynamic options") + .type(WorkParameterType.SELECT).optional(true).build()); } @Override diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/dto/WorkParameterValueResponseDTO.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/dto/WorkParameterValueResponseDTO.java index 6abfe9bf7..7e561df5c 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/dto/WorkParameterValueResponseDTO.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/dto/WorkParameterValueResponseDTO.java @@ -1,6 +1,7 @@ package com.redhat.parodos.workflow.definition.dto; import com.fasterxml.jackson.annotation.JsonInclude; +import com.redhat.parodos.workflow.parameter.WorkParameterValueResponse; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -23,4 +24,11 @@ public class WorkParameterValueResponseDTO { private String propertyPath; + public static WorkParameterValueResponseDTO convertToDto(WorkParameterValueResponse workParameterValueResponse, + String propertyPath) { + return WorkParameterValueResponseDTO.builder().key(workParameterValueResponse.getKey()) + .options(workParameterValueResponse.getOptions()).value(workParameterValueResponse.getValue()) + .propertyPath(propertyPath).build(); + } + } diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/entity/WorkFlowWorkDefinition.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/entity/WorkFlowWorkDefinition.java index aa79610ef..069dd8d37 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/entity/WorkFlowWorkDefinition.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/entity/WorkFlowWorkDefinition.java @@ -47,6 +47,7 @@ @Setter public class WorkFlowWorkDefinition extends AbstractEntity { + @Column(unique = true) private UUID workDefinitionId; @Enumerated(EnumType.STRING) diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/parameter/WorkParameterServiceImpl.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/parameter/WorkParameterServiceImpl.java index e854e88ca..6d0453fcf 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/parameter/WorkParameterServiceImpl.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/parameter/WorkParameterServiceImpl.java @@ -2,24 +2,30 @@ import com.redhat.parodos.workflow.definition.dto.WorkParameterValueRequestDTO; import com.redhat.parodos.workflow.definition.dto.WorkParameterValueResponseDTO; +import com.redhat.parodos.workflow.definition.entity.WorkFlowDefinition; import com.redhat.parodos.workflow.definition.service.WorkFlowDefinitionService; -import com.redhat.parodos.workflow.parameter.WorkParameterValueRequest; import com.redhat.parodos.workflow.parameter.WorkParameterValueProvider; +import com.redhat.parodos.workflow.parameter.WorkParameterValueRequest; +import com.redhat.parodos.workflow.parameter.WorkParameterValueResponse; import org.modelmapper.ModelMapper; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; -import java.util.UUID; @Service public class WorkParameterServiceImpl implements WorkParameterService { private final Map workFlowValueProviderMap; - public WorkParameterServiceImpl(Map workFlowValueProviderMap) { + private final WorkFlowDefinitionService workFlowDefinitionService; + + public WorkParameterServiceImpl(Map workFlowValueProviderMap, + WorkFlowDefinitionService workFlowDefinitionService) { this.workFlowValueProviderMap = workFlowValueProviderMap; + this.workFlowDefinitionService = workFlowDefinitionService; } @Override @@ -33,10 +39,28 @@ public List getValues(String workflowDefinitionNa .map(workParameterValueRequestDTO -> new ModelMapper().map( workParameterValueRequestDTO, WorkParameterValueRequest.class)) .toList()) - .stream().map(parameterValue -> new ModelMapper().map(parameterValue, - WorkParameterValueResponseDTO.class)) - .toList()) + .stream() + .map(parameterValue -> mappingParameterResponse(workflowDefinitionName, parameterValue)) + .filter(Objects::nonNull).toList()) .orElse(List.of()); } + private WorkParameterValueResponseDTO mappingParameterResponse(String workflowDefinitionName, + WorkParameterValueResponse workParameterValueResponse) { + String workName = Optional.ofNullable(workParameterValueResponse.getWorkName()).orElse(workflowDefinitionName); + Map parameters = workFlowDefinitionService.getWorkParametersByWorkName(workName); + if (parameters == null || !parameters.containsKey(workParameterValueResponse.getKey())) + return null; + + String propertyPath = workName; + WorkFlowDefinition workFlowDefinition = workFlowDefinitionService.getParentWorkFlowByWorkName(workName); + + while (workFlowDefinition != null) { + propertyPath = String.join(".", workFlowDefinition.getName(), propertyPath); + workFlowDefinition = workFlowDefinitionService.getParentWorkFlowByWorkName(workFlowDefinition.getName()); + } + + return WorkParameterValueResponseDTO.convertToDto(workParameterValueResponse, propertyPath); + } + } diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/repository/WorkFlowWorkRepository.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/repository/WorkFlowWorkRepository.java index 7019c0069..d4fa8db19 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/repository/WorkFlowWorkRepository.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/repository/WorkFlowWorkRepository.java @@ -31,6 +31,6 @@ public interface WorkFlowWorkRepository extends JpaRepository findByWorkFlowDefinitionIdOrderByCreateDateAsc(UUID workFlowDefinitionId); - List findByWorkDefinitionId(UUID workDefinitionId); + WorkFlowWorkDefinition findFirstByWorkDefinitionId(UUID workDefinitionId); } \ No newline at end of file diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/service/WorkFlowDefinitionService.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/service/WorkFlowDefinitionService.java index 0ecd9949d..f867b6eed 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/service/WorkFlowDefinitionService.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/definition/service/WorkFlowDefinitionService.java @@ -15,6 +15,7 @@ */ package com.redhat.parodos.workflow.definition.service; +import com.redhat.parodos.workflow.definition.entity.WorkFlowDefinition; import com.redhat.parodos.workflow.enums.WorkFlowProcessingType; import com.redhat.parodos.workflow.enums.WorkFlowType; import com.redhat.parodos.workflow.definition.dto.WorkFlowCheckerDTO; @@ -24,6 +25,7 @@ import com.redhat.parodos.workflows.workflow.WorkFlowPropertiesMetadata; import java.util.List; +import java.util.Map; import java.util.UUID; /** @@ -48,6 +50,10 @@ WorkFlowDefinitionResponseDTO save(String workFlowName, WorkFlowType workFlowTyp void saveWorkFlowChecker(String workFlowTaskName, String workFlowCheckerName, WorkFlowCheckerDTO workFlowCheckerDTO); + Map getWorkParametersByWorkName(String workName); + + WorkFlowDefinition getParentWorkFlowByWorkName(String workName); + void cleanAllDefinitionMappings(); } 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 39cbc65a8..e1b354fd3 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 @@ -15,12 +15,14 @@ */ package com.redhat.parodos.workflow.definition.service; +import com.fasterxml.jackson.core.type.TypeReference; import com.redhat.parodos.common.AbstractEntity; import com.redhat.parodos.workflow.definition.dto.WorkDefinitionResponseDTO; import com.redhat.parodos.workflow.definition.dto.WorkFlowCheckerDTO; import com.redhat.parodos.workflow.definition.dto.WorkFlowDefinitionResponseDTO; import com.redhat.parodos.workflow.definition.entity.WorkFlowCheckerMappingDefinition; import com.redhat.parodos.workflow.definition.entity.WorkFlowDefinition; +import com.redhat.parodos.workflow.definition.entity.WorkFlowPropertiesDefinition; import com.redhat.parodos.workflow.definition.entity.WorkFlowTaskDefinition; import com.redhat.parodos.workflow.definition.entity.WorkFlowWorkDefinition; import com.redhat.parodos.workflow.definition.repository.WorkFlowCheckerMappingDefinitionRepository; @@ -39,7 +41,6 @@ import lombok.extern.slf4j.Slf4j; import org.modelmapper.ModelMapper; import org.springframework.stereotype.Service; -import com.redhat.parodos.workflow.definition.entity.WorkFlowPropertiesDefinition; import java.util.ArrayList; import java.util.Comparator; @@ -50,7 +51,6 @@ import java.util.Optional; import java.util.UUID; import java.util.concurrent.CopyOnWriteArrayList; -import java.util.stream.Collectors; /** * workflow definition service implementation @@ -190,7 +190,7 @@ public WorkFlowDefinitionResponseDTO getWorkFlowDefinitionById(UUID id) { .orElseThrow(() -> new RuntimeException(String.format("Workflow definition id %s not found", id))); List workFlowWorkDependencies = workFlowWorkRepository .findByWorkFlowDefinitionIdOrderByCreateDateAsc(workFlowDefinition.getId()).stream() - .sorted(Comparator.comparing(WorkFlowWorkDefinition::getCreateDate)).collect(Collectors.toList()); + .sorted(Comparator.comparing(WorkFlowWorkDefinition::getCreateDate)).toList(); return WorkFlowDefinitionResponseDTO.fromEntity(workFlowDefinition, buildWorkFlowWorksDTOs(workFlowDefinition, workFlowWorkDependencies)); } @@ -203,7 +203,7 @@ public WorkFlowDefinitionResponseDTO getWorkFlowDefinitionByName(String name) { } List workFlowWorkDependencies = workFlowWorkRepository .findByWorkFlowDefinitionIdOrderByCreateDateAsc(workFlowDefinition.getId()).stream() - .sorted(Comparator.comparing(WorkFlowWorkDefinition::getCreateDate)).collect(Collectors.toList()); + .sorted(Comparator.comparing(WorkFlowWorkDefinition::getCreateDate)).toList(); return WorkFlowDefinitionResponseDTO.fromEntity(workFlowDefinition, buildWorkFlowWorksDTOs(workFlowDefinition, workFlowWorkDependencies)); @@ -230,6 +230,29 @@ public void saveWorkFlowChecker(String workFlowTaskName, String workFlowCheckerN } } + @Override + public Map getWorkParametersByWorkName(String workName) { + return Optional.ofNullable(workFlowTaskDefinitionRepository.findFirstByName(workName)) + .map(WorkFlowTaskDefinition::getParameters).map(stringifyParameters -> WorkFlowDTOUtil + .readStringAsObject(stringifyParameters, new TypeReference>() { + }, null)) + .orElse(Optional.ofNullable(workFlowDefinitionRepository.findFirstByName(workName)) + .map(WorkFlowDefinition::getParameters).map(stringifyParameters -> WorkFlowDTOUtil + .readStringAsObject(stringifyParameters, new TypeReference>() { + }, null)) + .orElse(null)); + } + + @Override + public WorkFlowDefinition getParentWorkFlowByWorkName(String workName) { + UUID workId = Optional.ofNullable(workFlowDefinitionRepository.findFirstByName(workName)) + .map(AbstractEntity::getId) + .orElse(Optional.ofNullable(workFlowTaskDefinitionRepository.findFirstByName(workName)) + .map(AbstractEntity::getId).orElse(null)); + return workId == null ? null : Optional.ofNullable(workFlowWorkRepository.findFirstByWorkDefinitionId(workId)) + .map(WorkFlowWorkDefinition::getWorkFlowDefinition).orElse(null); + } + private void getWorksFromWorkDefinition(List workFlowWorkDefinitions, CopyOnWriteArrayList responseDTOs) { workFlowWorkDefinitions.forEach(workFlowWorkDefinition -> { @@ -296,8 +319,7 @@ private List buildWorkFlowWorksDTOs(WorkFlowDefinitio List workFlowWorkUnits1Definition = workFlowWorkRepository .findByWorkFlowDefinitionIdOrderByCreateDateAsc(workDefinitionResponseDTOs.get(i).getId()) - .stream().sorted(Comparator.comparing(WorkFlowWorkDefinition::getCreateDate)) - .collect(Collectors.toList()); + .stream().sorted(Comparator.comparing(WorkFlowWorkDefinition::getCreateDate)).toList(); this.getWorksFromWorkDefinition(workFlowWorkUnits1Definition, workDefinitionResponseDTOs); } } diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImpl.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImpl.java index ec8735236..e1d540131 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImpl.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImpl.java @@ -349,7 +349,7 @@ private String validateWorkflow(String workflowName, WorkFlow workFlow) { return String.format("workflow '%s' is not registered!", workflowName); } - if (!workFlowWorkRepository.findByWorkDefinitionId(workFlowDefinition.getId()).isEmpty()) { + if (workFlowWorkRepository.findFirstByWorkDefinitionId(workFlowDefinition.getId()) != null) { log.error("workflow '{}' is not main workflow!", workflowName); return String.format("workflow '%s' is not main workflow!", workflowName); } diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/registry/BeanWorkFlowRegistryImpl.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/registry/BeanWorkFlowRegistryImpl.java index 7780b844a..27ed232cb 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/registry/BeanWorkFlowRegistryImpl.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/registry/BeanWorkFlowRegistryImpl.java @@ -25,7 +25,6 @@ import com.redhat.parodos.workflow.enums.WorkFlowProcessingType; import com.redhat.parodos.workflow.enums.WorkFlowType; import com.redhat.parodos.workflow.parameter.WorkParameter; -import com.redhat.parodos.workflow.parameter.WorkParameterType; import com.redhat.parodos.workflow.task.WorkFlowTask; import com.redhat.parodos.workflows.work.Work; import com.redhat.parodos.workflows.workflow.WorkFlow; @@ -41,12 +40,10 @@ import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import java.lang.annotation.Annotation; -import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; /** * An implementation of the WorkflowRegistry that loads all Bean definitions of type @@ -59,7 +56,7 @@ @Slf4j @Component -public class BeanWorkFlowRegistryImpl implements WorkFlowRegistry { +public class BeanWorkFlowRegistryImpl implements WorkFlowRegistry { // Spring will populate this through classpath scanning when the Context starts up private final ConfigurableListableBeanFactory beanFactory; @@ -109,19 +106,8 @@ private void saveWorkFlow(String workFlowBeanName, WorkFlow workFlowBean) { // workflow type WorkFlowType workFlowType = workFlowTypeDetailsPair.getFirst(); // workflow parameters from annotation attributes - AnnotationAttributes[] annotationAttributes = (AnnotationAttributes[]) workFlowTypeDetailsPair.getSecond() - .get("parameters"); - List workParameters = new ArrayList<>(); - if (annotationAttributes != null && annotationAttributes.length > 0) { - workParameters = Arrays.stream(annotationAttributes) - .map(annotationAttribute -> WorkParameter.builder().key(annotationAttribute.getString("key")) - .description(annotationAttribute.getString("description")) - .type((WorkParameterType) annotationAttribute.get("type")) - .optional(annotationAttribute.getBoolean("optional")) - .selectOptions(Arrays.stream(annotationAttribute.getStringArray("selectOptions")).toList()) - .valueProviderName(annotationAttribute.getString("valueProviderName")).build()) - .toList(); - } + List workParameters = WorkFlowRegistryDelegate + .getWorkParameters((AnnotationAttributes[]) workFlowTypeDetailsPair.getSecond().get("parameters")); workFlowDefinitionService.save(workFlowBeanName, workFlowType, workFlowBean.getProperties(), workParameters, works, getWorkFlowProcessingType(workFlowBean)); } @@ -129,7 +115,7 @@ private void saveWorkFlow(String workFlowBeanName, WorkFlow workFlowBean) { private List getWorks(String workFlowName) { return Arrays.stream(beanFactory.getDependenciesForBean(workFlowName)) .filter(dependency -> isBeanInstanceOf(beanFactory, dependency, WorkFlow.class, WorkFlowTask.class)) - .map(dependency -> beanFactory.getBean(dependency, Work.class)).collect(Collectors.toList()); + .map(dependency -> beanFactory.getBean(dependency, Work.class)).toList(); } private void saveChecker(Map workFlowTasks) { diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/registry/WorkFlowRegistry.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/registry/WorkFlowRegistry.java index e80cf7177..4f527cc5b 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/registry/WorkFlowRegistry.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/registry/WorkFlowRegistry.java @@ -20,15 +20,15 @@ /** * Contract for Registering and Obtaining Workflows. Implementations could use the * classpath, Spring Bean registry, a Database or some other custom means. - * + *

* For the WorkFlowRegistry in-memory collection the Key: BeanId, Value: WorkFlow * * @author Luke Shannon (Github: lshannon) * @author Annel Ketcha (Github: anludke) * @author Richard Wang (Github: richardw98) */ -public interface WorkFlowRegistry { +public interface WorkFlowRegistry { - WorkFlow getWorkFlowByName(T workFlowName); + WorkFlow getWorkFlowByName(String workFlowName); } diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/registry/WorkFlowRegistryDelegate.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/registry/WorkFlowRegistryDelegate.java new file mode 100644 index 000000000..4059f6982 --- /dev/null +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/registry/WorkFlowRegistryDelegate.java @@ -0,0 +1,33 @@ +package com.redhat.parodos.workflow.registry; + +import com.redhat.parodos.workflow.parameter.WorkParameter; +import com.redhat.parodos.workflow.parameter.WorkParameterType; +import lombok.NonNull; +import org.springframework.core.annotation.AnnotationAttributes; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class WorkFlowRegistryDelegate { + + private WorkFlowRegistryDelegate() { + } + + @NonNull + static List getWorkParameters(AnnotationAttributes[] annotationAttributes) { + List workParameters = new ArrayList<>(); + if (annotationAttributes != null && annotationAttributes.length > 0) { + workParameters = Arrays.stream(annotationAttributes) + .map(annotationAttribute -> WorkParameter.builder().key(annotationAttribute.getString("key")) + .description(annotationAttribute.getString("description")) + .type((WorkParameterType) annotationAttribute.get("type")) + .optional(annotationAttribute.getBoolean("optional")) + .selectOptions(Arrays.stream(annotationAttribute.getStringArray("selectOptions")).toList()) + .valueProviderName(annotationAttribute.getString("valueProviderName")).build()) + .toList(); + } + return workParameters; + } + +} \ No newline at end of file diff --git a/workflow-service/src/test/java/com/redhat/parodos/workflow/definition/parameter/WorkParameterServiceImplTest.java b/workflow-service/src/test/java/com/redhat/parodos/workflow/definition/parameter/WorkParameterServiceImplTest.java index 1df9e88c0..a890b9116 100644 --- a/workflow-service/src/test/java/com/redhat/parodos/workflow/definition/parameter/WorkParameterServiceImplTest.java +++ b/workflow-service/src/test/java/com/redhat/parodos/workflow/definition/parameter/WorkParameterServiceImplTest.java @@ -2,6 +2,8 @@ import com.redhat.parodos.workflow.definition.dto.WorkParameterValueRequestDTO; import com.redhat.parodos.workflow.definition.dto.WorkParameterValueResponseDTO; +import com.redhat.parodos.workflow.definition.entity.WorkFlowDefinition; +import com.redhat.parodos.workflow.definition.service.WorkFlowDefinitionService; import com.redhat.parodos.workflow.parameter.WorkParameterValueProvider; import com.redhat.parodos.workflow.parameter.WorkParameterValueRequest; import com.redhat.parodos.workflow.parameter.WorkParameterValueResponse; @@ -36,15 +38,22 @@ class WorkParameterServiceImplTest { private static final String TEST_RESPONSE_KEY = "test-response-key"; + private static final String TEST_INVALID_RESPONSE_KEY = "invalid-test-response-key"; + private static final String OPTION_1 = "option1"; private static final String OPTION_2 = "option2"; public static final String WORKFLOW_NAME = "workflow-name"; + public static final String SUB_WORKFLOW_NAME = "sub-workflow-name"; + @Mock private WorkParameterValueProvider workParameterValueProvider; + @Mock + private WorkFlowDefinitionService workFlowDefinitionService; + private WorkParameterServiceImpl workFlowParameterService; private static List workParameterValueRequestDTOs; @@ -53,7 +62,8 @@ class WorkParameterServiceImplTest { @BeforeEach void beforeEach() { - workFlowParameterService = new WorkParameterServiceImpl(Map.of(DATA_PROVIDER_NAME, workParameterValueProvider)); + workFlowParameterService = new WorkParameterServiceImpl(Map.of(DATA_PROVIDER_NAME, workParameterValueProvider), + workFlowDefinitionService); List workParameterValueRequests = getSampleParameterValueRequests(); List workParameterValueResponses = getSampleParameterValueResponses(); workParameterValueRequestDTOs = getSampleParameterValueRequestDTOs(); @@ -64,6 +74,13 @@ void beforeEach() { @Test void updateValue_when_dataProviderIsFound_then_returnModifiedOptions() { + Mockito.when(workFlowDefinitionService.getWorkParametersByWorkName(TEST_WORK)) + .thenReturn(Map.of(TEST_RESPONSE_KEY, "test-value")); + Mockito.when(workFlowDefinitionService.getParentWorkFlowByWorkName(TEST_WORK)) + .thenReturn(WorkFlowDefinition.builder().name(SUB_WORKFLOW_NAME).build()); + Mockito.when(workFlowDefinitionService.getParentWorkFlowByWorkName(SUB_WORKFLOW_NAME)) + .thenReturn(WorkFlowDefinition.builder().name(WORKFLOW_NAME).build()); + Mockito.when(workFlowDefinitionService.getParentWorkFlowByWorkName(WORKFLOW_NAME)).thenReturn(null); assertEquals(workParameterValueResponseDTOs, workFlowParameterService.getValues(WORKFLOW_NAME, DATA_PROVIDER_NAME, workParameterValueRequestDTOs)); } @@ -75,6 +92,23 @@ void updateValue_when_dataProviderIsNotFound_then_returnEmptyList() { .isEmpty(); } + @Test + void updateValue_when_workIsNotFound_then_returnEmptyList() { + Mockito.when(workFlowDefinitionService.getWorkParametersByWorkName(anyString())).thenReturn(null); + assertThat( + workFlowParameterService.getValues(WORKFLOW_NAME, INVALID_DATA_PROVIDER, workParameterValueRequestDTOs)) + .isEmpty(); + } + + @Test + void updateValue_when_parameterIsNotFound_then_returnEmptyList() { + Mockito.when(workFlowDefinitionService.getWorkParametersByWorkName(anyString())) + .thenReturn(Map.of(TEST_INVALID_RESPONSE_KEY, "test-value")); + assertThat( + workFlowParameterService.getValues(WORKFLOW_NAME, INVALID_DATA_PROVIDER, workParameterValueRequestDTOs)) + .isEmpty(); + } + private static List getSampleParameterValueRequestDTOs() { return List .of(WorkParameterValueRequestDTO.builder().key(TEST_KEY).value(TEST_VALUE).workName(TEST_WORK).build()); @@ -88,12 +122,13 @@ private static List getSampleParameterValueRequests() private static List getSampleParameterValueResponses() { return List.of(WorkParameterValueResponse.builder().key(TEST_RESPONSE_KEY).options(List.of(OPTION_1, OPTION_2)) - .value(OPTION_1).build()); + .value(OPTION_1).workName(TEST_WORK).build()); } private static List getSampleParameterValueResponseDTOs() { return List.of(WorkParameterValueResponseDTO.builder().key(TEST_RESPONSE_KEY) - .options(List.of(OPTION_1, OPTION_2)).value(OPTION_1).build()); + .options(List.of(OPTION_1, OPTION_2)).value(OPTION_1) + .propertyPath(String.join(".", WORKFLOW_NAME, SUB_WORKFLOW_NAME, TEST_WORK)).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 125c5d2ec..1c696e033 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 @@ -15,6 +15,8 @@ */ package com.redhat.parodos.workflow.definition.service; +import com.fasterxml.jackson.core.type.TypeReference; +import com.redhat.parodos.common.AbstractEntity; import com.redhat.parodos.workflow.definition.entity.WorkFlowCheckerMappingDefinition; import com.redhat.parodos.workflow.definition.entity.WorkFlowPropertiesDefinition; import com.redhat.parodos.workflow.definition.entity.WorkFlowWorkDefinition; @@ -38,8 +40,10 @@ import com.redhat.parodos.workflow.task.WorkFlowTask; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -372,6 +376,65 @@ public void saveWorkFlowCheckerTestWithInvalidData() { Mockito.verify(this.workFlowTaskDefinitionRepository, Mockito.times(0)).save(any()); } + @Test + void getWorkParametersByWorkName_when_workIsFound_then_returnParameters() { + String workFlowName = "test"; + String workFlowTaskName = "testTask"; + String KEY = "key"; + // given + WorkFlowDefinition workFlowDefinition = this.sampleWorkFlowDefinition(workFlowName); + WorkParameter workParameter = WorkParameter.builder().key(KEY).description("the key").optional(false) + .type(WorkParameterType.URL).build(); + WorkFlowTaskDefinition workFlowTaskDefinition = this.sampleWorkFlowTaskDefinition(workFlowDefinition, + workFlowTaskName, workParameter); + workFlowDefinition.setWorkFlowTaskDefinitions(List.of(workFlowTaskDefinition)); + Mockito.when(this.workFlowDefinitionRepository.findFirstByName(anyString())).thenReturn(null); + Mockito.when(this.workFlowTaskDefinitionRepository.findFirstByName(anyString())) + .thenReturn(workFlowTaskDefinition); + assertThat(workFlowDefinitionService.getWorkParametersByWorkName(workFlowTaskName)).isNotNull() + .containsKey(KEY); + } + + @Test + void getWorkParametersByWorkName_when_workIsNotFound_then_returnNull() { + Mockito.when(this.workFlowDefinitionRepository.findFirstByName(anyString())).thenReturn(null); + Mockito.when(this.workFlowTaskDefinitionRepository.findFirstByName(anyString())).thenReturn(null); + assertNull(workFlowDefinitionService.getWorkParametersByWorkName("test")); + } + + @Test + void getParentWorkFlowByWorkName_when_workIsTask_then_returnWorkflow() { + String workFlowName = "test"; + String workFlowTaskName = "testTask"; + // given + WorkFlowDefinition workFlowDefinition = this.sampleWorkFlowDefinition(workFlowName); + WorkParameter workParameter = WorkParameter.builder().key("key").description("the key").optional(false) + .type(WorkParameterType.URL).build(); + WorkFlowTaskDefinition workFlowTaskDefinition = this.sampleWorkFlowTaskDefinition(workFlowDefinition, + workFlowTaskName, workParameter); + workFlowDefinition.setWorkFlowTaskDefinitions(List.of(workFlowTaskDefinition)); + Mockito.when(this.workFlowDefinitionRepository.findFirstByName(anyString())).thenReturn(null); + Mockito.when(this.workFlowTaskDefinitionRepository.findFirstByName(anyString())) + .thenReturn(workFlowTaskDefinition); + Mockito.when(workFlowWorkRepository.findFirstByWorkDefinitionId(workFlowTaskDefinition.getId())) + .thenReturn(WorkFlowWorkDefinition.builder().workFlowDefinition(workFlowDefinition).build()); + assertEquals(workFlowDefinition, workFlowDefinitionService.getParentWorkFlowByWorkName(workFlowTaskName)); + } + + @Test + void getParentWorkFlowByWorkName_when_workIsWorkFlow_then_returnWorkflow() { + String workFlowName = "test"; + String workFlowParentName = "testParent"; + // given + WorkFlowDefinition workFlowDefinition = this.sampleWorkFlowDefinition(workFlowName); + WorkFlowDefinition workFlowParentDefinition = this.sampleWorkFlowDefinition(workFlowParentName); + + Mockito.when(this.workFlowDefinitionRepository.findFirstByName(workFlowName)).thenReturn(workFlowDefinition); + Mockito.when(workFlowWorkRepository.findFirstByWorkDefinitionId(workFlowDefinition.getId())) + .thenReturn(WorkFlowWorkDefinition.builder().workFlowDefinition(workFlowParentDefinition).build()); + assertEquals(workFlowParentDefinition, workFlowDefinitionService.getParentWorkFlowByWorkName(workFlowName)); + } + private WorkFlowTaskDefinition sampleWorkflowTaskDefinition() { WorkFlowTaskDefinition workFlowTaskDefinition = WorkFlowTaskDefinition.builder().name(TEST).build(); workFlowTaskDefinition.setId(UUID.randomUUID()); @@ -397,7 +460,9 @@ private WorkFlowTaskDefinition sampleWorkFlowTaskDefinition(WorkFlowDefinition w String workFlowTaskName, WorkParameter workParameter) { WorkFlowTaskDefinition workFlowTaskDefinition = WorkFlowTaskDefinition.builder() .workFlowDefinition(workFlowDefinition).name(workFlowTaskName) - .parameters(WorkFlowDTOUtil.writeObjectValueAsString(List.of(workParameter))).build(); + .parameters(WorkFlowDTOUtil + .writeObjectValueAsString(Map.of(workParameter.getKey(), workParameter.getAsJsonSchema()))) + .build(); workFlowTaskDefinition.setId(UUID.randomUUID()); return workFlowTaskDefinition; } diff --git a/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/aspect/WorkFlowExecutionAspectTest.java b/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/aspect/WorkFlowExecutionAspectTest.java index 5226d773d..cab8fedee 100644 --- a/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/aspect/WorkFlowExecutionAspectTest.java +++ b/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/aspect/WorkFlowExecutionAspectTest.java @@ -156,8 +156,8 @@ void ExecuteAroundAdviceWithInProgressWorkFlowTest() { Mockito.when(this.workFlowDefinitionRepository.findFirstByName(Mockito.any())).thenReturn(workFlowDefinition); Mockito.when(this.workFlowService.saveWorkFlow(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(workFlowExecution); - Mockito.when(workFlowWorkRepository.findByWorkDefinitionId(Mockito.any())) - .thenReturn(List.of(workFlowWorkDefinition)); + Mockito.when(workFlowWorkRepository.findFirstByWorkDefinitionId(Mockito.any())) + .thenReturn(workFlowWorkDefinition); ProceedingJoinPoint proceedingJoinPoint = Mockito.mock(ProceedingJoinPoint.class); WorkFlow workFlow = Mockito.mock(WorkFlow.class); Mockito.when(proceedingJoinPoint.getTarget()).thenReturn(workFlow); diff --git a/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImplTest.java b/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImplTest.java index 7785ae4c1..fe172a25d 100644 --- a/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImplTest.java +++ b/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImplTest.java @@ -176,7 +176,7 @@ void executeWithDTOWithValidData() { })); Mockito.when(this.workFlowDefinitionRepository.findFirstByName(Mockito.any())) .thenReturn(this.sampleWorkflowDefinition("test")); - Mockito.when(this.workFlowWorkRepository.findByWorkDefinitionId(Mockito.any())).thenReturn(List.of()); + Mockito.when(this.workFlowWorkRepository.findFirstByWorkDefinitionId(Mockito.any())).thenReturn(null); Mockito.when(this.workFlowDelegate.initWorkFlowContext(Mockito.any(), Mockito.any())) .thenReturn(new WorkContext()); Mockito.when(this.workFlowDelegate.getWorkFlowExecutionByName("test-workflow")).thenReturn(workFlow); @@ -203,8 +203,8 @@ void executeWithDTOWithNoMainWorkFlow() { SequentialFlow workFlow = SequentialFlow.Builder.aNewSequentialFlow().named("test").execute(work).build(); Mockito.when(this.workFlowDefinitionRepository.findFirstByName(Mockito.any())) .thenReturn(this.sampleWorkflowDefinition("test")); - Mockito.when(this.workFlowWorkRepository.findByWorkDefinitionId(Mockito.any())) - .thenReturn(List.of(WorkFlowWorkDefinition.builder().build())); + Mockito.when(this.workFlowWorkRepository.findFirstByWorkDefinitionId(Mockito.any())) + .thenReturn(WorkFlowWorkDefinition.builder().build()); Mockito.when(this.workFlowDelegate.getWorkFlowExecutionByName("test-workflow")).thenReturn(workFlow); @@ -244,7 +244,7 @@ void executeWithDTOWithNoWorkFlowDefinition() { Mockito.verify(this.workFlowDelegate, Mockito.times(1)).getWorkFlowExecutionByName(Mockito.any()); Mockito.verify(this.workFlowDelegate, Mockito.never()).initWorkFlowContext(Mockito.any(), Mockito.any()); Mockito.verify(this.workFlowDefinitionRepository, Mockito.times(1)).findFirstByName(Mockito.any()); - Mockito.verify(this.workFlowWorkRepository, Mockito.never()).findByWorkDefinitionId(Mockito.any()); + Mockito.verify(this.workFlowWorkRepository, Mockito.never()).findFirstByWorkDefinitionId(Mockito.any()); } @Test