ListWorkflowInstanceConverter { public ListWorkflowInstanceResponse convert(WorkflowInstance instance, Set<WorkflowInstanceInclude> includes) { ListWorkflowInstanceResponse resp = new ListWorkflowInstanceResponse(); resp.id = instance.id; resp.status = instance.status.name(); resp.type = instance.type; resp.priority = instance.priority; resp.parentWorkflowId = instance.parentWorkflowId; resp.parentActionId = instance.parentActionId; resp.businessKey = instance.businessKey; resp.externalId = instance.externalId; resp.state = instance.state; resp.stateText = instance.stateText; resp.nextActivation = instance.nextActivation; resp.created = instance.created; resp.modified = instance.modified; resp.started = instance.started; resp.retries = instance.retries; resp.signal = instance.signal.orElse(null); if (includes.contains(WorkflowInstanceInclude.ACTIONS)) { resp.actions = new ArrayList<>(); for (WorkflowInstanceAction action : instance.actions) { if (includes.contains(WorkflowInstanceInclude.ACTION_STATE_VARIABLES)) { resp.actions.add(new Action(action.id, action.type.name(), action.state, action.stateText, action.retryNo, action.executionStart, action.executionEnd, action.executorId, stateVariablesToJson(action.updatedStateVariables))); } else { resp.actions.add(new Action(action.id, action.type.name(), action.state, action.stateText, action.retryNo, action.executionStart, action.executionEnd, action.executorId)); } } } if (includes.contains(WorkflowInstanceInclude.CURRENT_STATE_VARIABLES)) { resp.stateVariables = stateVariablesToJson(instance.stateVariables); } if (includes.contains(WorkflowInstanceInclude.CHILD_WORKFLOW_IDS)) { resp.childWorkflows = instance.childWorkflows; } return resp; }  ListWorkflowInstanceResponse convert(WorkflowInstance instance, Set<WorkflowInstanceInclude> includes);  }
@Test public void convertWithActionsWorks() throws IOException { WorkflowInstanceAction a = new WorkflowInstanceAction.Builder().setId(929).setType(stateExecution).setState("oState") .setStateText("oState desc").setRetryNo(1).setExecutionStart(now().minusDays(1)).setExecutionEnd(now().plusDays(1)) .setExecutorId(999).build(); Map<String, String> stateVariables = new LinkedHashMap<>(); stateVariables.put("foo", "1"); stateVariables.put("bar", "quux"); DateTime started = now(); WorkflowInstance i = new WorkflowInstance.Builder().setId(1).setStatus(inProgress).setType("dummy") .setBusinessKey("businessKey").setParentWorkflowId(942L).setParentActionId(842L).setExternalId("externalId") .setState("cState").setStateText("cState desc").setNextActivation(now()).setActions(asList(a)) .setCreated(now().minusMinutes(1)).setCreated(now().minusHours(2)).setModified(now().minusHours(1)).setRetries(42) .setStateVariables(stateVariables).setSignal(Optional.of(42)).setStartedIfNotSet(started).build(); JsonNode node1 = mock(JsonNode.class); JsonNode nodeQuux = mock(JsonNode.class); when(nflowObjectMapper.readTree("1")).thenReturn(node1); when(nflowObjectMapper.readTree("quux")).thenReturn(nodeQuux); Map<String, Object> expectedStateVariables = new LinkedHashMap<>(); expectedStateVariables.put("foo", node1); expectedStateVariables.put("bar", nodeQuux); ListWorkflowInstanceResponse resp = converter.convert(i, EnumSet.of(WorkflowInstanceInclude.ACTIONS, WorkflowInstanceInclude.CURRENT_STATE_VARIABLES)); verify(nflowObjectMapper).readTree("1"); verify(nflowObjectMapper).readTree("quux"); assertThat(resp.id, is(i.id)); assertThat(resp.stateVariables, is(expectedStateVariables)); assertThat(resp.status, is(i.status.name())); assertThat(resp.type, is(i.type)); assertThat(resp.parentWorkflowId, is(i.parentWorkflowId)); assertThat(resp.parentActionId, is(i.parentActionId)); assertThat(resp.businessKey, is(i.businessKey)); assertThat(resp.externalId, is(i.externalId)); assertThat(resp.state, is(i.state)); assertThat(resp.stateText, is(i.stateText)); assertThat(resp.nextActivation, is(i.nextActivation)); assertThat(resp.created, is(i.created)); assertThat(resp.modified, is(i.modified)); assertThat(resp.started, is(i.started)); assertThat(resp.retries, is(i.retries)); assertThat(resp.signal, is(i.signal.get())); assertThat(resp.actions, contains(reflectEquals( new Action(a.id, a.type.name(), a.state, a.stateText, a.retryNo, a.executionStart, a.executionEnd, a.executorId)))); }
@Test public void convertWithActionStateVariablesWorks() throws IOException { Map<String, String> stateVariables = new LinkedHashMap<>(); stateVariables.put("foo", "1"); stateVariables.put("bar", "quux"); WorkflowInstanceAction a = new WorkflowInstanceAction.Builder().setId(929).setType(stateExecution).setState("oState") .setStateText("oState desc").setRetryNo(1).setExecutionStart(now().minusDays(1)).setExecutionEnd(now().plusDays(1)) .setExecutorId(999).setUpdatedStateVariables(stateVariables).build(); WorkflowInstance i = new WorkflowInstance.Builder().setId(1).setStatus(inProgress).setType("dummy") .setBusinessKey("businessKey").setParentWorkflowId(942L).setParentActionId(842L).setExternalId("externalId") .setState("cState").setStateText("cState desc").setNextActivation(now()).setActions(asList(a)) .setCreated(now().minusMinutes(1)).setCreated(now().minusHours(2)).setModified(now().minusHours(1)).setRetries(42) .setSignal(Optional.empty()).build(); JsonNode node1 = mock(JsonNode.class); JsonNode nodeQuux = mock(JsonNode.class); when(nflowObjectMapper.readTree("1")).thenReturn(node1); when(nflowObjectMapper.readTree("quux")).thenReturn(nodeQuux); Map<String, Object> expectedStateVariables = new LinkedHashMap<>(); expectedStateVariables.put("foo", node1); expectedStateVariables.put("bar", nodeQuux); ListWorkflowInstanceResponse resp = converter.convert(i, EnumSet.of(WorkflowInstanceInclude.ACTIONS, WorkflowInstanceInclude.ACTION_STATE_VARIABLES)); verify(nflowObjectMapper).readTree("1"); verify(nflowObjectMapper).readTree("quux"); assertThat(resp.id, is(i.id)); assertThat(resp.status, is(i.status.name())); assertThat(resp.type, is(i.type)); assertThat(resp.parentWorkflowId, is(i.parentWorkflowId)); assertThat(resp.parentActionId, is(i.parentActionId)); assertThat(resp.businessKey, is(i.businessKey)); assertThat(resp.externalId, is(i.externalId)); assertThat(resp.state, is(i.state)); assertThat(resp.stateText, is(i.stateText)); assertThat(resp.nextActivation, is(i.nextActivation)); assertThat(resp.created, is(i.created)); assertThat(resp.modified, is(i.modified)); assertThat(resp.started, is(i.started)); assertThat(resp.retries, is(i.retries)); assertThat(resp.signal, is(nullValue())); assertThat(resp.actions, contains(reflectEquals(new Action(a.id, a.type.name(), a.state, a.stateText, a.retryNo, a.executionStart, a.executionEnd, a.executorId, expectedStateVariables)))); }
@Test public void convertWithoutActionsWorks() { WorkflowInstanceAction a = new WorkflowInstanceAction.Builder().setId(929).setType(stateExecution).setState("oState") .setStateText("oState desc").setRetryNo(1).setExecutionStart(now().minusDays(1)).setExecutionEnd(now().plusDays(1)) .build(); WorkflowInstance i = new WorkflowInstance.Builder().setId(1).setStatus(inProgress).setType("dummy") .setBusinessKey("businessKey").setExternalId("externalId").setState("cState").setStateText("cState desc") .setNextActivation(now()).setActions(asList(a)).build(); ListWorkflowInstanceResponse resp = converter.convert(i, emptySet()); assertThat(resp.id, is(i.id)); assertThat(resp.status, is(i.status.name())); assertThat(resp.type, is(i.type)); assertThat(resp.parentWorkflowId, nullValue()); assertThat(resp.parentActionId, nullValue()); assertThat(resp.businessKey, is(i.businessKey)); assertThat(resp.externalId, is(i.externalId)); assertThat(resp.state, is(i.state)); assertThat(resp.stateText, is(i.stateText)); assertThat(resp.nextActivation, is(i.nextActivation)); assertThat(resp.actions, nullValue()); }
@Test public void convertWithoutStateVariablesWorks() { WorkflowInstanceAction a = new WorkflowInstanceAction.Builder().setId(929).setType(stateExecution).setState("oState") .setStateText("oState desc").setRetryNo(1).setExecutionStart(now().minusDays(1)).setExecutionEnd(now().plusDays(1)) .build(); WorkflowInstance i = new WorkflowInstance.Builder().setId(1).setStatus(inProgress).setType("dummy") .setBusinessKey("businessKey").setExternalId("externalId").setState("cState").setStateText("cState desc") .setNextActivation(now()).setActions(Arrays.asList(a)).build(); ListWorkflowInstanceResponse resp = converter.convert(i, emptySet()); assertThat(resp.id, is(i.id)); assertThat(resp.stateVariables, is((Map<String, Object>) null)); assertThat(resp.type, is(i.type)); assertThat(resp.businessKey, is(i.businessKey)); assertThat(resp.externalId, is(i.externalId)); assertThat(resp.state, is(i.state)); assertThat(resp.stateText, is(i.stateText)); assertThat(resp.nextActivation, is(i.nextActivation)); assertThat(resp.actions, nullValue()); }
@Test public void convertWithEmptyStateVariablesWorks() { WorkflowInstanceAction a = new WorkflowInstanceAction.Builder().setId(929).setType(stateExecution).setState("oState") .setStateText("oState desc").setRetryNo(1).setExecutionStart(now().minusDays(1)).setExecutionEnd(now().plusDays(1)) .build(); WorkflowInstance i = new WorkflowInstance.Builder().setId(1).setStatus(inProgress).setType("dummy") .setBusinessKey("businessKey").setExternalId("externalId").setState("cState").setStateText("cState desc") .setNextActivation(now()).setActions(Arrays.asList(a)).setStateVariables(new LinkedHashMap<String, String>()).build(); ListWorkflowInstanceResponse resp = converter.convert(i, EnumSet.of(WorkflowInstanceInclude.CURRENT_STATE_VARIABLES)); assertThat(resp.id, is(i.id)); assertThat(resp.status, is(i.status.name())); assertThat(resp.stateVariables, is((Map<String, Object>) null)); assertThat(resp.type, is(i.type)); assertThat(resp.businessKey, is(i.businessKey)); assertThat(resp.externalId, is(i.externalId)); assertThat(resp.state, is(i.state)); assertThat(resp.stateText, is(i.stateText)); assertThat(resp.nextActivation, is(i.nextActivation)); assertThat(resp.actions, nullValue()); }
@Test public void convertWithEmptyActionsWorks() { WorkflowInstance i = new WorkflowInstance.Builder().setId(1).setStatus(inProgress).setType("dummy") .setBusinessKey("businessKey").setExternalId("externalId").setState("cState").setStateText("cState desc") .setNextActivation(now()).build(); ListWorkflowInstanceResponse resp = converter.convert(i, EnumSet.of(WorkflowInstanceInclude.ACTIONS, WorkflowInstanceInclude.ACTION_STATE_VARIABLES)); assertThat(resp.id, is(i.id)); assertThat(resp.status, is(i.status.name())); assertThat(resp.stateVariables, is((Map<String, Object>) null)); assertThat(resp.type, is(i.type)); assertThat(resp.businessKey, is(i.businessKey)); assertThat(resp.externalId, is(i.externalId)); assertThat(resp.state, is(i.state)); assertThat(resp.stateText, is(i.stateText)); assertThat(resp.nextActivation, is(i.nextActivation)); assertThat(resp.actions, is(emptyList())); }
@Test public void convertWithMalformedStateVariablesWorks() throws JsonProcessingException, IOException { DateTime now = now(); WorkflowInstanceAction a = new WorkflowInstanceAction.Builder() .setId(929).setType(stateExecution).setState("oStDateTimeate").setStateText("oState desc").setRetryNo(1) .setExecutionStart(now.minusDays(1)).setExecutionEnd(now.plusDays(1)).build(); Map<String, String> stateVariables = new LinkedHashMap<>(); String value1 = "{\"a\":1"; String value2 = "quux\""; stateVariables.put("foo", value1); stateVariables.put("bar", value2); WorkflowInstance i = new WorkflowInstance.Builder() .setId(1) .setStatus(inProgress).setType("dummy").setBusinessKey("businessKey").setExternalId("externalId").setState("cState") .setStateText("cState desc").setNextActivation(now).setActions(asList(a)).setStateVariables(stateVariables).build(); when(nflowObjectMapper.readTree(value1)).thenThrow(new JsonParseException(null, "bad data")); when(nflowObjectMapper.readTree(value2)).thenThrow(new JsonParseException(null, "bad data")); ListWorkflowInstanceResponse resp = converter.convert(i, EnumSet.of(WorkflowInstanceInclude.CURRENT_STATE_VARIABLES)); verify(nflowObjectMapper).readTree(value1); verify(nflowObjectMapper).readTree(value2); Map<String, Object> expectedStateVariables = new LinkedHashMap<>(); expectedStateVariables.put("foo", new TextNode(value1)); expectedStateVariables.put("bar", new TextNode(value2)); assertThat(resp.id, is(i.id)); assertThat(resp.status, is(i.status.name())); assertThat(resp.stateVariables, is(expectedStateVariables)); assertThat(resp.type, is(i.type)); assertThat(resp.businessKey, is(i.businessKey)); assertThat(resp.externalId, is(i.externalId)); assertThat(resp.state, is(i.state)); assertThat(resp.stateText, is(i.stateText)); assertThat(resp.nextActivation, is(i.nextActivation)); assertThat(resp.actions, nullValue()); }