Skip to content

Commit

Permalink
Fix exception when no actions configured.
Browse files Browse the repository at this point in the history
  • Loading branch information
malithie committed Nov 8, 2024
1 parent 4cdc52a commit 2c4ccf5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class ActionExecutorServiceImpl implements ActionExecutorService {
private static final ActionExecutionDiagnosticLogger DIAGNOSTIC_LOGGER = new ActionExecutionDiagnosticLogger();
private final APIClient apiClient;
private final ExecutorService executorService = ThreadLocalAwareExecutors.newFixedThreadPool(THREAD_POOL_SIZE);

private ActionExecutorServiceImpl() {

apiClient = new APIClient();
Expand Down Expand Up @@ -118,7 +119,9 @@ public ActionExecutionStatus<?> execute(ActionType actionType, Map<String, Objec
} catch (ActionExecutionRuntimeException e) {
DIAGNOSTIC_LOGGER.logSkippedActionExecution(actionType);
LOG.debug("Skip executing actions for action type: " + actionType.name(), e);
throw new ActionExecutionException("Failed to execute actions for action type: " + actionType, e);
// Skip executing actions when no action available or due to a failure in retrieving actions,
// is considered as action execution being successful.
return new SuccessStatus.Builder().setResponseContext(eventContext).build();
}
}

Expand All @@ -143,7 +146,9 @@ public ActionExecutionStatus<?> execute(ActionType actionType, String[] actionId
} catch (ActionExecutionRuntimeException e) {
DIAGNOSTIC_LOGGER.logSkippedActionExecution(actionType);
LOG.debug("Skip executing actions for action type: " + actionType.name(), e);
throw new ActionExecutionException("Failed to execute actions for action type: " + actionType, e);
// Skip executing actions when no action available or due to a failure in retrieving actions,
// is considered as action execution being successful.
return new SuccessStatus.Builder().setResponseContext(eventContext).build();
}
}

Expand Down Expand Up @@ -230,7 +235,7 @@ private ActionExecutionRequest buildActionExecutionRequest(ActionType actionType
request.setAdditionalParams(RequestFilter.getFilteredParams(request.getAdditionalParams(), actionType));
return actionExecutionRequest;
} catch (ActionExecutionRequestBuilderException e) {
throw new ActionExecutionRuntimeException("Error occurred while building the request payload.", e);
throw new ActionExecutionException("Failed to build the request payload for action type: " + actionType, e);
}
}

Expand All @@ -246,10 +251,10 @@ private ActionExecutionResponseProcessor getResponseProcessor(ActionType actionT
}

private ActionExecutionStatus<?> executeAction(Action action,
ActionExecutionRequest actionRequest,
Map<String, Object> eventContext,
ActionExecutionResponseProcessor actionExecutionResponseProcessor)
throws ActionExecutionRuntimeException {
ActionExecutionRequest actionRequest,
Map<String, Object> eventContext,
ActionExecutionResponseProcessor actionExecutionResponseProcessor)
throws ActionExecutionException {

Authentication endpointAuthentication = action.getEndpoint().getAuthentication();
AuthMethods.AuthMethod authenticationMethod;
Expand All @@ -265,21 +270,21 @@ private ActionExecutionStatus<?> executeAction(Action action,
return processActionResponse(action, actionInvocationResponse, eventContext, actionRequest,
actionExecutionResponseProcessor);
} catch (ActionMgtException | JsonProcessingException | ActionExecutionResponseProcessorException e) {
throw new ActionExecutionRuntimeException("Error occurred while executing action: " + action.getId(), e);
throw new ActionExecutionException("Error occurred while executing action: " + action.getId(), e);
}
}

