CreateWorkflowConverter { public WorkflowInstance convert(CreateWorkflowInstanceRequest req) { WorkflowInstance.Builder builder = factory.newWorkflowInstanceBuilder().setType(req.type).setBusinessKey(req.businessKey) .setExternalId(req.externalId); if (!FALSE.equals(req.activate)) { if (req.activationTime != null) { builder.setNextActivation(req.activationTime); } } else { builder.setNextActivation(null); } builder.setParentWorkflowId(req.parentWorkflowId); if (isNotEmpty(req.startState)) { builder.setState(req.startState); } for (Entry<String, Object> entry : req.stateVariables.entrySet()) { Object value = entry.getValue(); if (value instanceof String) { builder.putStateVariable(entry.getKey(), (String) value); } else { builder.putStateVariable(entry.getKey(), value); } } return builder.build(); } @Inject  CreateWorkflowConverter(WorkflowInstanceFactory factory); WorkflowInstance convert(CreateWorkflowInstanceRequest req); CreateWorkflowInstanceResponse convert(WorkflowInstance instance);  }
@Test public void convertAndValidateWorks() { CreateWorkflowInstanceRequest req = new CreateWorkflowInstanceRequest(); req.activationTime = DateTime.now(); req.businessKey = "businessKey"; req.externalId = "externalId"; req.type = "wfType"; req.startState = "startState"; req.stateVariables.put("foo", "bar"); req.stateVariables.put("textNode", new TextNode("text")); WorkflowInstance i = converter.convert(req); assertThat(i.nextActivation, equalTo(req.activationTime)); assertThat(i.businessKey, equalTo(req.businessKey)); assertThat(i.externalId, equalTo(req.externalId)); assertThat(i.type, equalTo(req.type)); assertThat(i.state, equalTo("startState")); assertThat(i.stateVariables.get("foo"), is("bar")); assertThat(i.stateVariables.get("textNode"), is("\"text\"")); }
@Test public void convertAndValidateWorksWithMinimalData() { CreateWorkflowInstanceRequest req = new CreateWorkflowInstanceRequest(); req.type = "wfType"; WorkflowInstance i = converter.convert(req); assertThat(i.nextActivation, notNullValue(DateTime.class)); assertThat(i.businessKey, nullValue(String.class)); assertThat(i.externalId, nullValue(String.class)); assertThat(i.type, equalTo(req.type)); }
@Test public void nextActivationIsSetToNullWhenInstanceIsNotActivated() { CreateWorkflowInstanceRequest req = new CreateWorkflowInstanceRequest(); req.type = "wfType"; req.activationTime = now(); req.activate = false; WorkflowInstance i = converter.convert(req); assertThat(i.nextActivation, nullValue(DateTime.class)); assertThat(i.businessKey, nullValue(String.class)); assertThat(i.externalId, nullValue(String.class)); assertThat(i.type, equalTo(req.type)); }
@Test public void convertWorks() { WorkflowInstance i = new WorkflowInstance.Builder().setId(1).setType("dummy").setBusinessKey("businessKey") .setExternalId("externalId").build(); CreateWorkflowInstanceResponse resp = converter.convert(i); assertThat(resp.id, equalTo(i.id)); assertThat(resp.type, equalTo(i.type)); assertThat(resp.businessKey, equalTo(i.businessKey)); assertThat(resp.externalId, equalTo(i.externalId)); }