ScanEngineServiceImpl implements ScanEngineService { @Override public void beginPendingScans() { scanService.getPendingScans(ScanStatus.SUBMIT_PENDING).forEach(scan -> beginScan(scan)); } @Autowired  ScanEngineServiceImpl(ScanService scanService, ScannerService scannerService, LogService logService,
                                 ContainerHandler dockerHandler); @Override void beginPendingScans(); @Override void cancelScan(Scan scan); @Override Container removeContainer(Scan scan);  }
@Test(dataProvider = "getScanEngineData", dataProviderClass = ServiceTestDataProvider.class) public void testBeginPendingScans(Object scanDataObject, Object containerDataObject) { try { Scan scan = ServiceTestDataProvider.parseScanObject(scanDataObject); Container container = ServiceTestDataProvider.parseContainerObject(containerDataObject); if (scan != null && container != null) { scan.setStatus(ScanStatus.SUBMIT_PENDING); Mockito.when(scanService.getPendingScans(ScanStatus.SUBMIT_PENDING)) .thenReturn(Collections.singletonList(scan)); Mockito.when(scanService.getByJobId(scan.getJobId())).thenReturn(scan); Scan existingTestScan = new Scan(); existingTestScan.setScannerAppId(OCCUPIED_SCANNER_APP_ID); Mockito.when(scanService.getByStatusesAndScannerAndProduct(new ArrayList<>( Arrays.asList(ScanStatus.SUBMITTED, ScanStatus.RUNNING, ScanStatus.CANCEL_PENDING)), scan.getScanner(), scan.getProduct())).thenReturn(Collections.singletonList(existingTestScan)); List<ScannerApp> testScannerAppList = Arrays.asList(new ScannerApp(scan.getScanner(), OCCUPIED_SCANNER_APP_ID, OCCUPIED_SCANNER_APP_ID, scan.getProduct()), new ScannerApp(scan.getScanner(), SCANNER_APP_ID, SCANNER_APP_NAME, scan.getProduct())); Mockito.when(scannerService.getAppsByScannerAndAssignedProduct(scan.getScanner(), scan.getProduct())).thenReturn(testScannerAppList); Mockito.when(scanService.update(Mockito.any(Scan.class))).thenReturn(scan); Mockito.when(containerHandler.create(scan.getScanner().getImage(), ScanManagerConfiguration .getInstance().getScannerServiceHost(), ScanManagerConfiguration.getInstance() .getScannerServicePort(), container.getLabels(), new ArrayList<>(), new String[]{CONTAINER_ENV_NAME_SCAN_MANAGER_HOST + "=" + ScanManagerConfiguration .getInstance().getScanManagerHost(), CONTAINER_ENV_NAME_SCAN_MANAGER_PORT + "=" + ScanManagerConfiguration .getInstance().getScanManagerPort()})).thenReturn(container); Mockito.doNothing().when(containerHandler).start(container.getId()); PowerMockito.mockStatic(HTTPUtil.class); PowerMockito.when(HTTPUtil.sendPOST(Matchers.any(HTTPRequest.class))) .thenReturn(new ResponseEntity(HttpStatus.OK)); scanEngineService.beginPendingScans(); ArgumentCaptor<HTTPRequest> startScanRequestCaptor = ArgumentCaptor.forClass(HTTPRequest.class); PowerMockito.verifyStatic(); HTTPUtil.sendPOST(startScanRequestCaptor.capture()); HTTPRequest startScanRequest = startScanRequestCaptor.getValue(); Assert.assertNotNull(startScanRequest); Assert.assertEquals(scan.getJobId(), startScanRequest.getRequestParams().get(JOB_ID_PARAMETER_NAME)); Assert.assertEquals(scan.getScannerAppId(), startScanRequest.getRequestParams().get(SCANNER_APP_ID_PARAMETER_NAME)); Map<String, List<String>> fileMap = (Map<String, List<String>>) startScanRequest.getRequestParams().get(FILE_MAP_PARAMETER_NAME); Map<String, List<String>> propertyMap = (Map<String, List<String>>) startScanRequest.getRequestParams() .get(PROPERTY_MAP_PARAMETER_NAME); scan.getFileList().forEach(scanFile -> Assert.assertTrue(fileMap .get(scanFile.getName()).contains(scanFile.getLocation()))); scan.getPropertyList().forEach(scanProperty -> Assert.assertTrue(propertyMap .get(scanProperty.getName()).contains(scanProperty.getValue()))); } else { throw new ScanManagerException("Unable to get the scan test data"); } } catch (ScanManagerException e) { Assert.fail(e.getMessage()); } }