@Before public void setUp() { initMocks(this); when(notificationExtension.canHandlePlugin(PLUGIN_ID_1)).thenReturn(true); when(notificationExtension.canHandlePlugin(PLUGIN_ID_2)).thenReturn(true); when(notificationExtension.canHandlePlugin(PLUGIN_ID_3)).thenReturn(true); when(notificationExtension.canHandlePlugin(PLUGIN_ID_4)).thenReturn(false); when(notificationExtension.getNotificationsOfInterestFor(PLUGIN_ID_1)).thenReturn(asList(PIPELINE_STATUS, STAGE_STATUS, JOB_STATUS)); when(notificationExtension.getNotificationsOfInterestFor(PLUGIN_ID_2)).thenReturn(asList(PIPELINE_STATUS)); when(notificationExtension.getNotificationsOfInterestFor(PLUGIN_ID_3)).thenReturn(asList(STAGE_STATUS)); }
@Before public void setUp() throws Exception { initMocks(this); notificationExtension = new NotificationExtension(pluginManager); notificationExtension.getPluginSettingsMessageHandlerMap().put(apiVersion(), pluginSettingsJSONMessageHandler()); notificationExtension.getMessageHandlerMap().put(apiVersion(), jsonMessageHandler()); pluginSettingsConfiguration = new PluginSettingsConfiguration(); requestArgumentCaptor = ArgumentCaptor.forClass(GoPluginApiRequest.class); when(pluginManager.resolveExtensionVersion(PLUGIN_ID, NOTIFICATION_EXTENSION, NotificationExtension.goSupportedVersions)).thenReturn(apiVersion()); when(pluginManager.isPluginOfType(NOTIFICATION_EXTENSION, PLUGIN_ID)).thenReturn(true); when(pluginManager.submitTo(eq(PLUGIN_ID), eq(NOTIFICATION_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(RESPONSE_BODY)); }
@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(); }
@Test public void shouldNotifyPluginSettingsChange() throws Exception { String supportedVersion = "4.0"; Map<String, String> settings = Collections.singletonMap("foo", "bar"); ArgumentCaptor<GoPluginApiRequest> requestArgumentCaptor = ArgumentCaptor.forClass(GoPluginApiRequest.class); when(pluginManager.resolveExtensionVersion(eq("pluginId"), eq(NOTIFICATION_EXTENSION), anyList())).thenReturn(supportedVersion); when(pluginManager.isPluginOfType(NOTIFICATION_EXTENSION, "pluginId")).thenReturn(true); when(pluginManager.submitTo(eq("pluginId"), eq(NOTIFICATION_EXTENSION), requestArgumentCaptor.capture())).thenReturn(new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, "")); NotificationExtension extension = new NotificationExtension(pluginManager); extension.notifyPluginSettingsChange("pluginId", settings); assertRequest(requestArgumentCaptor.getValue(), NOTIFICATION_EXTENSION, supportedVersion, REQUEST_NOTIFY_PLUGIN_SETTINGS_CHANGE, "{\"foo\":\"bar\"}"); }
@Override public void onMessage(PluginNotificationMessage message) { HealthStateScope scope = HealthStateScope.aboutPlugin(message.pluginId()); try { LOGGER.debug("Sending {} notification message {} for plugin {}", message.getRequestName(), message, message.pluginId()); Result result = notificationExtension.notify(message.pluginId(), message.getRequestName(), message.getData()); if (result.isSuccessful()) { serverHealthService.removeByScope(scope); LOGGER.debug("Successfully sent {} notification message {} for plugin {}", message.getRequestName(), message, message.pluginId()); } else { String errorDescription = result.getMessages() == null ? null : StringUtils.join(result.getMessages(), ", "); handlePluginNotifyError(message.pluginId(), scope, errorDescription, null); } } catch (Exception e) { String errorDescription = e.getMessage() == null ? "Unknown error" : e.getMessage(); handlePluginNotifyError(message.pluginId(), scope, errorDescription, e); } }
@Override public void pluginUnLoaded(GoPluginDescriptor pluginDescriptor) { if (notificationExtension.canHandlePlugin(pluginDescriptor.id())) { notificationPluginRegistry.deregisterPlugin(pluginDescriptor.id()); notificationPluginRegistry.removePluginInterests(pluginDescriptor.id()); } } }
@Before public void setUp() throws Exception { extension = mock(NotificationExtension.class); PluginSettingsConfiguration value = new PluginSettingsConfiguration(); value.add(new PluginSettingsProperty("username", null).with(Property.REQUIRED, true).with(Property.SECURE, false)); value.add(new PluginSettingsProperty("password", null).with(Property.REQUIRED, true).with(Property.SECURE, true)); when(extension.getPluginSettingsConfiguration("plugin1")).thenReturn(value); when(extension.getPluginSettingsView("plugin1")).thenReturn("some-html"); }
@Test public void shouldTalkToPluginToGetNotificationsInterestedIn() throws Exception { List<String> response = asList("pipeline-status", "stage-status"); when(jsonMessageHandler().responseMessageForNotificationsInterestedIn(RESPONSE_BODY)).thenReturn(response); List<String> deserializedResponse = notificationExtension.getNotificationsOfInterestFor(PLUGIN_ID); assertRequest(requestArgumentCaptor.getValue(), NOTIFICATION_EXTENSION, apiVersion(), NotificationExtension.REQUEST_NOTIFICATIONS_INTERESTED_IN, null); verify(jsonMessageHandler()).responseMessageForNotificationsInterestedIn(RESPONSE_BODY); assertSame(response, deserializedResponse); }
@Test public void shouldTalkToPluginToGetPluginSettingsView() throws Exception { String deserializedResponse = ""; when(pluginSettingsJSONMessageHandler().responseMessageForPluginSettingsView(RESPONSE_BODY)).thenReturn(deserializedResponse); String response = notificationExtension.getPluginSettingsView(PLUGIN_ID); assertRequest(requestArgumentCaptor.getValue(), NOTIFICATION_EXTENSION, apiVersion(), PluginSettingsConstants.REQUEST_PLUGIN_SETTINGS_VIEW, null); verify(pluginSettingsJSONMessageHandler()).responseMessageForPluginSettingsView(RESPONSE_BODY); assertSame(response, deserializedResponse); }
@Test public void shouldTalkToPluginToGetPluginSettingsConfiguration() throws Exception { PluginSettingsConfiguration deserializedResponse = new PluginSettingsConfiguration(); when(pluginSettingsJSONMessageHandler().responseMessageForPluginSettingsConfiguration(RESPONSE_BODY)).thenReturn(deserializedResponse); PluginSettingsConfiguration response = notificationExtension.getPluginSettingsConfiguration(PLUGIN_ID); assertRequest(requestArgumentCaptor.getValue(), NOTIFICATION_EXTENSION, apiVersion(), PluginSettingsConstants.REQUEST_PLUGIN_SETTINGS_CONFIGURATION, null); verify(pluginSettingsJSONMessageHandler()).responseMessageForPluginSettingsConfiguration(RESPONSE_BODY); assertSame(response, deserializedResponse); }
@Test public void shouldNotifyPluginSettingsChange() throws Exception { String supportedVersion = "3.0"; Map<String, String> settings = Collections.singletonMap("foo", "bar"); ArgumentCaptor<GoPluginApiRequest> requestArgumentCaptor = ArgumentCaptor.forClass(GoPluginApiRequest.class); when(pluginManager.resolveExtensionVersion(eq("pluginId"), eq(NOTIFICATION_EXTENSION), anyList())).thenReturn(supportedVersion); when(pluginManager.isPluginOfType(NOTIFICATION_EXTENSION, "pluginId")).thenReturn(true); when(pluginManager.submitTo(eq("pluginId"), eq(NOTIFICATION_EXTENSION), requestArgumentCaptor.capture())).thenReturn(new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, "")); NotificationExtension extension = new NotificationExtension(pluginManager); extension.notifyPluginSettingsChange("pluginId", settings); assertRequest(requestArgumentCaptor.getValue(), NOTIFICATION_EXTENSION, supportedVersion, REQUEST_NOTIFY_PLUGIN_SETTINGS_CHANGE, "{\"foo\":\"bar\"}"); }
@Test public void shouldTalkToPluginToNotify() throws Exception { Result response = new Result(); String notificationName = "notification-name"; String jsonResponse = "json-response"; StageNotificationData stageNotificationData = new StageNotificationData(new Stage(), BuildCause.createWithEmptyModifications(), "group"); when(jsonMessageHandler().requestMessageForNotify(stageNotificationData)).thenReturn(jsonResponse); when(jsonMessageHandler().responseMessageForNotify(RESPONSE_BODY)).thenReturn(response); Result deserializedResponse = notificationExtension.notify(PLUGIN_ID, notificationName, stageNotificationData); assertRequest(requestArgumentCaptor.getValue(), NOTIFICATION_EXTENSION, apiVersion(), notificationName, jsonResponse); verify(jsonMessageHandler()).responseMessageForNotify(RESPONSE_BODY); assertSame(response, deserializedResponse); }
@Test public void shouldContinueWithBuildingPluginInfoWhenPluginViewIsNotProvidedByPlugin() throws Exception { GoPluginDescriptor descriptor = new GoPluginDescriptor("plugin1", null, null, null, null, false); when(extension.getPluginSettingsView("plugin1")).thenReturn(null); NotificationPluginInfo pluginInfo = new NotificationPluginInfoBuilder(extension) .pluginInfoFor(descriptor); assertThat(pluginInfo.getDescriptor(), is(descriptor)); assertThat(pluginInfo.getExtensionName(), is("notification")); assertNull(pluginInfo.getPluginSettings()); } }
@Test public void shouldContinueWithBuildingPluginInfoWhenPluginSettingsIsNotProvidedByPlugin() throws Exception { GoPluginDescriptor descriptor = new GoPluginDescriptor("plugin1", null, null, null, null, false); when(extension.getPluginSettingsConfiguration("plugin1")).thenReturn(null); NotificationPluginInfo pluginInfo = new NotificationPluginInfoBuilder(extension) .pluginInfoFor(descriptor); assertThat(pluginInfo.getDescriptor(), is(descriptor)); assertThat(pluginInfo.getExtensionName(), is("notification")); assertNull(pluginInfo.getPluginSettings()); }
@Override public void pluginLoaded(GoPluginDescriptor pluginDescriptor) { if (notificationExtension.canHandlePlugin(pluginDescriptor.id())) { try { notificationPluginRegistry.registerPlugin(pluginDescriptor.id()); List<String> notificationsInterestedIn = notificationExtension.getNotificationsOfInterestFor(pluginDescriptor.id()); if (notificationsInterestedIn != null && !notificationsInterestedIn.isEmpty()) { checkNotificationTypes(pluginDescriptor, notificationsInterestedIn); notificationPluginRegistry.registerPluginInterests(pluginDescriptor.id(), notificationsInterestedIn); } } catch (Exception e) { LOGGER.warn("Error occurred during plugin notification interest registration.", e); } } }