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); }
private void loadTaskConfigIntoPreferenceStore(GoPluginDescriptor descriptor) { if (taskExtension.canHandlePlugin(descriptor.id())) { taskExtension.doOnTask(descriptor.id(), (task, pluginDescriptor) -> PluggableTaskConfigStore.store().setPreferenceFor(pluginDescriptor.id(), new TaskPreference(task))); } }
when(task1.config()).thenReturn(config1); when(task1.view()).thenReturn(taskView1); TaskPreference taskPreference1 = new TaskPreference(task1); when(task2.config()).thenReturn(config2); when(task2.view()).thenReturn(taskView2); TaskPreference taskPreference2 = new TaskPreference(task2); TaskPreference taskPreference3 = new TaskPreference(task1); when(task3.view()).thenReturn(taskView1); TaskPreference taskPreference4 = new TaskPreference(task3); when(task5.config()).thenReturn(config1); when(task5.view()).thenReturn(taskView5); TaskPreference taskPreference5 = new TaskPreference(task5);
@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); }
@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 shouldGetOnlyConfiguredPropertiesIfACertainPropertyDefinedByPluginIsNotConfiguredByUser() throws Exception { Task taskDetails = mock(Task.class); TaskConfig taskConfig = new TaskConfig(); addProperty(taskConfig, "KEY2", "Key 2", 1); addProperty(taskConfig, "KEY1", "Key 1", 0); addProperty(taskConfig, "KEY3", "Key 3", 2); when(taskDetails.config()).thenReturn(taskConfig); when(taskDetails.view()).thenReturn(mock(TaskView.class)); String pluginId = "plugin_with_all_details"; PluggableTaskConfigStore.store().setPreferenceFor(pluginId, new TaskPreference(taskDetails)); Configuration configuration = new Configuration( ConfigurationPropertyMother.create("KEY1", false, "value1"), ConfigurationPropertyMother.create("KEY2", false, "value2") ); PluggableTask task = new PluggableTask(new PluginConfiguration(pluginId, "1"), configuration); List<TaskProperty> propertiesForDisplay = task.getPropertiesForDisplay(); assertThat(propertiesForDisplay.size(), is(2)); assertProperty(propertiesForDisplay.get(0), "Key 1", "value1", "key1"); assertProperty(propertiesForDisplay.get(1), "Key 2", "value2", "key2"); }
@Test public void shouldPopulatePropertiesForDisplayRetainingOrderAndDisplayNameIfConfigured() throws Exception { Task taskDetails = mock(Task.class); TaskConfig taskConfig = new TaskConfig(); addProperty(taskConfig, "KEY2", "Key 2", 1); addProperty(taskConfig, "KEY1", "Key 1", 0); addProperty(taskConfig, "KEY3", "Key 3", 2); when(taskDetails.config()).thenReturn(taskConfig); when(taskDetails.view()).thenReturn(mock(TaskView.class)); String pluginId = "plugin_with_all_details"; PluggableTaskConfigStore.store().setPreferenceFor(pluginId, new TaskPreference(taskDetails)); Configuration configuration = new Configuration( ConfigurationPropertyMother.create("KEY3", true, "encryptedValue1"), ConfigurationPropertyMother.create("KEY1", false, "value1"), ConfigurationPropertyMother.create("KEY2", false, "value2") ); PluggableTask task = new PluggableTask(new PluginConfiguration(pluginId, "1"), configuration); List<TaskProperty> propertiesForDisplay = task.getPropertiesForDisplay(); assertThat(propertiesForDisplay.size(), is(3)); assertProperty(propertiesForDisplay.get(0), "Key 1", "value1", "key1"); assertProperty(propertiesForDisplay.get(1), "Key 2", "value2", "key2"); assertProperty(propertiesForDisplay.get(2), "Key 3", "****", "key3"); }
PluggableTaskConfigStore.store().setPreferenceFor("plugin-id-1", new TaskPreference(new com.thoughtworks.go.plugin.api.task.Task() { @Override public TaskConfig config() {