private Property pluginConfigurationFor(String configKey) { TaskPreference taskPreference = taskPreference(); return taskPreference != null ? taskPreference.getConfig().get(configKey) : null; }
private List<PluggableTask> allPluginTasks() { final ArrayList<PluggableTask> tasks = new ArrayList<>(); for (final String pluginId : PluggableTaskConfigStore.store().pluginIds()) { GoPluginDescriptor pluginDescriptor = pluginManager.getPluginDescriptorFor(pluginId); TaskPreference taskPreference = PluggableTaskConfigStore.store().preferenceFor(pluginId); if (pluginDescriptor != null && taskPreference != null) { tasks.add(new PluggableTask(new PluginConfiguration(pluginId, pluginDescriptor.version()), getConfiguration(taskPreference.getConfig()))); } } return tasks; }
public PluggableTaskPluginInfo pluginInfoFor(GoPluginDescriptor descriptor) { final TaskPreference[] tp = {null}; extension.doOnTask(descriptor.id(), (task, pluginDescriptor) -> tp[0] = new TaskPreference(task)); TaskConfig config = tp[0].getConfig(); TaskView view = tp[0].getView(); if (config == null) { throw new RuntimeException(format("Plugin[%s] returned null task configuration", descriptor.id())); } if (view == null) { throw new RuntimeException(format("Plugin[%s] returned null task view", descriptor.id())); } String displayName = view.displayValue(); PluggableInstanceSettings taskSettings = new PluggableInstanceSettings(configurations(config), new PluginView(view.template())); return new PluggableTaskPluginInfo(descriptor, displayName, taskSettings); }
@Override public List<TaskProperty> getPropertiesForDisplay() { ArrayList<TaskProperty> taskProperties = new ArrayList<>(); if (PluggableTaskConfigStore.store().hasPreferenceFor(pluginConfiguration.getId())) { TaskPreference preference = taskPreference(); List<? extends Property> propertyDefinitions = preference.getConfig().list(); for (Property propertyDefinition : propertyDefinitions) { ConfigurationProperty configuredProperty = configuration.getProperty(propertyDefinition.getKey()); if (configuredProperty == null) continue; taskProperties.add(new TaskProperty(propertyDefinition.getOption(Property.DISPLAY_NAME), configuredProperty.getDisplayValue(), configuredProperty.getConfigKeyName())); } return taskProperties; } for (ConfigurationProperty property : configuration) { taskProperties.add(new TaskProperty(property.getConfigKeyName(), property.getDisplayValue())); } return taskProperties; }
@Override protected void setTaskConfigAttributes(Map attributes) { TaskConfig taskConfig = PluggableTaskConfigStore.store().preferenceFor(pluginConfiguration.getId()).getConfig(); for (Property property : taskConfig.list()) { String key = property.getKey(); if (attributes.containsKey(key)) { Boolean isSecure = property.getOption(Property.SECURE); if (configuration.getProperty(key) == null) { configuration.addNewConfiguration(property.getKey(), isSecure); } configuration.getProperty(key).setConfigurationValue(new ConfigurationValue((String) attributes.get(key))); configuration.getProperty(key).handleSecureValueConfiguration(isSecure); } } }
@Test public void shouldNotOverwriteValuesIfTheyAreNotAvailableInConfigAttributesMap() throws Exception { TaskPreference taskPreference = mock(TaskPreference.class); Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1"), ConfigurationPropertyMother.create("Key2")); PluggableTaskConfigStore.store().setPreferenceFor("abc.def", taskPreference); PluggableTask task = new PluggableTask(new PluginConfiguration("abc.def", "1"), configuration); Map<String, String> attributeMap = DataStructureUtils.m("KEY1", "value1"); TaskConfig taskConfig = new TaskConfig(); TaskProperty property1 = new TaskProperty("KEY1", "value1"); TaskProperty property2 = new TaskProperty("Key2", null); taskConfig.addProperty(property1.getName()); taskConfig.addProperty(property2.getName()); when(taskPreference.getConfig()).thenReturn(taskConfig); task.setTaskConfigAttributes(attributeMap); assertThat(task.configAsMap().get("KEY1").get(PluggableTask.VALUE_KEY), is("value1")); assertThat(task.configAsMap().get("Key2").get(PluggableTask.VALUE_KEY), is(nullValue())); }
@Test public void shouldAddPropertyComingFromAttributesMapIfPresentInConfigStoreEvenIfItISNotPresentInCurrentConfiguration() throws Exception { TaskPreference taskPreference = mock(TaskPreference.class); Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1")); PluggableTaskConfigStore.store().setPreferenceFor("abc.def", taskPreference); PluggableTask task = new PluggableTask(new PluginConfiguration("abc.def", "1"), configuration); Map<String, String> attributeMap = DataStructureUtils.m("KEY1", "value1", "Key2", "value2"); TaskConfig taskConfig = new TaskConfig(); TaskProperty property1 = new TaskProperty("KEY1", "value1"); TaskProperty property2 = new TaskProperty("Key2", "value2"); taskConfig.addProperty(property1.getName()); taskConfig.addProperty(property2.getName()); when(taskPreference.getConfig()).thenReturn(taskConfig); task.setTaskConfigAttributes(attributeMap); assertThat(task.configAsMap().get("KEY1").get(PluggableTask.VALUE_KEY), is("value1")); assertThat(task.configAsMap().get("Key2").get(PluggableTask.VALUE_KEY), is("value2")); }
@Test public void shouldIgnoreKeysPresentInConfigAttributesMapButNotPresentInConfigStore() throws Exception { TaskPreference taskPreference = mock(TaskPreference.class); Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1")); PluggableTaskConfigStore.store().setPreferenceFor("abc.def", taskPreference); PluggableTask task = new PluggableTask(new PluginConfiguration("abc.def", "1"), configuration); Map<String, String> attributeMap = DataStructureUtils.m("KEY1", "value1", "Key2", "value2"); TaskConfig taskConfig = new TaskConfig(); TaskProperty property1 = new TaskProperty("KEY1", "value1"); taskConfig.addProperty(property1.getName()); when(taskPreference.getConfig()).thenReturn(taskConfig); task.setTaskConfigAttributes(attributeMap); assertThat(task.configAsMap().get("KEY1").get(PluggableTask.VALUE_KEY), is("value1")); assertFalse(task.configAsMap().containsKey("Key2")); }
@Test public void shouldPopulateItselfFromConfigAttributesMap() throws Exception { TaskPreference taskPreference = mock(TaskPreference.class); Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1"), ConfigurationPropertyMother.create("Key2")); PluggableTaskConfigStore.store().setPreferenceFor("abc.def", taskPreference); PluggableTask task = new PluggableTask(new PluginConfiguration("abc.def", "1"), configuration); Map<String, String> attributeMap = DataStructureUtils.m("KEY1", "value1", "Key2", "value2"); TaskConfig taskConfig = new TaskConfig(); TaskProperty property1 = new TaskProperty("KEY1", "value1"); TaskProperty property2 = new TaskProperty("Key2", "value2"); taskConfig.addProperty(property1.getName()); taskConfig.addProperty(property2.getName()); when(taskPreference.getConfig()).thenReturn(taskConfig); task.setTaskConfigAttributes(attributeMap); assertThat(task.configAsMap().get("KEY1").get(PluggableTask.VALUE_KEY), is("value1")); assertThat(task.configAsMap().get("Key2").get(PluggableTask.VALUE_KEY), is("value2")); }
@Test public void shouldAddConfigurationProperties() { List<ConfigurationProperty> configurationProperties = Arrays.asList(ConfigurationPropertyMother.create("key", "value", "encValue"), new ConfigurationProperty()); PluginConfiguration pluginConfiguration = new PluginConfiguration("github.pr", "1.1"); TaskPreference taskPreference = mock(TaskPreference.class); TaskConfig taskConfig = new TaskConfig(); Configuration configuration = new Configuration(); Property property = new Property("key"); property.with(Property.SECURE, false); PluggableTaskConfigStore.store().setPreferenceFor(pluginConfiguration.getId(), taskPreference); TaskConfigProperty taskConfigProperty = taskConfig.addProperty("key"); when(taskPreference.getConfig()).thenReturn(taskConfig); PluggableTask pluggableTask = new PluggableTask(pluginConfiguration, configuration); pluggableTask.addConfigurations(configurationProperties); assertThat(configuration.size(), is(2)); }
@Test public void postConstructShouldDoNothingForAInvalidConfigurationProperty() throws Exception { TaskPreference taskPreference = mock(TaskPreference.class); ConfigurationProperty configurationProperty = ConfigurationPropertyMother.create("KEY1"); Configuration configuration = new Configuration(configurationProperty); PluggableTaskConfigStore.store().setPreferenceFor("abc.def", taskPreference); TaskConfig taskConfig = new TaskConfig(); when(taskPreference.getConfig()).thenReturn(taskConfig); PluggableTask task = new PluggableTask(new PluginConfiguration("abc.def", "1"), configuration); assertFalse(configurationProperty.isSecure()); task.applyPluginMetadata(); assertFalse(configurationProperty.isSecure()); }
@Test public void postConstructShouldHandleSecureConfigurationForConfigurationProperties() throws Exception { TaskPreference taskPreference = mock(TaskPreference.class); ConfigurationProperty configurationProperty = ConfigurationPropertyMother.create("KEY1"); Configuration configuration = new Configuration(configurationProperty); PluggableTaskConfigStore.store().setPreferenceFor("abc.def", taskPreference); TaskConfig taskConfig = new TaskConfig(); taskConfig.addProperty("KEY1").with(Property.SECURE, true); when(taskPreference.getConfig()).thenReturn(taskConfig); PluggableTask task = new PluggableTask(new PluginConfiguration("abc.def", "1"), configuration); assertFalse(configurationProperty.isSecure()); task.applyPluginMetadata(); assertTrue(configurationProperty.isSecure()); }
@Test public void shouldHandleSecureConfigurations() throws Exception { TaskPreference taskPreference = mock(TaskPreference.class); Configuration configuration = new Configuration(); PluggableTaskConfigStore.store().setPreferenceFor("abc.def", taskPreference); PluggableTask task = new PluggableTask(new PluginConfiguration("abc.def", "1"), configuration); Map<String, String> attributeMap = DataStructureUtils.m("KEY1", "value1"); TaskConfig taskConfig = new TaskConfig(); taskConfig.addProperty("KEY1").with(Property.SECURE, true); when(taskPreference.getConfig()).thenReturn(taskConfig); task.setTaskConfigAttributes(attributeMap); assertThat(task.getConfiguration().size(), is(1)); assertTrue(task.getConfiguration().first().isSecure()); assertThat(task.getConfiguration().first().getValue(), is("value1")); }
public boolean validate(final PluggableTask modifiedTask) { final TaskConfig configuration = new TaskConfig(); for (ConfigurationProperty configurationProperty : modifiedTask.getConfiguration()) { configuration.add(new TaskConfigProperty(configurationProperty.getConfigurationKey().getName(), configurationProperty.getValue())); } final String pluginId = modifiedTask.getPluginConfiguration().getId(); ValidationResult validationResult = taskExtension.validate(pluginId, configuration); final TaskPreference preference = PluggableTaskConfigStore.store().preferenceFor(pluginId); if (PluggableTaskConfigStore.store().hasPreferenceFor(pluginId)) { for (ConfigurationProperty configurationProperty : modifiedTask.getConfiguration()) { String key = configurationProperty.getConfigurationKey().getName(); final Property property = preference.getConfig().get(key); if (property != null) { final Boolean required = property.getOption(Property.REQUIRED); if (required && StringUtils.isBlank(configurationProperty.getConfigValue())) validationResult.addError(new ValidationError(property.getKey(), "This field is required")); } } } for (ValidationError validationError : validationResult.getErrors()) { modifiedTask.getConfiguration().getProperty(validationError.getKey()).addError(validationError.getKey(), validationError.getMessage()); } return validationResult.isSuccessful(); }