ListWorkflowDefinitionConverter { @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", justification = "cast is safe") public ListWorkflowDefinitionResponse convert(AbstractWorkflowDefinition<? extends WorkflowState> definition) { ListWorkflowDefinitionResponse resp = new ListWorkflowDefinitionResponse(); resp.type = definition.getType(); resp.name = definition.getName(); resp.description = definition.getDescription(); resp.onError = definition.getErrorState().name(); Map<String, State> states = new LinkedHashMap<>(definition.getStates().size() * 2); for (WorkflowState state : definition.getStates()) { states.put(state.name(), new State(state.name(), state.getType().name(), state.getDescription())); } for (Entry<String, List<String>> entry : definition.getAllowedTransitions().entrySet()) { State state = states.get(entry.getKey()); for(String targetState : entry.getValue()) { state.transitions.add(targetState); } } for (Entry<String, WorkflowState> entry : definition.getFailureTransitions().entrySet()) { State state = states.get(entry.getKey()); state.onFailure = entry.getValue().name(); } Collection<State> values = states.values(); resp.states = values.toArray(new State[values.size()]); WorkflowSettings workflowSettings = definition.getSettings(); TransitionDelays transitionDelays = new TransitionDelays(); transitionDelays.immediate = workflowSettings.immediateTransitionDelay; transitionDelays.waitShort = workflowSettings.shortTransitionDelay; transitionDelays.minErrorWait = workflowSettings.minErrorTransitionDelay; transitionDelays.maxErrorWait = workflowSettings.maxErrorTransitionDelay; Settings settings = new Settings(); settings.transitionDelaysInMilliseconds = transitionDelays; settings.maxRetries = workflowSettings.maxRetries; settings.historyDeletableAfter = workflowSettings.historyDeletableAfter; settings.defaultPriority = workflowSettings.defaultPriority; resp.settings = settings; resp.supportedSignals = definition.getSupportedSignals().entrySet().stream().map(entry -> { Signal signal = new Signal(); signal.value = entry.getKey(); signal.description = entry.getValue(); return signal; }).toArray(size -> new Signal[size]); return resp; }  @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", justification = "cast is safe") ListWorkflowDefinitionResponse convert(AbstractWorkflowDefinition<? extends WorkflowState> definition); ListWorkflowDefinitionResponse convert(StoredWorkflowDefinition storedDefinition);  }
@Test public void convertWorks() { DummyTestWorkflow def = new DummyTestWorkflow(); ListWorkflowDefinitionResponse resp = converter.convert(def); assertThat(resp.type, is(def.getType())); assertThat(resp.name, is(def.getName())); assertThat(resp.description, is(def.getDescription())); assertThat(resp.onError, is(def.getErrorState().name())); assertThat(resp.states, arrayContainingInAnyOrder( reflectEquals(getResponseState(end, Collections.<String>emptyList(), null)), reflectEquals(getResponseState(error, asList(end.name()), null)), reflectEquals(getResponseState(start, asList(end.name(), error.name()), error.name())))); assertThat(resp.supportedSignals, arrayContainingInAnyOrder( reflectEquals(getSignal(1, "one")), reflectEquals(getSignal(2, "two")))); assertThat((int)resp.settings.transitionDelaysInMilliseconds.immediate, is(def.getSettings().immediateTransitionDelay)); assertThat((int)resp.settings.transitionDelaysInMilliseconds.waitShort, is(def.getSettings().shortTransitionDelay)); assertThat((int)resp.settings.transitionDelaysInMilliseconds.minErrorWait, is(def.getSettings().minErrorTransitionDelay)); assertThat((int)resp.settings.transitionDelaysInMilliseconds.maxErrorWait, is(def.getSettings().maxErrorTransitionDelay)); assertThat(resp.settings.maxRetries, is(def.getSettings().maxRetries)); assertThat(resp.settings.historyDeletableAfter, is(def.getSettings().historyDeletableAfter)); }
@Test public void convertStoredDefinitionWorks() { StoredWorkflowDefinition stored = new StoredWorkflowDefinition(); stored.description = "desc"; stored.onError = "errorState"; stored.type = "storedDefinition"; StoredWorkflowDefinition.State storedState = new StoredWorkflowDefinition.State("first", "normal", "first state desc"); stored.states = asList(storedState); StoredWorkflowDefinition.Signal storedSignal = new StoredWorkflowDefinition.Signal(); storedSignal.value = 1; storedSignal.description = "one"; stored.supportedSignals = asList(storedSignal); ListWorkflowDefinitionResponse resp = converter.convert(stored); assertThat(resp.description, is(stored.description)); assertThat(resp.onError, is(stored.onError)); assertThat(resp.states.length, is(1)); assertThat(resp.states[0].description, is(storedState.description)); assertThat(resp.states[0].type, is(storedState.type)); assertThat(resp.states[0].id, is(storedState.id)); assertThat(resp.supportedSignals[0].value, is(storedSignal.value)); assertThat(resp.supportedSignals[0].description, is(storedSignal.description)); }