Skip to content

Commit

Permalink
refactor test code
Browse files Browse the repository at this point in the history
  • Loading branch information
Edvard Fonsell committed Apr 27, 2020
1 parent 7d5fb98 commit 39a2a85
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 123 deletions.
54 changes: 21 additions & 33 deletions nflow-tests/src/test/java/io/nflow/tests/AbstractNflowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
@ExtendWith({ NflowServerExtension.class, SpringExtension.class, SkipTestMethodsAfterFirstFailureExtension.class })
@ContextConfiguration(classes = { RestClientConfiguration.class, PropertiesConfiguration.class })
public abstract class AbstractNflowTest {
protected WebClient workflowInstanceResource;
protected WebClient workflowInstanceIdResource;
protected WebClient workflowDefinitionResource;
protected WebClient statisticsResource;
protected WebClient maintenanceResource;
private WebClient workflowInstanceResource;
private WebClient workflowInstanceIdResource;
private WebClient workflowDefinitionResource;
private WebClient statisticsResource;
private WebClient maintenanceResource;

private final NflowServerConfig server;

Expand Down Expand Up @@ -109,24 +109,24 @@ protected SetSignalResponse setSignal(long instanceId, int signal, String reason
return getInstanceResource(instanceId).path("signal").put(request, SetSignalResponse.class);
}

private WebClient getInstanceResource(long instanceId) {
WebClient client = fromClient(workflowInstanceResource, true).path(Long.toString(instanceId));
return client;
protected WebClient getInstanceResource() {
return fromClient(workflowInstanceResource, true);
}

protected WebClient getInstanceResource(long instanceId) {
return getInstanceResource().path(Long.toString(instanceId));
}

protected WebClient getInstanceIdResource(long instanceId) {
WebClient client = fromClient(workflowInstanceIdResource, true).path(Long.toString(instanceId));
return client;
return fromClient(workflowInstanceIdResource, true).path(Long.toString(instanceId));
}

protected ListWorkflowDefinitionResponse[] getWorkflowDefinitions() {
WebClient client = fromClient(workflowDefinitionResource, true);
return client.get(ListWorkflowDefinitionResponse[].class);
return fromClient(workflowDefinitionResource, true).get(ListWorkflowDefinitionResponse[].class);
}

public StatisticsResponse getStatistics() {
WebClient client = fromClient(statisticsResource, true);
return client.get(StatisticsResponse.class);
return fromClient(statisticsResource, true).get(StatisticsResponse.class);
}

public WorkflowDefinitionStatisticsResponse getDefinitionStatistics(String definitionType) {
Expand Down Expand Up @@ -173,7 +173,7 @@ public void validate(ListWorkflowInstanceResponse workflowInstance) {
}

protected CreateWorkflowInstanceResponse createWorkflowInstance(CreateWorkflowInstanceRequest request) {
return makeWorkflowInstanceQuery(request, CreateWorkflowInstanceResponse.class);
return fromClient(workflowInstanceResource, true).put(request, CreateWorkflowInstanceResponse.class);
}

protected ObjectMapper nflowObjectMapper() {
Expand All @@ -183,33 +183,21 @@ protected ObjectMapper nflowObjectMapper() {
return mapper;
}

protected MaintenanceResponse archiveAllFinishedWorkflows() {
MaintenanceRequest req = new MaintenanceRequest();
req.archiveWorkflows = new MaintenanceRequestItem();
req.archiveWorkflows.olderThanPeriod = seconds(0);
return assertTimeoutPreemptively(ofSeconds(15),
() -> fromClient(maintenanceResource).type(APPLICATION_JSON_TYPE).post(req, MaintenanceResponse.class));
}

protected MaintenanceResponse deleteAllFinishedWorkflows() {
MaintenanceRequest req = new MaintenanceRequest();
req.deleteWorkflows = new MaintenanceRequestItem();
req.deleteWorkflows.olderThanPeriod = seconds(0);
req.deleteArchivedWorkflows = req.deleteWorkflows;
return assertTimeoutPreemptively(ofSeconds(15),
() -> fromClient(maintenanceResource).type(APPLICATION_JSON_TYPE).post(req, MaintenanceResponse.class));
}

protected long insertWorkflow(CreateWorkflowInstanceRequest req) {
return fromClient(workflowInstanceResource, true).put(req, CreateWorkflowInstanceResponse.class).id;
return doMaintenance(req);
}

protected String updateWorkflowInstance(long instanceId, UpdateWorkflowInstanceRequest request) {
return getInstanceIdResource(instanceId).put(request, String.class);
protected MaintenanceResponse doMaintenance(MaintenanceRequest req) {
return assertTimeoutPreemptively(ofSeconds(15),
() -> fromClient(maintenanceResource).type(APPLICATION_JSON_TYPE).post(req, MaintenanceResponse.class));
}

private <T> T makeWorkflowInstanceQuery(CreateWorkflowInstanceRequest request, Class<T> responseClass) {
return fromClient(workflowInstanceResource, true).put(request, responseClass);
protected <T> T updateWorkflowInstance(long instanceId, UpdateWorkflowInstanceRequest request, Class<T> responseClass) {
return getInstanceIdResource(instanceId).put(request, responseClass);
}

public interface WorkflowInstanceValidator {
Expand Down
11 changes: 4 additions & 7 deletions nflow-tests/src/test/java/io/nflow/tests/BulkWorkflowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static java.time.Duration.ofSeconds;
import static java.util.Comparator.naturalOrder;
import static java.util.stream.Collectors.toList;
import static org.apache.cxf.jaxrs.client.WebClient.fromClient;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThan;
Expand Down Expand Up @@ -61,8 +60,7 @@ public void t01_startDemoBulkWorkflow() {
req.stateVariables.put(BulkWorkflow.VAR_CONCURRENCY, 3);
List<Integer> childData = IntStream.rangeClosed(1, CHILDREN_COUNT).boxed().collect(toList());
req.stateVariables.put(BulkWorkflow.VAR_CHILD_DATA, nflowObjectMapper().valueToTree(childData));
CreateWorkflowInstanceResponse resp = fromClient(workflowInstanceResource, true).put(req,
CreateWorkflowInstanceResponse.class);
CreateWorkflowInstanceResponse resp = createWorkflowInstance(req);
assertThat(resp.id, notNullValue());
workflowId = resp.id;
}
Expand All @@ -80,8 +78,7 @@ public void t11_createBulkWorkflow() {
req.type = BULK_WORKFLOW_TYPE;
req.stateVariables.put(BulkWorkflow.VAR_CONCURRENCY, 3);
req.activate = false;
CreateWorkflowInstanceResponse resp = fromClient(workflowInstanceResource, true).put(req,
CreateWorkflowInstanceResponse.class);
CreateWorkflowInstanceResponse resp = createWorkflowInstance(req);
assertThat(resp.id, notNullValue());
workflowId = resp.id;

Expand All @@ -90,7 +87,7 @@ public void t11_createBulkWorkflow() {
child.type = DEMO_WORKFLOW_TYPE;
child.activate = false;
child.parentWorkflowId = workflowId;
resp = fromClient(workflowInstanceResource, true).put(child, CreateWorkflowInstanceResponse.class);
resp = createWorkflowInstance(child);
assertThat(resp.id, notNullValue());
}
}
Expand All @@ -100,7 +97,7 @@ public void t11_createBulkWorkflow() {
public void t12_startBulkWorkflow() {
UpdateWorkflowInstanceRequest req = new UpdateWorkflowInstanceRequest();
req.nextActivationTime = now();
try (Response resp = fromClient(workflowInstanceResource, true).path("id").path(workflowId).put(req, Response.class)) {
try (Response resp = updateWorkflowInstance(workflowId, req, Response.class)) {
assertThat(resp.getStatus(), equalTo(204));
}
}
Expand Down
23 changes: 11 additions & 12 deletions nflow-tests/src/test/java/io/nflow/tests/ChildWorkflowTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package io.nflow.tests;

import io.nflow.rest.v1.msg.CreateWorkflowInstanceRequest;
import io.nflow.rest.v1.msg.CreateWorkflowInstanceResponse;
import io.nflow.rest.v1.msg.ListWorkflowInstanceResponse;
import io.nflow.tests.demo.workflow.FibonacciWorkflow;
import io.nflow.tests.extension.NflowServerConfig;
import static java.time.Duration.ofSeconds;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.notNullValue;

import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

import static java.time.Duration.ofSeconds;
import static org.apache.cxf.jaxrs.client.WebClient.fromClient;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.notNullValue;
import io.nflow.rest.v1.msg.CreateWorkflowInstanceRequest;
import io.nflow.rest.v1.msg.CreateWorkflowInstanceResponse;
import io.nflow.rest.v1.msg.ListWorkflowInstanceResponse;
import io.nflow.tests.demo.workflow.FibonacciWorkflow;
import io.nflow.tests.extension.NflowServerConfig;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ChildWorkflowTest extends AbstractNflowTest {
Expand All @@ -32,8 +32,7 @@ public void startFibonacciWorkflow() {
CreateWorkflowInstanceRequest req = new CreateWorkflowInstanceRequest();
req.type = "fibonacci";
req.stateVariables.put("requestData", nflowObjectMapper().valueToTree(new FibonacciWorkflow.FiboData(5)));
CreateWorkflowInstanceResponse resp = fromClient(workflowInstanceResource, true).put(req,
CreateWorkflowInstanceResponse.class);
CreateWorkflowInstanceResponse resp = createWorkflowInstance(req);
assertThat(resp.id, notNullValue());
workflowId = resp.id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static java.time.Duration.ofSeconds;
import static java.util.Arrays.asList;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
import static org.apache.cxf.jaxrs.client.WebClient.fromClient;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
Expand Down Expand Up @@ -52,7 +51,7 @@ public void createCreditApplicationWorkflow() {
req.stateVariables.put("requestData", (new ObjectMapper()).valueToTree(
new CreditApplicationWorkflow.CreditApplication("CUST123", new BigDecimal(100l))));
req.externalId = UUID.randomUUID().toString();
resp = fromClient(workflowInstanceResource, true).put(req, CreateWorkflowInstanceResponse.class);
resp = createWorkflowInstance(req);
assertThat(resp.id, notNullValue());
}

Expand All @@ -68,7 +67,7 @@ public void moveToGrantLoanState() {
UpdateWorkflowInstanceRequest ureq = new UpdateWorkflowInstanceRequest();
ureq.nextActivationTime = now();
ureq.state = "grantLoan";
fromClient(workflowInstanceIdResource, true).path(resp.id).put(ureq);
updateWorkflowInstance(resp.id, ureq, String.class);
}

@Test
Expand All @@ -77,7 +76,7 @@ public void moveToInvalidStateFails() {
UpdateWorkflowInstanceRequest ureq = new UpdateWorkflowInstanceRequest();
ureq.nextActivationTime = now();
ureq.state = "invalid";
try (Response response = fromClient(workflowInstanceIdResource, true).path(resp.id).put(ureq)) {
try (Response response = updateWorkflowInstance(resp.id, ureq, Response.class)) {
assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
assertThat(response.getMediaType(), is(APPLICATION_JSON_TYPE));
assertThat(response.readEntity(ErrorResponse.class).error, startsWith("No state 'invalid'"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static io.nflow.tests.demo.workflow.TestCronWorkflow.TYPE;
import static java.util.Collections.singletonMap;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.apache.cxf.jaxrs.client.WebClient.fromClient;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
Expand Down Expand Up @@ -49,7 +48,7 @@ public void startCronWorkflow() {
CreateWorkflowInstanceRequest req = new CreateWorkflowInstanceRequest();
req.type = TYPE;
req.stateVariables = singletonMap("cron", "*/3 * * * * *");
resp = fromClient(workflowInstanceResource, true).put(req, CreateWorkflowInstanceResponse.class);
resp = createWorkflowInstance(req);
assertNotNull(resp.id);
}

Expand All @@ -74,6 +73,6 @@ public void stopMaintenanceWorkflow() {
UpdateWorkflowInstanceRequest request = new UpdateWorkflowInstanceRequest();
request.nextActivationTime = null;
request.state = failed.name();
updateWorkflowInstance(resp.id, request);
updateWorkflowInstance(resp.id, request, String.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.nflow.tests;

import static java.time.Duration.ofSeconds;
import static org.apache.cxf.jaxrs.client.WebClient.fromClient;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -43,7 +42,7 @@ static class DeleteHistoryConfiguration {
public void createWorkflowInstance() {
CreateWorkflowInstanceRequest req = new CreateWorkflowInstanceRequest();
req.type = DeleteHistoryWorkflow.TYPE;
resp = fromClient(workflowInstanceResource, true).put(req, CreateWorkflowInstanceResponse.class);
resp = createWorkflowInstance(req);
assertThat(resp.id, is(notNullValue()));
}

Expand Down
18 changes: 8 additions & 10 deletions nflow-tests/src/test/java/io/nflow/tests/DemoWorkflowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import static javax.ws.rs.core.Response.Status.NO_CONTENT;
import static org.apache.cxf.jaxrs.client.WebClient.fromClient;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
Expand Down Expand Up @@ -53,7 +52,7 @@ public void startDemoWorkflow() {
CreateWorkflowInstanceRequest req = new CreateWorkflowInstanceRequest();
req.type = "demo";
req.businessKey = "1";
resp = fromClient(workflowInstanceResource, true).put(req, CreateWorkflowInstanceResponse.class);
resp = createWorkflowInstance(req);
assertThat(resp.id, notNullValue());
}

Expand All @@ -64,8 +63,8 @@ public void queryDemoWorkflowHistory() {
ListWorkflowInstanceResponse wf = null;
do {
sleep(200);
ListWorkflowInstanceResponse[] instances = fromClient(workflowInstanceResource, true).query("type", "demo")
.query("include", "actions").get(ListWorkflowInstanceResponse[].class);
ListWorkflowInstanceResponse[] instances = getInstanceResource().query("type", "demo").query("include", "actions")
.get(ListWorkflowInstanceResponse[].class);
assertThat(instances.length, greaterThanOrEqualTo(1));
for (ListWorkflowInstanceResponse instance : instances) {
if (instance.id == resp.id && "done".equals(instance.state) && instance.nextActivation == null) {
Expand All @@ -82,8 +81,8 @@ public void queryDemoWorkflowHistory() {
@Test
@Order(3)
public void queryDemoWorkflowWithMultipleStatuses() {
ListWorkflowInstanceResponse[] instances = fromClient(workflowInstanceResource, true).query("type", "demo")
.query("status", "finished").query("status", "manual").get(ListWorkflowInstanceResponse[].class);
ListWorkflowInstanceResponse[] instances = getInstanceResource().query("type", "demo").query("status", "finished")
.query("status", "manual").get(ListWorkflowInstanceResponse[].class);
assertThat(instances.length, greaterThanOrEqualTo(1));
}

Expand All @@ -103,8 +102,7 @@ public void queryWorkflowWithActionsReturnsEmptyActions() {
@Test
@Order(5)
public void queryWorkflowWithoutActionsReturnsNullActions() {
ListWorkflowInstanceResponse instance = fromClient(workflowInstanceIdResource, true).path(Long.toString(resp.id))
.get(ListWorkflowInstanceResponse.class);
ListWorkflowInstanceResponse instance = getInstanceIdResource(resp.id).get(ListWorkflowInstanceResponse.class);

assertThat(instance.actions, is(nullValue()));
}
Expand All @@ -115,7 +113,7 @@ public void updateWorkflowReturnsNoContentWhenInstanceIsUpdated() {
UpdateWorkflowInstanceRequest req = new UpdateWorkflowInstanceRequest();
req.nextActivationTime = now().plusDays(1);

try (Response response = getInstanceIdResource(resp.id).put(req)) {
try (Response response = updateWorkflowInstance(resp.id, req, Response.class)) {
assertThat(response.getStatus(), is(NO_CONTENT.getStatusCode()));
assertThat(response.readEntity(String.class), is(""));
}
Expand All @@ -127,7 +125,7 @@ public void updateWorkflowReturnsNotFoundWhenInstanceIsNotFound() {
UpdateWorkflowInstanceRequest req = new UpdateWorkflowInstanceRequest();
req.nextActivationTime = now().plusDays(2);

try (Response response = getInstanceIdResource(-1).put(req)) {
try (Response response = updateWorkflowInstance(-1, req, Response.class)) {
assertThat(response.getStatus(), is(NOT_FOUND.getStatusCode()));
assertThat(response.getMediaType(), is(APPLICATION_JSON_TYPE));
assertThat(response.readEntity(ErrorResponse.class).error, is("Workflow instance -1 not found"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@

import static io.nflow.tests.demo.workflow.SlowWorkflow.SLOW_WORKFLOW_TYPE;
import static java.lang.Thread.sleep;
import static org.apache.cxf.jaxrs.client.WebClient.fromClient;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.jupiter.api.Assertions.fail;

import io.nflow.tests.extension.NflowServerConfig;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

import io.nflow.rest.v1.msg.Action;
import io.nflow.rest.v1.msg.CreateWorkflowInstanceRequest;
import io.nflow.rest.v1.msg.CreateWorkflowInstanceResponse;
import io.nflow.rest.v1.msg.ListWorkflowInstanceResponse;
import io.nflow.tests.demo.workflow.SlowWorkflow;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import io.nflow.tests.extension.NflowServerConfig;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ExecutorRecoveryTest extends AbstractNflowTest {
Expand All @@ -42,7 +41,7 @@ public ExecutorRecoveryTest() {
public void submitSlowWorkflow() {
CreateWorkflowInstanceRequest req = new CreateWorkflowInstanceRequest();
req.type = SLOW_WORKFLOW_TYPE;
resp = fromClient(workflowInstanceResource, true).put(req, CreateWorkflowInstanceResponse.class);
resp = createWorkflowInstance(req);
assertThat(resp.id, notNullValue());
}

Expand Down
Loading

0 comments on commit 39a2a85

Please sign in to comment.