Skip to content

Commit

Permalink
MID-7487: message template localized content lookup fixes and test
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Feb 25, 2022
1 parent dfca73f commit 9b4c92a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.google.common.base.Strings;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -305,14 +304,18 @@ private MessageTemplateContentType findMessageContent(

private MessageTemplateContentType findLocalizedContent(
@NotNull MessageTemplateType messageTemplate, @NotNull ObjectReferenceType recipientRef) {
FocusType recipientFocus = (FocusType) recipientRef.getObjectable();
FocusType recipientFocus = (FocusType) recipientRef.asReferenceValue().getOriginObject();
if (recipientFocus == null) {
// TODO can focus be possibly null here? shouldn't it be resolved already if ref is not null?
return null;
}
// Locale recipientLocale = LocaleUtils.toLocale(
String recipientLocale = Objects.requireNonNullElse(
recipientFocus.getPreferredLanguage(), recipientFocus.getLocale());
// TODO: This order or locale first? This is how it's used in GUI.
// Also, utility method to get Locale from focus should probably be extracted, but where to?
String recipientLocale = recipientFocus.getPreferredLanguage();
if (recipientLocale == null) {
recipientLocale = recipientFocus.getLocale();
}
if (recipientLocale == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -218,9 +219,7 @@ public void test100CustomTransportSendingNotificationMessage() throws Exception
assertThat(testTransport.getMessages()).isEmpty();

when("event is sent to notification manager");
CustomEventImpl event = new CustomEventImpl(lightweightIdentifierGenerator, "test", null, null,
null, // TODO why is this not nullable?
EventStatusType.SUCCESS, "test-channel");
CustomEventImpl event = createCustomEvent();
// This is used as default recipient, no recipient results in no message.
event.setRequestee(new SimpleObjectRefImpl(notificationFunctions,
new UserType(prismContext).emailAddress("user@example.com")));
Expand Down Expand Up @@ -274,9 +273,7 @@ public void test110NotifierWithMessageTemplateReference() throws Exception {
assertThat(((TestMessageTransport) transportService.getTransport("test")).getMessages()).isEmpty();

when("event is sent to notification manager");
CustomEventImpl event = new CustomEventImpl(lightweightIdentifierGenerator, "test", null, null,
null, // TODO why is this not nullable?
EventStatusType.SUCCESS, "test-channel");
CustomEventImpl event = createCustomEvent();
// This is used as default recipient, no recipient results in no message.
event.setRequestee(new SimpleObjectRefImpl(notificationFunctions,
new UserType(prismContext).emailAddress("user@example.com")));
Expand Down Expand Up @@ -313,9 +310,7 @@ public void test200RecipientExpressionReturningFocus() throws Exception {
assertThat(testTransport.getMessages()).isEmpty();

when("event is sent to notification manager");
CustomEventImpl event = new CustomEventImpl(lightweightIdentifierGenerator, "test", null, null,
null, // TODO why is this not nullable?
EventStatusType.SUCCESS, "test-channel");
CustomEventImpl event = createCustomEvent();
// This is used as default recipient, no recipient results in no message.
event.setRequestee(new SimpleObjectRefImpl(notificationFunctions,
new UserType(prismContext)
Expand Down Expand Up @@ -354,9 +349,7 @@ public void test210RecipientExpressionReturningLiteralValue() throws Exception {
assertThat(testTransport.getMessages()).isEmpty();

when("event is sent to notification manager");
CustomEventImpl event = new CustomEventImpl(lightweightIdentifierGenerator, "test", null, null,
null, // TODO why is this not nullable?
EventStatusType.SUCCESS, "test-channel");
CustomEventImpl event = createCustomEvent();
notificationManager.processEvent(event, getTestTask(), result);

then("transport sends the message");
Expand Down Expand Up @@ -407,9 +400,7 @@ public void test120NotifierWithMessageTemplateReferenceAndOverridingContentParts
assertThat(((TestMessageTransport) transportService.getTransport("test")).getMessages()).isEmpty();

when("event is sent to notification manager");
CustomEventImpl event = new CustomEventImpl(lightweightIdentifierGenerator, "test", null, null,
null,
EventStatusType.SUCCESS, "test-channel");
CustomEventImpl event = createCustomEvent();
// This is used as default recipient, no recipient results in no message.
event.setRequestee(new SimpleObjectRefImpl(notificationFunctions,
new UserType(prismContext).emailAddress("user@example.com")));
Expand All @@ -423,8 +414,79 @@ public void test120NotifierWithMessageTemplateReferenceAndOverridingContentParts
assertThat(message.getBody()).isEqualTo("notifier-body");
assertThat(message.getSubject()).isEqualTo("notifier-subject");
assertThat(message.getAttachments()).hasSize(2)
.anyMatch(a -> a.getContent().equals("attachment1"))
.anyMatch(a -> a.getContent().equals("attachment2"));
.anyMatch(a -> a.getContent().toString().equals("RawType: (parsed:string): PPV(String:attachment1))"))
.anyMatch(a -> a.getContent().toString().equals("RawType: (parsed:string): PPV(String:attachment2))"));
}

@Test
public void test150NotifierWithLocalizedMessageTemplate() throws Exception {
OperationResult result = getTestOperationResult();

given("localized message template");
String objectName = "messageTemplate" + getTestNumber();
String templateOid = repositoryService.addObject(
new MessageTemplateType(prismContext)
.name(objectName)
.defaultContent(new MessageTemplateContentType(prismContext)
.bodyExpression(velocityExpression("template-body-default")))
.localizedContent(new LocalizedMessageTemplateContentType(prismContext)
.language("sk")
.bodyExpression(velocityExpression("template-body-sk")))
.asPrismObject(),
null, result);

and("configuration with transport and notifier using the template");
Collection<? extends ItemDelta<?, ?>> modifications = systemConfigModificationWithTestTransport("test")
.item(SystemConfigurationType.F_NOTIFICATION_CONFIGURATION)
.replace(new NotificationConfigurationType(prismContext)
.handler(new EventHandlerType()
.generalNotifier(new GeneralNotifierType()
.messageTemplateRef(createObjectReference(
templateOid, MessageTemplateType.COMPLEX_TYPE, null))
.transport("test"))))
.asItemDeltas();
repositoryService.modifyObject(
SystemConfigurationType.class, SYS_CONFIG_OID, modifications, result);
assertThat(((TestMessageTransport) transportService.getTransport("test")).getMessages()).isEmpty();

when("event is sent to notification manager, recipient has no language set");
CustomEventImpl event = createCustomEvent();
// This is used as default recipient, no recipient results in no message.
event.setRequestee(new SimpleObjectRefImpl(notificationFunctions,
new UserType(prismContext).emailAddress("user@example.com")));
notificationManager.processEvent(event, getTestTask(), result);

then("transport sends the message with default template content");
assertThat(((TestMessageTransport) transportService.getTransport("test")).getMessages()).hasSize(1);
Message message = ((TestMessageTransport) transportService.getTransport("test")).getMessages().get(0);
assertThat(message.getTo()).containsExactlyInAnyOrder("user@example.com");
assertThat(message.getBody()).isEqualTo("template-body-default");

// now when-then for sk language
when("event is sent to notification manager, recipient has 'sk' language set");
event = createCustomEvent();
event.setRequestee(new SimpleObjectRefImpl(notificationFunctions,
new UserType(prismContext).emailAddress("user2@example.com").preferredLanguage("sk")));
notificationManager.processEvent(event, getTestTask(), result);

then("transport sends the message with template content for 'sk' language");
assertThat(((TestMessageTransport) transportService.getTransport("test")).getMessages()).hasSize(2);
message = ((TestMessageTransport) transportService.getTransport("test")).getMessages().get(1);
assertThat(message.getTo()).containsExactlyInAnyOrder("user2@example.com");
assertThat(message.getBody()).isEqualTo("template-body-sk");

// now when-then for other language
when("event is sent to notification manager, recipient has 'uk' language set");
event = createCustomEvent();
event.setRequestee(new SimpleObjectRefImpl(notificationFunctions,
new UserType(prismContext).emailAddress("user3@example.com").preferredLanguage("uk")));
notificationManager.processEvent(event, getTestTask(), result);

then("transport sends the message with default template content, because no localized content for specified language is found");
assertThat(((TestMessageTransport) transportService.getTransport("test")).getMessages()).hasSize(3);
message = ((TestMessageTransport) transportService.getTransport("test")).getMessages().get(2);
assertThat(message.getTo()).containsExactlyInAnyOrder("user3@example.com");
assertThat(message.getBody()).isEqualTo("template-body-default");
}

@Test
Expand All @@ -441,9 +503,7 @@ public void test900NotifierWithoutTransportDoesNotSendAnything() throws Exceptio
SystemConfigurationType.class, SYS_CONFIG_OID, modifications, result);

when("event is sent to notification manager");
CustomEventImpl event = new CustomEventImpl(lightweightIdentifierGenerator, "test", null, null,
null, // TODO why is this not nullable?
EventStatusType.SUCCESS, "test-channel");
CustomEventImpl event = createCustomEvent();
notificationManager.processEvent(event, getTestTask(), result);

then("result is success, but no message was sent because there is no transport");
Expand All @@ -470,9 +530,7 @@ public void test910NotifierWithoutRecipientAndEventWithoutDefaultRecipientDoesNo
assertThat(testTransport.getMessages()).isEmpty();

when("event without default recipient is sent to notification manager");
CustomEventImpl event = new CustomEventImpl(lightweightIdentifierGenerator, "test", null, null,
null, // TODO why is this not nullable?
EventStatusType.SUCCESS, "test-channel");
CustomEventImpl event = createCustomEvent();
// No requestee set, that would be taken as default recipient, see test100
notificationManager.processEvent(event, getTestTask(), result);

Expand Down Expand Up @@ -501,9 +559,7 @@ public void test920NotifierWithoutBodyDoesNotSendAnything() throws Exception {
assertThat(testTransport.getMessages()).isEmpty();

when("event with default recipient is sent to notification manager");
CustomEventImpl event = new CustomEventImpl(lightweightIdentifierGenerator, "test", null, null,
null, // TODO why is this not nullable?
EventStatusType.SUCCESS, "test-channel");
CustomEventImpl event = createCustomEvent();
// this will be used as default recipient
event.setRequestee(new SimpleObjectRefImpl(notificationFunctions,
new UserType(prismContext).emailAddress("user@example.com")));
Expand Down Expand Up @@ -532,9 +588,7 @@ public void test930NotifierUsingNonexistentTransportDoesNotSendAnything() throws
SystemConfigurationType.class, SYS_CONFIG_OID, modifications, result);

when("event without default recipient is sent to notification manager");
CustomEventImpl event = new CustomEventImpl(lightweightIdentifierGenerator, "test", null, null,
null, // TODO why is this not nullable?
EventStatusType.SUCCESS, "test-channel");
CustomEventImpl event = createCustomEvent();
// this will be used as default recipient
event.setRequestee(new SimpleObjectRefImpl(notificationFunctions,
new UserType(prismContext).emailAddress("user@example.com")));
Expand All @@ -558,4 +612,11 @@ private S_ItemEntry systemConfigModificationWithTestTransport(String transportNa
.name(transportName)
.type(TestMessageTransport.class.getName())));
}

@NotNull
private CustomEventImpl createCustomEvent() {
return new CustomEventImpl(lightweightIdentifierGenerator, "test", null, null,
null, // TODO why is this not nullable?
EventStatusType.SUCCESS, "test-channel");
}
}

0 comments on commit 9b4c92a

Please sign in to comment.