Search in sources :

Example 16 with GoConfigValidity

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

the class GoConfigServiceIntegrationTest method shouldThrowUpOnConfigSavePostValidationError_ViaMergeFlow.

@Test
public void shouldThrowUpOnConfigSavePostValidationError_ViaMergeFlow() throws Exception {
    // User 1 adds a pipeline
    configHelper.addPipelineWithGroup("defaultGroup", "up_pipeline", "up_stage", "up_job");
    configHelper.addPipelineWithGroup("anotherGroup", "random_pipeline", "random_stage", "random_job");
    CruiseConfig user1SeeingConfig = configHelper.load();
    String user1SeeingMd5 = user1SeeingConfig.getMd5();
    // User 2 edits config
    configHelper.removePipeline("up_pipeline");
    // User 1 edits old config
    MaterialConfigs materialConfigs = new MaterialConfigs();
    materialConfigs.add(MaterialConfigsMother.dependencyMaterialConfig("up_pipeline", "up_stage"));
    new GoConfigMother().addPipelineWithGroup(user1SeeingConfig, "anotherGroup", "down_pipeline", materialConfigs, "down_stage", "down_job");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    configHelper.getXml(user1SeeingConfig, os);
    // User 1 saves edited config
    String xml = os.toString();
    GoConfigService.XmlPartialSaver saver = goConfigService.fileSaver(false);
    GoConfigValidity validity = saver.saveXml(xml, user1SeeingMd5);
    assertThat(validity.isValid(), is(false));
    assertThat(validity.toString(), validity.isType(GoConfigValidity.VT_MERGE_POST_VALIDATION_ERROR), is(true));
    assertThat(validity.errorMessage(), is("Pipeline \"up_pipeline\" does not exist. It is used from pipeline \"down_pipeline\"."));
}
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) GoConfigValidity(com.thoughtworks.go.config.validation.GoConfigValidity) Test(org.junit.Test)

Example 17 with GoConfigValidity

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

the class GoConfigServiceTest method shouldProvideDetailsWhenXmlConfigDomIsInvalid.

@Test
public void shouldProvideDetailsWhenXmlConfigDomIsInvalid() throws Exception {
    expectLoadForEditing(configWith(createPipelineConfig("pipeline", "stage", "build")));
    GoConfigService.XmlPartialSaver saver = goConfigService.fileSaver(false);
    String configContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<cruise xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"cruise-config.xsd\" schemaVersion=\"" + GoConstants.CONFIG_SCHEMA_VERSION + "\">\n" + "<server artifactsdir='artifactsDir></cruise>";
    GoConfigValidity validity = saver.saveXml(configContent, "junk_md5");
    assertThat(validity.isValid(), is(false));
    assertThat(validity.errorMessage(), is("Invalid Configuration - Error on line 3: The value of attribute \"artifactsdir\" associated with an element type \"server\" must not contain the '<' character."));
}
Also used : StringContains.containsString(org.hamcrest.core.StringContains.containsString) GoConfigValidity(com.thoughtworks.go.config.validation.GoConfigValidity) Test(org.junit.Test)

Example 18 with GoConfigValidity

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

the class GoConfigServiceTest method shouldReturnInvalidWhenWholeConfigIsInvalidAndShouldUpgrade.

@Test
public void shouldReturnInvalidWhenWholeConfigIsInvalidAndShouldUpgrade() throws Exception {
    CruiseConfig config = configWithPipeline();
    when(goConfigDao.loadForEditing()).thenReturn(config);
    String configContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<cruise xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"cruise-config.xsd\" schemaVersion=\"14\">\n" + "<server artifactsdir='artifactsDir'/><unknown/></cruise>";
    GoConfigValidity validity = goConfigService.fileSaver(true).saveXml(configContent, "md5");
    assertThat(validity.errorMessage(), is("Cruise config file with version 14 is invalid. Unable to upgrade."));
}
Also used : StringContains.containsString(org.hamcrest.core.StringContains.containsString) GoConfigValidity(com.thoughtworks.go.config.validation.GoConfigValidity) Test(org.junit.Test)

Example 19 with GoConfigValidity

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

the class GoConfigServiceTest method shouldReturnInvalidWhenPipelineGroupPartialHasInvalidAttributeValue.

@Test
public void shouldReturnInvalidWhenPipelineGroupPartialHasInvalidAttributeValue() throws Exception {
    String groupName = "group_name";
    String md5 = "md5";
    cruiseConfig = new BasicCruiseConfig();
    expectLoad(cruiseConfig);
    new GoConfigMother().addPipelineWithGroup(cruiseConfig, groupName, "pipeline_name", "stage_name", "job_name");
    expectLoadForEditing(cruiseConfig);
    when(goConfigDao.md5OfConfigFile()).thenReturn(md5);
    String pipelineGroupContent = groupXmlWithInvalidAttributeValue(groupName);
    GoConfigValidity validity = goConfigService.groupSaver(groupName).saveXml(pipelineGroupContent, "md5");
    assertThat(validity.isValid(), Matchers.is(false));
    assertThat(validity.errorMessage(), containsString("Name is invalid. \"pipeline@$^\""));
    verify(goConfigDao, never()).updateConfig(any(UpdateConfigCommand.class));
}
Also used : StringContains.containsString(org.hamcrest.core.StringContains.containsString) GoConfigMother(com.thoughtworks.go.helper.GoConfigMother) GoConfigValidity(com.thoughtworks.go.config.validation.GoConfigValidity) Test(org.junit.Test)

Example 20 with GoConfigValidity

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

the class AdminServiceTest method shouldReturnInvalidIfConfigIsNotSaved.

@Test
public void shouldReturnInvalidIfConfigIsNotSaved() throws Exception {
    HashMap attributes = new HashMap();
    String content = "invalid_config_xml";
    attributes.put("content", content);
    String md5 = "config_md5";
    attributes.put("md5", md5);
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    GoConfigService.XmlPartialSaver fileSaver = mock(GoConfigService.XmlPartialSaver.class);
    GoConfigValidity validity = GoConfigValidity.invalid("Wrong config xml");
    when(fileSaver.saveXml(content, md5)).thenReturn(validity);
    when(goConfigService.fileSaver(false)).thenReturn(fileSaver);
    GoConfigValidity actual = adminService.updateConfig(attributes, result);
    assertThat(result.isSuccessful(), is(false));
    assertThat(actual.isValid(), is(false));
    assertThat(actual.errorMessage(), is("Wrong config xml"));
    verify(fileSaver).saveXml(content, md5);
    verify(goConfigService).fileSaver(false);
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) HashMap(java.util.HashMap) 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