Написать тест на метод, который реализован в Camunda:
public void onMainUserTaskComplete(final DelegateTask delegateTask) {
final var selectedTasksVariableName = CamundaDataContainer.USER_DATA.createVariableName("selectedTasks");
final var selectedTasks = (JacksonJsonNode) delegateTask.getVariable(selectedTasksVariableName);
if (selectedTasks == null) {
return;
}
code
final var value = selectedTasks.prop("value");
if (value == null) {
return;
}
final var actionVariableName = CamundaDataContainer.USER_DATA.createVariableName(VARIABLE_USER_TASK_ACTION);
final var commentVariableName = CamundaDataContainer.USER_DATA.createVariableName(VARIABLE_USER_TASK_COMMENT);
final var actionValue = delegateTask.getVariable(actionVariableName);
final var commentValue = delegateTask.getVariable(commentVariableName);
final var assignee = delegateTask.getAssignee();
final Map<String, Object> variables;
if (commentValue != null) {
variables = Map.of(actionVariableName, actionValue, commentVariableName, commentValue);
} else {
variables = Map.of(actionVariableName, actionValue);
}
final var taskService = delegateTask.getProcessEngineServices().getTaskService();
value.elements().parallelStream()
.map(SpinJsonNode::stringValue)
.filter(taskId -> !taskId.equals(delegateTask.getId()))
.map(taskId -> taskService.createTaskQuery().taskId(taskId).singleResult())
.filter(task -> task.getName().equals(delegateTask.getName()))
.forEach(task -> {
try {
if (task.getAssignee() == null) {
taskService.claim(task.getId(), assignee);
} else if (!task.getAssignee().equals(assignee)) {
log.debug("task has another assignee {}", task.getId());
return;
}
taskService.complete(task.getId(), variables);
} catch (final Exception e) {
log.error("exception on claim or completing task {}", task.getId(), e);
}
});
}
Написать тест на метод, который реализован в Camunda:
@Transactional
public void onUserTaskComplete(final TaskEvent taskEvent, final ProcessEngine processEngine) {
try {
final Map<String, Object> variables = processEngine.getRuntimeService()
.getVariables(taskEvent.getExecutionId());
final Map<String, Object> userData = new HashMap<>(3);
@ExtendWith(MockitoExtension.class)
@ProcessTestCase
class CamundaTest {
code
@Mock
private ProcessEngine processEngine;
@Test
void testOnUserTaskComplete(@Mock ProcessStarter.ProcessStarterExecution starterExecution,
@Mock TaskService taskService,
@Mock VariableMap variables) {
Mocks.register("processEngine", processEngine);
when(processEngine.getRuntimeService()).thenReturn(runtimeService);
when(processEngine.getTaskService()).thenReturn(taskService);
when(runtimeService.getVariables(anyString())).thenReturn(variables);
when(taskService.createTaskQuery().taskId(anyString())).thenReturn(taskQuery);
// Set up test data
String executionId = "testExecutionId";
String assignee = "testAssignee";
String actionVariableName = "testActionVariableName";
String commentVariableName = "testCommentVariableName";
String actionValue = "testActionValue";
String commentValue = "testCommentValue";
Map<String, Object> variablesData = new HashMap<>();
variablesData.put(actionVariableName, actionValue);
variablesData.put(commentVariableName, commentValue);
when(runtimeService.getVariables(executionId)).thenReturn(variablesData);
DelegateTaskFake delegateTask = new DelegateTaskFake();
delegateTask.setId("testTaskId");
delegateTask.setExecutionId(executionId);
delegateTask.setAssignee(assignee);
when(taskQuery.singleResult()).thenReturn(delegateTask);
when(taskQuery.taskId(delegateTask.getId())).thenReturn(taskQuery);
// Create instance of the class under test
CamundaTestClass camundaTestClass = new CamundaTestClass();
// Execute the method
camundaTestClass.onUserTaskComplete(delegateTask, processEngine);
// Verify the results
verify(processEngine.getRuntimeService(), times(1)).setVariables(executionId, variables);
verify(taskService, times(1)).complete(delegateTask.getId(), variables);
// Additional assertions
assertNotNull(variables.get(actionVariableName));
assertNotNull(variables.get(commentVariableName));
}
static class CamundaTestClass {
@Transactional
public void onUserTaskComplete(final TaskEvent taskEvent, final ProcessEngine processEngine) {
// Method implementation here
}
}
}
дан класс
@Slf4j
@Service
@RequiredArgsConstructor
public class CamundaUserTaskService {
public static final String VARIABLE_USER_TASK_ASSIGNEE = "assignee";
public static final String VARIABLE_USER_TASK_COMMENT = "comment";
public static final String VARIABLE_USER_TASK_ACTION = "action";
public static final String VARIABLE_REQUEST_ID = "HDR_RMI_RequestId";
public static final String VARIABLE_SEGMENT = "PRC_segment";
public static final String VARIABLE_ERRORS = "TSK_errors";
public static final String ERROR_CODE_PROPERTY = "code";
public static final String VARIABLE_REQUEST = "request";
public static final String VARIABLE_ERROR_NOTIFICATIONS = "errorNotifications";
code
}
нужно написать Unit тесты для методов:
onUserTaskComplete,
onMainUserTaskComplete
использовать java 17