use of com.hp.octane.integrations.testhelpers.OctaneSPEndpointSimulator in project octane-ci-java-sdk by MicroFocus.
the class SSCIntegrationTest method initSPEPSimulatorsForSSC.
private Map<String, OctaneSPEndpointSimulator> initSPEPSimulatorsForSSC(Set<String> spIDs, Map<String, List<String>> preflightRequestsCollectors, Map<String, List<String>> pushRequestCollectors) {
Map<String, OctaneSPEndpointSimulator> result = new LinkedHashMap<>();
for (String spID : spIDs) {
OctaneSPEndpointSimulator simulator = OctaneSPEndpointSimulator.addInstance(spID);
// vulnerabilities preflight API
simulator.installApiHandler(HttpMethod.GET, "^.*/vulnerabilities/preflight$", request -> {
try {
// retrieve query parameters
request.mergeQueryParameters("", request.getQueryString(), false);
preflightRequestsCollectors.computeIfAbsent(spID, sid -> new LinkedList<>()).add(request.getQueryParameters().getString("instance-id") + "|" + request.getQueryParameters().getString("job-ci-id") + "|" + request.getQueryParameters().getString("build-ci-id"));
request.getResponse().setStatus(HttpStatus.SC_OK);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtils.octaneFormat);
request.getResponse().getWriter().write(getOctaneInput().baseline == null ? "true" : simpleDateFormat.format(getOctaneInput().baseline));
request.getResponse().getWriter().flush();
} catch (IOException ioe) {
throw new OctaneSDKGeneralException("failed to write response", ioe);
}
});
// vulnerabilities push API
simulator.installApiHandler(HttpMethod.POST, "^.*/vulnerabilities$", request -> {
try {
String rawVulnerabilitiesBody = CIPluginSDKUtils.inputStreamToUTF8String(new GZIPInputStream(request.getInputStream()));
pushRequestCollectors.computeIfAbsent(spID, sid -> new LinkedList<>()).add(rawVulnerabilitiesBody);
request.getResponse().setStatus(HttpStatus.SC_ACCEPTED);
request.getResponse().getWriter().write("{\"status\": \"queued\"}");
request.getResponse().getWriter().flush();
} catch (IOException ioe) {
throw new OctaneSDKGeneralException("failed to write response", ioe);
}
});
// vulnerabilities push API
simulator.installApiHandler(HttpMethod.GET, "^.*/vulnerabilities/remote-issue-ids.*", request -> {
try {
request.getResponse().setStatus(HttpStatus.SC_OK);
request.getResponse().getWriter().write(SSCTestUtils.getJson(this.getOctaneInput().remoteIds));
request.getResponse().getWriter().flush();
} catch (IOException ioe) {
throw new OctaneSDKGeneralException("failed to write response", ioe);
}
});
result.put(spID, simulator);
}
return result;
}
use of com.hp.octane.integrations.testhelpers.OctaneSPEndpointSimulator in project octane-ci-java-sdk by MicroFocus.
the class VulnerabilitiesServiceFunctionalityTest method testVulnerabilitiesFunctional.
@Test(timeout = 20000)
public void testVulnerabilitiesFunctional() {
Map<String, OctaneSPEndpointSimulator> simulators = null;
try {
String spIdA = UUID.randomUUID().toString();
String spIdB = UUID.randomUUID().toString();
String clientAInstanceId = UUID.randomUUID().toString();
String clientBInstanceId = UUID.randomUUID().toString();
Map<String, List<String>> preflightRequestCollectors = new LinkedHashMap<>();
Map<String, List<String>> pushVulnerabilitiesCollectors = new LinkedHashMap<>();
// init 2 shared space endpoints simulators
simulators = initSPEPSimulators(Stream.of(spIdA, spIdB).collect(Collectors.toSet()), preflightRequestCollectors, pushVulnerabilitiesCollectors);
//
// I
// add one client and verify it works okay
//
OctaneConfiguration configA = new OctaneConfigurationIntern(clientAInstanceId, OctaneSPEndpointSimulator.getSimulatorUrl(), spIdA);
OctaneClient clientA = OctaneSDK.addClient(configA, VulnerabilitiesServicePluginServicesTest.class);
VulnerabilitiesService vulnerabilitiesServiceA = clientA.getVulnerabilitiesService();
Assert.assertFalse(preflightRequestCollectors.containsKey(spIdA));
Assert.assertFalse(preflightRequestCollectors.containsKey(spIdB));
//
// II
// add one more client and verify they are both works okay
//
OctaneConfiguration configB = new OctaneConfigurationIntern(clientBInstanceId, OctaneSPEndpointSimulator.getSimulatorUrl(), spIdB);
OctaneClient clientB = OctaneSDK.addClient(configB, VulnerabilitiesServicePluginServicesTest.class);
VulnerabilitiesService vulnerabilitiesServiceB = clientB.getVulnerabilitiesService();
vulnerabilitiesServiceA.enqueueRetrieveAndPushVulnerabilities("job-preflight-true", "1", ToolType.SSC, System.currentTimeMillis(), 1, null, null);
vulnerabilitiesServiceA.enqueueRetrieveAndPushVulnerabilities("job-preflight-false", "1", ToolType.SSC, System.currentTimeMillis(), 1, null, null);
vulnerabilitiesServiceB.enqueueRetrieveAndPushVulnerabilities("job-preflight-true", "1", ToolType.SSC, System.currentTimeMillis(), 1, null, null);
vulnerabilitiesServiceB.enqueueRetrieveAndPushVulnerabilities("job-preflight-false", "1", ToolType.SSC, System.currentTimeMillis(), 1, null, null);
GeneralTestUtils.waitAtMostFor(12000, () -> {
if (preflightRequestCollectors.get(spIdA) != null && preflightRequestCollectors.get(spIdA).size() == 2 && preflightRequestCollectors.get(spIdB) != null && preflightRequestCollectors.get(spIdB).size() == 2) {
return true;
} else {
return null;
}
});
Assert.assertEquals(clientAInstanceId + "|job-preflight-true|1", preflightRequestCollectors.get(spIdA).get(0));
Assert.assertEquals(clientAInstanceId + "|job-preflight-false|1", preflightRequestCollectors.get(spIdA).get(1));
Assert.assertEquals(clientBInstanceId + "|job-preflight-true|1", preflightRequestCollectors.get(spIdB).get(0));
Assert.assertEquals(clientBInstanceId + "|job-preflight-false|1", preflightRequestCollectors.get(spIdB).get(1));
//
// III
// remove one client and verify it is shut indeed and the second continue to work okay
//
OctaneSDK.removeClient(clientA);
//
// IV
// remove second client and ensure no interactions anymore
//
OctaneSDK.removeClient(clientB);
} finally {
// remove clients
OctaneSDK.getClients().forEach(OctaneSDK::removeClient);
// remove simulators
if (simulators != null)
removeSPEPSimulators(simulators.values());
SSCServerSimulator.instance().endSimulation();
}
}
use of com.hp.octane.integrations.testhelpers.OctaneSPEndpointSimulator in project octane-ci-java-sdk by MicroFocus.
the class VulnerabilitiesServiceFunctionalityTest method initSPEPSimulatorsForSSC.
private Map<String, OctaneSPEndpointSimulator> initSPEPSimulatorsForSSC(Set<String> spIDs, Map<String, List<String>> preflightRequestsCollectors, Map<String, List<String>> pushRequestCollectors) {
Map<String, OctaneSPEndpointSimulator> result = new LinkedHashMap<>();
for (String spID : spIDs) {
OctaneSPEndpointSimulator simulator = OctaneSPEndpointSimulator.addInstance(spID);
// vulnerabilities preflight API
simulator.installApiHandler(HttpMethod.GET, "^.*/vulnerabilities/preflight$", request -> {
try {
// retrieve query parameters
request.mergeQueryParameters("", request.getQueryString(), false);
preflightRequestsCollectors.computeIfAbsent(spID, sid -> new LinkedList<>()).add(request.getQueryParameters().getString("instance-id") + "|" + request.getQueryParameters().getString("job-ci-id") + "|" + request.getQueryParameters().getString("build-ci-id"));
request.getResponse().setStatus(HttpStatus.SC_OK);
request.getResponse().getWriter().write("true");
request.getResponse().getWriter().flush();
} catch (IOException ioe) {
throw new OctaneSDKGeneralException("failed to write response", ioe);
}
});
// vulnerabilities push API
simulator.installApiHandler(HttpMethod.POST, "^.*/vulnerabilities$", request -> {
try {
String rawVulnerabilitiesBody = CIPluginSDKUtils.inputStreamToUTF8String(new GZIPInputStream(request.getInputStream()));
pushRequestCollectors.computeIfAbsent(spID, sid -> new LinkedList<>()).add(rawVulnerabilitiesBody);
request.getResponse().setStatus(HttpStatus.SC_ACCEPTED);
request.getResponse().getWriter().write("{\"status\": \"queued\"}");
request.getResponse().getWriter().flush();
} catch (IOException ioe) {
throw new OctaneSDKGeneralException("failed to write response", ioe);
}
});
result.put(spID, simulator);
}
return result;
}
use of com.hp.octane.integrations.testhelpers.OctaneSPEndpointSimulator in project octane-ci-java-sdk by MicroFocus.
the class VulnerabilitiesServiceFunctionalityTest method testVulnerabilitiesFunctionalSSC.
@Test(timeout = 20000)
public void testVulnerabilitiesFunctionalSSC() {
Map<String, OctaneSPEndpointSimulator> simulators = null;
try {
String spIdA = UUID.randomUUID().toString();
String clientAInstanceId = UUID.randomUUID().toString();
Map<String, List<String>> preflightRequestCollectors = new LinkedHashMap<>();
Map<String, List<String>> pushVulnerabilitiesCollectors = new LinkedHashMap<>();
simulators = initSPEPSimulatorsForSSC(Stream.of(spIdA).collect(Collectors.toSet()), preflightRequestCollectors, pushVulnerabilitiesCollectors);
//
// I
// add one client and verify it works okay
//
OctaneConfiguration configA = new OctaneConfigurationIntern(clientAInstanceId, OctaneSPEndpointSimulator.getSimulatorUrl(), spIdA);
OctaneClient clientA = OctaneSDK.addClient(configA, VulnerabilitiesServicePluginServicesTest.class);
VulnerabilitiesService vulnerabilitiesServiceA = clientA.getVulnerabilitiesService();
prepareSSCSimulator();
vulnerabilitiesServiceA.enqueueRetrieveAndPushVulnerabilities("jobSSC1", "1", ToolType.SSC, System.currentTimeMillis(), 1, null, null);
GeneralTestUtils.waitAtMostFor(12000, () -> {
if (preflightRequestCollectors.get(spIdA) != null && preflightRequestCollectors.get(spIdA).size() == 1) {
return true;
} else {
return null;
}
});
Assert.assertEquals(clientAInstanceId + "|jobSSC1|1", preflightRequestCollectors.get(spIdA).get(0));
OctaneSDK.removeClient(clientA);
} finally {
// remove clients
OctaneSDK.getClients().forEach(OctaneSDK::removeClient);
// remove simulators
if (simulators != null)
removeSPEPSimulators(simulators.values());
SSCServerSimulator.instance().endSimulation();
}
}
use of com.hp.octane.integrations.testhelpers.OctaneSPEndpointSimulator in project octane-ci-java-sdk by MicroFocus.
the class OctaneSDKBasicFunctionalityTest method initSPEPSimulators.
private Map<String, OctaneSPEndpointSimulator> initSPEPSimulators(Set<String> spIDs, Map<String, List<CIEventsList>> eventsCollectors, Map<String, List<TestsResult>> testResultsCollectors, Map<String, List<String>> logsCollectors, Map<String, List<String>> coverageCollectors) {
Map<String, OctaneSPEndpointSimulator> result = new LinkedHashMap<>();
for (String spID : spIDs) {
OctaneSPEndpointSimulator simulator = OctaneSPEndpointSimulator.addInstance(spID);
// for octane roots
simulator.setOctaneVersion("15.1.8");
// events API
simulator.installApiHandler(HttpMethod.PUT, "^.*events$", request -> {
try {
String rawEventsBody = CIPluginSDKUtils.inputStreamToUTF8String(new GZIPInputStream(request.getInputStream()));
CIEventsList eventsList = dtoFactory.dtoFromJson(rawEventsBody, CIEventsList.class);
eventsCollectors.computeIfAbsent(spID, sp -> new LinkedList<>()).add(eventsList);
request.getResponse().setStatus(HttpStatus.SC_OK);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
});
// test results preflight API
simulator.installApiHandler(HttpMethod.GET, "^.*tests-result-preflight$", request -> {
try {
request.getResponse().setStatus(HttpStatus.SC_OK);
request.getResponse().getWriter().write("true");
request.getResponse().getWriter().flush();
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
});
// test results push API
simulator.installApiHandler(HttpMethod.POST, "^.*test-results$", request -> {
try {
String rawTestResultBody = CIPluginSDKUtils.inputStreamToUTF8String(new GZIPInputStream(request.getInputStream()));
TestsResult testsResult = dtoFactory.dtoFromXml(rawTestResultBody, TestsResult.class);
// [YG] below validations are done to ensure NEW API (via query params) aligned with an OLD API (data within XML)
// [YG] in the future we'll remove OLD API and this validation should be done differently
request.mergeQueryParameters("", request.getQueryString(), false);
Assert.assertEquals(request.getQueryParameters().getString("instance-id"), testsResult.getBuildContext().getServerId());
Assert.assertEquals(request.getQueryParameters().getString("job-ci-id"), testsResult.getBuildContext().getJobId());
Assert.assertEquals(request.getQueryParameters().getString("build-ci-id"), testsResult.getBuildContext().getBuildId());
testResultsCollectors.computeIfAbsent(spID, sp -> new LinkedList<>()).add(testsResult);
request.getResponse().setStatus(HttpStatus.SC_ACCEPTED);
request.getResponse().getWriter().write("{\"status\": \"queued\"}");
request.getResponse().getWriter().flush();
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
});
// logs/coverage preflight API
simulator.installApiHandler(HttpMethod.GET, "^.*workspaceId$", request -> {
try {
request.getResponse().setStatus(HttpStatus.SC_OK);
request.getResponse().getWriter().write("[\"1001\"]");
request.getResponse().getWriter().flush();
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
});
// logs push API
simulator.installApiHandler(HttpMethod.POST, "^.*logs$", request -> {
try {
String rawLogBody = CIPluginSDKUtils.inputStreamToUTF8String(new GZIPInputStream(request.getInputStream()));
logsCollectors.computeIfAbsent(spID, sp -> new LinkedList<>()).add(rawLogBody);
request.getResponse().setStatus(HttpStatus.SC_OK);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
});
// coverage preflight API
// no need to configure, since it's the same API as for logs, see above
// coverage push API
simulator.installApiHandler(HttpMethod.PUT, "^.*coverage$", request -> {
try {
String rawCoverageBody = CIPluginSDKUtils.inputStreamToUTF8String(new GZIPInputStream(request.getInputStream()));
coverageCollectors.computeIfAbsent(spID, sp -> new LinkedList<>()).add(rawCoverageBody);
request.getResponse().setStatus(HttpStatus.SC_OK);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
});
// get roots
simulator.installApiHandler(HttpMethod.GET, "^.*pipeline-roots$", request -> {
try {
request.getResponse().setStatus(HttpStatus.SC_OK);
request.getResponse().getWriter().write("[]");
request.getResponse().getWriter().flush();
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
});
result.put(spID, simulator);
}
return result;
}
Aggregations