Skip to content

Commit

Permalink
Closes #2364 - Add Rerouting of Tasks in JAVA-API
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesrdi committed Sep 15, 2023
1 parent 24fb36a commit a267bf8
Show file tree
Hide file tree
Showing 13 changed files with 1,072 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public static <T> String determineChangesInAttributes(T oldObject, T newObject)

// this has to be checked after we deal with List data types, because
// we want to allow different implementations of the List interface to work as well.
if (!oldObject.getClass().equals(newObject.getClass())) {
if (!oldObject.getClass().equals(newObject.getClass())
&& !oldObject.getClass().isAssignableFrom(newObject.getClass())) {
throw new SystemException(
String.format(
"The classes differ between the oldObject(%s) and newObject(%s). "
Expand Down
6 changes: 6 additions & 0 deletions history/taskana-simplehistory-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>pro.taskana</groupId>
<artifactId>taskana-test-api</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>pro.taskana</groupId>
<artifactId>taskana-common-data</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
package acceptance.events.task;

import static org.assertj.core.api.Assertions.assertThat;

import acceptance.events.task.CreateHistoryEventOnTaskRerouteAccTest.TaskRoutingProviderForDomainA;
import java.lang.reflect.Field;
import java.util.List;
import org.apache.ibatis.session.SqlSessionManager;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import pro.taskana.classification.api.ClassificationService;
import pro.taskana.classification.api.models.ClassificationSummary;
import pro.taskana.common.api.TaskanaEngine;
import pro.taskana.common.test.security.JaasExtension;
import pro.taskana.common.test.security.WithAccessId;
import pro.taskana.simplehistory.impl.SimpleHistoryServiceImpl;
import pro.taskana.simplehistory.impl.TaskHistoryQueryImpl;
import pro.taskana.simplehistory.impl.TaskanaHistoryEngineImpl;
import pro.taskana.simplehistory.impl.task.TaskHistoryQueryMapper;
import pro.taskana.spi.history.api.TaskanaHistory;
import pro.taskana.spi.history.api.events.task.TaskHistoryEvent;
import pro.taskana.spi.history.api.events.task.TaskHistoryEventType;
import pro.taskana.spi.routing.api.TaskRoutingProvider;
import pro.taskana.task.api.TaskService;
import pro.taskana.task.api.models.Task;
import pro.taskana.testapi.DefaultTestEntities;
import pro.taskana.testapi.TaskanaInject;
import pro.taskana.testapi.TaskanaIntegrationTest;
import pro.taskana.testapi.WithServiceProvider;
import pro.taskana.testapi.builder.TaskBuilder;
import pro.taskana.testapi.builder.WorkbasketAccessItemBuilder;
import pro.taskana.workbasket.api.WorkbasketPermission;
import pro.taskana.workbasket.api.WorkbasketService;
import pro.taskana.workbasket.api.models.Workbasket;
import pro.taskana.workbasket.api.models.WorkbasketSummary;

@WithServiceProvider(
serviceProviderInterface = TaskRoutingProvider.class,
serviceProviders = TaskRoutingProviderForDomainA.class)
@WithServiceProvider(
serviceProviderInterface = TaskanaHistory.class,
serviceProviders = SimpleHistoryServiceImpl.class)
@TaskanaIntegrationTest
@ExtendWith(JaasExtension.class)
class CreateHistoryEventOnTaskRerouteAccTest {
@TaskanaInject TaskanaEngine taskanaEngine;
@TaskanaInject TaskService taskService;
@TaskanaInject WorkbasketService workbasketService;
@TaskanaInject ClassificationService classificationService;
ClassificationSummary classificationSummary;
WorkbasketSummary domainAWorkbasketSummary;
WorkbasketSummary domainBWorkbasketSummary;
Task task1;
Task task2;
Task task3;
Task task4;
private SimpleHistoryServiceImpl historyService = new SimpleHistoryServiceImpl();
private TaskanaHistoryEngineImpl taskanaHistoryEngine;

@WithAccessId(user = "admin")
@BeforeAll
void setUp() throws Exception {
taskanaHistoryEngine = TaskanaHistoryEngineImpl.createTaskanaEngine(taskanaEngine);
historyService.initialize(taskanaEngine);
classificationSummary =
DefaultTestEntities.defaultTestClassification()
.buildAndStoreAsSummary(classificationService);
domainAWorkbasketSummary =
DefaultTestEntities.defaultTestWorkbasket()
.domain("DOMAIN_A")
.buildAndStoreAsSummary(workbasketService);
domainBWorkbasketSummary =
DefaultTestEntities.defaultTestWorkbasket()
.domain("DOMAIN_B")
.buildAndStoreAsSummary(workbasketService);

task1 =
TaskBuilder.newTask()
.classificationSummary(classificationSummary)
.workbasketSummary(domainAWorkbasketSummary)
.primaryObjRef(DefaultTestEntities.defaultTestObjectReference().build())
.buildAndStore(taskService);
task2 =
TaskBuilder.newTask()
.classificationSummary(classificationSummary)
.workbasketSummary(domainAWorkbasketSummary)
.primaryObjRef(DefaultTestEntities.defaultTestObjectReference().build())
.buildAndStore(taskService);
task3 =
TaskBuilder.newTask()
.classificationSummary(classificationSummary)
.workbasketSummary(domainBWorkbasketSummary)
.primaryObjRef(DefaultTestEntities.defaultTestObjectReference().build())
.buildAndStore(taskService);

task4 =
TaskBuilder.newTask()
.classificationSummary(classificationSummary)
.workbasketSummary(domainAWorkbasketSummary)
.primaryObjRef(DefaultTestEntities.defaultTestObjectReference().build())
.buildAndStore(taskService);

WorkbasketAccessItemBuilder.newWorkbasketAccessItem()
.workbasketId(domainAWorkbasketSummary.getId())
.accessId("user-1-1")
.permission(WorkbasketPermission.OPEN)
.permission(WorkbasketPermission.READ)
.permission(WorkbasketPermission.APPEND)
.buildAndStore(workbasketService);

WorkbasketAccessItemBuilder.newWorkbasketAccessItem()
.workbasketId(domainBWorkbasketSummary.getId())
.accessId("user-1-1")
.permission(WorkbasketPermission.OPEN)
.permission(WorkbasketPermission.READ)
.permission(WorkbasketPermission.APPEND)
.buildAndStore(workbasketService);
}

@WithAccessId(user = "admin")
@Test
void should_CreateRerouteHistoryEvent_When_TaskIsRerouted() throws Exception {
historyService.deleteHistoryEventsByTaskIds(List.of(task4.getId()));
TaskHistoryQueryMapper taskHistoryQueryMapper = getHistoryQueryMapper();
List<TaskHistoryEvent> events =
taskHistoryQueryMapper.queryHistoryEvents(
(TaskHistoryQueryImpl) historyService.createTaskHistoryQuery().taskIdIn(task4.getId()));
assertThat(events).isEmpty();
taskService.rerouteTask(task4.getId());

events =
taskHistoryQueryMapper.queryHistoryEvents(
(TaskHistoryQueryImpl) historyService.createTaskHistoryQuery().taskIdIn(task4.getId()));

assertThat(events).hasSize(1);
String eventType = events.get(0).getEventType();
assertThat(eventType).isEqualTo(TaskHistoryEventType.REROUTED.getName());
assertRerouteHistoryEvent(
events.get(0).getId(),
domainAWorkbasketSummary.getId(),
domainBWorkbasketSummary.getId(),
"admin");

historyService.deleteHistoryEventsByTaskIds(List.of(task4.getId()));
}

@WithAccessId(user = "admin")
@Test
void should_CreateRerouteHistoryEvent_When_MultipleTasksAreRerouted() throws Exception {
List<String> taskIds = List.of(task1.getId(), task2.getId(), task3.getId());
historyService.deleteHistoryEventsByTaskIds(taskIds);
TaskHistoryQueryMapper taskHistoryQueryMapper = getHistoryQueryMapper();

List<TaskHistoryEvent> events =
taskHistoryQueryMapper.queryHistoryEvents(
(TaskHistoryQueryImpl)
historyService.createTaskHistoryQuery().taskIdIn(taskIds.toArray(new String[0])));
assertThat(events).isEmpty();
taskService.rerouteTasks(taskIds);

events =
taskHistoryQueryMapper.queryHistoryEvents(
(TaskHistoryQueryImpl)
historyService.createTaskHistoryQuery().taskIdIn(taskIds.toArray(new String[0])));

assertThat(events)
.extracting(TaskHistoryEvent::getTaskId)
.containsExactlyInAnyOrderElementsOf(taskIds);

for (TaskHistoryEvent event : events) {
if (event.getTaskId().equals(task1.getId())) {
assertRerouteHistoryEvent(
event.getId(),
domainAWorkbasketSummary.getId(),
domainBWorkbasketSummary.getId(),
"admin");
} else if (event.getTaskId().equals(task2.getId())) {
assertRerouteHistoryEvent(
event.getId(),
domainAWorkbasketSummary.getId(),
domainBWorkbasketSummary.getId(),
"admin");
} else {
assertRerouteHistoryEvent(
event.getId(),
domainBWorkbasketSummary.getId(),
domainAWorkbasketSummary.getId(),
"admin");
}
}
}

TaskHistoryQueryMapper getHistoryQueryMapper()
throws NoSuchFieldException, IllegalAccessException {
Field sessionManagerField = TaskanaHistoryEngineImpl.class.getDeclaredField("sessionManager");
sessionManagerField.setAccessible(true);
SqlSessionManager sqlSessionManager =
(SqlSessionManager) sessionManagerField.get(taskanaHistoryEngine);

return sqlSessionManager.getMapper(TaskHistoryQueryMapper.class);
}

private void assertRerouteHistoryEvent(
String eventId, String expectedOldValue, String expectedNewValue, String expectedUser)
throws Exception {
TaskHistoryEvent event = historyService.getTaskHistoryEvent(eventId);
assertThat(event.getDetails()).isNotNull();
JSONArray changes = new JSONObject(event.getDetails()).getJSONArray("changes");
assertThat(changes.length()).isPositive();
boolean foundField = false;
for (int i = 0; i < changes.length() && !foundField; i++) {
JSONObject change = changes.getJSONObject(i);
if (change.get("fieldName").equals("workbasketSummary")) {
foundField = true;
String oldWorkbasketStr = change.get("oldValue").toString();
String newWorkbasketStr = change.get("newValue").toString();
Workbasket oldWorkbasket = workbasketService.getWorkbasket(expectedOldValue);
assertThat(oldWorkbasketStr)
.isEqualTo(JSONObject.wrap(oldWorkbasket.asSummary()).toString());
Workbasket newWorkbasket = workbasketService.getWorkbasket(expectedNewValue);
assertThat(newWorkbasketStr)
.isEqualTo(JSONObject.wrap(newWorkbasket.asSummary()).toString());
}
}
assertThat(foundField).describedAs("changes do not contain field 'workbasketSummary'").isTrue();

assertThat(event.getId()).startsWith("THI:");
assertThat(event.getOldValue()).isEqualTo(expectedOldValue);
assertThat(event.getNewValue()).isEqualTo(expectedNewValue);
assertThat(event.getUserId()).isEqualTo(expectedUser);
assertThat(event.getEventType()).isEqualTo(TaskHistoryEventType.REROUTED.getName());
}

class TaskRoutingProviderForDomainA implements TaskRoutingProvider {

@Override
public void initialize(TaskanaEngine taskanaEngine) {}

@Override
public String determineWorkbasketId(Task task) {
if ("DOMAIN_A".equals(task.getDomain())) {
return domainBWorkbasketSummary.getId();
} else if ("DOMAIN_B".equals(task.getDomain())) {
return domainAWorkbasketSummary.getId();
}
return null;
}
}
}
Loading

0 comments on commit a267bf8

Please sign in to comment.