public static PluginConfiguration fromJSON(JsonReader jsonReader) { PluginConfiguration pluginConfiguration = new PluginConfiguration(); jsonReader.readStringIfPresent("id", pluginConfiguration::setId); jsonReader.readStringIfPresent("version", pluginConfiguration::setVersion); return pluginConfiguration; } }
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 validateTreeShouldVerifyIfConfigurationHasErrors() { Configuration configuration = mock(Configuration.class); PluggableTask pluggableTask = new PluggableTask(new PluginConfiguration(), configuration); when(configuration.hasErrors()).thenReturn(true); assertFalse(pluggableTask.validateTree(null)); }
@Test public void taskTypeShouldBeSanitizedToHaveNoSpecialCharacters() throws Exception { assertThat(new PluggableTask(new PluginConfiguration("abc.def", "1"), new Configuration()).getTaskType(), is("pluggable_task_abc_def")); assertThat(new PluggableTask(new PluginConfiguration("abc_def", "1"), new Configuration()).getTaskType(), is("pluggable_task_abc_def")); assertThat(new PluggableTask(new PluginConfiguration("abcdef", "1"), new Configuration()).getTaskType(), is("pluggable_task_abcdef")); assertThat(new PluggableTask(new PluginConfiguration("abc#def", "1"), new Configuration()).getTaskType(), is("pluggable_task_abc_def")); assertThat(new PluggableTask(new PluginConfiguration("abc#__def", "1"), new Configuration()).getTaskType(), is("pluggable_task_abc___def")); assertThat(new PluggableTask(new PluginConfiguration("Abc#dEF", "1"), new Configuration()).getTaskType(), is("pluggable_task_Abc_dEF")); assertThat(new PluggableTask(new PluginConfiguration("1234567890#ABCDEF", "1"), new Configuration()).getTaskType(), is("pluggable_task_1234567890_ABCDEF")); }
@Before public void setUp() { initMocks(this); PluginManagerReference.reference().setPluginManager(pluginManager); when(pluggableTask.getPluginConfiguration()).thenReturn(new PluginConfiguration(TEST_PLUGIN_ID, "1.0")); HashMap<String, Map<String, String>> pluginConfig = new HashMap<>(); when(pluggableTask.configAsMap()).thenReturn(pluginConfig); taskExtension = new TaskExtension(pluginManager); }
@Test public void validateTreeShouldVerifyIfCancelTasksHasNestedCancelTask() { PluggableTask pluggableTask = new PluggableTask(new PluginConfiguration(), new Configuration()); pluggableTask.onCancelConfig = mock(OnCancelConfig.class); com.thoughtworks.go.domain.Task cancelTask = mock(com.thoughtworks.go.domain.Task.class); when(pluggableTask.onCancelConfig.getTask()).thenReturn(cancelTask); when(cancelTask.hasCancelTask()).thenReturn(true); when(pluggableTask.onCancelConfig.validateTree(null)).thenReturn(true); assertFalse(pluggableTask.validateTree(null)); assertThat(pluggableTask.errors().get("onCancelConfig").get(0), is("Cannot nest 'oncancel' within a cancel task")); }
@Test public void shouldReturnPluggableTaskContext() throws Exception { PluggableTask task = mock(PluggableTask.class); when(task.getPluginConfiguration()).thenReturn(new PluginConfiguration()); String workingDir = "test-directory"; PluggableTaskBuilder taskBuilder = new PluggableTaskBuilder(runIfConfigs, cancelBuilder, task, TEST_PLUGIN_ID, workingDir); TaskExecutionContext taskExecutionContext = taskBuilder.buildTaskContext(goPublisher, variableContext, "utf-8"); assertThat(taskExecutionContext instanceof PluggableTaskContext, is(true)); assertThat(taskExecutionContext.workingDir(), is(workingDir)); }
@Test public void dataForViewShouldBeGotFromTheTaskInJSONFormat() throws Exception { Configuration configuration = new Configuration(create("key1", false, "value1"), create("KEY2", false, "value2")); PluggableTask taskConfig = new PluggableTask(new PluginConfiguration("plugin-1", "2"), configuration); PluggableTaskViewModelFactory factory = new PluggableTaskViewModelFactory(); PluggableViewModel<PluggableTask> viewModel = factory.viewModelFor(taskConfig, "new"); String actualData = (String) viewModel.getParameters().get("data"); Gson gson = new Gson(); Map actual = gson.fromJson(actualData, Map.class); Map expected = gson.fromJson("{\"KEY2\":{\"value\":\"value2\"},\"key1\":{\"value\":\"value1\"}}", Map.class); assertEquals(expected, actual); }
@Test public void shouldPopulatePropertiesForDisplay() throws Exception { Configuration configuration = new Configuration( ConfigurationPropertyMother.create("KEY1", false, "value1"), ConfigurationPropertyMother.create("Key2", false, "value2"), ConfigurationPropertyMother.create("key3", true, "encryptedValue1")); PluggableTask task = new PluggableTask(new PluginConfiguration("abc.def", "1"), configuration); List<TaskProperty> propertiesForDisplay = task.getPropertiesForDisplay(); assertThat(propertiesForDisplay.size(), is(3)); assertProperty(propertiesForDisplay.get(0), "KEY1", "value1", "key1"); assertProperty(propertiesForDisplay.get(1), "Key2", "value2", "key2"); assertProperty(propertiesForDisplay.get(2), "key3", "****", "key3"); }
@Test public void shouldReturnMissingPluginTaskViewIfPluginIsMissing() { String pluginId = "pluginId"; PluggableTaskViewModelFactory factory = new PluggableTaskViewModelFactory(); PluggableViewModel<PluggableTask> viewModel = factory.viewModelFor(new PluggableTask(new PluginConfiguration(pluginId, "1"), new Configuration()), "edit"); assertThat(viewModel.getParameters().get("template"), is(String.format("Associated plugin '%s' not found. Please contact the Go admin to install the plugin.", pluginId))); assertThat(viewModel.getTypeForDisplay(), is(pluginId)); assertThat(viewModel instanceof MissingPluggableTaskViewModel, is(true)); }
@Test public void shouldThrowUpOnSetConfigAttributesIfPluginIsNotAvailable() throws Exception { try { Map<String, String> attributeMap = DataStructureUtils.m(SCM.SCM_ID, "scm-id", SCM.NAME, "scm-name", SCM.AUTO_UPDATE, "false", "url", "http://localhost"); SCM scm = new SCM(null, new PluginConfiguration("plugin-id", "1"), new Configuration()); scm.setConfigAttributes(attributeMap); fail("should have thrown exception"); } catch (Exception e) { assertThat(e, instanceOf(RuntimeException.class)); assertThat(e.getMessage(), is("metadata unavailable for plugin: plugin-id")); } }
@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(); }
private void assertPluggableViewModel(String actionName, String expectedTemplatePath) { PluggableTask pluggableTask = new PluggableTask(new PluginConfiguration("plugin-1", "2"), new Configuration()); PluggableTaskViewModelFactory factory = new PluggableTaskViewModelFactory(); PluggableViewModel<PluggableTask> viewModel = factory.viewModelFor(pluggableTask, actionName); assertThat(viewModel.getModel(), is(pluggableTask)); assertThat(viewModel.getTaskType(), is("pluggable_task_plugin_1")); assertThat(viewModel.getTemplatePath(), is(expectedTemplatePath)); }
@Test public void isValidShouldVerifyIfPluginIdIsValid() { PluginConfiguration pluginConfiguration = new PluginConfiguration("does_not_exist", "1.1"); Configuration configuration = new Configuration(); PluggableTask pluggableTask = new PluggableTask(pluginConfiguration, configuration); pluggableTask.isValid(); assertThat(pluggableTask.errors().get("pluggable_task").get(0), is("Could not find plugin for given pluggable id:[does_not_exist].")); }
@Test public void shouldReturnPluginIdAsDisplayValue() throws Exception { PluginConfiguration pluginConfiguration = new PluginConfiguration("test-plugin-1", "1.0"); PluggableTask pluggableTask = new PluggableTask(pluginConfiguration, new Configuration()); MissingPluggableTaskViewModel viewModel = new MissingPluggableTaskViewModel(pluggableTask, null); assertThat(viewModel.getParameters().get("template"), is(String.format("Associated plugin '%s' not found. Please contact the Go admin to install the plugin.", pluginConfiguration.getId()))); assertThat(viewModel.getTypeForDisplay(), is(pluginConfiguration.getId())); } }
@Test public void shouldReturnFalseWhenPluginConfigurationForTwoPluggableTasksIsDifferent() { PluginConfiguration pluginConfiguration1 = new PluginConfiguration("test-plugin-1", "1.0"); PluginConfiguration pluginConfiguration2 = new PluginConfiguration("test-plugin-2", "1.0"); PluggableTask pluggableTask1 = new PluggableTask(pluginConfiguration1, new Configuration()); PluggableTask pluggableTask2 = new PluggableTask(pluginConfiguration2, new Configuration()); assertFalse(pluggableTask1.hasSameTypeAs(pluggableTask2)); }
private SCM createSCM(String id, String name, String pluginId, String version, Configuration configuration) { SCM scm = new SCM(); scm.setId(id); scm.setName(name); scm.setPluginConfiguration(new PluginConfiguration(pluginId, version)); scm.setConfiguration(configuration); return scm; } }
public static SCM create(String id, String name, String pluginId, String pluginVersion, Configuration configuration) { SCM scm = new SCM(); scm.setId(id); scm.setName(name); scm.setPluginConfiguration(new PluginConfiguration(pluginId, pluginVersion)); scm.setConfiguration(configuration); return scm; } }
private PackageRepository createPackageRepository(String pluginId, String version, String id, String name, Configuration configuration, Packages packages) { PackageRepository packageRepository = new PackageRepository(); packageRepository.setConfiguration(configuration); packageRepository.setPackages(packages); packageRepository.setPluginConfiguration(new PluginConfiguration(pluginId, version)); packageRepository.setId(id); packageRepository.setName(name); return packageRepository; }
@Test public void shouldReturnFalseWhenPluggableTaskIsComparedWithAnyOtherTask() { PluginConfiguration pluginConfiguration = new PluginConfiguration("test-plugin-1", "1.0"); PluggableTask pluggableTask = new PluggableTask(pluginConfiguration, new Configuration()); AntTask antTask = new AntTask(); assertFalse(pluggableTask.hasSameTypeAs(antTask)); }