Skip to content

Commit

Permalink
Fix allowing update task to modify creation date
Browse files Browse the repository at this point in the history
  • Loading branch information
ystxn committed Aug 22, 2024
1 parent 91651d4 commit ff43ed5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public void updateTaskDef(TaskDef taskDefinition) {
}
taskDefinition.setUpdatedBy(WorkflowContext.get().getClientApp());
taskDefinition.setUpdateTime(System.currentTimeMillis());
taskDefinition.setCreateTime(existing.getCreateTime());
taskDefinition.setCreatedBy(existing.getCreatedBy());
metadataDAO.updateTaskDef(taskDefinition);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.OngoingStubbing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.TestConfiguration;
Expand Down Expand Up @@ -54,7 +57,6 @@ public class MetadataServiceTest {

@TestConfiguration
static class TestMetadataConfiguration {

@Bean
public MetadataDAO metadataDAO() {
return mock(MetadataDAO.class);
Expand All @@ -72,8 +74,20 @@ public MetadataService metadataService(
MetadataDAO metadataDAO, ConductorProperties properties) {
EventHandlerDAO eventHandlerDAO = mock(EventHandlerDAO.class);

Map<String, TaskDef> taskDefinitions = new HashMap<>();

when(metadataDAO.getAllWorkflowDefs()).thenReturn(mockWorkflowDefs());

Answer<TaskDef> upsertTaskDef = (invocation) -> {
TaskDef argument = invocation.getArgument(0, TaskDef.class);
taskDefinitions.put(argument.getName(), argument);
return argument;
};
when(metadataDAO.createTaskDef(any(TaskDef.class))).then(upsertTaskDef);
when(metadataDAO.updateTaskDef(any(TaskDef.class))).then(upsertTaskDef);
when(metadataDAO.getTaskDef(any()))
.then(invocation -> taskDefinitions.get(invocation.getArgument(0, String.class)));

return new MetadataServiceImpl(metadataDAO, eventHandlerDAO, properties);
}

Expand Down Expand Up @@ -175,7 +189,6 @@ public void testUpdateTaskDefNotExisting() {
TaskDef taskDef = new TaskDef();
taskDef.setName("test");
taskDef.setOwnerEmail("[email protected]");
when(metadataDAO.getTaskDef(any())).thenReturn(null);
metadataService.updateTaskDef(taskDef);
}

Expand All @@ -184,7 +197,6 @@ public void testUpdateTaskDefDaoException() {
TaskDef taskDef = new TaskDef();
taskDef.setName("test");
taskDef.setOwnerEmail("[email protected]");
when(metadataDAO.getTaskDef(any())).thenReturn(null);
metadataService.updateTaskDef(taskDef);
}

Expand All @@ -198,6 +210,27 @@ public void testRegisterTaskDef() {
verify(metadataDAO, times(1)).createTaskDef(any(TaskDef.class));
}

@Test
public void testUpdateTask() {
String taskDefName = "another-task";
TaskDef taskDef = new TaskDef();
taskDef.setName(taskDefName);
taskDef.setOwnerEmail("[email protected]");
taskDef.setRetryCount(1);
metadataService.registerTaskDef(Collections.singletonList(taskDef));
TaskDef before = metadataService.getTaskDef(taskDefName);

taskDef.setRetryCount(2);
taskDef.setCreatedBy("someone-else");
taskDef.setCreateTime(1000L);
metadataService.updateTaskDef(taskDef);
verify(metadataDAO, times(1)).updateTaskDef(any(TaskDef.class));

TaskDef after = metadataService.getTaskDef(taskDefName);
assertEquals(2, after.getRetryCount());
assertEquals(before.getCreateTime(), after.getCreateTime());
}

@Test(expected = ConstraintViolationException.class)
public void testUpdateWorkflowDefNull() {
try {
Expand Down Expand Up @@ -272,7 +305,6 @@ public void testUpdateWorkflowDef() {
workflowTask.setName("hello");
tasks.add(workflowTask);
workflowDef.setTasks(tasks);
when(metadataDAO.getTaskDef(any())).thenReturn(new TaskDef());
metadataService.updateWorkflowDef(Collections.singletonList(workflowDef));
verify(metadataDAO, times(1)).updateWorkflowDef(workflowDef);
}
Expand Down Expand Up @@ -302,7 +334,7 @@ public void testUpdateWorkflowDefWithCaseExpression() {
workflowTask.setCaseExpression("1 >0abcd");
tasks.add(workflowTask);
workflowDef.setTasks(tasks);
when(metadataDAO.getTaskDef(any())).thenReturn(new TaskDef());

BulkResponse bulkResponse =
metadataService.updateWorkflowDef(Collections.singletonList(workflowDef));
}
Expand Down Expand Up @@ -332,7 +364,7 @@ public void testUpdateWorkflowDefWithJavscriptEvaluator() {
workflowTask.setDecisionCases(decisionCases);
tasks.add(workflowTask);
workflowDef.setTasks(tasks);
when(metadataDAO.getTaskDef(any())).thenReturn(new TaskDef());

BulkResponse bulkResponse =
metadataService.updateWorkflowDef(Collections.singletonList(workflowDef));
}
Expand Down Expand Up @@ -419,7 +451,7 @@ public void testRegisterWorkflowDef() {
workflowTask.setName("hello");
tasks.add(workflowTask);
workflowDef.setTasks(tasks);
when(metadataDAO.getTaskDef(any())).thenReturn(new TaskDef());

metadataService.registerWorkflowDef(workflowDef);
verify(metadataDAO, times(1)).createWorkflowDef(workflowDef);
assertEquals(2, workflowDef.getSchemaVersion());
Expand All @@ -437,7 +469,7 @@ public void testValidateWorkflowDef() {
workflowTask.setName("hello");
tasks.add(workflowTask);
workflowDef.setTasks(tasks);
when(metadataDAO.getTaskDef(any())).thenReturn(new TaskDef());

metadataService.validateWorkflowDef(workflowDef);
verify(metadataDAO, times(1)).createWorkflowDef(workflowDef);
assertEquals(2, workflowDef.getSchemaVersion());
Expand Down

0 comments on commit ff43ed5

Please sign in to comment.