Skip to content

Commit

Permalink
Test archive queries
Browse files Browse the repository at this point in the history
  • Loading branch information
gmokki committed Mar 5, 2020
1 parent 44c154d commit bd1f12b
Showing 1 changed file with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import static io.nflow.engine.workflow.instance.WorkflowInstanceAction.WorkflowActionType.stateExecution;
import static java.util.Arrays.asList;
import static java.util.Collections.emptySet;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.partitioningBy;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.joda.time.DateTime.now;
import static org.junit.Assert.assertArrayEquals;
Expand All @@ -16,15 +20,17 @@

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;

import io.nflow.engine.workflow.instance.QueryWorkflowInstances;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import io.nflow.engine.model.ModelObject;
Expand Down Expand Up @@ -110,6 +116,52 @@ public void archiveWorkflowsWorks() {
assertEquals(workflowCountAfter, workflowCountBefore - workflowIds.size());
}

@Test
public void instanceQueriesCanFetchFromArchive() {
List<Long> mainIds = new ArrayList<>();
mainIds.add(storeActiveWorkflow(archiveTime1));
mainIds.add(storeActiveWorkflow(prodTime1));
mainIds.add(storePassiveWorkflow(prodTime1));

List<Long> archiveIds = new ArrayList<>();
archiveIds.add(storePassiveWorkflow(archiveTime1));
archiveIds.add(storePassiveWorkflow(archiveTime2));

Set<Long> allIds = new HashSet<>();
allIds.addAll(mainIds);
allIds.addAll(archiveIds);

// same values found with querying with and without archive bit set
Map<Boolean, Long> withArchived = workflowInstanceDao.queryWorkflowInstancesAsStream(new QueryWorkflowInstances.Builder().addTypes("dummy").setQueryArchive(true).build()).filter(i -> allIds.contains(i.id)).collect(partitioningBy(i -> i.isArchived, counting()));
long withoutArchived = workflowInstanceDao.queryWorkflowInstancesAsStream(new QueryWorkflowInstances.Builder().addTypes("dummy").build()).filter(i -> allIds.contains(i.id)).count();

assertThat(withoutArchived, is((long) allIds.size()));
assertThat(withArchived.get(false), is((long) allIds.size()));
assertThat(withArchived.get(true), is(0L));

assertEquals(archiveIds.size(), maintenanceDao.archiveWorkflows(archiveIds));

// with archived bit set in query we find also the archived
withArchived = workflowInstanceDao.queryWorkflowInstancesAsStream(new QueryWorkflowInstances.Builder().addTypes("dummy").setQueryArchive(true).build()).filter(i -> allIds.contains(i.id)).collect(partitioningBy(i -> i.isArchived, counting()));
withoutArchived = workflowInstanceDao.queryWorkflowInstancesAsStream(new QueryWorkflowInstances.Builder().addTypes("dummy").build()).filter(i -> allIds.contains(i.id)).count();

assertThat(withoutArchived, is((long) mainIds.size()));
assertThat(withArchived.get(false), is((long) mainIds.size()));
assertThat(withArchived.get(true), is((long) archiveIds.size()));

// non-archived are found with and without archive queries
mainIds.forEach(id -> {
assertThat(workflowInstanceDao.getWorkflowInstance(id, emptySet(), null, true), notNullValue());
assertThat(workflowInstanceDao.getWorkflowInstance(id, emptySet(), null, false), notNullValue());
});

// archived are found only with archive queries
archiveIds.forEach(id -> {
assertThat(workflowInstanceDao.getWorkflowInstance(id, emptySet(), null, true), notNullValue());
assertThrows(EmptyResultDataAccessException.class, () -> workflowInstanceDao.getWorkflowInstance(id, emptySet(), null, false));
});
}

@Test
public void deleteWorkflowsFromMainTablesWorks() {
List<Long> workflowIds = new ArrayList<>();
Expand Down Expand Up @@ -454,7 +506,7 @@ private long addWorkflowAction(long workflowId, final WorkflowInstance instance,
.setExecutionEnd(ended).setRetryNo(1).setType(stateExecution).setState("test").setStateText("state text")
.setWorkflowInstanceId(workflowId).build();
return transaction
.execute((TransactionCallback<Long>) status -> workflowInstanceDao.insertWorkflowInstanceAction(instance, action));
.execute(status -> workflowInstanceDao.insertWorkflowInstanceAction(instance, action));
}

private void assertActiveWorkflowsRemoved(List<Long> workflowIds) {
Expand Down

0 comments on commit bd1f12b

Please sign in to comment.