Skip to content

Commit

Permalink
[Connectors API] Add tests for ConnectorStateMachine (elastic#105736)
Browse files Browse the repository at this point in the history
  • Loading branch information
timgrein authored Feb 23, 2024
1 parent 2ba37ff commit ae4e57d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public static boolean isValidTransition(ConnectorStatus current, ConnectorStatus
return validNextStates(current).contains(next);
}

/**
* Throws {@link ConnectorInvalidStatusTransitionException} if a
* transition from one {@link ConnectorStatus} to another is invalid.
*
* @param current The current {@link ConnectorStatus} of the {@link Connector}.
* @param next The proposed next {@link ConnectorStatus} of the {@link Connector}.
*/
public static void assertValidStateTransition(ConnectorStatus current, ConnectorStatus next)
throws ConnectorInvalidStatusTransitionException {
if (isValidTransition(current, next)) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,31 @@ public void testTransitionToSameState() {
assertFalse("Transition from " + state + " to itself should be invalid", ConnectorStateMachine.isValidTransition(state, state));
}
}

public void testAssertValidStateTransition_ExpectExceptionOnInvalidTransition() {
assertThrows(
ConnectorInvalidStatusTransitionException.class,
() -> ConnectorStateMachine.assertValidStateTransition(ConnectorStatus.CREATED, ConnectorStatus.CONFIGURED)
);
}

public void testAssertValidStateTransition_ExpectNoExceptionOnValidTransition() {
ConnectorStatus prevStatus = ConnectorStatus.CREATED;
ConnectorStatus nextStatus = ConnectorStatus.ERROR;

try {
ConnectorStateMachine.assertValidStateTransition(prevStatus, nextStatus);
} catch (ConnectorInvalidStatusTransitionException e) {
fail(
"Did not expect "
+ ConnectorInvalidStatusTransitionException.class.getSimpleName()
+ " to be thrown for valid state transition ["
+ prevStatus
+ "] -> "
+ "["
+ nextStatus
+ "]."
);
}
}
}

0 comments on commit ae4e57d

Please sign in to comment.