Skip to content

Commit

Permalink
add support for updating workflow instance business key
Browse files Browse the repository at this point in the history
  • Loading branch information
Edvard Fonsell committed Jan 21, 2021
1 parent f4ae2c5 commit 18e5933
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ public boolean updateNotRunningWorkflowInstance(WorkflowInstance instance) {
vars.add("status = " + sqlVariants.workflowStatus());
args.add(instance.status.name());
}
if (instance.businessKey != null) {
vars.add("business_key = ?");
args.add(instance.businessKey);
}
String sql = "update nflow_workflow set " + join(vars, ", ") + " where id = ? and executor_id is null";
args.add(instance.id);
return jdbc.update(sql, args.toArray()) == 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ public void roundTripTest() {

@Test
public void queryNonExistingWorkflowThrowsException() {
assertThrows(EmptyResultDataAccessException.class, () ->
dao.getWorkflowInstance(-42, emptySet(), null)
);
assertThrows(EmptyResultDataAccessException.class, () -> dao.getWorkflowInstance(-42, emptySet(), null));
}

@Test
Expand Down Expand Up @@ -382,6 +380,7 @@ public void processRow(ResultSet rs) throws SQLException {
assertThat(rs.getString("state"), is(instance.state));
assertThat(rs.getTimestamp("next_activation").getTime(), is(instance.nextActivation.getMillis()));
assertThat(rs.getString("state_text"), is("modified"));
assertThat(rs.getString("business_key"), is(instance.businessKey));
}
});
}
Expand All @@ -401,6 +400,7 @@ public void processRow(ResultSet rs) throws SQLException {
assertThat(rs.getString("state"), is("manualState"));
assertThat(rs.getTimestamp("next_activation").getTime(), is(instance.nextActivation.getMillis()));
assertThat(rs.getString("state_text"), is("modified"));
assertThat(rs.getString("business_key"), is(instance.businessKey));
}
});
}
Expand All @@ -421,6 +421,27 @@ public void processRow(ResultSet rs) throws SQLException {
assertThat(rs.getString("state"), is(instance.state));
assertThat(rs.getTimestamp("next_activation").getTime(), is(tomorrow.getMillis()));
assertThat(rs.getString("state_text"), is("modified"));
assertThat(rs.getString("business_key"), is(instance.businessKey));
}
});
}

