Search in sources :

Example 26 with GoConfigValidity

use of com.thoughtworks.go.config.validation.GoConfigValidity in project gocd by gocd.

the class GoConfigServiceIntegrationTest method shouldThrowUpOnConfigSavePreValidationError_ViaMergeFlow.

@Test
public void shouldThrowUpOnConfigSavePreValidationError_ViaMergeFlow() throws Exception {
    // User 1 loads page
    CruiseConfig user1SeeingConfig = goConfigDao.loadForEditing();
    String user1SeeingMd5 = user1SeeingConfig.getMd5();
    // User 2 edits config
    configHelper.addPipelineWithGroup("defaultGroup", "user2_pipeline", "user2_stage", "user2_job");
    CruiseConfig user2SeeingConfig = configHelper.load();
    // User 1 edits old config
    new GoConfigMother().addPipelineWithGroup(user1SeeingConfig, "defaultGroup", "user1_pipeline", "user1_stage", "user1_job");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    configHelper.getXml(user1SeeingConfig, os);
    // Introduce validation error on xml
    String xml = os.toString();
    xml = xml.replace("user1_pipeline", "user1 pipeline");
    // User 1 saves edited config
    GoConfigService.XmlPartialSaver saver = goConfigService.fileSaver(false);
    GoConfigValidity validity = saver.saveXml(xml, user1SeeingMd5);
    assertThat(validity.isValid(), is(false));
    // Pre throws VT_CONFLICT as user submitted xml is validated before attempting to save
    assertThat(validity.isType(GoConfigValidity.VT_CONFLICT), is(true));
    assertThat(validity.errorMessage(), containsString("Name is invalid. \"user1 pipeline\""));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GoConfigMother(com.thoughtworks.go.helper.GoConfigMother) GoConfigValidity(com.thoughtworks.go.config.validation.GoConfigValidity) Test(org.junit.Test)

Example 27 with GoConfigValidity

use of com.thoughtworks.go.config.validation.GoConfigValidity in project gocd by gocd.

the class GoConfigServiceIntegrationTest method shouldNotThrowUpOnConfigSave_ViaNormalFlow.

@Test
public void shouldNotThrowUpOnConfigSave_ViaNormalFlow() throws Exception {
    // User 1 loads page
    CruiseConfig user1SeeingConfig = goConfigDao.loadForEditing();
    // User 2 edits config
    configHelper.addPipelineWithGroup("defaultGroup", "user2_pipeline", "user2_stage", "user2_job");
    CruiseConfig user2SeeingConfig = configHelper.load();
    String user2SeeingMd5 = user2SeeingConfig.getMd5();
    // User 1 edits new config
    new GoConfigMother().addPipelineWithGroup(user2SeeingConfig, "defaultGroup", "user1_pipeline", "user1_stage", "user1_job");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    configHelper.getXml(user2SeeingConfig, os);
    // User 1 saves edited config
    GoConfigService.XmlPartialSaver saver = goConfigService.fileSaver(false);
    GoConfigValidity validity = saver.saveXml(os.toString(), user2SeeingMd5);
    assertThat(validity.isValid(), is(true));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GoConfigMother(com.thoughtworks.go.helper.GoConfigMother) GoConfigValidity(com.thoughtworks.go.config.validation.GoConfigValidity) Test(org.junit.Test)

Example 28 with GoConfigValidity

use of com.thoughtworks.go.config.validation.GoConfigValidity in project gocd by gocd.

the class GoConfigMigrationIntegrationTest method shouldLoadServerId_ifOneExists.

@Test
public void shouldLoadServerId_ifOneExists() {
    GoConfigService.XmlPartialSaver fileSaver = goConfigService.fileSaver(true);
    GoConfigValidity configValidity = fileSaver.saveXml("<cruise schemaVersion='" + 55 + "'>\n" + "<server artifactsdir=\"logs\" siteUrl=\"http://go-server-site-url:8153\" secureSiteUrl=\"https://go-server-site-url:8154\" jobTimeout=\"60\" serverId=\"foo\">\n" + "  </server>" + "</cruise>", goConfigService.configFileMd5());
    assertThat("Has error: " + configValidity.errorMessage(), configValidity.isValid(), is(true));
    CruiseConfig config = goConfigService.getCurrentConfig();
    ServerConfig server = config.server();
    assertThat(server.getServerId(), is("foo"));
}
Also used : GoConfigService(com.thoughtworks.go.server.service.GoConfigService) GoConfigValidity(com.thoughtworks.go.config.validation.GoConfigValidity) Test(org.junit.Test)

Example 29 with GoConfigValidity

use of com.thoughtworks.go.config.validation.GoConfigValidity in project gocd by gocd.

the class PipelineConfigsServiceTest method shouldReturnUnsuccessfulResultWhenTheGroupIsNotFound_onUpdateXml.

@Test
public void shouldReturnUnsuccessfulResultWhenTheGroupIsNotFound_onUpdateXml() throws Exception {
    String groupName = "non-existent-group_name";
    Localizer localizer = mock(Localizer.class);
    when(localizer.localize("PIPELINE_GROUP_NOT_FOUND", groupName)).thenReturn("Not found");
    when(securityService.isUserAdminOfGroup(validUser.getUsername(), groupName)).thenThrow(new PipelineGroupNotFoundException());
    when(goConfigService.configFileMd5()).thenReturn("md5");
    GoConfigOperationalResponse<PipelineConfigs> actual = service.updateXml(groupName, "", "md5", validUser, result);
    PipelineConfigs configs = actual.getConfigElement();
    GoConfigValidity validity = actual.getValidity();
    assertThat(configs, is(nullValue()));
    assertThat(result.httpCode(), is(404));
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.message(localizer), is("Not found"));
    assertThat(validity.isValid(), is(true));
    verify(securityService, times(1)).isUserAdminOfGroup(validUser.getUsername(), groupName);
}
Also used : Localizer(com.thoughtworks.go.i18n.Localizer) PipelineGroupNotFoundException(com.thoughtworks.go.config.exceptions.PipelineGroupNotFoundException) GoConfigValidity(com.thoughtworks.go.config.validation.GoConfigValidity) Test(org.junit.Test)

Example 30 with GoConfigValidity

use of com.thoughtworks.go.config.validation.GoConfigValidity in project gocd by gocd.

the class PipelineConfigsServiceTest method shouldReturnUnsuccessfulResultWhenXmlIsInvalid_onUpdateXml.

@Test
public void shouldReturnUnsuccessfulResultWhenXmlIsInvalid_onUpdateXml() throws Exception {
    String errorMessage = "Can not parse xml";
    final String groupName = "group_name";
    Localizer localizer = mock(Localizer.class);
    when(localizer.localize("UPDATE_GROUP_XML_FAILED", groupName, errorMessage)).thenReturn("Invalid");
    when(securityService.isUserAdminOfGroup(validUser.getUsername(), groupName)).thenReturn(true);
    String md5 = "md5";
    when(goConfigService.configFileMd5()).thenReturn(md5);
    GoConfigService.XmlPartialSaver groupSaver = mock(GoConfigService.XmlPartialSaver.class);
    when(goConfigService.groupSaver(groupName)).thenReturn(groupSaver);
    String updatedPartial = "foobar";
    when(groupSaver.saveXml(updatedPartial, md5)).thenReturn(GoConfigValidity.invalid(errorMessage));
    GoConfigOperationalResponse<PipelineConfigs> actual = service.updateXml(groupName, updatedPartial, md5, validUser, result);
    PipelineConfigs configs = actual.getConfigElement();
    GoConfigValidity validity = actual.getValidity();
    assertThat(configs, is(nullValue()));
    assertThat(result.httpCode(), is(500));
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.message(localizer), is("Invalid"));
    assertThat(validity.isValid(), is(false));
    verify(securityService, times(1)).isUserAdminOfGroup(validUser.getUsername(), groupName);
}
Also used : Localizer(com.thoughtworks.go.i18n.Localizer) GoConfigValidity(com.thoughtworks.go.config.validation.GoConfigValidity) Test(org.junit.Test)

