Skip to content

Commit

Permalink
Closes Taskana#2463: Refactor WorkbasketCleanupJobAccTest to use Test…
Browse files Browse the repository at this point in the history
…-API
  • Loading branch information
jamesrdi committed Dec 22, 2023
1 parent 7f1c740 commit 00c8ab4
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import pro.taskana.testapi.TaskanaInject;
import pro.taskana.testapi.TaskanaIntegrationTest;
import pro.taskana.testapi.security.WithAccessId;
import pro.taskana.workbasket.internal.jobs.WorkbasketCleanupJob;

@TaskanaIntegrationTest
class AbstractTaskanaJobAccTest {
Expand Down Expand Up @@ -105,30 +106,34 @@ void should_DeleteOldTaskCleanupJobs_When_InitializingSchedule() {
assertThat(jobsToRun).doesNotContainAnyElementsOf(taskCleanupJobs);
}

@Nested
@TestInstance(Lifecycle.PER_CLASS)
class CleanCompletedTasks implements TaskanaConfigurationModifier {
@TaskanaInject TaskanaEngine taskanaEngine;

@TaskanaInject JobMapper jobMapper;
@WithAccessId(user = "admin")
@Test
void should_DeleteOldWorkbasketCleanupJobs_When_InitializingSchedule() throws Exception {

@Override
public Builder modify(Builder builder) {
return builder
.taskCleanupJobEnabled(true)
.jobRunEvery(Duration.ofMillis(1))
.jobFirstRun(Instant.now().plus(5, ChronoUnit.MINUTES));
for (int i = 0; i < 10; i++) {
ScheduledJob job = new ScheduledJob();
job.setType(WorkbasketCleanupJob.class.getName());
taskanaEngine.getJobService().createJob(job);
job.setType(TaskRefreshJob.class.getName());
taskanaEngine.getJobService().createJob(job);
job.setType(ClassificationChangedJob.class.getName());
taskanaEngine.getJobService().createJob(job);
}
List<ScheduledJob> jobsToRun = jobMapper.findJobsToRun(Instant.now());

@WithAccessId(user = "admin")
@Test
void should_FindNoJobsToRunUntilFirstRunIsReached_When_CleanupScheduleIsInitialized()
throws Exception {
AbstractTaskanaJob.initializeSchedule(taskanaEngine, TaskCleanupJob.class);
assertThat(jobsToRun).hasSize(30);

List<ScheduledJob> nextJobs = jobMapper.findJobsToRun(Instant.now());
assertThat(nextJobs).isEmpty();
}
List<ScheduledJob> workbasketCleanupJobs =
jobsToRun.stream()
.filter(
scheduledJob -> scheduledJob.getType().equals(WorkbasketCleanupJob.class.getName()))
.toList();

AbstractTaskanaJob.initializeSchedule(taskanaEngine, WorkbasketCleanupJob.class);

jobsToRun = jobMapper.findJobsToRun(Instant.now());

assertThat(jobsToRun).doesNotContainAnyElementsOf(workbasketCleanupJobs);
}

@Test
Expand Down Expand Up @@ -172,4 +177,30 @@ protected String getType() {
@Override
protected void execute() throws TaskanaException {}
}

@Nested
@TestInstance(Lifecycle.PER_CLASS)
class CleanCompletedTasks implements TaskanaConfigurationModifier {
@TaskanaInject TaskanaEngine taskanaEngine;

@TaskanaInject JobMapper jobMapper;

@Override
public Builder modify(Builder builder) {
return builder
.taskCleanupJobEnabled(true)
.jobRunEvery(Duration.ofMillis(1))
.jobFirstRun(Instant.now().plus(5, ChronoUnit.MINUTES));
}

@WithAccessId(user = "admin")
@Test
void should_FindNoJobsToRunUntilFirstRunIsReached_When_CleanupScheduleIsInitialized()
throws Exception {
AbstractTaskanaJob.initializeSchedule(taskanaEngine, TaskCleanupJob.class);

List<ScheduledJob> nextJobs = jobMapper.findJobsToRun(Instant.now());
assertThat(nextJobs).isEmpty();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package acceptance.jobs;

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

import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import pro.taskana.classification.api.ClassificationService;
import pro.taskana.classification.api.models.ClassificationSummary;
import pro.taskana.common.api.TaskanaEngine;
import pro.taskana.task.api.TaskService;
import pro.taskana.task.api.TaskState;
import pro.taskana.task.api.models.ObjectReference;
import pro.taskana.testapi.DefaultTestEntities;
import pro.taskana.testapi.TaskanaInject;
import pro.taskana.testapi.TaskanaIntegrationTest;
import pro.taskana.testapi.builder.TaskBuilder;
import pro.taskana.testapi.security.WithAccessId;
import pro.taskana.workbasket.api.WorkbasketService;
import pro.taskana.workbasket.api.models.Workbasket;
import pro.taskana.workbasket.api.models.WorkbasketSummary;
import pro.taskana.workbasket.internal.jobs.WorkbasketCleanupJob;

// All tests are executed as admin, because the jobrunner needs admin rights.
@TaskanaIntegrationTest
class WorkbasketCleanupJobAccTest {
@TaskanaInject TaskService taskService;
@TaskanaInject WorkbasketService workbasketService;
@TaskanaInject ClassificationService classificationService;
@TaskanaInject TaskanaEngine taskanaEngine;

ClassificationSummary classification;
ObjectReference primaryObjRef;

@WithAccessId(user = "businessadmin")
@BeforeAll
void setup() throws Exception {
classification =
DefaultTestEntities.defaultTestClassification()
.buildAndStoreAsSummary(classificationService);
primaryObjRef = DefaultTestEntities.defaultTestObjectReference().build();
}

@WithAccessId(user = "admin")
@Test
void should_CleanWorkbasketMarkedForDeletion_When_WorkbasketHasNoTasks() throws Exception {
DefaultTestEntities.defaultTestWorkbasket()
.markedForDeletion(true)
.buildAndStore(workbasketService);
List<WorkbasketSummary> wbSummariesOld = workbasketService.createWorkbasketQuery().list();
assertThat(wbSummariesOld).hasSize(1);

WorkbasketCleanupJob job = new WorkbasketCleanupJob(taskanaEngine, null, null);
job.run();

List<WorkbasketSummary> wbSummaries = workbasketService.createWorkbasketQuery().list();
assertThat(wbSummaries).isEmpty();
}

@WithAccessId(user = "admin")
@Test
void should_NotCleanWorkbasketMarkedForDeletion_When_WorkbasketHasTasks() throws Exception {
Workbasket wb = DefaultTestEntities.defaultTestWorkbasket().buildAndStore(workbasketService);
TaskBuilder.newTask()
.workbasketSummary(wb.asSummary())
.classificationSummary(classification)
.primaryObjRef(primaryObjRef)
.state(TaskState.COMPLETED)
.buildAndStore(taskService);

// Workbasket with completed task will be marked for deletion.
workbasketService.deleteWorkbasket(wb.getId());
WorkbasketCleanupJob job = new WorkbasketCleanupJob(taskanaEngine, null, null);
job.run();

List<WorkbasketSummary> wbSummaries = workbasketService.createWorkbasketQuery().list();
assertThat(wbSummaries).hasSize(1);
}
}

This file was deleted.

0 comments on commit 00c8ab4

Please sign in to comment.