JobStatusChangerImpl implements JobStatusChanger { @Override public boolean changeToEndStatus(String jobSequenceId, BLogicResult blogicResult) { TransactionStatus transactionStatus = null; try { transactionStatus = adminTransactionManager.getTransaction( new DefaultTransactionDefinition()); BatchJobData batchJobData = getBatchJobData(jobSequenceId); if (!isJobStatusValid(batchJobData, JOB_STATUS_EXECUTING, JOB_STATUS_PROCESSED)) { return false; } String appStatus = Integer.toString(blogicResult.getBlogicStatus()); if (!updateBatchJobStatus(jobSequenceId, appStatus, JOB_STATUS_PROCESSED)) { 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 testChangeToEndStatus01() { BLogicResult blogicResult = new BLogicResult(); 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_EXECUTING); } }); when(mockSystemDao.updateJobTable(any( BatchJobManagementUpdateParam.class))).thenReturn(1); when(mockTran.isCompleted()).thenReturn(true); assertTrue(jobStatusChanger.changeToEndStatus("00000001", blogicResult)); assertThat(logger.getLoggingEvents(), is(asList(debug( "[DAL025023] Try to update status jobSequenceId:00000001 changeStatus:2")))); verify(mockPlatformTransactionManager).commit(mockTran); verify(mockPlatformTransactionManager, never()).rollback(mockTran); }
@Test public void testChangeToEndStatus02() { BLogicResult blogicResult = new BLogicResult(); TransactionStatus mockTran = mock(TransactionStatus.class); when(mockPlatformTransactionManager.getTransaction(any( DefaultTransactionDefinition.class))).thenReturn(mockTran); when(mockSystemDao.selectJob(any(BatchJobManagementParam.class))) .thenReturn(null); assertFalse(jobStatusChanger.changeToEndStatus("00000001", blogicResult)); 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); }
@Test public void testChangeToEndStatus03() { BLogicResult blogicResult = new BLogicResult(); TransactionStatus mockTran = mock(TransactionStatus.class); when(mockPlatformTransactionManager.getTransaction(any( DefaultTransactionDefinition.class))).thenReturn(mockTran); when(mockSystemDao.selectJob(any(BatchJobManagementParam.class))) .thenReturn(null); assertFalse(jobStatusChanger.changeToEndStatus(null, blogicResult)); 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); }
@Test public void testChangeToEndStatus04() { 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_EXECUTING); } }); try { jobStatusChanger.changeToEndStatus("00000001", null); fail(); } catch (Exception e) { assertTrue(e instanceof NullPointerException); } 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).rollback(mockTran); }
@Test public void testChangeToEndStatus05() { BLogicResult blogicResult = new BLogicResult(); 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_PROCESSED); } }); assertFalse(jobStatusChanger.changeToEndStatus("00000001", blogicResult)); 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:1 actualCurAppStatus:2 changeTo:2"), info("[IAL025023] Skipped processing of updating the job status. This transaction will be attempt to roll-back. jobSequenceId:00000001")))); verify(mockPlatformTransactionManager).rollback(mockTran); }
@Test public void testChangeToEndStatus06() { BLogicResult blogicResult = new BLogicResult(); 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_EXECUTING); } }); assertFalse(jobStatusChanger.changeToEndStatus("00000001", blogicResult)); assertThat(logger.getLoggingEvents(), is(asList(debug( "[DAL025023] Try to update status jobSequenceId:00000001 changeStatus:2"), error("[EAL025025] Job status update error. jobSequenceId:00000001 blogicStatus:255"), info("[IAL025023] Skipped processing of updating the job status. This transaction will be attempt to roll-back. jobSequenceId:00000001")))); verify(mockPlatformTransactionManager).rollback(mockTran); }
@Test public void testChangeToEndStatus07() { BLogicResult blogicResult = new BLogicResult(); Exception ex = new RuntimeException("test"); when(mockPlatformTransactionManager.getTransaction(any( DefaultTransactionDefinition.class))).thenThrow(ex); try { jobStatusChanger.changeToEndStatus("00000001", blogicResult); fail(); } catch (Exception e) { assertSame(ex, e); verify(mockPlatformTransactionManager, never()).commit(any( TransactionStatus.class)); verify(mockPlatformTransactionManager, never()).rollback(any( TransactionStatus.class)); } }
@Test public void testChangeToEndStatus08() { BLogicResult blogicResult = new BLogicResult(); 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.changeToEndStatus("00000001", blogicResult)); 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 testChangeToEndStatus09() { BLogicResult blogicResult = new BLogicResult(); TransactionStatus mockTran = mock(TransactionStatus.class); RuntimeException runtimeException = new RuntimeException("Test exception."); when(mockPlatformTransactionManager.getTransaction(any( DefaultTransactionDefinition.class))).thenReturn(mockTran); when(mockSystemDao.selectJob(any(BatchJobManagementParam.class))) .thenReturn(new BatchJobData() { { setJobSequenceId("0000000001"); setCurAppStatus( JobStatusConstants.JOB_STATUS_EXECUTING); } }); when(mockSystemDao.updateJobTable(any( BatchJobManagementUpdateParam.class))).thenThrow( runtimeException); try { assertFalse(jobStatusChanger.changeToEndStatus("00000001", blogicResult)); fail(); } catch (Exception e) { assertSame(runtimeException, e); } assertThat(logger.getLoggingEvents(), is(asList(debug( "[DAL025023] Try to update status jobSequenceId:00000001 changeStatus:2"), 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 testChangeToEndStatus10() { BLogicResult blogicResult = new BLogicResult(); 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("0000000001"); setCurAppStatus( JobStatusConstants.JOB_STATUS_EXECUTING); } }); when(mockSystemDao.updateJobTable(any( BatchJobManagementUpdateParam.class))).thenThrow(re); doThrow(reAtRollback).when(mockPlatformTransactionManager).rollback(any( TransactionStatus.class)); try { assertFalse(jobStatusChanger.changeToEndStatus("00000001", blogicResult)); fail(); } catch (Exception e) { assertSame(re, e); } assertThat(logger.getLoggingEvents(), is(asList(debug( "[DAL025023] Try to update status jobSequenceId:00000001 changeStatus:2"), 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); }