JobStatusChangerImpl implements JobStatusChanger { @Override public boolean changeToStartStatus(String jobSequenceId) { TransactionStatus transactionStatus = null; try { transactionStatus = adminTransactionManager.getTransaction( new DefaultTransactionDefinition()); BatchJobData batchJobData = getBatchJobData(jobSequenceId); if (!isJobStatusValid(batchJobData, JOB_STATUS_UNEXECUTION, JOB_STATUS_EXECUTING)) { return false; } if (!updateBatchJobStatus(jobSequenceId, batchJobData .getBLogicAppStatus(), JOB_STATUS_EXECUTING)) { return false; } adminTransactionManager.commit(transactionStatus); } finally { if (transactionStatus != null && !transactionStatus.isCompleted()) { LOGGER.info(LogId.IAL025023, jobSequenceId); try { adminTransactionManager.rollback(transactionStatus); } catch (Exception e) { LOGGER.error(LogId.EAL025064, e, jobSequenceId); } } } return true; } JobStatusChangerImpl(SystemDao systemDao,
            PlatformTransactionManager adminTransactionManager); @Override boolean changeToStartStatus(String jobSequenceId); @Override boolean changeToEndStatus(String jobSequenceId,
            BLogicResult blogicResult);  }
@Test public void testChangeToStartStatus01() { TransactionStatus mockTran = mock(TransactionStatus.class); when(mockPlatformTransactionManager.getTransaction(any( DefaultTransactionDefinition.class))).thenReturn(mockTran); when(mockSystemDao.selectJob(any(BatchJobManagementParam.class))) .thenReturn(new BatchJobData() { { setJobSequenceId("00000001"); setCurAppStatus( JobStatusConstants.JOB_STATUS_UNEXECUTION); } }); when(mockSystemDao.updateJobTable(any( BatchJobManagementUpdateParam.class))).thenReturn(1); when(mockTran.isCompleted()).thenReturn(true); assertTrue(jobStatusChanger.changeToStartStatus("00000001")); assertThat(logger.getLoggingEvents(), is(asList(debug( "[DAL025023] Try to update status jobSequenceId:00000001 changeStatus:1")))); verify(mockPlatformTransactionManager).commit(mockTran); verify(mockPlatformTransactionManager, never()).rollback(mockTran); }
@Test public void testChangeToStartStatus02() { TransactionStatus mockTran = mock(TransactionStatus.class); when(mockPlatformTransactionManager.getTransaction(any( DefaultTransactionDefinition.class))).thenReturn(mockTran); when(mockSystemDao.selectJob(any(BatchJobManagementParam.class))) .thenReturn(null); assertFalse(jobStatusChanger.changeToStartStatus("00000001")); assertThat(logger.getLoggingEvents(), is(asList(info( "[IAL025024] This job has already been started by another. jobSequenceId:00000001"), info("[IAL025023] Skipped processing of updating the job status. This transaction will be attempt to roll-back. jobSequenceId:00000001")))); verify(mockPlatformTransactionManager).rollback(mockTran); verify(mockPlatformTransactionManager, never()).commit(mockTran); }
@Test public void testChangeToStartStatus03() { TransactionStatus mockTran = mock(TransactionStatus.class); when(mockPlatformTransactionManager.getTransaction(any( DefaultTransactionDefinition.class))).thenReturn(mockTran); when(mockSystemDao.selectJob(any(BatchJobManagementParam.class))) .thenReturn(null); assertFalse(jobStatusChanger.changeToStartStatus(null)); assertThat(logger.getLoggingEvents(), is(asList(info( "[IAL025024] This job has already been started by another. jobSequenceId:null"), info("[IAL025023] Skipped processing of updating the job status. This transaction will be attempt to roll-back. jobSequenceId:null")))); verify(mockPlatformTransactionManager).rollback(mockTran); verify(mockPlatformTransactionManager, never()).commit(mockTran); }
@Test public void testChangeToStartStatus04() { TransactionStatus mockTran = mock(TransactionStatus.class); when(mockPlatformTransactionManager.getTransaction(any( DefaultTransactionDefinition.class))).thenReturn(mockTran); when(mockSystemDao.selectJob(any(BatchJobManagementParam.class))) .thenReturn(new BatchJobData() { { setJobSequenceId("00000001"); setCurAppStatus(JobStatusConstants.JOB_STATUS_FAILURE); } }); assertFalse(jobStatusChanger.changeToStartStatus("00000001")); assertThat(logger.getLoggingEvents(), is(asList(debug( "[DAL025055] This job status at the job control table is already updated by another worker. It will be skip. jobSequenceId:00000001 expectedCurAppStatus:0 actualCurAppStatus:3 changeTo:1"), info("[IAL025023] Skipped processing of updating the job status. This transaction will be attempt to roll-back. jobSequenceId:00000001")))); verify(mockPlatformTransactionManager).rollback(mockTran); verify(mockPlatformTransactionManager, never()).commit(mockTran); }
@Test public void testChangeToStartStatus05() { TransactionStatus mockTran = mock(TransactionStatus.class); when(mockPlatformTransactionManager.getTransaction(any( DefaultTransactionDefinition.class))).thenReturn(mockTran); when(mockSystemDao.selectJob(any(BatchJobManagementParam.class))) .thenReturn(new BatchJobData() { { setJobSequenceId("0000000001"); setCurAppStatus( JobStatusConstants.JOB_STATUS_UNEXECUTION); } }); when(mockSystemDao.updateJobTable(any( BatchJobManagementUpdateParam.class))).thenReturn(0); assertFalse(jobStatusChanger.changeToStartStatus("00000001")); assertThat(logger.getLoggingEvents(), is(asList(debug( "[DAL025023] Try to update status jobSequenceId:00000001 changeStatus:1"), error("[EAL025025] Job status update error. jobSequenceId:00000001 blogicStatus:null"), info("[IAL025023] Skipped processing of updating the job status. This transaction will be attempt to roll-back. jobSequenceId:00000001")))); verify(mockPlatformTransactionManager).rollback(mockTran); verify(mockPlatformTransactionManager, never()).commit(mockTran); }
@Test public void testChangeToStartStatus06() { Exception ex = new RuntimeException("test"); when(mockPlatformTransactionManager.getTransaction(any( DefaultTransactionDefinition.class))).thenThrow(ex); try { jobStatusChanger.changeToStartStatus("00000001"); fail(); } catch (Exception e) { assertSame(ex, e); verify(mockPlatformTransactionManager, never()).commit(any( TransactionStatus.class)); verify(mockPlatformTransactionManager, never()).rollback(any( TransactionStatus.class)); } }
@Test public void testChangeToStartStatus07() { TransactionStatus mockTran = mock(TransactionStatus.class); RuntimeException re = new RuntimeException("Test exception."); when(mockPlatformTransactionManager.getTransaction(any( DefaultTransactionDefinition.class))).thenReturn(mockTran); when(mockSystemDao.selectJob(any(BatchJobManagementParam.class))) .thenThrow(re); try { assertFalse(jobStatusChanger.changeToStartStatus("00000001")); fail(); } catch (Exception e) { assertSame(re, e); } assertThat(logger.getLoggingEvents(), is(asList(info( "[IAL025023] Skipped processing of updating the job status. This transaction will be attempt to roll-back. jobSequenceId:00000001")))); verify(mockPlatformTransactionManager, never()).commit(mockTran); verify(mockPlatformTransactionManager).rollback(mockTran); }
@Test public void testChangeToStartStatus08() { TransactionStatus mockTran = mock(TransactionStatus.class); RuntimeException re = new RuntimeException("Test exception."); when(mockPlatformTransactionManager.getTransaction(any( DefaultTransactionDefinition.class))).thenReturn(mockTran); when(mockSystemDao.selectJob(any(BatchJobManagementParam.class))) .thenReturn(new BatchJobData() { { setJobSequenceId("00000001"); setCurAppStatus( JobStatusConstants.JOB_STATUS_UNEXECUTION); } }); when(mockSystemDao.updateJobTable(any( BatchJobManagementUpdateParam.class))).thenThrow(re); try { assertFalse(jobStatusChanger.changeToStartStatus("00000001")); fail(); } catch (Exception e) { assertSame(re, e); } assertThat(logger.getLoggingEvents(), is(asList(debug( "[DAL025023] Try to update status jobSequenceId:00000001 changeStatus:1"), info("[IAL025023] Skipped processing of updating the job status. This transaction will be attempt to roll-back. jobSequenceId:00000001")))); verify(mockPlatformTransactionManager, never()).commit(mockTran); verify(mockPlatformTransactionManager).rollback(mockTran); }
@Test public void testChangeToStartStatus09() { TransactionStatus mockTran = mock(TransactionStatus.class); RuntimeException re = new RuntimeException("Test exception."); RuntimeException reAtRollback = new RuntimeException("Test exception at Rollback."); when(mockPlatformTransactionManager.getTransaction(any( DefaultTransactionDefinition.class))).thenReturn(mockTran); when(mockSystemDao.selectJob(any(BatchJobManagementParam.class))) .thenReturn(new BatchJobData() { { setJobSequenceId("00000001"); setCurAppStatus( JobStatusConstants.JOB_STATUS_UNEXECUTION); } }); when(mockSystemDao.updateJobTable(any( BatchJobManagementUpdateParam.class))).thenThrow(re); doThrow(reAtRollback).when(mockPlatformTransactionManager).rollback(any( TransactionStatus.class)); try { assertFalse(jobStatusChanger.changeToStartStatus("00000001")); fail(); } catch (Exception e) { assertSame(re, e); } assertThat(logger.getLoggingEvents(), is(asList(debug( "[DAL025023] Try to update status jobSequenceId:00000001 changeStatus:1"), info("[IAL025023] Skipped processing of updating the job status. This transaction will be attempt to roll-back. jobSequenceId:00000001"), error(reAtRollback, "[EAL025064] Failed to rollback transaction. jobSequenceId:00000001")))); verify(mockPlatformTransactionManager, never()).commit(mockTran); verify(mockPlatformTransactionManager).rollback(mockTran); }