test_class.file: herd-code/herd-service/src/test/java/org/finra/herd/service/activiti/HerdCommandInvokerTest.java
test_class.identifier: HerdCommandInvokerTest
test_class.superclass: extends HerdActivitiServiceTaskTest
test_case.body: @Test
      public void testExecuteWithExceptionAndGetCreateTransactionException()
      {
          // Mock dependencies.
          CommandConfig config = mock(CommandConfig.class);
          JobEntity job = mock(JobEntity.class);
          JobEntityManager jobEntityManager = mock(JobEntityManager.class);
          CommandContext commandContext = mock(CommandContext.class);
          //Save the command context to use later
          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)
          {
             //Get expected exception
          }
          finally
          {
              Context.setCommandContext(commandContextSaved);
          }
  
      }
test_case.class_method_signature: HerdCommandInvokerTest.testExecuteWithExceptionAndGetCreateTransactionException()
test_case.full_signature: @Test public void testExecuteWithExceptionAndGetCreateTransactionException()
test_case.identifier: testExecuteWithExceptionAndGetCreateTransactionException
test_case.invocations: mock mock mock mock getCommandContext setCommandContext thenReturn when getId thenReturn when getProcessInstanceId thenReturn when getRetries execute when doThrow any thenReturn when getJobEntityManager thenReturn when findJobById execute fail setCommandContext
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testExecuteWithExceptionAndGetCreateTransactionException()
focal_class.declarator: LOGGER = LoggerFactory.getLogger(HerdCommandInvoker.class)
focal_class.modifier: private static final
focal_class.original_string: private static final Logger LOGGER = LoggerFactory.getLogger(HerdCommandInvoker.class);
focal_class.type: Logger
focal_class.var_name: LOGGER
focal_class.declarator: CANNOT_CREATE_TRANSACTION_EXCEPTION = "CannotCreateTransactionException"
focal_class.modifier: private static final
focal_class.original_string: private static final String CANNOT_CREATE_TRANSACTION_EXCEPTION = "CannotCreateTransactionException";
focal_class.type: String
focal_class.var_name: CANNOT_CREATE_TRANSACTION_EXCEPTION
focal_class.file: herd-code/herd-service/src/main/java/org/finra/herd/service/activiti/HerdCommandInvoker.java
focal_class.identifier: HerdCommandInvoker
focal_class.class_method_signature: HerdCommandInvoker.execute(CommandConfig config, Command<T> command)
focal_class.full_signature: @Override public T execute(CommandConfig config, Command<T> command)
focal_class.identifier: execute
focal_class.modifiers: @Override public
focal_class.parameters: (CommandConfig config, Command<T> command)
focal_class.return: T
focal_class.signature: T execute(CommandConfig config, Command<T> command)
focal_class.class_method_signature: HerdCommandInvoker.getJobEntity(ExecuteAsyncJobCmd executeAsyncJobCmd)
focal_class.full_signature: private JobEntity getJobEntity(ExecuteAsyncJobCmd executeAsyncJobCmd)
focal_class.identifier: getJobEntity
focal_class.modifiers: private
focal_class.parameters: (ExecuteAsyncJobCmd executeAsyncJobCmd)
focal_class.return: JobEntity
focal_class.signature: JobEntity getJobEntity(ExecuteAsyncJobCmd executeAsyncJobCmd)
focal_class.superclass: extends CommandInvoker
focal_method.body: @Override
      public <T> T execute(CommandConfig config, Command<T> command)
      {
          LOGGER.debug("command=\"{}\"", command.getClass().getName());
          try
          {
              // Perform the normal execution.
              return super.execute(config, command);
          }
          catch (Exception e)
          {
              LOGGER.warn(String.format("HerdCommandInvoker caught an exception."), e);
  
              /*
               * Attempt to handle exception based on the command.
               * If we bubble the exception up here, the transaction will be rolled back and all variables which were not committed will not be persisted.
               * The problem with swallowing the exception, however, is that the exception message is not persisted automatically. To get around it, we must save
               * the exception message and stacktrace into a JobEntity which is associated with the current execution.
               */
  
              if (command instanceof ExecuteAsyncJobCmd)
              {
                  /*
                   * ExecuteAsyncJobCmd is executed when a task is asynchronous.
                   * Save the exception information in the command's JobEntity
                   */
                  ExecuteAsyncJobCmd executeAsyncJobCmd = (ExecuteAsyncJobCmd) command;
                  JobEntity jobEntity = getJobEntity(executeAsyncJobCmd);
  
                  /*
                   * If the exception is unable to create transaction due to JDBC connection issue,
                   * throw out the exception, then let the Activiti engine take care of retries
                   */
                  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
              {
                  /*
                   * We do not know how to handle any other commands, so just bubble it up and let Activiti's default mechanism kick in.
                   */
                  throw e;
              }
          }
      }
focal_method.class_method_signature: HerdCommandInvoker.execute(CommandConfig config, Command<T> command)
focal_method.full_signature: @Override public T execute(CommandConfig config, Command<T> command)
focal_method.identifier: execute
focal_method.invocations: debug getName getClass execute warn format getJobEntity containsIgnoreCase getMessage warn format getId getProcessInstanceId getRetries setExceptionMessage getMessage setExceptionStacktrace getStackTrace
focal_method.modifiers: @Override public
focal_method.parameters: (CommandConfig config, Command<T> command)
focal_method.return: T
focal_method.signature: T execute(CommandConfig config, Command<T> command)