Java Code Examples for com.thoughtworks.go.plugin.api.response.validation.ValidationResult#addError()
The following examples show how to use
com.thoughtworks.go.plugin.api.response.validation.ValidationResult#addError() .
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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
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")); }
Example 14
Source File: PluginConfig.java From go-generic-artifactory-poller with Apache License 2.0 | 5 votes |
private void detectInvalidKeys(Configuration config, ValidationResult errors, String[] validKeys) { for (Property property : config.list()) { boolean valid = false; for (String validKey : validKeys) { if (validKey.equals(property.getKey())) { valid = true; break; } } if (!valid) errors.addError(new ValidationError(String.format("Unsupported key: %s. Valid keys: %s", property.getKey(), Arrays.toString(validKeys)))); } }
Example 15
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 16
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 17
Source File: PluggableScmServiceTest.java From gocd with Apache License 2.0 | 5 votes |
@Test public void shouldHandleIncorrectKeyForValidateSCM() { Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1")); configuration.getProperty("KEY1").setConfigurationValue(new ConfigurationValue("junk")); SCM modifiedSCM = new SCM("scm-id", new PluginConfiguration(pluginId, "1"), configuration); ValidationResult validationResult = new ValidationResult(); validationResult.addError(new ValidationError("NON-EXISTENT-KEY", "error message")); when(scmExtension.isSCMConfigurationValid(eq(modifiedSCM.getPluginConfiguration().getId()), any(SCMPropertyConfiguration.class))).thenReturn(validationResult); pluggableScmService.validate(modifiedSCM); assertTrue(modifiedSCM.errors().isEmpty()); }
Example 18
Source File: PluggableTaskServiceTest.java From gocd with Apache License 2.0 | 5 votes |
@Test public void shouldValidateTask() { Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1")); PluggableTask modifiedTask = new PluggableTask(new PluginConfiguration(pluginId, "1"), configuration); ValidationResult validationResult = new ValidationResult(); validationResult.addError(new ValidationError("KEY1", "error message")); when(taskExtension.validate(eq(modifiedTask.getPluginConfiguration().getId()), any(TaskConfig.class))).thenReturn(validationResult); pluggableTaskService.validate(modifiedTask); assertThat(modifiedTask.getConfiguration().getProperty("KEY1").errors().isEmpty(), is(false)); assertThat(modifiedTask.getConfiguration().getProperty("KEY1").errors().firstError(), is("error message")); verify(taskExtension).validate(eq(modifiedTask.getPluginConfiguration().getId()), any(TaskConfig.class)); }
Example 19
Source File: CurlTask.java From go-plugins with Apache License 2.0 | 5 votes |
@Override public ValidationResult validate(TaskConfig configuration) { ValidationResult validationResult = new ValidationResult(); if (configuration.getValue(URL_PROPERTY) == null || configuration.getValue(URL_PROPERTY).trim().isEmpty()) { validationResult.addError(new ValidationError(URL_PROPERTY, "URL cannot be empty")); } return validationResult; }
Example 20
Source File: SecretConfigCreateCommandTest.java From gocd with Apache License 2.0 | 4 votes |
@Test public void shouldInvokePluginValidationsBeforeSave() throws Exception { ValidationResult validationResult = new ValidationResult(); validationResult.addError(new ValidationError("key", "error")); SecretConfig newSecretConfig = new SecretConfig("foo", "file", new ConfigurationProperty(new ConfigurationKey("key"), new ConfigurationValue("val"))); RuleAwarePluginProfileCommand command = new SecretConfigCreateCommand(mock(GoConfigService.class), newSecretConfig, extension, null, new HttpLocalizedOperationResult()); when(extension.validateSecretsConfig(eq("file"), anyMap())).thenReturn(validationResult); BasicCruiseConfig cruiseConfig = new BasicCruiseConfig(); thrown.expect(RecordNotFoundException.class); thrown.expectMessage(EntityType.SecretConfig.notFoundMessage(newSecretConfig.getId())); command.isValid(cruiseConfig); command.update(cruiseConfig); assertThat(newSecretConfig.getConfiguration().first().errors().size(), is(1)); assertThat(newSecretConfig.getConfiguration().first().errors().asString(), is("error")); }