CallbackServiceImpl implements CallbackService { @Override public void updateScan(Scan scan, ScanStatus scanStatus, String scannerScanId, String scanReportPath) throws InvalidRequestException { synchronized (Constants.LOCK) { Scan newScanObject = scanService.getByJobId(scan.getJobId()); switch (scanStatus) { case RUNNING: if (newScanObject.getStatus() == ScanStatus.SUBMITTED) { Timestamp timestamp = new Timestamp(System.currentTimeMillis()); newScanObject.setStartTimestamp(timestamp); if (StringUtils.isNotBlank(scannerScanId)) { newScanObject.setScannerScanId(scannerScanId); } else { throw new InvalidRequestException("Scanner scan id cannot be found"); } newScanObject.setStatus(scanStatus); } break; case COMPLETED: if (StringUtils.isNotBlank(scanReportPath)) { newScanObject.setReportPath(scanReportPath); } else { throw new InvalidRequestException("Scan report path cannot be found"); } newScanObject.setStatus(scanStatus); scanEngineService.removeContainer(newScanObject); new Thread(() -> scanEngineService.beginPendingScans(), "BeginPendingScansFromCallback").start(); break; case ERROR: case CANCELED: newScanObject.setStatus(scanStatus); scanEngineService.removeContainer(newScanObject); new Thread(() -> scanEngineService.beginPendingScans(), "BeginPendingScansFromCallback").start(); break; default: throw new InvalidRequestException("Unsupported scan status: " + scanStatus); } scanService.update(newScanObject); } } @Autowired  CallbackServiceImpl(ScanEngineService scanEngineService, ScanService scanService); @Override void updateScan(Scan scan, ScanStatus scanStatus, String scannerScanId, String scanReportPath);  }
@Test(dataProvider = "getScanData", dataProviderClass = ServiceTestDataProvider.class) public void testUpdateScanWithRunningStatus(Scan scan) throws InvalidRequestException { Mockito.when(scanService.getByJobId(scan.getJobId())).thenReturn(scan); Mockito.when(scanService.update(Mockito.any(Scan.class))).thenReturn(scan); callbackService.updateScan(scan, ScanStatus.RUNNING, TEST_SCANNER_SCAN_ID, null); ArgumentCaptor<Scan> scanUpdateCapture = ArgumentCaptor.forClass(Scan.class); Mockito.verify(scanService, Mockito.atLeastOnce()).update(scanUpdateCapture.capture()); Scan updatedScan = scanUpdateCapture.getValue(); Assert.assertNotNull(updatedScan); Assert.assertEquals(ScanStatus.RUNNING, updatedScan.getStatus()); Assert.assertNotNull(updatedScan.getStartTimestamp()); Assert.assertEquals(TEST_SCANNER_SCAN_ID, updatedScan.getScannerScanId()); }
@Test(dataProvider = "getScanEngineData", dataProviderClass = ServiceTestDataProvider.class) public void testUpdateScanWithCompletedStatus(Scan scan, Container container) throws InvalidRequestException { Mockito.when(scanService.getByJobId(scan.getJobId())).thenReturn(scan); Mockito.doNothing().when(scanEngineService).beginPendingScans(); Mockito.when(scanEngineService.removeContainer(scan)).thenReturn(container); Mockito.when(scanService.update(Mockito.any(Scan.class))).thenReturn(scan); callbackService.updateScan(scan, ScanStatus.COMPLETED, null, TEST_SCAN_REPORT_PATH); ArgumentCaptor<Scan> scanUpdateCapture = ArgumentCaptor.forClass(Scan.class); Mockito.verify(scanService, Mockito.atLeastOnce()).update(scanUpdateCapture.capture()); Scan updatedScan = scanUpdateCapture.getValue(); Assert.assertNotNull(updatedScan); Assert.assertEquals(ScanStatus.COMPLETED, updatedScan.getStatus()); Assert.assertEquals(TEST_SCAN_REPORT_PATH, updatedScan.getReportPath()); }
@Test(dataProvider = "getScanEngineData", dataProviderClass = ServiceTestDataProvider.class) public void testUpdateScanWithErrorStatus(Scan scan, Container container) throws InvalidRequestException { Mockito.when(scanService.getByJobId(scan.getJobId())).thenReturn(scan); Mockito.doNothing().when(scanEngineService).beginPendingScans(); Mockito.when(scanEngineService.removeContainer(scan)).thenReturn(container); Mockito.when(scanService.update(Mockito.any(Scan.class))).thenReturn(scan); callbackService.updateScan(scan, ScanStatus.ERROR, null, null); ArgumentCaptor<Scan> scanUpdateCapture = ArgumentCaptor.forClass(Scan.class); Mockito.verify(scanService, Mockito.atLeastOnce()).update(scanUpdateCapture.capture()); Scan updatedScan = scanUpdateCapture.getValue(); Assert.assertNotNull(updatedScan); Assert.assertEquals(ScanStatus.ERROR, updatedScan.getStatus()); }
@Test(dataProvider = "getScanEngineData", dataProviderClass = ServiceTestDataProvider.class) public void testUpdateScanWithCanceledStatus(Scan scan, Container container) throws InvalidRequestException { Mockito.when(scanService.getByJobId(scan.getJobId())).thenReturn(scan); Mockito.doNothing().when(scanEngineService).beginPendingScans(); Mockito.when(scanEngineService.removeContainer(scan)).thenReturn(container); Mockito.when(scanService.update(Mockito.any(Scan.class))).thenReturn(scan); callbackService.updateScan(scan, ScanStatus.CANCELED, null, null); ArgumentCaptor<Scan> scanUpdateCapture = ArgumentCaptor.forClass(Scan.class); Mockito.verify(scanService, Mockito.atLeastOnce()).update(scanUpdateCapture.capture()); Scan updatedScan = scanUpdateCapture.getValue(); Assert.assertNotNull(updatedScan); Assert.assertEquals(ScanStatus.CANCELED, updatedScan.getStatus()); }