提交 b8a3fe3a 编写于 作者: I Illia Goncharov 提交者: Elias Ricken de Medeiros

Update task is implemented for TaskAdminRuntimeImpl (#2397)

Related to https://github.com/Activiti/Activiti/issues/2372
上级 493d9f3a
......@@ -64,6 +64,7 @@ import org.activiti.runtime.api.event.internal.TaskCreatedListenerDelegate;
import org.activiti.runtime.api.event.internal.TaskSuspendedListenerDelegate;
import org.activiti.runtime.api.event.internal.TaskUpdatedListenerDelegate;
import org.activiti.runtime.api.impl.TaskAdminRuntimeImpl;
import org.activiti.runtime.api.impl.TaskRuntimeHelper;
import org.activiti.runtime.api.impl.TaskRuntimeImpl;
import org.activiti.runtime.api.model.impl.APITaskCandidateGroupConverter;
import org.activiti.runtime.api.model.impl.APITaskCandidateUserConverter;
......@@ -72,10 +73,8 @@ import org.activiti.runtime.api.model.impl.APIVariableInstanceConverter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@AutoConfigureAfter(CommonRuntimeAutoConfiguration.class)
......@@ -87,20 +86,39 @@ public class TaskRuntimeAutoConfiguration {
SecurityManager securityManager,
APITaskConverter taskConverter,
APIVariableInstanceConverter variableInstanceConverter,
TaskRuntimeConfiguration configuration) {
TaskRuntimeConfiguration configuration,
TaskRuntimeHelper taskRuntimeHelper
) {
return new TaskRuntimeImpl(taskService,
userGroupManager,
securityManager,
taskConverter,
variableInstanceConverter,
configuration);
configuration,
taskRuntimeHelper);
}
@Bean
public TaskAdminRuntime taskAdminRuntime(TaskService taskService,
APITaskConverter taskConverter) {
APITaskConverter taskConverter,
TaskRuntimeHelper taskRuntimeHelper) {
return new TaskAdminRuntimeImpl(taskService,
taskConverter
taskConverter,
taskRuntimeHelper
);
}
@Bean
public TaskRuntimeHelper taskRuntimeHelper(TaskService taskService,
APITaskConverter taskConverter,
SecurityManager securityManager,
UserGroupManager userGroupManager) {
return new TaskRuntimeHelper(
taskService,
taskConverter,
securityManager,
userGroupManager
);
}
......@@ -116,7 +134,7 @@ public class TaskRuntimeAutoConfiguration {
return new TaskRuntimeConfigurationImpl(getInitializedTaskRuntimeEventListeners(taskRuntimeEventListeners),
getInitializedTaskRuntimeEventListeners(variableEventListeners));
}
@Bean
public InitializingBean registerTaskCreatedEventListener(RuntimeService runtimeService,
@Autowired(required = false) List<TaskRuntimeEventListener<TaskCreatedEvent>> listeners,
......
......@@ -19,7 +19,6 @@ package org.activiti.runtime.api.impl;
import java.util.ArrayList;
import java.util.List;
import org.activiti.api.runtime.shared.NotFoundException;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.activiti.api.task.model.Task;
......@@ -50,20 +49,20 @@ public class TaskAdminRuntimeImpl implements TaskAdminRuntime {
private final TaskService taskService;
private final APITaskConverter taskConverter;
private final TaskRuntimeHelper taskRuntimeHelper;
public TaskAdminRuntimeImpl(TaskService taskService,
APITaskConverter taskConverter) {
APITaskConverter taskConverter,
TaskRuntimeHelper taskRuntimeHelper) {
this.taskService = taskService;
this.taskConverter = taskConverter;
this.taskRuntimeHelper=taskRuntimeHelper;
}
@Override
public Task task(String taskId) {
org.activiti.engine.task.Task internalTask = taskService.createTaskQuery().taskId(taskId).singleResult();
if (internalTask == null) {
throw new NotFoundException("Unable to find task for the given id: " + taskId);
}
return taskConverter.from(internalTask);
return taskConverter.from(taskRuntimeHelper.getInternalTask(taskId));
}
@Override
......@@ -89,6 +88,11 @@ public class TaskAdminRuntimeImpl implements TaskAdminRuntime {
return new PageImpl<>(tasks,
Math.toIntExact(taskQuery.count()));
}
@Override
public Task update(UpdateTaskPayload updateTaskPayload) {
return taskRuntimeHelper.applyUpdateTaskPayload(true, updateTaskPayload);
}
@Override
public Task delete(DeleteTaskPayload deleteTaskPayload) {
......@@ -230,8 +234,4 @@ public class TaskAdminRuntimeImpl implements TaskAdminRuntime {
return taskService.getIdentityLinksForTask(taskId);
}
@Override
public Task update(UpdateTaskPayload updateTaskPayload) {
throw new IllegalArgumentException("To be implemented");
}
}
package org.activiti.runtime.api.impl;
import java.util.List;
import java.util.Objects;
import org.activiti.api.runtime.shared.NotFoundException;
import org.activiti.api.runtime.shared.identity.UserGroupManager;
import org.activiti.api.runtime.shared.security.SecurityManager;
import org.activiti.api.task.model.Task;
import org.activiti.api.task.model.payloads.UpdateTaskPayload;
import org.activiti.engine.TaskService;
import org.activiti.runtime.api.model.impl.APITaskConverter;
public class TaskRuntimeHelper {
private final TaskService taskService;
private final SecurityManager securityManager;
private final UserGroupManager userGroupManager;
private final APITaskConverter taskConverter;
public TaskRuntimeHelper(TaskService taskService,
APITaskConverter taskConverter,
SecurityManager securityManager,
UserGroupManager userGroupManager) {
this.taskService = taskService;
this.securityManager=securityManager;
this.userGroupManager = userGroupManager;
this.taskConverter = taskConverter;
}
public Task applyUpdateTaskPayload(boolean isAdmin, UpdateTaskPayload updateTaskPayload) {
org.activiti.engine.task.Task internalTask;
if (isAdmin) {
internalTask = getInternalTask(updateTaskPayload.getTaskId());
} else {
internalTask = getTaskToUpdate(updateTaskPayload.getTaskId());
}
int updates = updateName(updateTaskPayload,
internalTask,
0);
updates = updateDescription(updateTaskPayload,
internalTask,
updates);
updates = updatePriority(updateTaskPayload,
internalTask,
updates);
updates = updateDueDate(updateTaskPayload,
internalTask,
updates);
updates = updateParentTaskId(updateTaskPayload,
internalTask,
updates);
updates = updateFormKey(updateTaskPayload,
internalTask,
updates);
if (updates > 0) {
taskService.saveTask(internalTask);
}
return taskConverter.from(getInternalTask(updateTaskPayload.getTaskId()));
}
private org.activiti.engine.task.Task getTaskToUpdate(String taskId) {
String authenticatedUserId = getAuthenticatedUser();
org.activiti.engine.task.Task internalTask = getInternalTaskWithChecks(taskId);
// validate that you are trying to update task where you are the assignee
if (!Objects.equals(internalTask.getAssignee(), authenticatedUserId)) {
throw new IllegalStateException("You cannot update a task where you are not the assignee");
}
return internalTask;
}
private int updateFormKey(UpdateTaskPayload updateTaskPayload,
org.activiti.engine.task.Task internalTask,
int updates) {
String newValue;
if ((newValue=updateTaskPayload.getFormKey()) != null) {
String oldValue = internalTask.getFormKey();
if (!Objects.equals(oldValue, newValue)) {
updates++;
internalTask.setFormKey(newValue);
}
}
return updates;
}
private int updateParentTaskId(UpdateTaskPayload updateTaskPayload,
org.activiti.engine.task.Task internalTask,
int updates) {
String newValue;
if ((newValue=updateTaskPayload.getParentTaskId()) != null) {
String oldValue = internalTask.getParentTaskId();
if (!Objects.equals(oldValue, newValue)) {
updates++;
internalTask.setParentTaskId(newValue);
}
}
return updates;
}
private int updateDueDate(UpdateTaskPayload updateTaskPayload,
org.activiti.engine.task.Task internalTask,
int updates) {
if (updateTaskPayload.getDueDate() != null && !Objects.equals(internalTask.getDueDate(),
updateTaskPayload.getDueDate())) {
updates++;
internalTask.setDueDate(updateTaskPayload.getDueDate());
}
return updates;
}
private int updatePriority(UpdateTaskPayload updateTaskPayload,
org.activiti.engine.task.Task internalTask,
int updates) {
if (updateTaskPayload.getPriority() != null && internalTask.getPriority() != updateTaskPayload.getPriority()) {
updates++;
internalTask.setPriority(updateTaskPayload.getPriority());
}
return updates;
}
private int updateDescription(UpdateTaskPayload updateTaskPayload,
org.activiti.engine.task.Task internalTask,
int updates) {
String newValue;
if ((newValue = updateTaskPayload.getDescription()) != null) {
String oldValue = internalTask.getDescription();
if (!Objects.equals(oldValue, newValue)) {
updates++;
internalTask.setDescription(newValue);
}
}
return updates;
}
private int updateName(UpdateTaskPayload updateTaskPayload,
org.activiti.engine.task.Task internalTask,
int updates) {
String newValue;
if ((newValue = updateTaskPayload.getName()) != null) {
String oldValue = internalTask.getName();
if (!Objects.equals(oldValue, newValue)) {
updates++;
internalTask.setName(newValue);
}
}
return updates;
}
public org.activiti.engine.task.Task getInternalTaskWithChecks(String taskId) {
String authenticatedUserId = getAuthenticatedUser();
if (authenticatedUserId != null && !authenticatedUserId.isEmpty() && userGroupManager!=null) {
List<String> userRoles = userGroupManager.getUserRoles(authenticatedUserId);
List<String> userGroups = userGroupManager.getUserGroups(authenticatedUserId);
org.activiti.engine.task.Task task = taskService.createTaskQuery().taskCandidateOrAssigned(authenticatedUserId,
userGroups).taskId(taskId).singleResult();
if (task == null) {
throw new NotFoundException("Unable to find task for the given id: " + taskId + " for user: " + authenticatedUserId + " (with groups: " + userGroups + " & with roles: " + userRoles + ")");
}
return task;
}
throw new IllegalStateException("There is no authenticated user, we need a user authenticated to find tasks");
}
private String getAuthenticatedUser() {
return securityManager!=null ? securityManager.getAuthenticatedUserId() : null;
}
public org.activiti.engine.task.Task getInternalTask(String taskId) {
org.activiti.engine.task.Task internalTask = taskService.createTaskQuery().taskId(taskId).singleResult();
if (internalTask == null) {
throw new NotFoundException("Unable to find task for the given id: " + taskId);
}
return internalTask;
}
}
......@@ -65,19 +65,23 @@ public class TaskRuntimeImpl implements TaskRuntime {
private final UserGroupManager userGroupManager;
private final SecurityManager securityManager;
private final TaskRuntimeHelper taskRuntimeHelper;
public TaskRuntimeImpl(TaskService taskService,
UserGroupManager userGroupManager,
SecurityManager securityManager,
APITaskConverter taskConverter,
APIVariableInstanceConverter variableInstanceConverter,
TaskRuntimeConfiguration configuration) {
TaskRuntimeConfiguration configuration,
TaskRuntimeHelper taskRuntimeHelper) {
this.taskService = taskService;
this.userGroupManager = userGroupManager;
this.securityManager = securityManager;
this.taskConverter = taskConverter;
this.variableInstanceConverter = variableInstanceConverter;
this.configuration = configuration;
this.taskRuntimeHelper=taskRuntimeHelper;
}
@Override
......@@ -87,7 +91,7 @@ public class TaskRuntimeImpl implements TaskRuntime {
@Override
public Task task(String taskId) {
return taskConverter.from(getInternalTaskWithChecks(taskId));
return taskConverter.from(taskRuntimeHelper.getInternalTaskWithChecks(taskId));
}
@Override
......@@ -210,80 +214,9 @@ public class TaskRuntimeImpl implements TaskRuntime {
return task(releaseTaskPayload.getTaskId());
}
@Override
public Task update(UpdateTaskPayload updateTaskPayload) {
// Validate that the task is visible by the authenticated user
Task task;
int updates=0;
String oldValue,newValue;
try {
task = task(updateTaskPayload.getTaskId());
} catch (IllegalStateException ex) {
throw new IllegalStateException("The authenticated user cannot update the task" + updateTaskPayload.getTaskId() + " due it is not the current assignee");
}
String authenticatedUserId = securityManager.getAuthenticatedUserId();
// validate that you are trying to update task where you are the assignee
if (!Objects.equals(task.getAssignee(),authenticatedUserId)) {
throw new IllegalStateException("You cannot update a task where you are not the assignee");
}
org.activiti.engine.task.Task internalTask = getInternalTask(updateTaskPayload.getTaskId());
if ((newValue = updateTaskPayload.getName()) != null) {
oldValue = internalTask.getName();
if (!Objects.equals(oldValue,newValue)) {
updates++;
internalTask.setName(newValue);
}
}
if ((newValue = updateTaskPayload.getDescription()) != null) {
oldValue = internalTask.getDescription();
if (!Objects.equals(oldValue,newValue)) {
updates++;
internalTask.setDescription(newValue);
}
}
if (updateTaskPayload.getPriority() != null) {
if (internalTask.getPriority()!=updateTaskPayload.getPriority()) {
updates++;
internalTask.setPriority(updateTaskPayload.getPriority());
}
}
if (updateTaskPayload.getDueDate() != null) {
if (!Objects.equals(internalTask.getDueDate(),updateTaskPayload.getDueDate())) {
updates++;
internalTask.setDueDate(updateTaskPayload.getDueDate());
}
}
//@TODO: check if this value can be updated
if ((newValue=updateTaskPayload.getParentTaskId()) != null) {
oldValue = internalTask.getParentTaskId();
if (!Objects.equals(oldValue,newValue)) {
updates++;
internalTask.setParentTaskId(newValue);
}
}
if ((newValue=updateTaskPayload.getFormKey()) != null) {
oldValue = internalTask.getFormKey();
if (!Objects.equals(oldValue,newValue)) {
updates++;
internalTask.setFormKey(newValue);
}
}
if (updates > 0) {
taskService.saveTask(internalTask);
}
return taskConverter.from(getInternalTask(updateTaskPayload.getTaskId()));
return taskRuntimeHelper.applyUpdateTaskPayload(false, updateTaskPayload);
}
@Override
......@@ -351,7 +284,7 @@ public class TaskRuntimeImpl implements TaskRuntime {
public void addCandidateUsers(CandidateUsersPayload candidateUsersPayload) {
org.activiti.engine.task.Task internalTask;
try {
internalTask = getInternalTaskWithChecks(candidateUsersPayload.getTaskId());
internalTask = taskRuntimeHelper.getInternalTaskWithChecks(candidateUsersPayload.getTaskId());
} catch (IllegalStateException ex) {
throw new IllegalStateException("The authenticated user cannot update the task" + candidateUsersPayload.getTaskId() + " due it is not the current assignee");
......@@ -377,7 +310,7 @@ public class TaskRuntimeImpl implements TaskRuntime {
public void deleteCandidateUsers(CandidateUsersPayload candidateUsersPayload) {
org.activiti.engine.task.Task internalTask;
try {
internalTask = getInternalTaskWithChecks(candidateUsersPayload.getTaskId());
internalTask = taskRuntimeHelper.getInternalTaskWithChecks(candidateUsersPayload.getTaskId());
} catch (IllegalStateException ex) {
throw new IllegalStateException("The authenticated user cannot update the task" + candidateUsersPayload.getTaskId() + " due it is not the current assignee");
......@@ -403,7 +336,7 @@ public class TaskRuntimeImpl implements TaskRuntime {
public void addCandidateGroups(CandidateGroupsPayload candidateGroupsPayload) {
org.activiti.engine.task.Task internalTask;
try {
internalTask = getInternalTaskWithChecks(candidateGroupsPayload.getTaskId());
internalTask = taskRuntimeHelper.getInternalTaskWithChecks(candidateGroupsPayload.getTaskId());
} catch (IllegalStateException ex) {
throw new IllegalStateException("The authenticated user cannot update the task" + candidateGroupsPayload.getTaskId() + " due it is not the current assignee");
......@@ -428,7 +361,7 @@ public class TaskRuntimeImpl implements TaskRuntime {
public void deleteCandidateGroups(CandidateGroupsPayload candidateGroupsPayload) {
org.activiti.engine.task.Task internalTask;
try {
internalTask = getInternalTaskWithChecks(candidateGroupsPayload.getTaskId());
internalTask = taskRuntimeHelper.getInternalTaskWithChecks(candidateGroupsPayload.getTaskId());
} catch (IllegalStateException ex) {
throw new IllegalStateException("The authenticated user cannot update the task" + candidateGroupsPayload.getTaskId() + " due it is not the current assignee");
......@@ -491,29 +424,6 @@ public class TaskRuntimeImpl implements TaskRuntime {
setTaskVariablesPayload.getVariables());
}
private org.activiti.engine.task.Task getInternalTask(String taskId) {
org.activiti.engine.task.Task internalTask = taskService.createTaskQuery().taskId(taskId).singleResult();
if (internalTask == null) {
throw new NotFoundException("Unable to find task for the given id: " + taskId);
}
return internalTask;
}
private org.activiti.engine.task.Task getInternalTaskWithChecks(String taskId) {
String authenticatedUserId = securityManager.getAuthenticatedUserId();
if (authenticatedUserId != null && !authenticatedUserId.isEmpty()) {
List<String> userRoles = userGroupManager.getUserRoles(authenticatedUserId);
List<String> userGroups = userGroupManager.getUserGroups(authenticatedUserId);
org.activiti.engine.task.Task internalTask = taskService.createTaskQuery().taskCandidateOrAssigned(authenticatedUserId,
userGroups).taskId(taskId).singleResult();
if (internalTask == null) {
throw new NotFoundException("Unable to find task for the given id: " + taskId + " for user: " + authenticatedUserId + " (with groups: " + userGroups + " & with roles: " + userRoles + ")");
}
return internalTask;
}
throw new IllegalStateException("There is no authenticated user, we need a user authenticated to find tasks");
}
private List<IdentityLink> getIdentityLinks(String taskId) {
String authenticatedUserId = securityManager.getAuthenticatedUserId();
if (authenticatedUserId != null && !authenticatedUserId.isEmpty()) {
......
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.runtime.api.impl;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.activiti.api.runtime.shared.NotFoundException;
import org.activiti.api.runtime.shared.identity.UserGroupManager;
import org.activiti.api.runtime.shared.security.SecurityManager;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.model.impl.TaskImpl;
import org.activiti.api.task.model.payloads.UpdateTaskPayload;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.activiti.runtime.api.model.impl.APITaskConverter;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
public class TaskRuntimeHelperTest {
private static final String AUTHENTICATED_USER = "user";
private TaskRuntimeHelper taskRuntimeHelper;
@Mock
private SecurityManager securityManager;
@Mock
private UserGroupManager userGroupManager;
@Mock
private TaskService taskService;
@Mock
private APITaskConverter taskConverter;
@Before
public void setUp() {
initMocks(this);
taskRuntimeHelper = spy(new TaskRuntimeHelper(taskService,
taskConverter,
securityManager,
userGroupManager));
when(securityManager.getAuthenticatedUserId()).thenReturn(AUTHENTICATED_USER);
}
@Test
public void updateShouldSetAllFieldsAndSaveChangesWhenAssignee() {
//given
Date now = new Date();
UpdateTaskPayload updateTaskPayload = TaskPayloadBuilder
.update()
.withTaskId("taskId")
.withDescription("new description")
.withName("New name")
.withPriority(42)
.withDueDate(now)
.withFormKey("new form key")
.build();
Task internalTask = buildInternalTask(AUTHENTICATED_USER);
doReturn(internalTask).when(taskRuntimeHelper).getInternalTaskWithChecks("taskId");
doReturn(internalTask).when(taskRuntimeHelper).getInternalTask("taskId");
//when
taskRuntimeHelper.applyUpdateTaskPayload(false,
updateTaskPayload);
//then
verify(internalTask).setDescription("new description");
verify(internalTask).setName("New name");
verify(internalTask).setPriority(42);
verify(internalTask).setDueDate(now);
verify(internalTask).setFormKey("new form key");
verify(taskService).saveTask(internalTask);
}
private Task buildInternalTask(String assignee) {
Task internalTask = mock(Task.class);
given(internalTask.getAssignee()).willReturn(assignee);
return internalTask;
}
@Test
public void applyUpdateTaskPayloadShouldThrowExceptionWhenAssigneeIsNotSetAndIsNotAdmin() {
//given
TaskQuery taskQuery = mock(TaskQuery.class);
given(taskService.createTaskQuery()).willReturn(taskQuery);
given(taskQuery.taskCandidateOrAssigned(any(),
any())).willReturn(taskQuery);
given(taskQuery.taskId("taskId")).willReturn(taskQuery);
Task internalTask = mock(Task.class);
doReturn(internalTask).when(taskRuntimeHelper).getInternalTaskWithChecks("taskId");
UpdateTaskPayload updateTaskPayload = TaskPayloadBuilder
.update()
.withTaskId("taskId")
.withDescription("new description")
.build();
//when
Throwable throwable = catchThrowable(() -> taskRuntimeHelper.applyUpdateTaskPayload(false,
updateTaskPayload));
//then
assertThat(throwable)
.isInstanceOf(IllegalStateException.class)
.hasMessage("You cannot update a task where you are not the assignee");
}
@Test
public void updateShouldBeAbleToUpdateDescriptionOnly() {
//given
UpdateTaskPayload updateTaskPayload = TaskPayloadBuilder
.update()
.withTaskId("taskId")
.withDescription("new description")
.build();
TaskImpl task = new TaskImpl();
String assignee = AUTHENTICATED_USER;
task.setAssignee(assignee);
Task internalTask = buildInternalTask(assignee);
doReturn(internalTask).when(taskRuntimeHelper).getInternalTaskWithChecks("taskId");
doReturn(internalTask).when(taskRuntimeHelper).getInternalTask("taskId");
TaskQuery taskQuery = mock(TaskQuery.class);
given(taskQuery.taskId("taskId")).willReturn(taskQuery);
given(taskService.createTaskQuery()).willReturn(taskQuery);
TaskRuntimeHelper taskUpdater = mock(TaskRuntimeHelper.class);
given(taskQuery.singleResult()).willReturn(internalTask);
when(taskUpdater.getInternalTaskWithChecks(any())).thenReturn(internalTask);
when(taskConverter.from(any(Task.class))).thenReturn(task);
//when
taskRuntimeHelper.applyUpdateTaskPayload(false,
updateTaskPayload);
//then
verify(internalTask).getDescription();
verify(internalTask).setDescription("new description");
verify(taskService).saveTask(internalTask);
}
@Test
public void getInternalTaskWithChecksShouldReturnMatchinTaskFromTaskQuery() {
//given
List<String> groups = Collections.singletonList("doctor");
given(userGroupManager.getUserGroups(AUTHENTICATED_USER)).willReturn(groups);
TaskQuery taskQuery = mock(TaskQuery.class);
given(taskQuery.taskCandidateOrAssigned(AUTHENTICATED_USER,
groups)).willReturn(taskQuery);
given(taskQuery.taskId("taskId")).willReturn(taskQuery);
Task internalTask = mock(Task.class);
given(taskQuery.singleResult()).willReturn(internalTask);
given(taskService.createTaskQuery()).willReturn(taskQuery);
//when
Task retrievedTask = taskRuntimeHelper.getInternalTaskWithChecks("taskId");
//then
assertThat(retrievedTask).isEqualTo(internalTask);
}
@Test
public void getInternalTaskWithChecksShouldThrowNotFoundExceptionWhenNoTaskIsFound() {
//given
List<String> groups = Collections.singletonList("doctor");
given(userGroupManager.getUserGroups(AUTHENTICATED_USER)).willReturn(groups);
TaskQuery taskQuery = mock(TaskQuery.class);
given(taskQuery.taskCandidateOrAssigned(AUTHENTICATED_USER,
groups)).willReturn(taskQuery);
given(taskQuery.taskId("taskId")).willReturn(taskQuery);
given(taskQuery.singleResult()).willReturn(null);
given(taskService.createTaskQuery()).willReturn(taskQuery);
//when
Throwable thrown = catchThrowable(() ->
taskRuntimeHelper.getInternalTaskWithChecks("taskId")
);
//then
assertThat(thrown)
.isInstanceOf(NotFoundException.class)
.hasMessageStartingWith("Unable to find task for the given id:");
}
@Test
public void getInternalTaskWithChecksShouldThrowExceptionIfAuthenticatedUserIsNotSet() {
//given
given(securityManager.getAuthenticatedUserId()).willReturn(null);
//when
Throwable thrown = catchThrowable(() ->
taskRuntimeHelper.getInternalTaskWithChecks("taskId")
);
//then
assertThat(thrown)
.isInstanceOf(IllegalStateException.class)
.hasMessage("There is no authenticated user, we need a user authenticated to find tasks");
}
}
\ No newline at end of file
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
* Copyright 2019 Alfresco, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -16,107 +16,49 @@
package org.activiti.runtime.api.impl;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import org.activiti.api.runtime.shared.identity.UserGroupManager;
import org.activiti.api.runtime.shared.security.SecurityManager;
import org.activiti.api.task.model.Task;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.model.impl.TaskImpl;
import org.activiti.api.task.model.payloads.UpdateTaskPayload;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.activiti.runtime.api.model.impl.APITaskConverter;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.MockitoAnnotations.initMocks;
public class TaskRuntimeImplTest {
@InjectMocks
private TaskRuntimeImpl taskRuntime;
@Mock
private SecurityManager securityManager;
@Mock
private UserGroupManager userGroupManager;
private TaskRuntimeHelper taskRuntimeHelper;
@Mock
private TaskService taskService;
@Mock
private APITaskConverter taskConverter;
@Before
public void setUp() {
initMocks(this);
taskRuntime = spy(new TaskRuntimeImpl(taskService,
userGroupManager,
securityManager,
taskConverter,
null,
null));
when(securityManager.getAuthenticatedUserId()).thenReturn("user");
}
@Test
public void updateShouldThrowExceptionWhenAssigneeIsNotSet() {
public void updateShouldReturnResultOfHelper() {
//given
UpdateTaskPayload updateTaskPayload = TaskPayloadBuilder
.update()
.withTaskId("taskId")
.withDescription("new description")
.build();
doReturn(new TaskImpl()).when(taskRuntime).task("taskId");
//when
Throwable throwable = catchThrowable(() -> taskRuntime.update(updateTaskPayload));
//then
assertThat(throwable)
.isInstanceOf(IllegalStateException.class)
.hasMessage("You cannot update a task where you are not the assignee");
}
TaskImpl updatedTask = new TaskImpl();
given(taskRuntimeHelper.applyUpdateTaskPayload(false,
updateTaskPayload)).willReturn(updatedTask);
@Test
public void updateShouldBeAbleToUpdateDescriptionOnly() {
//given
UpdateTaskPayload updateTaskPayload = TaskPayloadBuilder
.update()
.withTaskId("taskId")
.withDescription("new description")
.build();
TaskImpl task = new TaskImpl();
task.setAssignee("user");
doReturn(task).when(taskRuntime).task("taskId");
TaskQuery taskQuery = mock(TaskQuery.class);
given(taskQuery.taskId("taskId")).willReturn(taskQuery);
given(taskService.createTaskQuery()).willReturn(taskQuery);
Task internalTask = mock(Task.class);
given(taskQuery.singleResult()).willReturn(internalTask);
Mockito.when(taskConverter.from(Mockito.any(Task.class))).thenReturn(task);
//when
org.activiti.api.task.model.Task updatedTask = taskRuntime.update(updateTaskPayload);
Task retrievedTask = taskRuntime.update(updateTaskPayload);
//then
verify(internalTask).getDescription();
verify(internalTask).setDescription("new description");
verifyNoMoreInteractions(internalTask);
verify(taskService).saveTask(internalTask);
assertThat(retrievedTask).isEqualTo(updatedTask);
}
}
\ No newline at end of file
package org.activiti.spring.boot.tasks;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.api.Assertions.tuple;
import java.util.Date;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.activiti.api.runtime.shared.security.SecurityManager;
import org.activiti.api.task.model.Task;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.model.payloads.UpdateTaskPayload;
......@@ -14,24 +17,21 @@ import org.activiti.spring.boot.RuntimeTestConfiguration;
import org.activiti.spring.boot.security.util.SecurityUtil;
import org.activiti.spring.boot.test.util.TaskCleanUpUtil;
import org.junit.After;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.*;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class TaskRuntimeUpdateTaskTest {
@Autowired
private TaskRuntime taskRuntime;
@Autowired
private TaskAdminRuntime taskAdminRuntime;
@Autowired
private SecurityUtil securityUtil;
......@@ -137,6 +137,58 @@ public class TaskRuntimeUpdateTaskTest {
.contains(tuple(Task.TaskStatus.ASSIGNED,
standaloneTask.getId()));
}
@Test
public void createClaimAndAdminUpdateStandaloneTask() {
securityUtil.logInAs("garth");
// create
Task standaloneTask = taskRuntime.create(TaskPayloadBuilder.create()
.withName("test task update")
.withDescription("test task update description")
.withDueDate(new Date())
.withPriority(50)
.build());
assertThat(RuntimeTestConfiguration.createdTasks).contains(standaloneTask.getId());
Page<Task> tasks = taskRuntime.tasks(Pageable.of(0,
50));
assertThat(tasks.getTotalItems()).isEqualTo(1);
assertThat(tasks.getContent())
.extracting("status",
"id")
.contains(tuple(Task.TaskStatus.CREATED,
standaloneTask.getId()));
final UpdateTaskPayload updateTaskPayload = TaskPayloadBuilder.update()
.withTaskId(standaloneTask.getId())
.withName(standaloneTask.getName() + " [UPDATED]")
.withPriority(60)
.withDueDate(new Date())
.withDescription(standaloneTask.getDescription() + " [UPDATED]")
.build();
// admin should update a task
securityUtil.logInAs("admin");
final Task updatedTask = taskAdminRuntime.update(updateTaskPayload);
tasks = taskAdminRuntime.tasks(Pageable.of(0,
50));
assertThat(RuntimeTestConfiguration.updatedTasks).contains(updatedTask.getId());
assertThat(tasks.getContent())
.extracting("id",
"name",
"description",
"priority"
)
.contains(tuple(standaloneTask.getId(),
updateTaskPayload.getName(),
updateTaskPayload.getDescription(),
updateTaskPayload.getPriority()
));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册