private void loadTaskConfigIntoPreferenceStore(GoPluginDescriptor descriptor) { if (taskExtension.canHandlePlugin(descriptor.id())) { taskExtension.doOnTask(descriptor.id(), (task, pluginDescriptor) -> PluggableTaskConfigStore.store().setPreferenceFor(pluginDescriptor.id(), new TaskPreference(task))); } }
public PluggableViewModel<PluggableTask> viewModelFor(final PluggableTask pluggableTask, String actionName) { if (PluggableTaskConfigStore.store().hasPreferenceFor(pluggableTask.getPluginConfiguration().getId())) { TaskPreference taskPreference = PluggableTaskConfigStore.store().preferenceFor(pluggableTask.getPluginConfiguration().getId()); return new PluggableTaskViewModel(pluggableTask, viewTemplates.get(actionName), taskPreference.getView().displayValue(), getTemplate(taskPreference.getView())); } return new MissingPluggableTaskViewModel(pluggableTask, viewTemplates.get(actionName)); }
private void cleanupTaskPreferences() { Set<String> plugins = PluggableTaskConfigStore.store().pluginIds(); for (String pluginId : plugins) { PluggableTaskConfigStore.store().removePreferenceFor(pluginId); } }
@Deprecated // only for test usage public void clear() { Set<String> plugins = pluginIds(); for (String pluginId : plugins) { removePreferenceFor(pluginId); } }
@Override public void pluginUnLoaded(GoPluginDescriptor descriptor) { PluggableTaskConfigStore.store().removePreferenceFor(descriptor.id()); } }
private TaskPreference taskPreference() { return PluggableTaskConfigStore.store().preferenceFor(pluginConfiguration.getId()); }
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; }
@Test public void shouldRemoveConfigForTheTaskCorrespondingToGivenPluginId() throws Exception { final GoPluginDescriptor descriptor = mock(GoPluginDescriptor.class); String pluginId = "test-plugin-id"; when(descriptor.id()).thenReturn(pluginId); final Task task = mock(Task.class); TaskConfig config = new TaskConfig(); TaskView taskView = mock(TaskView.class); when(task.config()).thenReturn(config); when(task.view()).thenReturn(taskView); PluggableTaskConfigStore.store().setPreferenceFor(pluginId, new TaskPreference(task)); PluginManager pluginManager = mock(PluginManager.class); PluggableTaskPreferenceLoader pluggableTaskPreferenceLoader = new PluggableTaskPreferenceLoader(pluginManager, taskExtension); assertThat(PluggableTaskConfigStore.store().hasPreferenceFor(pluginId), is(true)); pluggableTaskPreferenceLoader.pluginUnLoaded(descriptor); assertThat(PluggableTaskConfigStore.store().hasPreferenceFor(pluginId), is(false)); verify(pluginManager).addPluginChangeListener(pluggableTaskPreferenceLoader); }
@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; }
public boolean isValid() { if (PluggableTaskConfigStore.store().preferenceFor(pluginConfiguration.getId()) == null) { addError(TYPE, String.format("Could not find plugin for given pluggable id:[%s].", pluginConfiguration.getId())); } configuration.validateTree(); return (errors.isEmpty() && !configuration.hasErrors()); }
@Test public void shouldLoadPreferencesOnlyForTaskPlugins() { final GoPluginDescriptor descriptor = mock(GoPluginDescriptor.class); String pluginId = "test-plugin-id"; when(descriptor.id()).thenReturn(pluginId); final Task task = mock(Task.class); TaskConfig config = new TaskConfig(); TaskView taskView = mock(TaskView.class); when(task.config()).thenReturn(config); when(task.view()).thenReturn(taskView); PluginManager pluginManager = mock(PluginManager.class); final TaskExtension taskExtension = mock(TaskExtension.class); when(taskExtension.canHandlePlugin(pluginId)).thenReturn(false); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { final Action<Task> action = (Action<Task>) invocationOnMock.getArguments()[1]; action.execute(task, descriptor); return null; } }).when(taskExtension).doOnTask(eq(pluginId), any(Action.class)); PluggableTaskPreferenceLoader pluggableTaskPreferenceLoader = new PluggableTaskPreferenceLoader(pluginManager, taskExtension); pluggableTaskPreferenceLoader.pluginLoaded(descriptor); assertThat(PluggableTaskConfigStore.store().hasPreferenceFor(pluginId), is(false)); verify(pluginManager).addPluginChangeListener(pluggableTaskPreferenceLoader); } }
@Before public void setUp() throws Exception { cleanupTaskPreferences(); taskPreference = mock(TaskPreference.class); PluggableTaskConfigStore.store().setPreferenceFor("plugin-1", taskPreference); TaskView view = mock(TaskView.class); when(taskPreference.getView()).thenReturn(view); when(view.template()).thenReturn("<input type='text' ng-model='abc'></input>"); when(view.displayValue()).thenReturn("First plugin"); }
@Test public void shouldSetConfigForTheTaskCorrespondingToGivenPluginId() throws Exception { final GoPluginDescriptor descriptor = mock(GoPluginDescriptor.class); String pluginId = "test-plugin-id"; when(descriptor.id()).thenReturn(pluginId); final Task task = mock(Task.class); TaskConfig config = new TaskConfig(); TaskView taskView = mock(TaskView.class); when(task.config()).thenReturn(config); when(task.view()).thenReturn(taskView); PluginManager pluginManager = mock(PluginManager.class); final TaskExtension taskExtension = mock(TaskExtension.class); when(taskExtension.canHandlePlugin(pluginId)).thenReturn(true); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { final Action<Task> action = (Action<Task>) invocationOnMock.getArguments()[1]; action.execute(task, descriptor); return null; } }).when(taskExtension).doOnTask(eq(pluginId), any(Action.class)); PluggableTaskPreferenceLoader pluggableTaskPreferenceLoader = new PluggableTaskPreferenceLoader(pluginManager, taskExtension); pluggableTaskPreferenceLoader.pluginLoaded(descriptor); assertThat(PluggableTaskConfigStore.store().hasPreferenceFor(pluginId), is(true)); assertThat(PluggableTaskConfigStore.store().preferenceFor(pluginId), is(new TaskPreference(task))); verify(pluginManager).addPluginChangeListener(pluggableTaskPreferenceLoader); }
@After public void teardown() { for (String pluginId : PluggableTaskConfigStore.store().pluginIds()) { PluggableTaskConfigStore.store().removePreferenceFor(pluginId); } }
@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); } } }
public static void clearSingletons() { AnalyticsMetadataStore.instance().clear(); ArtifactMetadataStore.instance().clear(); AuthorizationMetadataStore.instance().clear(); ConfigRepoMetadataStore.instance().clear(); ElasticAgentMetadataStore.instance().clear(); NewSCMMetadataStore.instance().clear(); NotificationMetadataStore.instance().clear(); PackageMaterialMetadataStore.instance().clear(); PluggableTaskMetadataStore.instance().clear(); new CachingSubjectDnX509PrincipalExtractor().getCache().removeAll(); // SessionUtils.unsetCurrentUser(); // PackageMetadataStore.getInstance().clear(); PluggableTaskConfigStore.store().clear(); PluginSettingsMetadataStore.getInstance().clear(); RepositoryMetadataStore.getInstance().clear(); SCMMetadataStore.getInstance().clear(); } }
@Test public void isValidShouldVerifyForValidConfigurationProperties() { PluginConfiguration pluginConfiguration = new PluginConfiguration("github.pr", "1.1"); Configuration configuration = mock(Configuration.class); PluggableTaskConfigStore.store().setPreferenceFor(pluginConfiguration.getId(), mock(TaskPreference.class)); when(configuration.hasErrors()).thenReturn(true); PluggableTask pluggableTask = new PluggableTask(pluginConfiguration, configuration); assertFalse(pluggableTask.isValid()); verify(configuration).validateTree(); verify(configuration).hasErrors(); }
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(); }