Search in sources :

Example 6 with LocalizedOperationResult

use of com.thoughtworks.go.server.service.result.LocalizedOperationResult in project gocd by gocd.

the class MaterialServiceTest method shouldReturnNotFoundIfTheMaterialDoesNotBelongToTheGivenPipeline.

@Test
public void shouldReturnNotFoundIfTheMaterialDoesNotBelongToTheGivenPipeline() {
    Username pavan = Username.valueOf("pavan");
    when(securityService.hasViewPermissionForPipeline(pavan, "pipeline")).thenReturn(true);
    LocalizedOperationResult operationResult = mock(LocalizedOperationResult.class);
    when(goConfigService.materialForPipelineWithFingerprint("pipeline", "sha")).thenThrow(new RuntimeException("Not found"));
    materialService.searchRevisions("pipeline", "sha", "23", pavan, operationResult);
    verify(operationResult).notFound(LocalizedMessage.materialWithFingerPrintNotFound("pipeline", "sha"), HealthStateType.general(HealthStateScope.forPipeline("pipeline")));
}
Also used : Username(com.thoughtworks.go.server.domain.Username) LocalizedOperationResult(com.thoughtworks.go.server.service.result.LocalizedOperationResult) Test(org.junit.Test)

Example 7 with LocalizedOperationResult

use of com.thoughtworks.go.server.service.result.LocalizedOperationResult in project gocd by gocd.

the class MaterialServiceTest method shouldReturnTheRevisionsThatMatchTheGivenSearchString.

@Test
public void shouldReturnTheRevisionsThatMatchTheGivenSearchString() {
    Username pavan = Username.valueOf("pavan");
    when(securityService.hasViewPermissionForPipeline(pavan, "pipeline")).thenReturn(true);
    LocalizedOperationResult operationResult = mock(LocalizedOperationResult.class);
    MaterialConfig materialConfig = mock(MaterialConfig.class);
    when(goConfigService.materialForPipelineWithFingerprint("pipeline", "sha")).thenReturn(materialConfig);
    List<MatchedRevision> expected = asList(new MatchedRevision("23", "revision", "revision", "user", new DateTime(2009, 10, 10, 12, 0, 0, 0).toDate(), "comment"));
    when(materialRepository.findRevisionsMatching(materialConfig, "23")).thenReturn(expected);
    assertThat(materialService.searchRevisions("pipeline", "sha", "23", pavan, operationResult), is(expected));
}
Also used : Username(com.thoughtworks.go.server.domain.Username) LocalizedOperationResult(com.thoughtworks.go.server.service.result.LocalizedOperationResult) GitMaterialConfig(com.thoughtworks.go.config.materials.git.GitMaterialConfig) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 8 with LocalizedOperationResult

use of com.thoughtworks.go.server.service.result.LocalizedOperationResult in project gocd by gocd.

the class PackageRepositoryService method checkConnection.

public void checkConnection(final PackageRepository packageRepository, final LocalizedOperationResult result) {
    try {
        Result checkConnectionResult = packageRepositoryExtension.checkConnectionToRepository(packageRepository.getPluginConfiguration().getId(), populateConfiguration(packageRepository.getConfiguration()));
        String messages = checkConnectionResult.getMessagesForDisplay();
        if (!checkConnectionResult.isSuccessful()) {
            result.connectionError(LocalizedMessage.string("CHECK_CONNECTION_FAILED", "package repository", messages));
            return;
        }
        result.setMessage(LocalizedMessage.string("CONNECTION_OK", messages));
        return;
    } catch (Exception e) {
        result.internalServerError(LocalizedMessage.string("CHECK_CONNECTION_FAILED", "package repository", e.getMessage()));
    }
}
Also used : GoConfigInvalidException(com.thoughtworks.go.config.exceptions.GoConfigInvalidException) Result(com.thoughtworks.go.plugin.api.response.Result) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) LocalizedOperationResult(com.thoughtworks.go.server.service.result.LocalizedOperationResult) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)

Example 9 with LocalizedOperationResult

use of com.thoughtworks.go.server.service.result.LocalizedOperationResult in project gocd by gocd.

the class ScheduleService method cancelAndTriggerRelevantStages.

