@SuppressWarnings("unchecked")
@Test
public void testFinalizeBatch() throws Exception {

  Clusters clustersMock = createMock(Clusters.class);
  Cluster clusterMock = createMock(Cluster.class);
  Configuration configurationMock = createNiceMock(Configuration.class);
  ExecutionScheduler executionSchedulerMock = createMock(ExecutionScheduler.class);
  InternalTokenStorage tokenStorageMock = createMock(InternalTokenStorage.class);
  ActionDBAccessor actionDBAccessorMock = createMock(ActionDBAccessor.class);
  Gson gson = new Gson();
  RequestExecution requestExecutionMock = createMock(RequestExecution.class);
  Batch batchMock = createMock(Batch.class);
  JobDetail jobDetailMock = createMock(JobDetail.class);
  final BatchRequest batchRequestMock = createMock(BatchRequest.class);
  final Trigger triggerMock = createNiceMock(Trigger.class);
  final List<Trigger> triggers = new ArrayList<Trigger>()  {{ add(triggerMock); }};
  long executionId = 11L;
  String clusterName = "c1";
  Date pastDate = new Date(new Date().getTime() - 2);
  Map<Long, RequestExecution> executionMap = new HashMap<Long, RequestExecution>();
  executionMap.put(executionId, requestExecutionMock);
  EasyMock.expect(configurationMock.getApiSSLAuthentication()).andReturn(Boolean.FALSE);
  EasyMock.replay(configurationMock);
  ExecutionScheduleManager scheduleManager =
      createMockBuilder(ExecutionScheduleManager.class)
          .withConstructor(configurationMock, executionSchedulerMock, tokenStorageMock,
              clustersMock, actionDBAccessorMock, gson).createMock();
  expect(clustersMock.getCluster(clusterName)).andReturn(clusterMock).anyTimes();
  expect(clusterMock.getAllRequestExecutions()).andReturn(executionMap).anyTimes();
  expect(requestExecutionMock.getBatch()).andReturn(batchMock).anyTimes();
  expect(batchMock.getBatchRequests()).andReturn
      (new ArrayList<BatchRequest>() {{
        add(batchRequestMock);
      }});
  expect(batchRequestMock.getOrderId()).andReturn(1L).anyTimes();
  expect(executionSchedulerMock.getJobDetail((JobKey) anyObject()))
      .andReturn(jobDetailMock).anyTimes();
  expect((List<Trigger>) executionSchedulerMock
      .getTriggersForJob((JobKey) anyObject())).andReturn(triggers).anyTimes();
  expect(triggerMock.mayFireAgain()).andReturn(true).anyTimes();
  expect(triggerMock.getFinalFireTime()).andReturn(pastDate).anyTimes();
  requestExecutionMock.updateStatus(RequestExecution.Status.COMPLETED);
  expectLastCall();
  replay(clustersMock, clusterMock, requestExecutionMock,
      executionSchedulerMock, scheduleManager, batchMock, batchRequestMock,
      triggerMock, jobDetailMock, actionDBAccessorMock);
  scheduleManager.finalizeBatch(executionId, clusterName);
  verify(clustersMock, clusterMock, configurationMock, requestExecutionMock,
      executionSchedulerMock, scheduleManager, batchMock, batchRequestMock,
      triggerMock, jobDetailMock, actionDBAccessorMock);
}