@Test
public void updateNotRunningWorkflowInstanceUpdatesBusinessKeyForNotRunningInstance() {
final WorkflowInstance instance = constructWorkflowInstanceBuilder().build();
long id = dao.insertWorkflowInstance(instance);
WorkflowInstance modifiedInstance = new WorkflowInstance.Builder(instance).setId(id).setBusinessKey("modifiedKey")
.setStateText("modified").build();
boolean updated = dao.updateNotRunningWorkflowInstance(modifiedInstance);
assertThat(updated, is(true));
jdbc.query("select * from nflow_workflow where id = " + id, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
assertThat(rs.getString("status"), is(instance.status.name()));
assertThat(rs.getString("state"), is(instance.state));
assertThat(rs.getTimestamp("next_activation").getTime(), is(instance.nextActivation.getMillis()));
assertThat(rs.getString("state_text"), is("modified"));
assertThat(rs.getString("business_key"), is("modifiedKey"));
}
});
}
Expand Down Expand Up @@ -565,19 +586,17 @@ public void fakePostgreSQLinsertWorkflowInstance() {

DateTime started = now();
WorkflowInstance wf = new WorkflowInstance.Builder().setStatus(inProgress).setState("updateState").setStateText("update text")
.setParentWorkflowId(110L).setParentActionId(421L).setNextActivation(started.plusSeconds(1))
.setRetries(3).setId(43).putStateVariable("A", "B").putStateVariable("C", "D").setSignal(Optional.of(1))
.setStartedIfNotSet(started).setPriority((short) 10).build();
.setParentWorkflowId(110L).setParentActionId(421L).setNextActivation(started.plusSeconds(1)).setRetries(3).setId(43)
.putStateVariable("A", "B").putStateVariable("C", "D").setSignal(Optional.of(1)).setStartedIfNotSet(started)
.setPriority((short) 10).build();

d.insertWorkflowInstance(wf);
assertEquals(
"with wf as (insert into nflow_workflow(type, priority, parent_workflow_id, parent_action_id, business_key, "
+ "external_id, executor_group, status, state, state_text, next_activation, workflow_signal) values "
+ "(?, ?, ?, ?, ?, ?, ?, ?::workflow_status, ?, ?, ?, ?) returning id), ins12 as "
+ "(insert into nflow_workflow_state(workflow_id, action_id, state_key, state_value) select wf.id,0,?,? from wf), "
+ "ins14 as (insert into nflow_workflow_state(workflow_id, action_id, state_key, state_value) "
+ "select wf.id,0,?,? from wf) select wf.id from wf",
sql.getValue());
assertEquals("with wf as (insert into nflow_workflow(type, priority, parent_workflow_id, parent_action_id, business_key, "
+ "external_id, executor_group, status, state, state_text, next_activation, workflow_signal) values "
+ "(?, ?, ?, ?, ?, ?, ?, ?::workflow_status, ?, ?, ?, ?) returning id), ins12 as "
+ "(insert into nflow_workflow_state(workflow_id, action_id, state_key, state_value) select wf.id,0,?,? from wf), "
+ "ins14 as (insert into nflow_workflow_state(workflow_id, action_id, state_key, state_value) "
+ "select wf.id,0,?,? from wf) select wf.id from wf", sql.getValue());
assertThat(args.getAllValues().size(), is(countMatches(sql.getValue(), "?")));

int i = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ public boolean updateWorkflowInstance(final long id,
msg += "API updated state variables. ";
}
}
if (!isEmpty(req.businessKey)) {
builder.setBusinessKey(req.businessKey);
if (isBlank(req.actionDescription)) {
msg = "API changed business key to " + req.businessKey + ". ";
}
}
if (msg.isEmpty()) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public class UpdateWorkflowInstanceRequest extends ModelObject {
@ApiModelProperty("State variables to be added or updated.")
public Map<String, Object> stateVariables = new HashMap<>();

@ApiModelProperty("Business key related to the workflow instance.")
public String businessKey;
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,30 @@ public void whenUpdatingStateVariablesUpdateWorkflowInstanceWorks() {
assertThat(instance.getStateVariable("textNode"), is("\"text\""));
}

@Test
public void whenUpdatingBusinessKeyUpdateWorkflowInstanceWorks() {
UpdateWorkflowInstanceRequest req = new UpdateWorkflowInstanceRequest();
req.businessKey = "modifiedKey";
makeRequest(() -> resource.updateWorkflowInstance(3, req));
verify(workflowInstances).updateWorkflowInstance(
(WorkflowInstance) argThat(hasField("businessKey", equalTo("modifiedKey"))),
(WorkflowInstanceAction) argThat(
allOf(hasField("stateText", equalTo("API changed business key to " + req.businessKey + ".")),
hasField("type", equalTo(externalChange)))));
}

@Test
public void whenUpdatingBusinessKeyWithDescriptionUpdateWorkflowInstanceWorks() {
UpdateWorkflowInstanceRequest req = new UpdateWorkflowInstanceRequest();
req.businessKey = "modifiedKey";
req.actionDescription = "description";
makeRequest(() -> resource.updateWorkflowInstance(3, req));
verify(workflowInstances).updateWorkflowInstance(
(WorkflowInstance) argThat(hasField("businessKey", equalTo("modifiedKey"))),
(WorkflowInstanceAction) argThat(
allOf(hasField("stateText", equalTo("description")), hasField("type", equalTo(externalChange)))));
}

@Test
public void listWorkflowInstancesWorks() {
makeRequest(() -> resource.listWorkflowInstances(asList(42L), asList("type"), 99L, 88L, asList("state"),
Expand Down

0 comments on commit 18e5933

Please sign in to comment.