// synchronized for updating job
public Stage cancelAndTriggerRelevantStages(Long stageId, Username username, LocalizedOperationResult result) throws Exception {
    Stage stageForId;
    LocalizedOperationResult opResult = result == null ? new DefaultLocalizedOperationResult() : result;
    try {
        stageForId = stageService.stageById(stageId);
    } catch (Exception e) {
        LOGGER.error("[Stage Cancellation] Failed to retrieve stage identifier", e);
        opResult.notFound(LocalizedMessage.string("STAGE_FOR_LOCATOR_NOT_FOUND", stageId), HealthStateType.general(HealthStateScope.GLOBAL));
        return null;
    }
    if (!stageForId.isActive()) {
        opResult.setMessage(LocalizedMessage.string("STAGE_IS_NOT_ACTIVE_FOR_CANCELLATION"));
        return stageForId;
    }
    String stageMutex = mutexForStageInstance(stageForId.getIdentifier());
    synchronized (stageMutex) {
        // reload stage so we see committed state after acquiring mutex
        final Stage stage = stageService.stageById(stageId);
        String pipelineName = stage.getIdentifier().getPipelineName();
        String stageName = stage.getIdentifier().getStageName();
        String user = username == null ? null : username.getUsername().toString();
        if (!securityService.hasOperatePermissionForStage(pipelineName, stageName, user)) {
            opResult.unauthorized(LocalizedMessage.string("UNAUTHORIZED_TO_OPERATE_STAGE", stageName), HealthStateType.unauthorised());
            return null;
        }
        LOGGER.info("[Stage Cancellation] Cancelling stage {}", stage.getIdentifier());
        transactionTemplate.executeWithExceptionHandling(new com.thoughtworks.go.server.transaction.TransactionCallbackWithoutResult() {

            @Override
            public void doInTransactionWithoutResult(TransactionStatus status) throws Exception {
                stageService.cancelStage(stage);
            }
        });
        transactionTemplate.executeWithExceptionHandling(new com.thoughtworks.go.server.transaction.TransactionCallbackWithoutResult() {

            @Override
            public void doInTransactionWithoutResult(TransactionStatus status) throws Exception {
                automaticallyTriggerRelevantStagesFollowingCompletionOf(stage);
            }
        });
        opResult.setMessage(LocalizedMessage.string("STAGE_CANCELLED_SUCCESSFULLY"));
        return stage;
    }
}
Also used : DefaultLocalizedOperationResult(com.thoughtworks.go.server.service.result.DefaultLocalizedOperationResult) LocalizedOperationResult(com.thoughtworks.go.server.service.result.LocalizedOperationResult) DefaultLocalizedOperationResult(com.thoughtworks.go.server.service.result.DefaultLocalizedOperationResult) TransactionStatus(org.springframework.transaction.TransactionStatus) GoUnauthorizedException(com.thoughtworks.go.server.GoUnauthorizedException) InvalidAgentException(com.thoughtworks.go.remote.work.InvalidAgentException)

Example 10 with LocalizedOperationResult

use of com.thoughtworks.go.server.service.result.LocalizedOperationResult in project gocd by gocd.

the class GoConfigServiceIntegrationTest method shouldNotUpdateConfigFromUIWhentheUserDoesNotHavePermissions.

@Test
public void shouldNotUpdateConfigFromUIWhentheUserDoesNotHavePermissions() {
    final PipelineConfig pipelineConfig = configHelper.addPipeline("pipeline", "stage");
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    String md5 = goConfigService.getConfigForEditing().getMd5();
    final CruiseConfig[] configObtainedInCheckPermissions = new CruiseConfig[1];
    ConfigUpdateResponse response = goConfigService.updateConfigFromUI(new AddStageToPipelineCommand("secondStage") {

        public void checkPermission(CruiseConfig cruiseConfig, LocalizedOperationResult result) {
            result.unauthorized(LocalizedMessage.string("UNAUTHORIZED_TO_EDIT_GROUP", "groupName"), null);
            configObtainedInCheckPermissions[0] = cruiseConfig;
        }
    }, md5, Username.ANONYMOUS, result);
    assertThat(configObtainedInCheckPermissions[0], is(goConfigService.getCurrentConfig()));
    PipelineConfig config = goConfigService.getConfigForEditing().pipelineConfigByName(new CaseInsensitiveString("pipeline"));
    assertThat(config.size(), is(1));
    assertThat(config.get(0).name(), is(new CaseInsensitiveString("stage")));
    assertThat(response.getCruiseConfig(), is(goConfigService.getConfigForEditing()));
    assertThat(response.getNode(), is(pipelineConfig));
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.httpCode(), is(401));
    assertThat(result.message(localizer), is("Unauthorized to edit 'groupName' group."));
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) LocalizedOperationResult(com.thoughtworks.go.server.service.result.LocalizedOperationResult) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Matchers.containsString(org.hamcrest.Matchers.containsString) ConfigUpdateResponse(com.thoughtworks.go.config.update.ConfigUpdateResponse) Test(org.junit.Test)

Aggregations

LocalizedOperationResult (com.thoughtworks.go.server.service.result.LocalizedOperationResult)10 Test (org.junit.Test)7 Username (com.thoughtworks.go.server.domain.Username)6 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)6 ConfigUpdateResponse (com.thoughtworks.go.config.update.ConfigUpdateResponse)4 UpdateConfigFromUI (com.thoughtworks.go.config.update.UpdateConfigFromUI)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 GoConfigInvalidException (com.thoughtworks.go.config.exceptions.GoConfigInvalidException)2 Result (com.thoughtworks.go.plugin.api.response.Result)2 ValidationResult (com.thoughtworks.go.plugin.api.response.validation.ValidationResult)2 MaterialConfigs (com.thoughtworks.go.config.materials.MaterialConfigs)1 GitMaterialConfig (com.thoughtworks.go.config.materials.git.GitMaterialConfig)1 ConfigUpdateAjaxResponse (com.thoughtworks.go.config.update.ConfigUpdateAjaxResponse)1 PackageRepository (com.thoughtworks.go.domain.packagerepository.PackageRepository)1 GoConfigMother (com.thoughtworks.go.helper.GoConfigMother)1 InvalidAgentException (com.thoughtworks.go.remote.work.InvalidAgentException)1 GoUnauthorizedException (com.thoughtworks.go.server.GoUnauthorizedException)1 DefaultLocalizedOperationResult (com.thoughtworks.go.server.service.result.DefaultLocalizedOperationResult)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DateTime (org.joda.time.DateTime)1