Search in sources :

Example 1 with Result

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;
}
Also used : ValidationException(org.jboss.pnc.spi.exception.ValidationException) BuildRecord(org.jboss.pnc.model.BuildRecord) Result(org.jboss.pnc.spi.coordinator.Result)

Example 2 with Result

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;
}
Also used : ValidationException(org.jboss.pnc.spi.exception.ValidationException) BuildConfigSetRecord(org.jboss.pnc.model.BuildConfigSetRecord) Result(org.jboss.pnc.spi.coordinator.Result)

Example 3 with Result

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);
}
Also used : ValidationException(org.jboss.pnc.spi.exception.ValidationException) BuildRecord(org.jboss.pnc.model.BuildRecord) BuildConfigSetRecord(org.jboss.pnc.model.BuildConfigSetRecord) Result(org.jboss.pnc.spi.coordinator.Result) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 4 with Result

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();
}
Also used : Consumer(java.util.function.Consumer) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) Result(org.jboss.pnc.spi.coordinator.Result) Test(org.junit.Test)

Example 5 with Result

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();
}
Also used : Base32LongID(org.jboss.pnc.model.Base32LongID) Consumer(java.util.function.Consumer) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) Result(org.jboss.pnc.spi.coordinator.Result) Test(org.junit.Test)

Aggregations

Result (org.jboss.pnc.spi.coordinator.Result)9 ValidationException (org.jboss.pnc.spi.exception.ValidationException)4 BuildRecord (org.jboss.pnc.model.BuildRecord)3 BuildRecordPushResult (org.jboss.pnc.model.BuildRecordPushResult)3 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)2 Consumer (java.util.function.Consumer)2 BuildConfigSetRecord (org.jboss.pnc.model.BuildConfigSetRecord)2 Test (org.junit.Test)2 TransactionAttribute (javax.ejb.TransactionAttribute)1 Indy (org.commonjava.indy.client.core.Indy)1 IndyClientException (org.commonjava.indy.client.core.IndyClientException)1 IndyStoresClientModule (org.commonjava.indy.client.core.module.IndyStoresClientModule)1 IndyFoloAdminClientModule (org.commonjava.indy.folo.client.IndyFoloAdminClientModule)1 BatchDeleteRequest (org.commonjava.indy.model.core.BatchDeleteRequest)1 Group (org.commonjava.indy.model.core.Group)1 StoreKey (org.commonjava.indy.model.core.StoreKey)1 BuildType (org.jboss.pnc.enums.BuildType)1 Base32LongID (org.jboss.pnc.model.Base32LongID)1