Search in sources :

Example 1 with LocalizedOperationResult

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

the class PackageDefinitionService method checkConnection.

public void checkConnection(final PackageDefinition packageDefinition, final LocalizedOperationResult result) {
    try {
        String pluginId = packageDefinition.getRepository().getPluginConfiguration().getId();
        Result checkConnectionResult = packageRepositoryExtension.checkConnectionToPackage(pluginId, buildPackageConfigurations(packageDefinition), buildRepositoryConfigurations(packageDefinition.getRepository()));
        String messages = checkConnectionResult.getMessagesForDisplay();
        if (!checkConnectionResult.isSuccessful()) {
            result.connectionError(LocalizedMessage.string("PACKAGE_CHECK_FAILED", messages));
            return;
        }
        result.setMessage(LocalizedMessage.string("PACKAGE_CHECK_OK", messages));
        return;
    } catch (Exception e) {
        result.internalServerError(LocalizedMessage.string("PACKAGE_CHECK_FAILED", 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 2 with LocalizedOperationResult

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

the class MaterialServiceTest method shouldNotBeAuthorizedToViewAPipeline.

@Test
public void shouldNotBeAuthorizedToViewAPipeline() {
    Username pavan = Username.valueOf("pavan");
    when(securityService.hasViewPermissionForPipeline(pavan, "pipeline")).thenReturn(false);
    LocalizedOperationResult operationResult = mock(LocalizedOperationResult.class);
    materialService.searchRevisions("pipeline", "sha", "search-string", pavan, operationResult);
    verify(operationResult).unauthorized(LocalizedMessage.cannotViewPipeline("pipeline"), 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 3 with LocalizedOperationResult

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

the class GoConfigServiceIntegrationTest method shouldReturnAllErrorsAppliedOverEditedCopy.

@Test
public void shouldReturnAllErrorsAppliedOverEditedCopy() {
    configHelper.addPipeline("pipeline", "stage");
    configHelper.addParamToPipeline("pipeline", "mingle_url", "http://foo.bar");
    String md5 = goConfigService.getConfigForEditing().getMd5();
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    ConfigUpdateResponse response = goConfigService.updateConfigFromUI(new UpdateConfigFromUI() {

        public void checkPermission(CruiseConfig cruiseConfig, LocalizedOperationResult result) {
        }

        public Validatable node(CruiseConfig cruiseConfig) {
            return cruiseConfig.pipelineConfigByName(new CaseInsensitiveString("pipeline"));
        }

        public Validatable updatedNode(CruiseConfig cruiseConfig) {
            return node(cruiseConfig);
        }

        public void update(Validatable pipeline) {
            PipelineConfig pipelineConfig = (PipelineConfig) pipeline;
            pipelineConfig.setMingleConfig(new MingleConfig("#{mingle_url}", "go"));
        }

        public Validatable subject(Validatable node) {
            return node;
        }

        public Validatable updatedSubject(Validatable updatedNode) {
            return subject(updatedNode);
        }
    }, md5, new Username(new CaseInsensitiveString("admin")), result);
    MingleConfig mingleConfig = ((PipelineConfig) response.getNode()).getMingleConfig();
    assertThat(mingleConfig.errors().on(MingleConfig.BASE_URL), is("Should be a URL starting with https://"));
    assertThat(mingleConfig.getBaseUrl(), is("#{mingle_url}"));
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Username(com.thoughtworks.go.server.domain.Username) 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) UpdateConfigFromUI(com.thoughtworks.go.config.update.UpdateConfigFromUI) Test(org.junit.Test)

Example 4 with LocalizedOperationResult

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

the class GoConfigServiceIntegrationTest method shouldInternallyGetGoConfigInvalidExceptionOnValidationErrorAndFailWithATopLevelConfigError.

@Test
public void shouldInternallyGetGoConfigInvalidExceptionOnValidationErrorAndFailWithATopLevelConfigError() throws Exception {
    String oldMd5 = goConfigService.getConfigForEditing().getMd5();
    CruiseConfig user1SeeingConfig = configHelper.load();
    // Setup a pipeline group in the config
    new GoConfigMother().addPipelineWithGroup(user1SeeingConfig, "defaultGroup", "user1_pipeline", "user1_stage", "user1_job");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    configHelper.getXml(user1SeeingConfig, os);
    GoConfigService.XmlPartialSaver saver = goConfigService.fileSaver(false);
    saver.saveXml(os.toString(), oldMd5);
    CruiseConfig configBeforePipelineGroupWasAddedAtBeginning = configHelper.load();
    String md5BeforeAddingGroupAtBeginning = configBeforePipelineGroupWasAddedAtBeginning.getMd5();
    // User 1 edits config XML and adds a pipeline group before the first group in config
    String configXMLWithGroupAddedAtBeginning = os.toString().replace("</pipelines>", "</pipelines><pipelines group=\"first_group\"/>");
    saver.saveXml(configXMLWithGroupAddedAtBeginning, md5BeforeAddingGroupAtBeginning);
    // User 2 adds another pipeline group, with the same name, through UI, but using the older MD5.
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    ConfigUpdateResponse response = goConfigService.updateConfigFromUI(new UpdateConfigFromUI() {

        public void checkPermission(CruiseConfig cruiseConfig, LocalizedOperationResult result) {
        }

        public Validatable node(CruiseConfig cruiseConfig) {
            return cruiseConfig;
        }

        public Validatable updatedNode(CruiseConfig cruiseConfig) {
            return node(cruiseConfig);
        }

        public void update(Validatable config) {
            CruiseConfig cruiseConfig = (CruiseConfig) config;
            MaterialConfigs materials = new MaterialConfigs(MaterialConfigsMother.mockMaterialConfigs("file:///tmp/foo"));
            new GoConfigMother().addPipelineWithGroup(cruiseConfig, "first_group", "up_pipeline", materials, "down_stage", "down_job");
        }

        public Validatable subject(Validatable node) {
            return node;
        }

        public Validatable updatedSubject(Validatable updatedNode) {
            return subject(updatedNode);
        }
    }, md5BeforeAddingGroupAtBeginning, new Username(new CaseInsensitiveString("admin")), result);
    CruiseConfig config = response.getCruiseConfig();
    assertThat(config.getMd5(), is(md5BeforeAddingGroupAtBeginning));
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.httpCode(), is(SC_CONFLICT));
    assertThat(result.message(localizer), is("Save failed. Duplicate unique value [first_group] declared for identity constraint \"uniquePipelines\" of element \"cruise\"."));
}
Also used : MaterialConfigs(com.thoughtworks.go.config.materials.MaterialConfigs) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GoConfigMother(com.thoughtworks.go.helper.GoConfigMother) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Username(com.thoughtworks.go.server.domain.Username) LocalizedOperationResult(com.thoughtworks.go.server.service.result.LocalizedOperationResult) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) ConfigUpdateResponse(com.thoughtworks.go.config.update.ConfigUpdateResponse) UpdateConfigFromUI(com.thoughtworks.go.config.update.UpdateConfigFromUI) Test(org.junit.Test)

Example 5 with LocalizedOperationResult

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

the class PackageRepositoryServiceTest method shouldFailAndReturnReturnFailureResponse.

@Test
public void shouldFailAndReturnReturnFailureResponse() throws Exception {
    service = spy(service);
    Username username = new Username(new CaseInsensitiveString("user"));
    final PackageRepository packageRepository = new PackageRepository();
    packageRepository.errors().add("name", "Name is invalid");
    final CruiseConfig cruiseConfig = GoConfigMother.configWithPipelines("sample");
    cruiseConfig.errors().add("global", "error");
    final UpdateConfigFromUI updateConfigFromUI = mock(UpdateConfigFromUI.class);
    final ConfigAwareUpdate configAwareUpdate = mock(ConfigAwareUpdate.class);
    doNothing().when(service).performPluginValidationsFor(packageRepository);
    doReturn(updateConfigFromUI).when(service).getPackageRepositoryUpdateCommand(packageRepository, username);
    when(configAwareUpdate.configAfter()).thenReturn(cruiseConfig);
    when(goConfigService.updateConfigFromUI(eq(updateConfigFromUI), eq("md5"), eq(username), any(LocalizedOperationResult.class))).then(new Answer<ConfigUpdateResponse>() {

        @Override
        public ConfigUpdateResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            LocalizedOperationResult result = (LocalizedOperationResult) invocationOnMock.getArguments()[3];
            result.badRequest(LocalizedMessage.string("BAD_REQUEST"));
            return new ConfigUpdateResponse(cruiseConfig, cruiseConfig, packageRepository, configAwareUpdate, ConfigSaveState.UPDATED);
        }
    });
    when(localizer.localize("BAD_REQUEST", new Object[] {})).thenReturn("Save Failed");
    ConfigUpdateAjaxResponse response = service.savePackageRepositoryToConfig(packageRepository, "md5", username);
    assertThat(response.isSuccessful(), is(false));
    assertThat(response.getMessage(), is("Save Failed"));
    assertThat(response.getFieldErrors().size(), is(1));
    assertThat(response.getFieldErrors().get("package_repository[name]"), is(asList("Name is invalid")));
    assertThat(response.getGlobalErrors().size(), is(1));
    assertThat(response.getGlobalErrors().contains("error"), is(true));
    assertThat(response.getStatusCode(), is(HttpStatus.SC_BAD_REQUEST));
    verify(service).performPluginValidationsFor(packageRepository);
    verify(service).getPackageRepositoryUpdateCommand(packageRepository, username);
}
Also used : PackageRepository(com.thoughtworks.go.domain.packagerepository.PackageRepository) Username(com.thoughtworks.go.server.domain.Username) LocalizedOperationResult(com.thoughtworks.go.server.service.result.LocalizedOperationResult) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ConfigUpdateAjaxResponse(com.thoughtworks.go.config.update.ConfigUpdateAjaxResponse) UpdateConfigFromUI(com.thoughtworks.go.config.update.UpdateConfigFromUI) 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