use of com.google.cloud.websecurityscanner.v1beta.ScanRun in project java-websecurityscanner by googleapis.
the class VPCServiceControlPositiveTest method test.
private void test(final WebSecurityScannerClient wssClient, final String displayName) {
// This test performs following steps:
// 0. Create scan-config
// 1. Make sure that created scan-config is present
// 2. Start scan run on created scan-config
// 3. Make sure that created scan-run is present.
// 5. Stop the scan-run
// 6. Make sure that scan-run is stopped
// 7. Delete scan-config
// 8. Make sure that deleted scan-config is not present.
final String formattedParent = ProjectName.format(IN_VPCSC_PROJECT);
class TestResource implements AutoCloseable {
private WebSecurityScannerClient wssClient;
private final String displayName;
private ScanConfig createdScanConfig;
private ScanRun startedScanRun;
TestResource(WebSecurityScannerClient wssClient, String displayName) {
this.wssClient = wssClient;
this.displayName = displayName;
}
ScanConfig createScanConfig() {
ScanConfig scanConfig = ScanConfig.newBuilder().addAllStartingUrls(Lists.newArrayList(IN_VPCSC_HOSTNAME)).setDisplayName(displayName).build();
CreateScanConfigRequest request = CreateScanConfigRequest.newBuilder().setParent(formattedParent).setScanConfig(scanConfig).build();
createdScanConfig = wssClient.createScanConfig(request);
return createdScanConfig;
}
void deleteScanConfig() {
DeleteScanConfigRequest deleteScanConfigRequest = DeleteScanConfigRequest.newBuilder().setName(createdScanConfig.getName()).build();
wssClient.deleteScanConfig(deleteScanConfigRequest);
createdScanConfig = null;
}
ScanRun startScanRun() {
StartScanRunRequest startScanRunRequest = StartScanRunRequest.newBuilder().setName(createdScanConfig.getName()).build();
startedScanRun = wssClient.startScanRun(startScanRunRequest);
return startedScanRun;
}
ScanRun stopScanRun() {
StopScanRunRequest stopScanRunRequest = StopScanRunRequest.newBuilder().setName(startedScanRun.getName()).build();
ScanRun stoppedScanRun = wssClient.stopScanRun(stopScanRunRequest);
startedScanRun = null;
return stoppedScanRun;
}
@Override
public void close() {
if (startedScanRun != null) {
stopScanRun();
startedScanRun = null;
}
if (createdScanConfig != null) {
deleteScanConfig();
createdScanConfig = null;
}
}
}
try (TestResource testResource = new TestResource(wssClient, displayName)) {
ScanConfig responseScanConfig = testResource.createScanConfig();
assertEquals("Display name is response must be equal to display name in request", displayName, responseScanConfig.getDisplayName());
// Make sure that created scan config is present
GetScanConfigRequest scanConfigRequest = GetScanConfigRequest.newBuilder().setName(responseScanConfig.getName()).build();
ScanConfig obtainedScanConfig = wssClient.getScanConfig(scanConfigRequest);
assertEquals("Scan-Config name must be equal", responseScanConfig.getName(), obtainedScanConfig.getName());
ListScanConfigsRequest lscRequest = ListScanConfigsRequest.newBuilder().setParent(formattedParent).build();
boolean oneDisplayNameMatches = false;
for (ScanConfig sc : wssClient.listScanConfigs(lscRequest).iterateAll()) {
// since display name is never null so object equality can be used
if (displayName.equals(sc.getDisplayName())) {
oneDisplayNameMatches = true;
break;
}
}
assertTrue("One scan-config with " + displayName + " must be present", oneDisplayNameMatches);
ScanRun scanRunResponse = testResource.startScanRun();
assertTrue("Scan-run name must not be empty", isNotEmpty(scanRunResponse.getName()));
assertNotEquals("Scan-run state must not be KILLED", ResultState.KILLED, scanRunResponse.getResultState());
ScanRun stoppedScanRun = testResource.stopScanRun();
assertEquals("Result state must be killed", ResultState.KILLED, stoppedScanRun.getResultState());
testResource.deleteScanConfig();
// make sure that deleted scan-config do not exist
GetScanConfigRequest getScanConfigRequest = GetScanConfigRequest.newBuilder().setName(responseScanConfig.getName()).build();
try {
ScanConfig deletedScanConfig = wssClient.getScanConfig(getScanConfigRequest);
fail("NotFoundException must be throw as scan-config must already be deleted");
} catch (NotFoundException expected) {
}
}
}
Aggregations