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); }
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); assertThat(taskPreference1.equals(taskPreference2), is(false)); assertThat(taskPreference1.equals(taskPreference3), is(true)); assertThat(taskPreference1.equals(taskPreference4), is(true)); assertThat(taskPreference1.equals(taskPreference5), is(false));
private Property pluginConfigurationFor(String configKey) { TaskPreference taskPreference = taskPreference(); return taskPreference != null ? taskPreference.getConfig().get(configKey) : null; }
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 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 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); }
@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"); }
@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 shouldRegisterViewEngineForPluggableTask() { TaskPreference taskPreference = mock(TaskPreference.class); TaskView view = mock(TaskView.class); when(taskPreference.getView()).thenReturn(view); when(view.template()).thenReturn("plugin-template-value"); when(view.displayValue()).thenReturn("Plugin display value"); PluggableTaskConfigStore.store().setPreferenceFor("plugin1", taskPreference); PluggableTask pluggableTask = new PluggableTask(new PluginConfiguration("plugin1", "2"), new Configuration()); PluggableViewModel<PluggableTask> pluggableTaskViewModel = registry.getViewModelFor(pluggableTask, "new"); assertEquals(PluggableTaskViewModel.class, pluggableTaskViewModel.getClass()); assertThat(pluggableTaskViewModel.getModel(), is(pluggableTask)); }
@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 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"); }
private PluggableViewModel<PluggableTask> getModelWithTaskTemplateHavingValue(final String templateContentValue) { when(taskPreference.getView()).thenReturn(new TaskView() { @Override public String displayValue() { return "view"; } @Override public String template() { return templateContentValue; } }); PluggableTask pluggableTask = new PluggableTask(new PluginConfiguration("plugin-1", "2"), new Configuration()); PluggableTaskViewModelFactory factory = new PluggableTaskViewModelFactory(); return factory.viewModelFor(pluggableTask, "new"); } }
@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 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"); }
@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")); }
PluggableTaskConfigStore.store().setPreferenceFor("plugin-id-1", new TaskPreference(new com.thoughtworks.go.plugin.api.task.Task() { @Override public TaskConfig config() {
@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 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)); }