Skip to content

Commit

Permalink
Feature #13791
Browse files Browse the repository at this point in the history
A light improvment in the message building within the Notification API.
From now on, the modification of the message by the Notification API for
HTML rendering is removed as it's not the goal of this API to decide how
the text has to be rendered at the place of the templating; the layout
and the way the message is rendered is the goal of the templates used to
build any messages to send.

In the unit tests on the user notification API, replace the use of the
FieldMocker by NotificationSettingsMocker to mock the SettingsBundle
class instances of the NotificationManagerSettings static class.
  • Loading branch information
mmoqui committed Mar 7, 2024
1 parent b45b879 commit 1602c32
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ public class NotificationManagerSettings {
private static final int DEFAULT_SSE_ASYNC_TIMEOUT = 180;
private static final int DEFAULT_SSE_STORE_EVENT_LIFETIME = 40;
private static final int MS = 1000;
private static SettingBundle settings = ResourceLocator.getSettingBundle(
static final SettingBundle DEFAULT_SETTINGS = ResourceLocator.getSettingBundle(
"org.silverpeas.notificationManager.settings.notificationManagerSettings");

private static SettingBundle silvermailIconsSettings = ResourceLocator.getSettingBundle(
static final SettingBundle DEFAULT_ICON_SETTINGS = ResourceLocator.getSettingBundle(
"org.silverpeas.notificationserver.channel.silvermail.settings.silvermailIcons");

private static SettingBundle settings = DEFAULT_SETTINGS;
private static SettingBundle iconSettings = DEFAULT_ICON_SETTINGS;

/**
* Hidden constructor.
*/
Expand All @@ -70,7 +73,7 @@ private NotificationManagerSettings() {
*/
public static DelayedNotificationFrequency getDefaultDelayedNotificationFrequency() {
DelayedNotificationFrequency defaultFrequency = DelayedNotificationFrequency
.decode(settings.getString("DEFAULT_DELAYED_NOTIFICATION_FREQUENCY", null));
.decode(getSettings().getString("DEFAULT_DELAYED_NOTIFICATION_FREQUENCY", null));
if (defaultFrequency == null) {
defaultFrequency = DelayedNotificationFrequency.NONE;
}
Expand All @@ -89,8 +92,8 @@ public static Set<DelayedNotificationFrequency> getDelayedNotificationFrequencyC

// The parameter value
final String frequencyChoiceList =
settings.getString("DELAYED_NOTIFICATION_FREQUENCY_CHOICE_LIST", "")
.replaceAll("[ ]+", ",");
getSettings().getString("DELAYED_NOTIFICATION_FREQUENCY_CHOICE_LIST", "")
.replaceAll(" +", ",");

// The possible frequencies
if (StringUtils.isNotBlank(frequencyChoiceList)) {
Expand Down Expand Up @@ -123,7 +126,7 @@ public static boolean isUserManualNotificationRecipientLimitEnabled() {
* @return the maximum number of recipient for manual user notification.
*/
public static int getUserManualNotificationRecipientLimit() {
return settings.getInteger("notif.manual.receiver.limit", 0);
return getSettings().getInteger("notif.manual.receiver.limit", 0);
}

/**
Expand All @@ -132,52 +135,52 @@ public static int getUserManualNotificationRecipientLimit() {
* @return an integer value, 0 = no threshold.
*/
public static int getReceiverThresholdAfterThatReplaceUserNameListByGroupName() {
return settings.getInteger("notif.receiver.displayUser.threshold", 0);
return getSettings().getInteger("notif.receiver.displayUser.threshold", 0);
}

/**
* Indicates if group name must be displayed instead of user names of group.
* @return true if enabled, false otherwise.
*/
public static boolean isDisplayingUserNameListInsteadOfGroupEnabled() {
return settings.getBoolean("notif.receiver.displayGroup", false);
return getSettings().getBoolean("notif.receiver.displayGroup", false);
}

/**
* Indicates if the feature of displaying receivers in the notification message is enabled.
* @return true if enabled, false otherwise.
*/
public static boolean isDisplayingReceiversInNotificationMessageEnabled() {
return settings.getBoolean("addReceiversInBody", false);
return getSettings().getBoolean("addReceiversInBody", false);
}

/**
* Gets the cron configuration of the delayed notification sending.
* @return the cron configuration of the delayed notification sending.
*/
public static String getCronOfDelayedNotificationSending() {
return settings.getString("cronDelayedNotificationSending", "");
return getSettings().getString("cronDelayedNotificationSending", "");
}

/**
* Indicates if the multi channel for user notification is enabled.
* @return true if enabled, false otherwise.
*/
public static boolean isMultiChannelNotificationEnabled() {
return settings.getBoolean("multiChannelNotification", false);
return getSettings().getBoolean("multiChannelNotification", false);
}

/**
* Gets the addresses as default notification channels. If the multi channel isn't supported,
* then
* returns only one among the channels set up as default. In the case no default channels are set
* up, then the previous behaviour is used; the SMTP is used as default channel.
* @return a set of default notification channels.
* @return a list of default notification channels.
*/
static List<NotifChannel> getDefaultChannels() {
final String defaultChannelSetting = settings.getString("notif.defaultChannels", "");
final String defaultChannelSetting = getSettings().getString("notif.defaultChannels", "");
final boolean isMultiChannelSupported = isMultiChannelNotificationEnabled();
final String[] defaultChannels = defaultChannelSetting.replaceAll("[ ]{2,}", " ").split(" ");
final String[] defaultChannels = defaultChannelSetting.replaceAll(" {2,}", " ").split(" ");
final List<NotifChannel> channels;
final Stream<NotifChannel> streamOfChannels = Stream.of(defaultChannels)
.map(NotifChannel::decode)
Expand All @@ -201,47 +204,47 @@ static List<NotifChannel> getDefaultChannels() {
* @return true if enabled, false otherwise.
*/
public static boolean isRemoveSenderFromSubscriptionNotificationReceiversEnabled() {
return settings.getBoolean("notification.subscription.removeSenderFromReceivers.enabled", true);
return getSettings().getBoolean("notification.subscription.removeSenderFromReceivers.enabled", true);
}

/**
* Indicates if the the confirmation of subscription notification is enabled.
* @return true if enabled (default value), false otherwise.
*/
public static boolean isSubscriptionNotificationConfirmationEnabled() {
return settings.getBoolean("notification.subscription.confirmation.enabled", true);
return getSettings().getBoolean("notification.subscription.confirmation.enabled", true);
}

/**
* Gets the trigger of SSE communication jobs.
* @return the timeout as long (seconds).
*/
public static int getSseAsyncJobTrigger() {
return settings.getInteger("notification.sse.job.trigger", DEFAULT_SSE_JOB_TRIGGER);
return getSettings().getInteger("notification.sse.job.trigger", DEFAULT_SSE_JOB_TRIGGER);
}

/**
* Gets the timeout of asynchronous context cleanup of SSE communication.
* @return the timeout as long (milliseconds).
*/
public static int getSseAsyncTimeout() {
return settings.getInteger("notification.sse.async.timeout", DEFAULT_SSE_ASYNC_TIMEOUT) * MS;
return getSettings().getInteger("notification.sse.async.timeout", DEFAULT_SSE_ASYNC_TIMEOUT) * MS;
}

/**
* Indicates if previous sse context opened with same session id MUST be checked.
* @return true if enabled, false otherwise.
*/
public static boolean isCheckPreviousAsyncContextEnabled() {
return settings.getBoolean("notification.sse.async.previous.check", true);
return getSettings().getBoolean("notification.sse.async.previous.check", true);
}

/**
* Gets the number of thread used to perform the send of a server event.
* @return the maximum number of thread for send thread pool.
*/
public static int getSseSendMaxThreadPool() {
return settings.getInteger("notification.sse.send.thread.pool.max", 0);
return getSettings().getInteger("notification.sse.send.thread.pool.max", 0);
}


Expand All @@ -250,7 +253,7 @@ public static int getSseSendMaxThreadPool() {
* @return the timeout as long (milliseconds).
*/
public static int getSseStoreEventLifeTime() {
return settings
return getSettings()
.getInteger("notification.sse.store.event.lifetime", DEFAULT_SSE_STORE_EVENT_LIFETIME) * MS;
}

Expand All @@ -260,7 +263,7 @@ public static int getSseStoreEventLifeTime() {
* @return true in order to enable, false otherwise.
*/
public static boolean isSseEnabled() {
return settings.getBoolean("notification.sse.enabled", true);
return getSettings().getBoolean("notification.sse.enabled", true);
}


Expand All @@ -269,7 +272,7 @@ public static boolean isSseEnabled() {
* @return true in order to enable, false otherwise.
*/
public static boolean usingWebSocket() {
return settings.getBoolean("notification.sse.usingWebSocket", false);
return getSettings().getBoolean("notification.sse.usingWebSocket", false);
}


Expand All @@ -278,7 +281,7 @@ public static boolean usingWebSocket() {
* @return timeout in ms, 0 means no timeout.
*/
public static int getWebSocketSendTimeout() {
return settings.getInteger("notification.sse.websocket.send.timeout", 180000);
return getSettings().getInteger("notification.sse.websocket.send.timeout", 180000);
}


Expand All @@ -287,7 +290,7 @@ public static int getWebSocketSendTimeout() {
* @return true in order to handle, false otherwise.
*/
public static boolean isSseEnabledFor(final ServerEvent serverEvent) {
return isSseEnabled() && settings
return isSseEnabled() && getSettings()
.getBoolean("notification.sse.event." + serverEvent.getName().asString() + ".enabled",
true);
}
Expand All @@ -305,7 +308,7 @@ public static boolean isSseEnabledFor(final ServerEvent serverEvent) {
* @return an integer representing an amount of seconds.
*/
public static int sendEveryAmountOfSecondsFor(final ServerEventName serverEventName) {
return settings.getInteger(
return getSettings().getInteger(
"notification.sse." + serverEventName.asString() + ".send.every", 0);
}

Expand All @@ -314,7 +317,7 @@ public static int sendEveryAmountOfSecondsFor(final ServerEventName serverEventN
* @return url as string without the application context.
*/
public static String getUserNotificationDesktopIconUrl() {
return silvermailIconsSettings
return iconSettings
.getString("silvermail.desktop.url", "/util/icons/desktop-user-notification.png");
}

Expand All @@ -324,7 +327,7 @@ public static String getUserNotificationDesktopIconUrl() {
* @return true if the space label should be set in the notification source. False otherwise.
*/
public static boolean isSpaceLabelInNotificationSource() {
return settings.getBoolean("notification.source.spaceLabel");
return getSettings().getBoolean("notification.source.spaceLabel");
}

/**
Expand All @@ -334,7 +337,26 @@ public static boolean isSpaceLabelInNotificationSource() {
* otherwise.
*/
public static boolean isComponentInstanceLabelInNotificationSource() {
return settings.getBoolean("notification.source.componentLabel");
return getSettings().getBoolean("notification.source.componentLabel");
}

/**
* Setter dedicated to tests.
* @param newSettings the settings to use instead of the default one.
*/
static void setSettings(final SettingBundle newSettings) {
settings = newSettings;
}

/**
* Setters dedicated to tests.
* @param newIconSettings the icon settings to use instead of the default one.
*/
static void setIconSettings(final SettingBundle newIconSettings) {
iconSettings = newIconSettings;
}

static SettingBundle getSettings() {
return settings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import org.silverpeas.core.template.SilverpeasTemplates;
import org.silverpeas.core.ui.DisplayI18NHelper;
import org.silverpeas.core.util.Link;
import org.silverpeas.kernel.util.StringUtil;
import org.silverpeas.core.util.WebEncodeHelper;
import org.silverpeas.kernel.util.StringUtil;
import org.silverpeas.kernel.logging.SilverLogger;

import java.util.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
<p>Fichiers joints&nbsp;:</p>
$resource.attachmentLinks:{attachment | $attachmentLinks_fr(attachment)$}$
$endif$

$if(resource.url)$<div style="height:40px;"><a target="_blank" href="$resource.url$" style="display:inline-block; float:right; margin:0;border-radius:10px; border:1px solid #ccc; font-size:12px; color:#333; font-weight:bold;text-decoration:none;padding:10px;background: linear-gradient(#fff, #eee) repeat scroll 0 0 #eee;"> &#9658; $resource.linkLabel$</a></div>$endif$
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ $if(notification_receiver_users)$<div style="padding:10px 0 0 0;">
$elseif(notification_receiver_groups)$<div style="padding:10px 0 0 0;">
$endif$
$if(notification_receiver_users)$
<i>Diese Nachricht wurde gesendet nutzer : $notification_receiver_users$</i>
<i>Diese Nachricht wurde gesendet nutzer: $notification_receiver_users$</i>
$endif$
$if(notification_receiver_groups)$
$if(!notification_receiver_users)$
<i>Diese Nachricht wurde gesendet gruppen : $notification_receiver_groups$</i>
<i>Diese Nachricht wurde gesendet gruppen: $notification_receiver_groups$</i>
$endif$
$if(notification_receiver_users)$
<br/><i>gruppen : $notification_receiver_groups$</i>
<br/><i>gruppen: $notification_receiver_groups$</i>
$endif$
$endif$
$if(notification_receiver_users)$</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ $if(notification_receiver_users)$<div style="padding:10px 0 0 0;">
$elseif(notification_receiver_groups)$<div style="padding:10px 0 0 0;">
$endif$
$if(notification_receiver_users)$
<i>This message has been sent to users : $notification_receiver_users$</i>
<i>This message has been sent to users: $notification_receiver_users$</i>
$endif$
$if(notification_receiver_groups)$
$if(!notification_receiver_users)$
<i>This message has been sent to groups : $notification_receiver_groups$</i>
<i>This message has been sent to groups: $notification_receiver_groups$</i>
$endif$
$if(notification_receiver_users)$
<br/><i>to groups : $notification_receiver_groups$</i>
<br/><i>to groups: $notification_receiver_groups$</i>
$endif$
$endif$
$if(notification_receiver_users)$</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ $if(notification_receiver_users)$<div style="padding:10px 0 0 0;">
$elseif(notification_receiver_groups)$<div style="padding:10px 0 0 0;">
$endif$
$if(notification_receiver_users)$
<i>Ce message a été envoyé aux utilisateurs : $notification_receiver_users$</i>
<i>Ce message a été envoyé aux utilisateurs&nbsp;: $notification_receiver_users$</i>
$endif$
$if(notification_receiver_groups)$
$if(!notification_receiver_users)$
<i>Ce message a été envoyé aux groupes : $notification_receiver_groups$</i>
<i>Ce message a été envoyé aux groupes&nbsp;: $notification_receiver_groups$</i>
$endif$
$if(notification_receiver_users)$
<br/><i>aux groupes : $notification_receiver_groups$</i>
<br/><i>aux groupes&nbsp;: $notification_receiver_groups$</i>
$endif$
$endif$
$if(notification_receiver_users)$</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,12 @@
*/
public class DefaultUserNotification implements UserNotification {

private NotificationMetaData notification;
private final NotificationMetaData notification;

public DefaultUserNotification() {
this(null, null);
}

public DefaultUserNotification(final NotificationMetaData metaData) {
this.notification = metaData;
}

public DefaultUserNotification(final String title, final String content) {
if (StringUtils.isNotBlank(title) && StringUtils.isNotBlank(content)) {
notification = new NotificationMetaData(NotifMessageType.NORMAL.getId(), title, content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public final UserNotification build() {
/**
* A collection of user identifiers. All the users in this collection will be notified. This
* method requires to be implemented.
* @return
* @return the identifier of all the users to notify
*/
protected abstract Collection<String> getUserIdsToNotify();

Expand Down Expand Up @@ -381,7 +381,7 @@ protected void stop() {
* Used to stop the treatment at any time
* @author Yohann Chastagnier
*/
private class Stop extends RuntimeException {
private static class Stop extends RuntimeException {
private static final long serialVersionUID = 1L;
// Nothing to do
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
import org.junit.jupiter.api.extension.RegisterExtension;
import org.silverpeas.core.admin.user.constant.UserAccessLevel;
import org.silverpeas.core.admin.user.model.UserDetail;
import org.silverpeas.core.notification.user.client.NotificationManagerSettings;
import org.silverpeas.core.notification.user.client.NotificationSettingsMocker;
import org.silverpeas.core.test.unit.extention.JEETestContext;
import org.silverpeas.kernel.test.extension.EnableSilverTestEnv;
import org.silverpeas.core.test.unit.extention.FieldMocker;
import org.silverpeas.kernel.bundle.SettingBundle;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -46,15 +45,14 @@ class UserManualNotificationUserReceiverLimitUserDetailTest {
private static final int LIMITED = 5;

@RegisterExtension
FieldMocker mocker = new FieldMocker();
public NotificationSettingsMocker mocker = new NotificationSettingsMocker();

private SettingBundle mockedSettings;
private UserDetail currentUser;

@BeforeEach
public void setup() {
mockedSettings = mocker.mockField(NotificationManagerSettings.class,
SettingBundle.class, "settings");
mockedSettings = mocker.getMockedSettings();
currentUser = spy(new UserDetail());
// By default, a user is not an anonymous one
assertThat(currentUser.isAnonymous(), is(false));
Expand Down

0 comments on commit 1602c32

Please sign in to comment.