private ActionInvocationResponse executeActionAsynchronously(Action action,
AuthMethods.AuthMethod authenticationMethod,
String payload) {
String payload) throws ActionExecutionException {

String apiEndpoint = action.getEndpoint().getUri();
CompletableFuture<ActionInvocationResponse> actionExecutor = CompletableFuture.supplyAsync(
() -> apiClient.callAPI(apiEndpoint, authenticationMethod, payload), executorService);
try {
return actionExecutor.get();
} catch (InterruptedException | ExecutionException e) {
throw new ActionExecutionRuntimeException("Error occurred while executing action: " + action.getId(),
throw new ActionExecutionException("Error occurred while executing action: " + action.getId(),
e);
}
}
Expand All @@ -299,12 +304,12 @@ private void logActionRequest(Action action, String payload) {
}

private ActionExecutionStatus<?> processActionResponse(Action action,
ActionInvocationResponse actionInvocationResponse,
Map<String, Object> eventContext,
ActionExecutionRequest actionRequest,
ActionExecutionResponseProcessor
actionExecutionResponseProcessor)
throws ActionExecutionResponseProcessorException {
ActionInvocationResponse actionInvocationResponse,
Map<String, Object> eventContext,
ActionExecutionRequest actionRequest,
ActionExecutionResponseProcessor
actionExecutionResponseProcessor)
throws ActionExecutionResponseProcessorException, ActionExecutionException {

if (actionInvocationResponse.isSuccess()) {
return processSuccessResponse(action,
Expand All @@ -318,8 +323,8 @@ private ActionExecutionStatus<?> processActionResponse(Action action,
eventContext, actionRequest, actionExecutionResponseProcessor);
}
logErrorResponse(action, actionInvocationResponse);
throw new ActionExecutionRuntimeException("Received an invalid or unexpected action status for action type: "
+ action.getType() + " and action ID: " + action.getId());
throw new ActionExecutionException("Received an invalid or unexpected response for action type: "
+ action.getType() + " action ID: " + action.getId());
}

private ActionExecutionStatus<Success> processSuccessResponse(Action action,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,16 @@ public void tearDown() {
actionExecutorConfigStatic.close();
}

@Test(expectedExceptions = ActionExecutionException.class)
public void testActionExecuteFailureWhenNoActionsAvailableForActionType() throws Exception {
@Test
public void testActionExecuteSuccessWhenNoActionsAvailableForActionType() throws Exception {

when(actionManagementService.getActionsByActionType(any(), any())).thenReturn(Collections.emptyList());

ActionExecutionStatus actionExecutionStatus =
ActionExecutionStatus expectedStatus = new SuccessStatus.Builder().build();
ActionExecutionStatus actualStatus =
actionExecutorService.execute(ActionType.PRE_ISSUE_ACCESS_TOKEN, any(), any());
assertEquals(actionExecutionStatus.getStatus(), ActionExecutionStatus.Status.FAILED);

assertEquals(actualStatus.getStatus(), expectedStatus.getStatus());
}

@Test
Expand Down Expand Up @@ -229,7 +231,8 @@ public void testActionExecuteWithActionIdFailureWhenInvalidActionIdGiven() throw
}

@Test(expectedExceptions = ActionExecutionException.class,
expectedExceptionsMessageRegExp = "Failed to execute actions for action type: PRE_ISSUE_ACCESS_TOKEN")
expectedExceptionsMessageRegExp = "Failed to build the request payload for action type: " +
"PRE_ISSUE_ACCESS_TOKEN")
public void testActionExecuteFailureWhenBuildingActionExecutionRequestForActionId() throws Exception {

ActionType actionType = ActionType.PRE_ISSUE_ACCESS_TOKEN;
Expand Down Expand Up @@ -271,7 +274,7 @@ public void testActionExecuteFailureAtExceptionFromRequestBuilderForActionType()
assertEquals(actionExecutionStatus.getStatus(), ActionExecutionStatus.Status.FAILED);

ActionExecutionStatus actionExecutionStatusWithActionIds = actionExecutorService.execute(
ActionType.PRE_ISSUE_ACCESS_TOKEN, new String[]{any()}, any(), any());
ActionType.PRE_ISSUE_ACCESS_TOKEN, new String[]{any()}, any(), any());
assertEquals(actionExecutionStatusWithActionIds.getStatus(), ActionExecutionStatus.Status.FAILED);
}

Expand Down Expand Up @@ -488,14 +491,14 @@ public void testExecuteFailure() throws Exception {
assertEquals(((FailedStatus) actualStatus).getResponse().getFailureReason(), "Error_reason");
assertEquals(((FailedStatus) actualStatus).getResponse().getFailureDescription(), "Error_description");


ActionExecutionStatus actionExecutionStatusWithActionIds = actionExecutorService.execute(
actionType, new String[]{action.getId()}, eventContext, "tenantDomain");
assertEquals(actionExecutionStatusWithActionIds.getStatus(), expectedStatus.getStatus());
}

@Test(expectedExceptions = ActionExecutionException.class,
expectedExceptionsMessageRegExp = "Failed to execute actions for action type: PRE_ISSUE_ACCESS_TOKEN")
expectedExceptionsMessageRegExp = "Received an invalid or unexpected response for action type: " +
"PRE_ISSUE_ACCESS_TOKEN action ID: actionId")
public void testActionExecuteFailureForUnexpectedAPIResponse() throws Exception {

// Setup
Expand Down Expand Up @@ -703,6 +706,7 @@ private void setField(Object target, String fieldName, Object value) throws Exce
}

private void setFinalField(Object target, String fieldName, Object value) throws Exception {

Field field;
try {
field = target.getClass().getDeclaredField(fieldName);
Expand All @@ -719,7 +723,6 @@ private void setFinalField(Object target, String fieldName, Object value) throws
field.set(target, value);
}


private List<AllowedOperation> getAllowedOperations() {

AllowedOperation addOperation =
Expand Down

0 comments on commit 2c4ccf5

Please sign in to comment.