Search in sources :

Example 1 with ConfigUpdateAjaxResponse

use of com.thoughtworks.go.config.update.ConfigUpdateAjaxResponse 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 2 with ConfigUpdateAjaxResponse

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

the class PackageRepositoryServiceTest method shouldSavePackageRepositoryAndReturnSuccess.

@Test
public void shouldSavePackageRepositoryAndReturnSuccess() throws Exception {
    service = spy(service);
    PackageRepository packageRepository = new PackageRepository();
    packageRepository.setId("repoid");
    Username username = new Username(new CaseInsensitiveString("user"));
    UpdateConfigFromUI updateConfigFromUI = mock(UpdateConfigFromUI.class);
    doNothing().when(service).performPluginValidationsFor(packageRepository);
    doReturn(updateConfigFromUI).when(service).getPackageRepositoryUpdateCommand(packageRepository, username);
    when(goConfigService.updateConfigFromUI(eq(updateConfigFromUI), eq("md5"), eq(username), any(LocalizedOperationResult.class))).then(new Answer<ConfigUpdateResponse>() {

        @Override
        public ConfigUpdateResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            return new ConfigUpdateResponse(null, null, null, mock(ConfigAwareUpdate.class), ConfigSaveState.UPDATED);
        }
    });
    when(localizer.localize("SAVED_CONFIGURATION_SUCCESSFULLY")).thenReturn("SAVED_CONFIGURATION_SUCCESSFULLY");
    ConfigUpdateAjaxResponse response = service.savePackageRepositoryToConfig(packageRepository, "md5", username);
    assertThat(response.isSuccessful(), is(true));
    assertThat(response.getMessage(), is("SAVED_CONFIGURATION_SUCCESSFULLY"));
    assertThat(response.getSubjectIdentifier(), is("repoid"));
    assertThat(response.getStatusCode(), is(HttpStatus.SC_OK));
    verify(service).performPluginValidationsFor(packageRepository);
    verify(service).getPackageRepositoryUpdateCommand(packageRepository, username);
}
Also used : 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) PackageRepository(com.thoughtworks.go.domain.packagerepository.PackageRepository) 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

ConfigUpdateAjaxResponse (com.thoughtworks.go.config.update.ConfigUpdateAjaxResponse)2 ConfigUpdateResponse (com.thoughtworks.go.config.update.ConfigUpdateResponse)2 UpdateConfigFromUI (com.thoughtworks.go.config.update.UpdateConfigFromUI)2 PackageRepository (com.thoughtworks.go.domain.packagerepository.PackageRepository)2 Username (com.thoughtworks.go.server.domain.Username)2 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)2 LocalizedOperationResult (com.thoughtworks.go.server.service.result.LocalizedOperationResult)2 Test (org.junit.Test)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2