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)); }
@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; }
@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); }
@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); } }
@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); }
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(); }