ScanEngineServiceImpl implements ScanEngineService { @Override public void cancelScan(Scan scan) throws ScanManagerException { synchronized (Constants.LOCK) { Scan newScanObject = scanService.getByJobId(scan.getJobId()); if (newScanObject.getStatus() == ScanStatus.SUBMIT_PENDING || newScanObject.getStatus() == ScanStatus.SUBMITTED || newScanObject.getStatus() == ScanStatus.RUNNING) { try { List<Container> containerInfos = dockerHandler.list(); boolean isContainerFound = false; for (Container containerInfo : containerInfos) { Map<String, String> labels = containerInfo.getLabels(); if (labels != null && labels.containsKey(CONTAINER_SCAN_JOB_ID_LABEL_NAME) && labels.get(CONTAINER_SCAN_JOB_ID_LABEL_NAME).equals(scan.getJobId())) { isContainerFound = true; URI uri = buildScannerScanURI(containerInfo); Map<String, Object> requestParams = new HashMap<>(); requestParams.put(JOB_ID_PARAMETER_NAME, scan.getJobId()); requestParams.put(SCANNER_APP_ID_PARAMETER_NAME, labels.get(CONTAINER_APP_LABEL_NAME)); MultiValueMap<String, String> requestHeaders = new LinkedMultiValueMap<>(); HTTPRequest scanCancelRequest = new HTTPRequest(uri.toString(), requestHeaders, requestParams); ResponseEntity response = HTTPUtil.sendDELETE(scanCancelRequest); if (response.getStatusCode().isError()) { throw new ScanManagerException("Unable to submit the cancel scan request"); } scanService.updateStatus(scan.getJobId(), ScanStatus.CANCEL_PENDING); logService.insert(newScanObject, LogType.INFO, "Cancel scan request submitted"); break; } } if (!isContainerFound) { scanService.updateStatus(scan.getJobId(), ScanStatus.CANCELED); logService.insert(newScanObject, LogType.INFO, "Scan cancelled"); } } catch (RestClientException | ScanManagerException e) { logService.insertError(newScanObject, e); throw new ScanManagerException("Error occurred while cancelling the scan", e); } } } } @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 testCancelScan(Object scanDataObject, Object containerDataObject) { try { Scan scan = ServiceTestDataProvider.parseScanObject(scanDataObject); Container container = ServiceTestDataProvider.parseContainerObject(containerDataObject); if (scan != null && container != null) { Mockito.when(scanService.getByJobId(scan.getJobId())).thenReturn(scan); PowerMockito.mockStatic(HTTPUtil.class); PowerMockito.when(HTTPUtil.sendDELETE(Matchers.any(HTTPRequest.class))) .thenReturn(new ResponseEntity(HttpStatus.OK)); PowerMockito.when(containerHandler.list()).thenReturn(Collections.singletonList(container)); Mockito.doNothing().when(scanService).updateStatus(scan.getJobId(), ScanStatus.CANCEL_PENDING); scanEngineService.cancelScan(scan); PowerMockito.verifyStatic(); ArgumentCaptor<HTTPRequest> requestParamsCaptor = ArgumentCaptor.forClass(HTTPRequest.class); HTTPUtil.sendDELETE(requestParamsCaptor.capture()); HTTPRequest requestParam = requestParamsCaptor.getValue(); Assert.assertNotNull(requestParam); Assert.assertEquals(scan.getJobId(), requestParam.getRequestParams().get(JOB_ID_PARAMETER_NAME)); Assert.assertEquals(scan.getScannerAppId(), requestParam.getRequestParams().get(SCANNER_APP_ID_PARAMETER_NAME)); } else { throw new ScanManagerException("Unable to get the scan test data"); } } catch (ScanManagerException e) { Assert.fail(e.getMessage()); } }