@Test public void shouldRegisterTaskConfigDuringExecutionAndUnregisterOnSuccessfulCompletion() throws CruiseControlException { final PluggableTaskBuilder builder = spy(new PluggableTaskBuilder(runIfConfigs, cancelBuilder, pluggableTask, "", "")); taskExtension = mock(TaskExtension.class); when(taskExtension.execute(eq(TEST_PLUGIN_ID), any(ActionWithReturn.class))).thenReturn(ExecutionResult.success("yay")); builder.build(goPublisher, variableContext, taskExtension, null, null, "utf-8"); assertThat(ReflectionUtil.getStaticField(JobConsoleLogger.class, "context"), is(nullValue())); }
@Before public void setup() { initMocks(this); extension = new TaskExtension(pluginManager); pluginId = "plugin-id"; when(pluginManager.resolveExtensionVersion(eq(pluginId), eq(PLUGGABLE_TASK_EXTENSION), any(List.class))).thenReturn("1.0"); pluginSettingsConfiguration = new PluginSettingsConfiguration(); requestArgumentCaptor = ArgumentCaptor.forClass(GoPluginApiRequest.class); }
private void loadTaskConfigIntoPreferenceStore(GoPluginDescriptor descriptor) { if (taskExtension.canHandlePlugin(descriptor.id())) { taskExtension.doOnTask(descriptor.id(), (task, pluginDescriptor) -> PluggableTaskConfigStore.store().setPreferenceFor(pluginDescriptor.id(), new TaskPreference(task))); } }
@Test public void shouldTalkToPluginToGetPluginSettingsConfiguration() throws Exception { extension.registerHandler("1.0", pluginSettingsJSONMessageHandler); extension.messageHandlerMap.put("1.0", jsonMessageHandler); String responseBody = "expected-response"; PluginSettingsConfiguration deserializedResponse = new PluginSettingsConfiguration(); when(pluginSettingsJSONMessageHandler.responseMessageForPluginSettingsConfiguration(responseBody)).thenReturn(deserializedResponse); when(pluginManager.isPluginOfType(PLUGGABLE_TASK_EXTENSION, pluginId)).thenReturn(true); when(pluginManager.submitTo(eq(pluginId), eq(PLUGGABLE_TASK_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(responseBody)); PluginSettingsConfiguration response = extension.getPluginSettingsConfiguration(pluginId); assertRequest(requestArgumentCaptor.getValue(), PLUGGABLE_TASK_EXTENSION, "1.0", PluginSettingsConstants.REQUEST_PLUGIN_SETTINGS_CONFIGURATION, null); verify(pluginSettingsJSONMessageHandler).responseMessageForPluginSettingsConfiguration(responseBody); assertSame(response, deserializedResponse); }
@Test public void shouldTalkToPluginToGetPluginSettingsView() throws Exception { extension.registerHandler("1.0", pluginSettingsJSONMessageHandler); extension.messageHandlerMap.put("1.0", jsonMessageHandler); String responseBody = "expected-response"; String deserializedResponse = ""; when(pluginSettingsJSONMessageHandler.responseMessageForPluginSettingsView(responseBody)).thenReturn(deserializedResponse); when(pluginManager.isPluginOfType(PLUGGABLE_TASK_EXTENSION, pluginId)).thenReturn(true); when(pluginManager.submitTo(eq(pluginId), eq(PLUGGABLE_TASK_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(responseBody)); String response = extension.getPluginSettingsView(pluginId); assertRequest(requestArgumentCaptor.getValue(), PLUGGABLE_TASK_EXTENSION, "1.0", PluginSettingsConstants.REQUEST_PLUGIN_SETTINGS_VIEW, null); verify(pluginSettingsJSONMessageHandler).responseMessageForPluginSettingsView(responseBody); assertSame(response, deserializedResponse); }
@Test public void shouldValidateTask() { GoPluginApiResponse response = mock(GoPluginApiResponse.class); TaskExtension jsonBasedTaskExtension = new TaskExtension(pluginManager); TaskConfig taskConfig = mock(TaskConfig.class); when(response.responseCode()).thenReturn(DefaultGoApiResponse.SUCCESS_RESPONSE_CODE); when(pluginManager.isPluginOfType(PLUGGABLE_TASK_EXTENSION, pluginId)).thenReturn(true); when(response.responseBody()).thenReturn("{\"errors\":{\"key\":\"error\"}}"); when(pluginManager.submitTo(eq(pluginId), eq(PLUGGABLE_TASK_EXTENSION), any(GoPluginApiRequest.class))).thenReturn(response); ValidationResult validationResult = jsonBasedTaskExtension.validate(pluginId, taskConfig); verify(pluginManager).submitTo(eq(pluginId), eq(PLUGGABLE_TASK_EXTENSION), any(GoPluginApiRequest.class)); assertFalse(validationResult.isSuccessful()); assertEquals(validationResult.getErrors().get(0).getKey(), "key"); assertEquals(validationResult.getErrors().get(0).getMessage(), "error"); }
@Test public void shouldTalkToPluginToValidatePluginSettings() throws Exception { extension.registerHandler("1.0", pluginSettingsJSONMessageHandler); extension.messageHandlerMap.put("1.0", jsonMessageHandler); String requestBody = "expected-request"; when(pluginSettingsJSONMessageHandler.requestMessageForPluginSettingsValidation(pluginSettingsConfiguration)).thenReturn(requestBody); String responseBody = "expected-response"; ValidationResult deserializedResponse = new ValidationResult(); when(pluginSettingsJSONMessageHandler.responseMessageForPluginSettingsValidation(responseBody)).thenReturn(deserializedResponse); when(pluginManager.isPluginOfType(PLUGGABLE_TASK_EXTENSION, pluginId)).thenReturn(true); when(pluginManager.submitTo(eq(pluginId), eq(PLUGGABLE_TASK_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(responseBody)); ValidationResult response = extension.validatePluginSettings(pluginId, pluginSettingsConfiguration); assertRequest(requestArgumentCaptor.getValue(), PLUGGABLE_TASK_EXTENSION, "1.0", PluginSettingsConstants.REQUEST_VALIDATE_PLUGIN_SETTINGS, requestBody); verify(pluginSettingsJSONMessageHandler).responseMessageForPluginSettingsValidation(responseBody); assertSame(response, deserializedResponse); }
@Test public void shouldPerformTheActionOnTask() { Action action = mock(Action.class); final GoPluginDescriptor descriptor = mock(GoPluginDescriptor.class); when(pluginManager.getPluginDescriptorFor(pluginId)).thenReturn(descriptor); extension.doOnTask(pluginId, action); verify(action).execute(any(JsonBasedPluggableTask.class), eq(descriptor)); }
public boolean isValid(PluggableTask task) { if(!task.isValid()) { return false; } ValidationResult validationResult = taskExtension.validate(task.getPluginConfiguration().getId(), task.toTaskConfig()); mapErrorsToConfiguration(validationResult, task); return validationResult.isSuccessful(); }
@Autowired public TaskExtension(PluginManager pluginManager) { super(pluginManager, new PluginRequestHelper(pluginManager, supportedVersions, PLUGGABLE_TASK_EXTENSION), PLUGGABLE_TASK_EXTENSION); registerHandler(JsonBasedTaskExtensionHandler_V1.VERSION, new PluginSettingsJsonMessageHandler1_0()); messageHandlerMap.put(JsonBasedTaskExtensionHandler_V1.VERSION, new JsonBasedTaskExtensionHandler_V1()); }
@Test public void shouldNotFetchPluginSettingsMetadataForTaskPlugin() throws Exception { PluginSettingsConfiguration configuration = new PluginSettingsConfiguration(); configuration.add(new PluginSettingsProperty("k1").with(Property.REQUIRED, true).with(Property.SECURE, false)); GoPluginDescriptor pluginDescriptor = new GoPluginDescriptor(UUID.randomUUID().toString(), "1.0", null, null, null, true); setupSettingsResponses(taskExtension, pluginDescriptor.id(), configuration, "template"); metadataLoader.fetchPluginSettingsMetaData(pluginDescriptor); verify(taskExtension, never()).getPluginSettingsConfiguration(pluginDescriptor.id()); verify(taskExtension, never()).getPluginSettingsView(pluginDescriptor.id()); assertThat(PluginSettingsMetadataStore.getInstance().configuration(pluginDescriptor.id()), is(nullValue())); }
@Before public void setUp() { initMocks(this); when(packageRepositoryExtension.extensionName()).thenReturn(PluginConstants.PACKAGE_MATERIAL_EXTENSION); when(scmExtension.extensionName()).thenReturn(PluginConstants.SCM_EXTENSION); when(notificationExtension.extensionName()).thenReturn(PluginConstants.NOTIFICATION_EXTENSION); when(configRepoExtension.extensionName()).thenReturn(PluginConstants.CONFIG_REPO_EXTENSION); when(taskExtension.extensionName()).thenReturn(PluginConstants.PLUGGABLE_TASK_EXTENSION); List<GoPluginExtension> extensions = asList(packageRepositoryExtension, scmExtension, notificationExtension, configRepoExtension, taskExtension); metadataLoader = new PluginSettingsMetadataLoader(extensions, pluginManager); PluginSettingsMetadataStore.getInstance().clear(); }
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); }
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(); }
@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 shouldUnsetTaskExecutionContextFromJobConsoleLoggerWhenTaskExecutionThrowsException() throws CruiseControlException { final PluggableTaskBuilder builder = spy(new PluggableTaskBuilder(runIfConfigs, cancelBuilder, pluggableTask, "", "")); taskExtension = mock(TaskExtension.class); when(taskExtension.execute(eq(TEST_PLUGIN_ID), any(ActionWithReturn.class))).thenThrow(new RuntimeException("something")); try { builder.build(goPublisher, variableContext, taskExtension, null, null, "utf-8"); fail("should throw exception"); } catch (Exception e) { assertThat(ReflectionUtil.getStaticField(JobConsoleLogger.class, "context"), is(nullValue())); } } }
return null; }).when(extension).doOnTask(eq("plugin1"), any(Action.class));
@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 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 shouldUnsetTaskExecutionContextFromJobConsoleLoggerWhenTaskExecutionFails() throws CruiseControlException { final PluggableTaskBuilder builder = spy(new PluggableTaskBuilder(runIfConfigs, cancelBuilder, pluggableTask, "", "")); taskExtension = mock(TaskExtension.class); when(taskExtension.execute(eq(TEST_PLUGIN_ID), any(ActionWithReturn.class))).thenReturn(ExecutionResult.failure("oh no")); try { builder.build(goPublisher, variableContext, taskExtension, null, null, "utf-8"); fail("should throw exception"); } catch (Exception e) { assertThat(ReflectionUtil.getStaticField(JobConsoleLogger.class, "context"), is(nullValue())); } }