@Test
public void fakePostgreSQLupdateWorkflowInstance() {

  JdbcTemplate j = mock(JdbcTemplate.class);
  WorkflowInstanceDao d = preparePostgreSQLDao(j);
  ArgumentCaptor<String> sql = ArgumentCaptor.forClass(String.class);
  ArgumentCaptor<Object> args = ArgumentCaptor.forClass(Object.class);
  when(j.queryForObject(sql.capture(), eq(Long.class), args.capture())).thenReturn(42L);
  DateTime started = now();
  WorkflowInstance i2 = new WorkflowInstance.Builder().setState("updateState").setStateText("update text")
      .setNextActivation(started.plusSeconds(1)).setStatus(executing).setRetries(3).setId(43).putStateVariable("A", "B")
      .build();
  WorkflowInstanceAction a1 = new WorkflowInstanceAction.Builder().setExecutionStart(started).setExecutorId(4)
      .setExecutionEnd(started.plusMillis(100)).setRetryNo(1).setType(externalChange).setState("test")
      .setStateText("state text").setWorkflowInstanceId(43).build();
  d.updateWorkflowInstanceAfterExecution(i2, a1, noChildWorkflows, emptyWorkflows, false);
  assertEquals("with wf as (update nflow_workflow set status = ?::workflow_status, state = ?, state_text = ?, "
      + "next_activation = (case when ?::timestamptz is null then null when external_next_activation is null then "
      + "?::timestamptz else least(?::timestamptz, external_next_activation) end), external_next_activation = null, "
      + "executor_id = ?, retries = ?, started = (case when started is null then ? else started end) where id = ? "
      + "and executor_id = 42 returning id), "
      + "act as (insert into nflow_workflow_action(workflow_id, executor_id, type, state, state_text, retry_no, "
      + "execution_start, execution_end) select wf.id, ?, ?::action_type, ?, ?, ?, ?, ? from wf returning id), "
      + "ins17 as (insert into nflow_workflow_state(workflow_id, action_id, state_key, state_value) "
      + "select wf.id,act.id,?,? from wf,act) select act.id from act", sql.getValue());
  assertThat(args.getAllValues().size(), is(countMatches(sql.getValue(), "?")));
  int i = 0;
  assertThat(args.getAllValues().get(i++), is((Object) i2.status.name()));
  assertThat(args.getAllValues().get(i++), is((Object) i2.state));
  assertThat(args.getAllValues().get(i++), is((Object) i2.stateText));
  assertThat(args.getAllValues().get(i++), is((Object) new Timestamp(i2.nextActivation.getMillis())));
  assertThat(args.getAllValues().get(i++), is((Object) new Timestamp(i2.nextActivation.getMillis())));
  assertThat(args.getAllValues().get(i++), is((Object) new Timestamp(i2.nextActivation.getMillis())));
  assertThat(args.getAllValues().get(i++), is((Object) 42));
  assertThat(args.getAllValues().get(i++), is((Object) i2.retries));
  assertThat(args.getAllValues().get(i++), is((Object) new Timestamp(a1.executionStart.getMillis())));
  assertThat(args.getAllValues().get(i++), is((Object) i2.id));
  assertThat(args.getAllValues().get(i++), is((Object) 42));
  assertThat(args.getAllValues().get(i++), is((Object) a1.type.name()));
  assertThat(args.getAllValues().get(i++), is((Object) a1.state));
  assertThat(args.getAllValues().get(i++), is((Object) a1.stateText));
  assertThat(args.getAllValues().get(i++), is((Object) a1.retryNo));
  assertThat(args.getAllValues().get(i++), is((Object) new Timestamp(a1.executionStart.getMillis())));
  assertThat(args.getAllValues().get(i++), is((Object) new Timestamp(a1.executionEnd.getMillis())));
  assertThat(args.getAllValues().get(i++), is((Object) "A"));
  assertThat(args.getAllValues().get(i++), is((Object) "B"));
}