Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip calling notify method if there are no recipients #2835

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void testNotify() {
NotificationToEmailConverter notificationToEmailConverter = notificationToConvert -> {
String subject = "test subject";
String body = "test body";
return new NotificationEmail(subject, body, new HashSet<>());
return new NotificationEmail(subject, body, Collections.singleton("[email protected]"));
};

NotificationToMetricConverter notificationToMetricConverter =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.yahoo.athenz.common.server.notification;

import com.yahoo.athenz.auth.Authority;
import org.eclipse.jetty.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -257,7 +258,7 @@ public Set<String> getFullyQualifiedEmailAddresses(Set<String> recipients) {
.map(userName -> {
if (notificationUserAuthority != null) {
String email = notificationUserAuthority.getUserEmail(userName);
if (email != null) {
if (!StringUtil.isEmpty(email)) {
return email;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,16 @@ public boolean notify(Notification notification) {
final String subject = notificationAsEmail.getSubject();
final String body = notificationAsEmail.getBody();
Set<String> recipients = notificationAsEmail.getFullyQualifiedRecipientsEmail();
if (sendEmail(recipients, subject, body)) {

// if our list of recipients is empty then we have nothing to do,
// but we want to log it for debugging purposes

if (recipients.isEmpty()) {
LOGGER.error("No recipients specified in the notification. Subject={}", subject);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be warn instead of error?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should treat it as an error since we're trying to send a notification but we don't have recipients.

return false;
}

if (sendEmail(recipients, subject, body)) {
LOGGER.info("Successfully sent email notification. Subject={}, Recipients={}", subject, recipients);
return true;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@
package com.yahoo.athenz.common.server.notification.impl;

import com.yahoo.athenz.common.server.notification.EmailProvider;
import com.yahoo.athenz.common.server.notification.Notification;
import com.yahoo.athenz.common.server.notification.NotificationEmail;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.*;

public class EmailNotificationServiceTest {
Expand Down Expand Up @@ -58,4 +65,16 @@ public void testReadBinaryFromFileNull() {
assertNull(svc.readBinaryFromFile("resources/non-existent"));
}

@Test
public void testNotifyNoRecipients() {
EmailProvider emailProvider = mock(EmailProvider.class);
EmailNotificationService svc = new EmailNotificationService(emailProvider);

Notification notification = mock(Notification.class);
NotificationEmail notificationAsEmail = new NotificationEmail("subject", "body", Collections.emptySet());
when(notification.getNotificationAsEmail()).thenReturn(notificationAsEmail);

boolean status = svc.notify(notification);
assertFalse(status);
}
}
Loading