com.thoughtworks.go.plugin.api.response.validation.ValidationError Java Examples
The following examples show how to use
com.thoughtworks.go.plugin.api.response.validation.ValidationError.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RoleConfigurationValidator.java From gocd with Apache License 2.0 | 6 votes |
public void validate(PluginRoleConfig role, String pluginId) { try { ValidationResult result = authorizationExtension.validateRoleConfiguration(pluginId, role.getConfigurationAsMap(true)); if (!result.isSuccessful()) { for (ValidationError error : result.getErrors()) { ConfigurationProperty property = role.getProperty(error.getKey()); if (property == null) { role.addNewConfiguration(error.getKey(), false); property = role.getProperty(error.getKey()); } property.addError(error.getKey(), error.getMessage()); } } } catch (RecordNotFoundException e) { role.addError("pluginRole", String.format("Unable to validate `pluginRole` configuration, missing plugin: %s", pluginId)); } }
Example #2
Source File: ArtifactExtensionTestBase.java From gocd with Apache License 2.0 | 6 votes |
@Test public void shouldValidatePluggableArtifactConfig() { String responseBody = "[{\"message\":\"Filename must not be blank.\",\"key\":\"Filename\"}]"; when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ARTIFACT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, responseBody)); ValidationResult validationResult = artifactExtension.validatePluggableArtifactConfig(PLUGIN_ID, Collections.singletonMap("Filename", "")); final GoPluginApiRequest request = requestArgumentCaptor.getValue(); assertThat(request.extension(), is(ARTIFACT_EXTENSION)); assertThat(request.requestName(), is(REQUEST_PUBLISH_ARTIFACT_VALIDATE)); assertThat(request.requestBody(), is("{\"Filename\":\"\"}")); assertThat(validationResult.isSuccessful(), is(false)); assertThat(validationResult.getErrors(), containsInAnyOrder( new ValidationError("Filename", "Filename must not be blank.") )); }
Example #3
Source File: AuthorizationExtensionTest.java From gocd with Apache License 2.0 | 6 votes |
@Test void shouldTalkToPlugin_To_ValidateAuthConfig() { String responseBody = "[{\"message\":\"Url must not be blank.\",\"key\":\"Url\"},{\"message\":\"SearchBase must not be blank.\",\"key\":\"SearchBase\"}]"; when(pluginManager.submitTo(eq(PLUGIN_ID), eq(AUTHORIZATION_EXTENSION), requestArgumentCaptor.capture())).thenReturn(new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, responseBody)); ValidationResult validationResult = authorizationExtension.validateAuthConfig(PLUGIN_ID, Collections.emptyMap()); assertRequest(requestArgumentCaptor.getValue(), AUTHORIZATION_EXTENSION, "1.0", REQUEST_VALIDATE_AUTH_CONFIG, "{}"); assertThat(validationResult.isSuccessful()).isEqualTo(false); assertThat(validationResult.getErrors()).hasSize(2) .contains( new ValidationError("Url", "Url must not be blank."), new ValidationError("SearchBase", "SearchBase must not be blank.") ); }
Example #4
Source File: JsonBasedTaskExtensionHandler_V1Test.java From gocd with Apache License 2.0 | 6 votes |
@Test public void shouldConvertJsonResponseToValidationResultWhenValidationFails() { String jsonResponse = "{\"errors\":{\"key1\":\"err1\",\"key2\":\"err2\"}}"; TaskConfig configuration = new TaskConfig(); TaskConfigProperty property = new TaskConfigProperty("URL", "http://foo"); property.with(Property.SECURE, false); property.with(Property.REQUIRED, true); configuration.add(property); ValidationResult result = new JsonBasedTaskExtensionHandler_V1().toValidationResult(jsonResponse); Assert.assertThat(result.isSuccessful(), is(false)); ValidationError error1 = result.getErrors().get(0); ValidationError error2 = result.getErrors().get(1); Assert.assertThat(error1.getKey(), is("key1")); Assert.assertThat(error1.getMessage(), is("err1")); Assert.assertThat(error2.getKey(), is("key2")); Assert.assertThat(error2.getMessage(), is("err2")); }
Example #5
Source File: JsonBasedTaskExtensionHandler_V1.java From gocd with Apache License 2.0 | 6 votes |
@Override public ValidationResult toValidationResult(String responseBody) { ValidationResult validationResult = new ValidationResult(); ArrayList<String> exceptions = new ArrayList<>(); try { Map result = (Map) new GsonBuilder().create().fromJson(responseBody, Object.class); if (result == null) return validationResult; final Map<String, Object> errors = (Map<String, Object>) result.get("errors"); if (errors != null) { for (Map.Entry<String, Object> entry : errors.entrySet()) { if (!(entry.getValue() instanceof String)) { exceptions.add(String.format("Key: '%s' - The Json for Validation Request must contain a not-null error message of type String", entry.getKey())); } else { validationResult.addError(new ValidationError(entry.getKey(), entry.getValue().toString())); } } } if (!exceptions.isEmpty()) { throw new RuntimeException(StringUtils.join(exceptions, ", ")); } return validationResult; } catch (Exception e) { LOGGER.error("Error occurred while converting the Json to Validation Result. Error: {}. The Json received was '{}'.", e.getMessage(), responseBody); throw new RuntimeException(String.format("Error occurred while converting the Json to Validation Result. Error: %s.", e.getMessage())); } }
Example #6
Source File: ArtifactExtensionTestBase.java From gocd with Apache License 2.0 | 6 votes |
@Test public void shouldValidateArtifactStoreConfig() { String responseBody = "[{\"message\":\"ACCESS_KEY must not be blank.\",\"key\":\"ACCESS_KEY\"}]"; when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ARTIFACT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, responseBody)); ValidationResult validationResult = artifactExtension.validateArtifactStoreConfig(PLUGIN_ID, Collections.singletonMap("ACCESS_KEY", "")); final GoPluginApiRequest request = requestArgumentCaptor.getValue(); assertThat(request.extension(), is(ARTIFACT_EXTENSION)); assertThat(request.requestName(), is(REQUEST_STORE_CONFIG_VALIDATE)); assertThat(request.requestBody(), is("{\"ACCESS_KEY\":\"\"}")); assertThat(validationResult.isSuccessful(), is(false)); assertThat(validationResult.getErrors(), containsInAnyOrder( new ValidationError("ACCESS_KEY", "ACCESS_KEY must not be blank.") )); }
Example #7
Source File: ArtifactExtensionTestBase.java From gocd with Apache License 2.0 | 6 votes |
@Test public void shouldValidateFetchArtifactConfig() { String responseBody = "[{\"message\":\"Filename must not be blank.\",\"key\":\"Filename\"}]"; when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ARTIFACT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, responseBody)); ValidationResult validationResult = artifactExtension.validateFetchArtifactConfig(PLUGIN_ID, Collections.singletonMap("Filename", "")); final GoPluginApiRequest request = requestArgumentCaptor.getValue(); assertThat(request.extension(), is(ARTIFACT_EXTENSION)); assertThat(request.requestName(), is(REQUEST_FETCH_ARTIFACT_VALIDATE)); assertThat(request.requestBody(), is("{\"Filename\":\"\"}")); assertThat(validationResult.isSuccessful(), is(false)); assertThat(validationResult.getErrors(), containsInAnyOrder( new ValidationError("Filename", "Filename must not be blank.") )); }
Example #8
Source File: PluggableTaskServiceTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void shouldValidateMandatoryFields() { Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1")); PluggableTask modifiedTask = new PluggableTask(new PluginConfiguration(pluginId, "1"), configuration); ValidationResult validationResult = new ValidationResult(); when(taskExtension.validate(eq(modifiedTask.getPluginConfiguration().getId()), any(TaskConfig.class))).thenReturn(validationResult); pluggableTaskService.validate(modifiedTask); final List<ValidationError> validationErrors = validationResult.getErrors(); assertFalse(validationErrors.isEmpty()); final ValidationError validationError = validationErrors.stream().filter(new Predicate<ValidationError>() { @Override public boolean test(ValidationError item) { return ((ValidationError) item).getKey().equals("KEY1"); } }).findFirst().orElse(null); assertNotNull(validationError); assertThat(validationError.getMessage(), is("This field is required")); }
Example #9
Source File: PluggableTaskServiceTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void isValidShouldMapPluginValidationErrorsToPluggableTaskConfigrations() { PluginConfiguration pluginConfiguration = new PluginConfiguration("plugin_id", "version"); Configuration configuration = new Configuration(); configuration.add(ConfigurationPropertyMother.create("source", false, "src_dir")); configuration.add(ConfigurationPropertyMother.create("destination", false, "dest_dir")); ValidationResult validationResult = new ValidationResult(); validationResult.addError(new ValidationError("source", "source directory format is invalid")); validationResult.addError(new ValidationError("destination", "destination directory format is invalid")); PluggableTask pluggableTask = mock(PluggableTask.class); when(pluggableTask.isValid()).thenReturn(true); when(pluggableTask.toTaskConfig()).thenReturn(new TaskConfig()); when(pluggableTask.getPluginConfiguration()).thenReturn(pluginConfiguration); when(pluggableTask.getConfiguration()).thenReturn(configuration); when(taskExtension.validate(any(String.class), any(TaskConfig.class))).thenReturn(validationResult); assertFalse(pluggableTaskService.isValid(pluggableTask)); assertThat(configuration.getProperty("source").errors().get("source").get(0), is("source directory format is invalid")); assertThat(configuration.getProperty("destination").errors().get("destination").get(0), is("destination directory format is invalid")); }
Example #10
Source File: PluggableTaskServiceTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void isValidShouldMapPluginValidationErrorsToPluggableTaskForMissingConfigurations() { PluginConfiguration pluginConfiguration = new PluginConfiguration("plugin_id", "version"); ValidationResult validationResult = new ValidationResult(); validationResult.addError(new ValidationError("source", "source is mandatory")); PluggableTask pluggableTask = mock(PluggableTask.class); when(pluggableTask.isValid()).thenReturn(true); when(pluggableTask.toTaskConfig()).thenReturn(new TaskConfig()); when(pluggableTask.getPluginConfiguration()).thenReturn(pluginConfiguration); when(pluggableTask.getConfiguration()).thenReturn(new Configuration()); when(taskExtension.validate(any(String.class), any(TaskConfig.class))).thenReturn(validationResult); assertFalse(pluggableTaskService.isValid(pluggableTask)); verify(pluggableTask).addError("source", "source is mandatory"); }
Example #11
Source File: PackageDefinitionServiceTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void shouldPerformPluginValidationsBeforeValidationsByGoAndGoDoesNotAddErrorIfAlreadyPresent() throws Exception { Configuration configuration = new Configuration(); configuration.add(ConfigurationPropertyMother.create("required-field", false, "")); PackageDefinition packageDefinition = PackageDefinitionMother.create("1", "name", configuration, packageRepository); ValidationResult expectedValidationResult = new ValidationResult(); expectedValidationResult.addError(new ValidationError("required-field", "error-one")); expectedValidationResult.addError(new ValidationError("required-field", "error-two")); when(packageRepositoryExtension.isPackageConfigurationValid(eq(packageRepository.getPluginConfiguration().getId()), any(com.thoughtworks.go.plugin.api.material.packagerepository.PackageConfiguration.class), any(RepositoryConfiguration.class))).thenReturn(expectedValidationResult); service.performPluginValidationsFor(packageDefinition); assertThat(packageDefinition.getConfiguration().get(0).getConfigurationValue().errors().getAllOn("value").size(), is(2)); assertThat(packageDefinition.getConfiguration().get(0).getConfigurationValue().errors().getAllOn("value"), is(hasItems("error-one", "error-two"))); }
Example #12
Source File: PluggableScmServiceTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void shouldValidateSCM() { SCMConfiguration scmConfig = new SCMConfiguration(new SCMProperty("KEY2").with(Property.REQUIRED, false)); scmConfigurations.add(scmConfig); Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1")); SCM modifiedSCM = new SCM("scm-id", new PluginConfiguration(pluginId, "1"), configuration); ValidationResult validationResult = new ValidationResult(); validationResult.addError(new ValidationError("KEY1", "error message")); when(scmExtension.isSCMConfigurationValid(eq(modifiedSCM.getPluginConfiguration().getId()), any(SCMPropertyConfiguration.class))).thenReturn(validationResult); pluggableScmService.validate(modifiedSCM); assertFalse(modifiedSCM.getConfiguration().getProperty("KEY1").errors().isEmpty()); assertThat(modifiedSCM.getConfiguration().getProperty("KEY1").errors().firstError(), is("error message")); verify(scmExtension).isSCMConfigurationValid(eq(modifiedSCM.getPluginConfiguration().getId()), any(SCMPropertyConfiguration.class)); }
Example #13
Source File: PluggableScmServiceTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void shouldValidateMandatoryAndSecureFieldsForSCM() { SCMConfiguration scmConfig = new SCMConfiguration(new SCMProperty("KEY2").with(Property.REQUIRED, true).with(Property.SECURE, true)); scmConfigurations.add(scmConfig); Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1"), ConfigurationPropertyMother.create("KEY2", true, "")); SCM modifiedSCM = new SCM("scm-id", new PluginConfiguration(pluginId, "1"), configuration); ValidationResult validationResult = new ValidationResult(); when(scmExtension.isSCMConfigurationValid(eq(modifiedSCM.getPluginConfiguration().getId()), any(SCMPropertyConfiguration.class))).thenReturn(validationResult); pluggableScmService.validate(modifiedSCM); final List<ValidationError> validationErrors = validationResult.getErrors(); assertFalse(validationErrors.isEmpty()); final ValidationError validationErrorForKey1 = getValidationErrorFor(validationErrors, "KEY1"); assertNotNull(validationErrorForKey1); assertThat(validationErrorForKey1.getMessage(), is("This field is required")); final ValidationError validationErrorForKey2 = getValidationErrorFor(validationErrors, "KEY2"); assertNotNull(validationErrorForKey2); assertThat(validationErrorForKey2.getMessage(), is("This field is required")); }
Example #14
Source File: PluggableScmServiceTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void isValidShouldMapPluginValidationErrorsToPluggableSCMConfigurations() { PluginConfiguration pluginConfiguration = new PluginConfiguration("plugin_id", "version"); Configuration configuration = new Configuration(); configuration.add(ConfigurationPropertyMother.create("url", false, "url")); configuration.add(ConfigurationPropertyMother.create("username", false, "admin")); ValidationResult validationResult = new ValidationResult(); validationResult.addError(new ValidationError("url", "invalid")); validationResult.addError(new ValidationError("username", "invalid")); SCM scmConfig = mock(SCM.class); when(scmConfig.doesPluginExist()).thenReturn(true); when(scmConfig.getPluginConfiguration()).thenReturn(pluginConfiguration); when(scmConfig.getConfiguration()).thenReturn(configuration); when(scmExtension.isSCMConfigurationValid(any(String.class), any(SCMPropertyConfiguration.class))).thenReturn(validationResult); assertFalse(pluggableScmService.isValid(scmConfig)); assertThat(configuration.getProperty("url").errors().get("url").get(0), is("invalid")); assertThat(configuration.getProperty("username").errors().get("username").get(0), is("invalid")); }
Example #15
Source File: PluggableScmServiceTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void isValidShouldMapPluginValidationErrorsToPluggableSCMForMissingConfigurations() { PluginConfiguration pluginConfiguration = new PluginConfiguration("plugin_id", "version"); ValidationResult validationResult = new ValidationResult(); validationResult.addError(new ValidationError("url", "URL is a required field")); SCM scmConfig = mock(SCM.class); when(scmConfig.doesPluginExist()).thenReturn(true); when(scmConfig.getPluginConfiguration()).thenReturn(pluginConfiguration); when(scmConfig.getConfiguration()).thenReturn(new Configuration()); when(scmExtension.isSCMConfigurationValid(any(String.class), any(SCMPropertyConfiguration.class))).thenReturn(validationResult); assertFalse(pluggableScmService.isValid(scmConfig)); verify(scmConfig).addError("url", "URL is a required field"); }
Example #16
Source File: PackageRepositoryServiceTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void shouldInvokePluginValidationsBeforeSavingPackageRepository() throws Exception { String pluginId = "yum"; PackageRepository packageRepository = new PackageRepository(); RepositoryMetadataStore.getInstance().addMetadataFor(pluginId, new PackageConfigurations()); packageRepository.setPluginConfiguration(new PluginConfiguration(pluginId, "1.0")); packageRepository.getConfiguration().add(ConfigurationPropertyMother.create("url", false, "junk-url")); ArgumentCaptor<RepositoryConfiguration> packageConfigurationsArgumentCaptor = ArgumentCaptor.forClass(RepositoryConfiguration.class); ValidationResult expectedValidationResult = new ValidationResult(); expectedValidationResult.addError(new ValidationError("url", "url format incorrect")); when(pluginManager.getPluginDescriptorFor(pluginId)).thenReturn(getPluginDescriptor("yum")); when(packageRepositoryExtension.isRepositoryConfigurationValid(eq(pluginId), packageConfigurationsArgumentCaptor.capture())).thenReturn(expectedValidationResult); service = new PackageRepositoryService(pluginManager, packageRepositoryExtension, goConfigService, securityService, entityHashingService); service.performPluginValidationsFor(packageRepository); assertThat(packageRepository.getConfiguration().get(0).getConfigurationValue().errors().getAllOn("value"), is(Arrays.asList("url format incorrect"))); }
Example #17
Source File: PluginServiceTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void validatePluginSettingsFor_shouldPopulateValidationErrorsInPluginSettingsObject() { final ElasticAgentPluginInfo elasticAgentPluginInfo = mockElasticAgentPluginInfo(getPluggableInstanceSettings()); final ValidationResult validationResult = new ValidationResult(); validationResult.addError(new ValidationError("key-1", "m1")); validationResult.addError(new ValidationError("key-3", "m3")); addPluginSettingsMetadataToStore(elasticAgentPluginId, ELASTIC_AGENT_EXTENSION, getPluginSettingsConfiguration()); when(elasticAgentExtension.extensionName()).thenReturn(ELASTIC_AGENT_EXTENSION); when(elasticAgentExtension.canHandlePlugin(elasticAgentPluginId)).thenReturn(true); when(elasticAgentExtension.validatePluginSettings(eq(elasticAgentPluginId), any(PluginSettingsConfiguration.class))) .thenReturn(validationResult); PluginSettings pluginSettings = PluginSettings.from(getPlugin(elasticAgentPluginId), elasticAgentPluginInfo); pluginService.validatePluginSettings(pluginSettings); assertThat(pluginSettings.hasErrors(), is(true)); assertThat(pluginSettings.getErrorFor("key-1"), is(Arrays.asList("m1"))); assertThat(pluginSettings.getErrorFor("key-3"), is(Arrays.asList("m3"))); }
Example #18
Source File: PluginServiceTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void createPluginSettings_shouldValidatePluginSettingUsingExtensionAndReturnUnprocessableEntityWhenThereIsAValidationError() { setUpElasticPluginForTheTest(true); when(pluginDao.findPlugin(elasticAgentPluginId)).thenReturn(new NullPlugin()); final ValidationResult validationResult = new ValidationResult(); validationResult.addError(new ValidationError("key-1", "error-message")); when(elasticAgentExtension.validatePluginSettings(eq(elasticAgentPluginId), any(PluginSettingsConfiguration.class))).thenReturn(validationResult); when(pluginSettings.hasErrors()).thenReturn(true); pluginService.createPluginSettings(pluginSettings, currentUser, result); verify(pluginDao, times(0)).saveOrUpdate(any()); verify(elasticAgentExtension, times(0)).notifyPluginSettingsChange(eq(elasticAgentPluginId), anyMap()); verify(result, times(1)).unprocessableEntity("Save failed. There are errors in the plugin settings. Please fix them and resubmit."); verifyNoMoreInteractions(result); }
Example #19
Source File: PluginServiceTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void updatePluginSettings_shouldValidatePluginSettingUsingExtensionAndReturnUnprocessableEntityWhenThereIsAValidationFailure() { setUpElasticPluginForTheTest(true); when(pluginDao.findPlugin(elasticAgentPluginId)).thenReturn(getPlugin(elasticAgentPluginId)); final ValidationResult validationResult = new ValidationResult(); validationResult.addError(new ValidationError("key-1", "error-message")); when(elasticAgentExtension.validatePluginSettings(eq(elasticAgentPluginId), any(PluginSettingsConfiguration.class))).thenReturn(validationResult); when(pluginSettings.hasErrors()).thenReturn(true); when(entityHashingService.hashForEntity(any(PluginSettings.class))).thenReturn("foo"); when(result.isSuccessful()).thenReturn(false); pluginService.updatePluginSettings(pluginSettings, currentUser, result, "foo"); verify(pluginDao, times(0)).saveOrUpdate(any()); verify(elasticAgentExtension, times(0)).notifyPluginSettingsChange(eq(elasticAgentPluginId), anyMap()); verify(result, times(1)).unprocessableEntity("Save failed. There are errors in the plugin settings. Please fix them and resubmit."); }
Example #20
Source File: ExternalArtifactsServiceTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void shouldMapPluginValidationErrorsToConfigrationProperties() { Configuration configuration = new Configuration(); configuration.add(ConfigurationPropertyMother.create("Image", false, "alpine")); configuration.add(ConfigurationPropertyMother.create("Tag", false, "fml")); pluggableArtifactConfig.setConfiguration(configuration); ValidationResult validationResult = new ValidationResult(); validationResult.addError(new ValidationError("Image", "invalid")); validationResult.addError(new ValidationError("Tag", "invalid")); ArtifactStore artifactStore = mock(ArtifactStore.class); when(artifactStore.getPluginId()).thenReturn(pluginId); when(artifactExtension.validatePluggableArtifactConfig(any(String.class), any())).thenReturn(validationResult); externalArtifactsService.validateExternalArtifactConfig(pluggableArtifactConfig, artifactStore, true); assertThat(configuration.getProperty("Image").errors().get("Image").get(0), is("invalid")); assertThat(configuration.getProperty("Tag").errors().get("Tag").get(0), is("invalid")); }
Example #21
Source File: SecurityAuthConfigCreateCommandTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void shouldInvokePluginValidationsBeforeSave() throws Exception { ValidationResult validationResult = new ValidationResult(); validationResult.addError(new ValidationError("key", "error")); when(extension.validateAuthConfig(eq("aws"), anyMap())).thenReturn(validationResult); SecurityAuthConfig newProfile = new SecurityAuthConfig("foo", "aws", new ConfigurationProperty(new ConfigurationKey("key"), new ConfigurationValue("val"))); PluginProfileCommand command = new SecurityAuthConfigCreateCommand(mock(GoConfigService.class), newProfile, extension, null, new HttpLocalizedOperationResult()); BasicCruiseConfig cruiseConfig = new BasicCruiseConfig(); thrown.expect(RecordNotFoundException.class); thrown.expectMessage(EntityType.SecurityAuthConfig.notFoundMessage(newProfile.getId())); command.isValid(cruiseConfig); command.update(cruiseConfig); assertThat(newProfile.first().errors().size(), is(1)); assertThat(newProfile.first().errors().asString(), is("error")); }
Example #22
Source File: ElasticAgentProfileCreateCommandTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void shouldInvokePluginValidationsBeforeSave() throws Exception { ValidationResult validationResult = new ValidationResult(); validationResult.addError(new ValidationError("key", "error")); when(extension.validate(eq("aws"), anyMap())).thenReturn(validationResult); ElasticProfile newProfile = new ElasticProfile("foo", "prod-cluster", new ConfigurationProperty(new ConfigurationKey("key"), new ConfigurationValue("val"))); EntityConfigUpdateCommand command = new ElasticAgentProfileCreateCommand(mock(GoConfigService.class), newProfile, extension, null, new HttpLocalizedOperationResult()); BasicCruiseConfig cruiseConfig = new BasicCruiseConfig(); thrown.expect(RecordNotFoundException.class); thrown.expectMessage(EntityType.ElasticProfile.notFoundMessage(newProfile.getId())); command.isValid(cruiseConfig); command.update(cruiseConfig); assertThat(newProfile.first().errors().size(), is(1)); assertThat(newProfile.first().errors().asString(), is("error")); }
Example #23
Source File: PackageDefinitionService.java From gocd with Apache License 2.0 | 6 votes |
public void performPluginValidationsFor(final PackageDefinition packageDefinition) { String pluginId = packageDefinition.getRepository().getPluginConfiguration().getId(); ValidationResult validationResult = packageRepositoryExtension.isPackageConfigurationValid(pluginId, buildPackageConfigurations(packageDefinition), buildRepositoryConfigurations(packageDefinition.getRepository())); for (ValidationError error : validationResult.getErrors()) { packageDefinition.addConfigurationErrorFor(error.getKey(), error.getMessage()); } for (ConfigurationProperty configurationProperty : packageDefinition.getConfiguration()) { String key = configurationProperty.getConfigurationKey().getName(); if (PackageMetadataStore.getInstance().hasOption(packageDefinition.getRepository().getPluginConfiguration().getId(), key, PackageConfiguration.REQUIRED)) { if (configurationProperty.getValue().isEmpty() && configurationProperty.doesNotHaveErrorsAgainstConfigurationValue()) { configurationProperty.addErrorAgainstConfigurationValue("Field: '" + configurationProperty.getConfigurationKey().getName() + "' is required"); } } } }
Example #24
Source File: PackageRepositoryService.java From gocd with Apache License 2.0 | 6 votes |
void performPluginValidationsFor(final PackageRepository packageRepository) { if (!validatePluginId(packageRepository)) { return; } for (ConfigurationProperty configurationProperty : packageRepository.getConfiguration()) { String key = configurationProperty.getConfigurationKey().getName(); String pluginId = packageRepository.getPluginConfiguration().getId(); if (repositoryMetadataStore.hasOption(pluginId, key, PackageConfiguration.REQUIRED)) { if (configurationProperty.getValue().isEmpty()) { configurationProperty.addErrorAgainstConfigurationValue("This field is required"); } } } ValidationResult validationResult = packageRepositoryExtension.isRepositoryConfigurationValid(packageRepository.getPluginConfiguration().getId(), populateConfiguration(packageRepository.getConfiguration())); for (ValidationError error : validationResult.getErrors()) { packageRepository.addConfigurationErrorFor(error.getKey(), error.getMessage()); } }
Example #25
Source File: PluginService.java From gocd with Apache License 2.0 | 6 votes |
void validatePluginSettings(PluginSettings pluginSettings) { pluginSettings.validateTree(); if (pluginSettings.hasErrors()) { return; } final String pluginId = pluginSettings.getPluginId(); final PluginSettingsConfiguration configuration = pluginSettings.toPluginSettingsConfiguration(); final GoPluginExtension extension = findExtensionWhichCanHandleSettingsFor(pluginId); if (extension == null) { throw new IllegalArgumentException(String.format("Plugin '%s' does not exist or does not implement settings validation.", pluginId)); } final ValidationResult result = extension.validatePluginSettings(pluginId, configuration); if (!result.isSuccessful()) { for (ValidationError error : result.getErrors()) { pluginSettings.populateErrorMessageFor(error.getKey(), error.getMessage()); } } }
Example #26
Source File: ElasticAgentProfileConfigurationValidator.java From gocd with Apache License 2.0 | 6 votes |
public void validate(ElasticProfile elasticAgentProfile, String pluginId) { try { ValidationResult result = elasticAgentExtension.validate(pluginId, elasticAgentProfile.getConfigurationAsMap(true)); if (!result.isSuccessful()) { for (ValidationError error : result.getErrors()) { ConfigurationProperty property = elasticAgentProfile.getProperty(error.getKey()); if (property == null) { elasticAgentProfile.addNewConfiguration(error.getKey(), false); property = elasticAgentProfile.getProperty(error.getKey()); } property.addError(error.getKey(), error.getMessage()); } } } catch (RecordNotFoundException e) { elasticAgentProfile.addError("pluginId", String.format("Unable to validate Elastic Agent Profile configuration, missing plugin: %s", pluginId)); } }
Example #27
Source File: PackageDefinitionServiceTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void shouldPerformPluginValidationsUsingMetaDataBeforeSavingPackageRepository() throws Exception { Configuration configuration = new Configuration(); configuration.add(ConfigurationPropertyMother.create("required", false, "")); configuration.add(ConfigurationPropertyMother.create("required_secure", true, "")); configuration.add(ConfigurationPropertyMother.create("secure", true, "")); configuration.add(ConfigurationPropertyMother.create("not_required_not_secure", false, "")); configuration.add(ConfigurationPropertyMother.create("spec", false, "xyz?")); PackageDefinition packageDefinition = PackageDefinitionMother.create("1", "name", configuration, packageRepository); ValidationResult expectedValidationResult = new ValidationResult(); expectedValidationResult.addError(new ValidationError("spec", "invalid spec")); when(packageRepositoryExtension.isPackageConfigurationValid(eq(packageRepository.getPluginConfiguration().getId()), any(com.thoughtworks.go.plugin.api.material.packagerepository.PackageConfiguration.class), any(RepositoryConfiguration.class))).thenReturn(expectedValidationResult); service.performPluginValidationsFor(packageDefinition); assertThat(packageDefinition.getConfiguration().get(0).getConfigurationValue().errors().getAllOn("value"), is(Arrays.asList("Field: 'required' is required"))); assertThat(packageDefinition.getConfiguration().get(1).getEncryptedConfigurationValue().errors().getAllOn("value"), is(Arrays.asList("Field: 'required_secure' is required"))); assertThat(packageDefinition.getConfiguration().get(2).getEncryptedConfigurationValue().errors().isEmpty(), is(true)); assertThat(packageDefinition.getConfiguration().get(3).getConfigurationValue().errors().isEmpty(), is(true)); assertThat(packageDefinition.getConfiguration().get(4).getConfigurationValue().errors().getAllOn("value"), is(Arrays.asList("invalid spec"))); }
Example #28
Source File: RoleConfigurationValidatorTest.java From gocd with Apache License 2.0 | 5 votes |
@Test public void shouldAddConfigurationAndMapErrorsInAbsenceOfConfiguration() throws Exception { ConfigurationProperty property = new ConfigurationProperty(new ConfigurationKey("username"), new ConfigurationValue("view")); PluginRoleConfig roleConfig = new PluginRoleConfig("admin", "auth_id", property); ValidationResult result = new ValidationResult(); result.addError(new ValidationError("password", "password is required")); when(extension.validateRoleConfiguration("pluginId", Collections.singletonMap("username", "view"))).thenReturn(result); validator.validate(roleConfig, "pluginId"); assertTrue(roleConfig.hasErrors()); assertThat(roleConfig.getProperty("password").errors().get("password").get(0), is("password is required")); assertNull(roleConfig.getProperty("password").getValue()); }
Example #29
Source File: RoleConfigurationValidatorTest.java From gocd with Apache License 2.0 | 5 votes |
@Test public void shouldMapValidationErrorsToRoleConfiguration() throws Exception { ConfigurationProperty property = new ConfigurationProperty(new ConfigurationKey("username"), new ConfigurationValue("view")); PluginRoleConfig roleConfig = new PluginRoleConfig("admin", "auth_id", property); ValidationResult result = new ValidationResult(); result.addError(new ValidationError("username", "username format is incorrect")); when(extension.validateRoleConfiguration("pluginId", Collections.singletonMap("username", "view"))).thenReturn(result); validator.validate(roleConfig, "pluginId"); assertTrue(roleConfig.hasErrors()); assertThat(roleConfig.getProperty("username").errors().get("username").get(0), is("username format is incorrect")); }
Example #30
Source File: ArtifactStoreServiceTest.java From gocd with Apache License 2.0 | 5 votes |
@Test public void shouldInvokePluginValidationsBeforeUpdatingTheArtifactStore() { ArtifactStore artifactStore = new ArtifactStore("docker", "cd.go.artifact.docker", create("key", false, "val")); ValidationResult validationResult = new ValidationResult(); validationResult.addError(new ValidationError("key", "some-error")); when(extension.validateArtifactStoreConfig(eq("cd.go.artifact.docker"), anyMap())).thenReturn(validationResult); artifactStoreService.update(new Username("admin"), "md5", artifactStore, new HttpLocalizedOperationResult()); MatcherAssert.assertThat(artifactStore.first().errors().size(), is(1)); MatcherAssert.assertThat(artifactStore.first().errors().on("key"), is("some-error")); }