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\""));
}
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));
}
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"));
}
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);
}
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);
}
Aggregations