Search in sources :

Example 21 with GoConfigValidity

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

the class AdminService method updateConfig.

public GoConfigValidity updateConfig(Map attributes, HttpLocalizedOperationResult result) {
    GoConfigValidity validity;
    String configXml = (String) attributes.get("content");
    String configMd5 = (String) attributes.get("md5");
    GoConfigService.XmlPartialSaver fileSaver = goConfigService.fileSaver(false);
    validity = fileSaver.saveXml(configXml, configMd5);
    if (!validity.isValid()) {
        result.badRequest(LocalizedMessage.string("SAVE_FAILED"));
    }
    return validity;
}
Also used : GoConfigValidity(com.thoughtworks.go.config.validation.GoConfigValidity)

Example 22 with GoConfigValidity

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

the class PipelineConfigsService method updateXml.

public GoConfigOperationalResponse<PipelineConfigs> updateXml(String groupName, String xmlPartial, final String md5, Username username, HttpLocalizedOperationResult result) throws Exception {
    if (!userHasPermissions(username, groupName, result)) {
        return new GoConfigOperationalResponse<>(GoConfigValidity.valid(), null);
    }
    GoConfigValidity goConfigValidity = goConfigService.groupSaver(groupName).saveXml(xmlPartial, md5);
    if (!goConfigValidity.isValid()) {
        handleError(groupName, goConfigValidity, result);
        return new GoConfigOperationalResponse<>(goConfigValidity, null);
    }
    Localizable savedSuccessMessage = LocalizedMessage.string("SAVED_CONFIGURATION_SUCCESSFULLY");
    Localizable localizableMessage = goConfigValidity.wasMerged() ? LocalizedMessage.composite(savedSuccessMessage, LocalizedMessage.string("CONFIG_MERGED")) : savedSuccessMessage;
    result.setMessage(localizableMessage);
    PipelineConfigs pipelineConfigs = magicalGoConfigXmlLoader.fromXmlPartial(xmlPartial, BasicPipelineConfigs.class);
    return new GoConfigOperationalResponse<>(goConfigValidity, pipelineConfigs);
}
Also used : GoConfigOperationalResponse(com.thoughtworks.go.server.service.responses.GoConfigOperationalResponse) Localizable(com.thoughtworks.go.i18n.Localizable) GoConfigValidity(com.thoughtworks.go.config.validation.GoConfigValidity)

Example 23 with GoConfigValidity

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

the class GoConfigAdministrationController method postXmlPartial.

private RestfulAction postXmlPartial(String groupName, GoConfigService.XmlPartialSaver xmlPartialSaver, String xmlPartial, String successMessage, String expectedMd5) {
    if (!isTemplate(groupName) && !isCurrentUserAdminOfGroup(groupName)) {
        return JsonAction.jsonUnauthorized(errorMessageForGroup(groupName));
    }
    if (isTemplate(groupName) && !isCurrentUserAdmin()) {
        return JsonAction.jsonUnauthorized();
    }
    GoConfigValidity configValidity = xmlPartialSaver.saveXml(xmlPartial, expectedMd5);
    if (configValidity.isValid()) {
        return JsonAction.jsonFound(JsonView.getSimpleAjaxResult("result", successMessage));
    } else {
        Map<String, Object> jsonMap = new LinkedHashMap<>();
        jsonMap.put("result", configValidity.errorMessage());
        jsonMap.put("originalContent", xmlPartial);
        return jsonByValidity(jsonMap, configValidity);
    }
}
Also used : GoConfigValidity(com.thoughtworks.go.config.validation.GoConfigValidity) LinkedHashMap(java.util.LinkedHashMap)

Example 24 with GoConfigValidity

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

the class GoConfigServiceIntegrationTest method shouldNotThrowUpOnConfigSaveWhenIndependentChangesAreMade_ViaMergeFlow.

@Test
public void shouldNotThrowUpOnConfigSaveWhenIndependentChangesAreMade_ViaMergeFlow() throws Exception {
    // Priming current configuration to add lines simulating the license section before removal
    for (int i = 0; i < 10; i++) {
        configHelper.addRole(new RoleConfig(new CaseInsensitiveString("admin_role_" + i), new RoleUser(new CaseInsensitiveString("admin_user_" + i))));
    }
    // 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 to make an independent change
    new GoConfigMother().addRole(user1SeeingConfig, new RoleConfig(new CaseInsensitiveString("admin_role"), new RoleUser(new CaseInsensitiveString("admin_user"))));
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    configHelper.getXml(user1SeeingConfig, os);
    // User 1 saves edited config
    GoConfigService.XmlPartialSaver saver = goConfigService.fileSaver(false);
    GoConfigValidity validity = saver.saveXml(os.toString(), user1SeeingMd5);
    assertThat(validity.errorMessage(), 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 25 with GoConfigValidity

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

the class GoConfigServiceIntegrationTest method shouldThrowUpOnConfigSaveMergeConflict_ViaMergeFlow.

@Test
public void shouldThrowUpOnConfigSaveMergeConflict_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);
    // User 1 saves edited config
    GoConfigService.XmlPartialSaver saver = goConfigService.fileSaver(false);
    GoConfigValidity validity = saver.saveXml(os.toString(), user1SeeingMd5);
    assertThat(validity.isValid(), is(false));
    assertThat(validity.isType(GoConfigValidity.VT_MERGE_OPERATION_ERROR), is(true));
    assertThat(validity.errorMessage(), is("Configuration file has been modified by someone else."));
}
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)

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