Skip to content

Commit

Permalink
Merge pull request #39 from NitorCreations/check-stop-state-type
Browse files Browse the repository at this point in the history
Check stop state type
  • Loading branch information
jsyrjala committed Sep 23, 2014
2 parents 0c0c0d2 + d4b733a commit c290e11
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,27 @@ public static NextAction moveToState(WorkflowState nextState, String reason) {

/**
* Set next state to {@code finalState} and do not schedule its
* processing. Used to indicate final state of workflow or a manual state.
* @param state Final or manual workflow state.
* processing. The {@code finalState} workflow state type must
* be final, e.g. it must be either end state or manual state.
* The state handler method of the state, even if it exists, is
* not executed. Additionally, there is no workflow action
* recorded for the {@code finalState}. If you want to execute
* the state handler method and record a workflow action, use
* one of the {@code moveToState} methods instead.
* @param state Final workflow state (end state or manual state).
* @param reason The reason for the action.
* @return A valid {@code NextAction} value.
*/
public static NextAction stopInState(WorkflowState state, String reason) {
assertNotNull(state, "State can not be null");
return new NextAction(null, state, reason);
public static NextAction stopInState(WorkflowState finalState, String reason) {
assertNotNull(finalState, "State can not be null");
assertFinalState(finalState);
return new NextAction(null, finalState, reason);
}

private static void assertFinalState(WorkflowState state) {
if (!state.getType().isFinal()) {
throw new InvalidNextActionException("Cannot stop in a state that is not final");
}
}

private static void assertNotNull(Object object, String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,38 @@ public enum WorkflowStateType {
/**
* Initial states of the workflow.
*/
start,
start(false),

/**
* State that requires manual action. Workflow execution is stopped.
*/
manual,
manual(true),

/**
* State that can be normally executed.
*/
normal,
normal(false),

/**
* Final state of the workflow. Workflow execution is stopped.
*/
end;
end(true);

private final boolean isFinal;

private WorkflowStateType(boolean isFinal) {
this.isFinal = isFinal;
}

/**
* Returns true is the state of this type is a final state, e.g. a state after
* which the workflow processing is stopped. The workflow state can be moved
* to another state only by manual action.
*
* @return True for states of type {@code manual} and {@code end}, false for
* states of type {@code start} and {@code normal}.
*/
public boolean isFinal() {
return isFinal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public Builder setExecutorGroup(String executorGroup) {
* Set the executor group name.
* @param executorGroup The executor group name.
* @return this.
* @depracated Use setExecutorGroup instead. Will be removed in 2.0.
* @deprecated Use setExecutorGroup instead. Will be removed in 2.0.
*/
@Deprecated
public Builder setOwner(String executorGroup) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.nitorcreations.nflow.engine.workflow.definition;

import static com.nitorcreations.nflow.engine.workflow.definition.WorkflowStateType.end;
import static com.nitorcreations.nflow.engine.workflow.definition.WorkflowStateType.manual;
import static com.nitorcreations.nflow.engine.workflow.definition.WorkflowStateType.normal;
import static com.nitorcreations.nflow.engine.workflow.definition.WorkflowStateType.start;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;

import org.junit.Test;

import com.nitorcreations.nflow.engine.internal.executor.InvalidNextActionException;

public class NextActionTest {

@Test(expected = InvalidNextActionException.class)
public void stopInStartStateThrowsException() {
NextAction.stopInState(TestState.initial, "stop reason");
}

@Test(expected = InvalidNextActionException.class)
public void stopInNormalStateThrowsException() {
NextAction.stopInState(TestState.state1, "stop reason");
}

@Test
public void stopInManualStateSetsActivationToNull() {
NextAction nextAction = NextAction.stopInState(TestState.error, "stop reason");
assertThat(nextAction.getActivation(), is(nullValue()));
}

@Test
public void stopInEndStateSetsActivationToNull() {
NextAction nextAction = NextAction.stopInState(TestState.done, "stop reason");
assertThat(nextAction.getActivation(), is(nullValue()));
}

static enum TestState implements WorkflowState {
initial(start), state1(normal), error(manual), done(end);

private final WorkflowStateType stateType;

private TestState(WorkflowStateType stateType) {
this.stateType = stateType;
}

@Override
public WorkflowStateType getType() {
return stateType;
}

@Override
public String getName() {
return name();
}

@Override
public String getDescription() {
return name();
}
}
}

0 comments on commit c290e11

Please sign in to comment.