Aggregations

GoConfigValidity (com.thoughtworks.go.config.validation.GoConfigValidity)34 Test (org.junit.Test)28 GoConfigMother (com.thoughtworks.go.helper.GoConfigMother)11 StringContains.containsString (org.hamcrest.core.StringContains.containsString)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 GoConfigService (com.thoughtworks.go.server.service.GoConfigService)4 Localizer (com.thoughtworks.go.i18n.Localizer)3 LocalizedKeyValueMessage (com.thoughtworks.go.i18n.LocalizedKeyValueMessage)2 Cloner (com.rits.cloning.Cloner)1 PipelineGroupNotFoundException (com.thoughtworks.go.config.exceptions.PipelineGroupNotFoundException)1 MaterialConfigs (com.thoughtworks.go.config.materials.MaterialConfigs)1 PartialConfig (com.thoughtworks.go.config.remote.PartialConfig)1 RepoConfigOrigin (com.thoughtworks.go.config.remote.RepoConfigOrigin)1 FullConfigUpdateCommand (com.thoughtworks.go.config.update.FullConfigUpdateCommand)1 Localizable (com.thoughtworks.go.i18n.Localizable)1 Username (com.thoughtworks.go.server.domain.Username)1 GoConfigOperationalResponse (com.thoughtworks.go.server.service.responses.GoConfigOperationalResponse)1 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)1 File (java.io.File)1