use of org.jboss.pnc.spi.coordinator.Result in project pnc by project-ncl.
the class TemporaryBuildsCleanerAsyncInvoker method deleteTemporaryBuild.
/**
* Deletes a single temporary build.
*
* @param buildRecordId ID of the build to be deleted
* @param authToken Bearer token
* @param onComplete Operation to be executed after deletion operation
* @return True if the build exists and deletion started otherwise, false is build doesn't exist
* @throws ValidationException Thrown when build cannot be deleted
*/
public boolean deleteTemporaryBuild(Base32LongID buildRecordId, String authToken, Consumer<Result> onComplete) throws ValidationException {
BuildRecord buildRecord = buildRecordRepository.findByIdFetchAllProperties(buildRecordId);
if (buildRecord == null) {
return false;
}
if (!buildRecord.isTemporaryBuild()) {
throw new ValidationException("Only deletion of the temporary builds is allowed");
}
executorService.submit(() -> {
try {
Result result = temporaryBuildsCleaner.deleteTemporaryBuild(buildRecordId, authToken);
onComplete.accept(result);
} catch (ValidationException e) {
logger.error("Failed to delete temporary buildRecord.id: " + buildRecordId + ".", e);
onComplete.accept(new Result(buildRecordId.toString(), ResultStatus.FAILED, "Failed to delete temporary buildRecord."));
}
});
return true;
}
use of org.jboss.pnc.spi.coordinator.Result in project pnc by project-ncl.
the class TemporaryBuildsCleanerAsyncInvoker method deleteTemporaryBuildConfigSetRecord.
/**
* Deletes a temporary BuildConfigSetRecord.
*
* @param buildConfigSetRecordId ID of the BuildConfigSetRecord to be deleted
* @param authToken Bearer token
* @param onComplete Operation to be executed after deletion operation
* @return True if the build exists and deletion started otherwise, false is build doesn't exist
* @throws ValidationException Thrown when build cannot be deleted
*/
public boolean deleteTemporaryBuildConfigSetRecord(Integer buildConfigSetRecordId, String authToken, Consumer<Result> onComplete) throws ValidationException {
BuildConfigSetRecord buildConfigSetRecord = buildConfigSetRecordRepository.queryById(buildConfigSetRecordId);
if (buildConfigSetRecord == null) {
return false;
}
if (!buildConfigSetRecord.isTemporaryBuild()) {
throw new ValidationException("Only deletion of the temporary builds is allowed");
}
executorService.submit(() -> {
try {
Result result = temporaryBuildsCleaner.deleteTemporaryBuildConfigSetRecord(buildConfigSetRecordId, authToken);
onComplete.accept(result);
} catch (ValidationException e) {
logger.error("Failed to delete temporary buildConfigSetRecord.id: " + buildConfigSetRecordId + ".", e);
onComplete.accept(new Result(buildConfigSetRecordId.toString(), ResultStatus.FAILED, "Failed to delete temporary buildConfigSetRecord."));
}
});
return true;
}
use of org.jboss.pnc.spi.coordinator.Result in project pnc by project-ncl.
the class TemporaryBuildsCleaner method deleteTemporaryBuildConfigSetRecord.
/**
* Deletes a BuildConfigSetRecord and BuildRecords produced in the build
*
* @param buildConfigSetRecordId BuildConfigSetRecord to be deleted
* @param authToken
*/
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public Result deleteTemporaryBuildConfigSetRecord(Integer buildConfigSetRecordId, String authToken) throws ValidationException {
BuildConfigSetRecord buildConfigSetRecord = buildConfigSetRecordRepository.queryById(buildConfigSetRecordId);
if (buildConfigSetRecord == null) {
throw new ValidationException("Cannot delete temporary BuildConfigSetRecord with id " + buildConfigSetRecordId + " as no BuildConfigSetRecord with this id exists");
}
if (!buildConfigSetRecord.isTemporaryBuild()) {
throw new ValidationException("Only deletion of the temporary builds is allowed");
}
log.info("Starting deletion of a temporary build record set " + buildConfigSetRecord);
for (BuildRecord br : buildConfigSetRecord.getBuildRecords()) {
br.setBuildConfigSetRecord(null);
buildRecordRepository.save(br);
}
buildConfigSetRecordRepository.delete(buildConfigSetRecord.getId());
log.info("Deletion of a temporary build record set {} finished successfully.", buildConfigSetRecord);
return new Result(buildConfigSetRecordId.toString(), ResultStatus.SUCCESS);
}
use of org.jboss.pnc.spi.coordinator.Result in project pnc by project-ncl.
the class GroupBuildProviderTest method shouldProvideCallbackOnDeletion.
@Test
public void shouldProvideCallbackOnDeletion() throws Exception {
// given
final int buildId = 88;
final String buildIdString = String.valueOf(buildId);
final String callbackUrl = "http://localhost:8088/callback";
WireMockServer wireMockServer = new WireMockServer(8088);
wireMockServer.start();
wireMockServer.stubFor(post(urlEqualTo("/callback")).willReturn(aResponse().withStatus(200)));
given(temporaryBuildsCleanerAsyncInvoker.deleteTemporaryBuildConfigSetRecord(eq(buildId), eq(USER_TOKEN), any())).willAnswer(invocation -> {
Result result = new Result(buildIdString, ResultStatus.SUCCESS, "BuildConfigSetRecord was deleted " + "successfully");
((Consumer<Result>) invocation.getArgument(2)).accept(result);
return true;
});
// when
boolean result = provider.delete(buildIdString, callbackUrl);
// then
assertThat(result).isTrue();
wireMockServer.verify(1, postRequestedFor(urlEqualTo("/callback")).withRequestBody(matchingJsonPath("$.id", equalTo(buildIdString))));
wireMockServer.stop();
}
use of org.jboss.pnc.spi.coordinator.Result in project pnc by project-ncl.
the class BuildProviderImplTest method shouldPerformCallbackAfterDeletion.
@Test
public void shouldPerformCallbackAfterDeletion() throws Exception {
// given
final Base32LongID buildId = new Base32LongID(88L);
final String buildIdString = BuildMapper.idMapper.toDto(buildId);
final String callbackUrl = "http://localhost:8088/callback";
WireMockServer wireMockServer = new WireMockServer(8088);
wireMockServer.start();
wireMockServer.stubFor(post(urlEqualTo("/callback")).willReturn(aResponse().withStatus(200)));
given(temporaryBuildsCleanerAsyncInvoker.deleteTemporaryBuild(eq(buildId), eq(USER_TOKEN), any())).willAnswer(invocation -> {
Result result = new Result(buildIdString, ResultStatus.SUCCESS, "Build was deleted successfully");
((Consumer<Result>) invocation.getArgument(2)).accept(result);
return true;
});
// when
boolean result = provider.delete(buildIdString, callbackUrl);
// then
assertThat(result).isTrue();
wireMockServer.verify(1, postRequestedFor(urlEqualTo("/callback")).withRequestBody(matchingJsonPath("$.id", equalTo(buildIdString))));
wireMockServer.stop();
}
Aggregations