Search in sources :

Example 6 with ConfigUpdateResponse

use of com.thoughtworks.go.config.update.ConfigUpdateResponse 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)

Example 7 with ConfigUpdateResponse

use of com.thoughtworks.go.config.update.ConfigUpdateResponse in project gocd by gocd.

the class GoConfigServiceTest method badConfigShouldContainOldMD5_WhenConfigUpdateFailed.

@Test
public void badConfigShouldContainOldMD5_WhenConfigUpdateFailed() {
    when(goConfigDao.updateConfig(org.mockito.Matchers.<UpdateConfigCommand>any())).thenThrow(new RuntimeException(getGoConfigInvalidException()));
    ConfigUpdateResponse configUpdateResponse = goConfigService.updateConfigFromUI(mock(UpdateConfigFromUI.class), "old-md5", new Username(new CaseInsensitiveString("user")), new HttpLocalizedOperationResult());
    assertThat(configUpdateResponse.wasMerged(), is(false));
    assertThat(configUpdateResponse.getCruiseConfig().getMd5(), is("old-md5"));
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Username(com.thoughtworks.go.server.domain.Username) ConfigUpdateResponse(com.thoughtworks.go.config.update.ConfigUpdateResponse) UpdateConfigFromUI(com.thoughtworks.go.config.update.UpdateConfigFromUI) Test(org.junit.Test)

Example 8 with ConfigUpdateResponse

use of com.thoughtworks.go.config.update.ConfigUpdateResponse in project gocd by gocd.

the class GoConfigServiceTest method shouldReturnNotMergedInConfigUpdateResponse_WhenConfigIsUpdated.

@Test
public void shouldReturnNotMergedInConfigUpdateResponse_WhenConfigIsUpdated() {
    when(goConfigDao.updateConfig(org.mockito.Matchers.<UpdateConfigCommand>any())).thenReturn(ConfigSaveState.UPDATED);
    ConfigUpdateResponse configUpdateResponse = goConfigService.updateConfigFromUI(mock(UpdateConfigFromUI.class), "md5", new Username(new CaseInsensitiveString("user")), new HttpLocalizedOperationResult());
    assertThat(configUpdateResponse.wasMerged(), is(false));
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Username(com.thoughtworks.go.server.domain.Username) ConfigUpdateResponse(com.thoughtworks.go.config.update.ConfigUpdateResponse) UpdateConfigFromUI(com.thoughtworks.go.config.update.UpdateConfigFromUI) Test(org.junit.Test)

Example 9 with ConfigUpdateResponse

use of com.thoughtworks.go.config.update.ConfigUpdateResponse in project gocd by gocd.

the class GoConfigServiceIntegrationTest method shouldReturnTheLatestConfigAsResultWhenThereIsAnMd5Conflict.

@Test
public void shouldReturnTheLatestConfigAsResultWhenThereIsAnMd5Conflict() {
    configHelper.addPipeline("pipeline", "stage");
    String md5 = goConfigService.getConfigForEditing().getMd5();
    goConfigService.updateConfigFromUI(new AddStageToPipelineCommand("secondStage"), md5, Username.ANONYMOUS, new HttpLocalizedOperationResult());
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    ConfigUpdateResponse response = goConfigService.updateConfigFromUI(new AddStageToPipelineCommand("thirdStage"), md5, Username.ANONYMOUS, result);
    assertFailedResult(result, "Save failed. Configuration file has been modified by someone else.");
    CruiseConfig expectedConfig = goConfigService.getConfigForEditing();
    CruiseConfig modifiedConfig = new Cloner().deepClone(expectedConfig);
    ReflectionUtil.setField(modifiedConfig, "md5", expectedConfig.getMd5());
    PipelineConfig expected = modifiedConfig.pipelineConfigByName(new CaseInsensitiveString("pipeline"));
    expected.addStageWithoutValidityAssertion(StageConfigMother.custom("thirdStage", "job"));
    PipelineConfig actual = (PipelineConfig) response.getNode();
    assertThat(response.configAfterUpdate(), is(expectedConfig));
    assertThat(response.getCruiseConfig(), is(modifiedConfig));
    assertThat(actual, is(expected));
    assertFailedResult(result, "Save failed. Configuration file has been modified by someone else.");
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Matchers.containsString(org.hamcrest.Matchers.containsString) ConfigUpdateResponse(com.thoughtworks.go.config.update.ConfigUpdateResponse) Cloner(com.rits.cloning.Cloner) Test(org.junit.Test)

Example 10 with ConfigUpdateResponse

use of com.thoughtworks.go.config.update.ConfigUpdateResponse in project gocd by gocd.

the class TimeReportingTest method write.

private ConfigUpdateResponse write(int numberOfNewPipelines, String oldMd5) throws InterruptedException {
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    Date a = new Date();
    ConfigUpdateResponse res = goConfigService.updateConfigFromUI(new ReplaceConfigCommand(numberOfNewPipelines), oldMd5, null, result);
    Date b = new Date();
    assertThat(result.isSuccessful(), is(true));
    //        System.out.println("WRITE = " + (b.getTime() - a.getTime()) / 1000.0 + "s");
    return res;
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) ConfigUpdateResponse(com.thoughtworks.go.config.update.ConfigUpdateResponse) Date(java.util.Date)

Aggregations

ConfigUpdateResponse (com.thoughtworks.go.config.update.ConfigUpdateResponse)19 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)19 Test (org.junit.Test)18 UpdateConfigFromUI (com.thoughtworks.go.config.update.UpdateConfigFromUI)12 Username (com.thoughtworks.go.server.domain.Username)10 Matchers.containsString (org.hamcrest.Matchers.containsString)10 LocalizedOperationResult (com.thoughtworks.go.server.service.result.LocalizedOperationResult)5 ConfigFileHasChangedException (com.thoughtworks.go.config.exceptions.ConfigFileHasChangedException)2 ConfigUpdateAjaxResponse (com.thoughtworks.go.config.update.ConfigUpdateAjaxResponse)2 PackageRepository (com.thoughtworks.go.domain.packagerepository.PackageRepository)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Cloner (com.rits.cloning.Cloner)1 MaterialConfigs (com.thoughtworks.go.config.materials.MaterialConfigs)1 GoConfigMother (com.thoughtworks.go.helper.GoConfigMother)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Date (java.util.Date)1