HerdCommandInvoker extends CommandInvoker { @Override public <T> T execute(CommandConfig config, Command<T> command) { LOGGER.debug("command=\"{}\"", command.getClass().getName()); try { return super.execute(config, command); } catch (Exception e) { LOGGER.warn(String.format("HerdCommandInvoker caught an exception."), e); if (command instanceof ExecuteAsyncJobCmd) { ExecuteAsyncJobCmd executeAsyncJobCmd = (ExecuteAsyncJobCmd) command; JobEntity jobEntity = getJobEntity(executeAsyncJobCmd); if (StringUtils.containsIgnoreCase(ExceptionUtils.getMessage(e), CANNOT_CREATE_TRANSACTION_EXCEPTION)) { LOGGER.warn(String.format("HerdCommandInvoker caught a CannotCreateTransactionException. Job id %s, Process instance id %s, Retries %d.", jobEntity.getId(), jobEntity.getProcessInstanceId(), jobEntity.getRetries()), e); throw e; } jobEntity.setExceptionMessage(ExceptionUtils.getMessage(e)); jobEntity.setExceptionStacktrace(ExceptionUtils.getStackTrace(e)); return null; } else { throw e; } } }  @Override T execute(CommandConfig config, Command<T> command);  }
@Test public void testExecuteWithExceptionAndGetJobEntityWithNoSuchFieldException() { CommandConfig config = mock(CommandConfig.class); JobEntity jobEntity = mock(JobEntity.class); ExecuteAsyncJobCmd command = new ExecuteAsyncJobCmd(jobEntity); doThrow(NoSuchFieldException.class).when(jobEntity).getId(); try { herdCommandInvoker.execute(config, command); fail(); } catch (IllegalStateException e) { assertEquals(NoSuchFieldException.class.getName(), e.getMessage()); } }
@Test public void testExecuteWithExceptionAndGetJobEntityWithSecurityException() { CommandConfig config = mock(CommandConfig.class); JobEntity jobEntity = mock(JobEntity.class); ExecuteAsyncJobCmd command = new ExecuteAsyncJobCmd(jobEntity); doThrow(SecurityException.class).when(jobEntity).getId(); try { herdCommandInvoker.execute(config, command); fail(); } catch (IllegalStateException e) { assertEquals(SecurityException.class.getName(), e.getMessage()); } }
@Test public void testExecuteWithExceptionAndGetCreateTransactionException() { CommandConfig config = mock(CommandConfig.class); JobEntity job = mock(JobEntity.class); JobEntityManager jobEntityManager = mock(JobEntityManager.class); CommandContext commandContext = mock(CommandContext.class); CommandContext commandContextSaved = Context.getCommandContext(); Context.setCommandContext(commandContext); String jobId = "testId100"; when(job.getId()).thenReturn(jobId); when(job.getProcessInstanceId()).thenReturn("testProcessId100"); when(job.getRetries()).thenReturn(3); ExecuteAsyncJobCmd command = new ExecuteAsyncJobCmd(job); doThrow(CannotCreateTransactionException.class).when(job).execute(any()); when(commandContext.getJobEntityManager()).thenReturn(jobEntityManager); when(jobEntityManager.findJobById(jobId)).thenReturn(job); try { herdCommandInvoker.execute(config, command); fail(); } catch (CannotCreateTransactionException e) { } finally { Context.setCommandContext(commandContextSaved); } }