-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add email notification of BSA job status
- Loading branch information
Showing
10 changed files
with
342 additions
and
23 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
core/src/main/java/google/registry/bsa/BsaEmailSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2024 The Nomulus Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package google.registry.bsa; | ||
|
||
import google.registry.config.RegistryConfig.Config; | ||
import google.registry.groups.GmailClient; | ||
import google.registry.util.EmailMessage; | ||
import javax.inject.Inject; | ||
import javax.mail.internet.InternetAddress; | ||
|
||
/** Sends BSA-related email notifications. */ | ||
class BsaEmailSender { | ||
|
||
private final InternetAddress alertRecipientAddress; | ||
private final GmailClient gmailClient; | ||
|
||
@Inject | ||
BsaEmailSender( | ||
GmailClient gmailClient, | ||
@Config("newAlertRecipientEmailAddress") InternetAddress alertRecipientAddress) { | ||
this.alertRecipientAddress = alertRecipientAddress; | ||
this.gmailClient = gmailClient; | ||
} | ||
|
||
/** Sends an email to the configured alert recipient. */ | ||
void sendNotification(String subject, String body) { | ||
this.gmailClient.sendEmail(EmailMessage.create(subject, body, alertRecipientAddress)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
core/src/test/java/google/registry/bsa/BsaRefreshActionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2024 The Nomulus Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package google.registry.bsa; | ||
|
||
import static com.google.common.base.Throwables.getStackTraceAsString; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import google.registry.bsa.api.BsaReportSender; | ||
import google.registry.bsa.persistence.RefreshScheduler; | ||
import google.registry.groups.GmailClient; | ||
import google.registry.request.Response; | ||
import google.registry.testing.FakeClock; | ||
import google.registry.util.EmailMessage; | ||
import javax.mail.internet.InternetAddress; | ||
import org.joda.time.DateTime; | ||
import org.joda.time.Duration; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
/** Unit tests for {@link BsaRefreshAction}. */ | ||
@ExtendWith(MockitoExtension.class) | ||
public class BsaRefreshActionTest { | ||
|
||
FakeClock fakeClock = new FakeClock(DateTime.parse("2023-11-09T02:08:57.880Z")); | ||
|
||
@Mock RefreshScheduler scheduler; | ||
|
||
@Mock GmailClient gmailClient; | ||
|
||
@Mock private InternetAddress emailRecipient; | ||
|
||
@Mock Response response; | ||
|
||
@Mock private BsaLock bsaLock; | ||
|
||
@Mock private GcsClient gcsClient; | ||
|
||
@Mock private BsaReportSender bsaReportSender; | ||
|
||
BsaRefreshAction action; | ||
|
||
@BeforeEach | ||
void setup() { | ||
action = | ||
new BsaRefreshAction( | ||
scheduler, | ||
gcsClient, | ||
bsaReportSender, | ||
/* transactionBatchSize= */ 5, | ||
/* domainCreateTxnCommitTimeLag= */ Duration.millis(1), | ||
new BsaEmailSender(gmailClient, emailRecipient), | ||
bsaLock, | ||
fakeClock, | ||
response); | ||
} | ||
|
||
@Test | ||
void notificationSent_cannotAcquireLock() { | ||
when(bsaLock.executeWithLock(any())).thenReturn(false); | ||
action.run(); | ||
verify(gmailClient, times(1)) | ||
.sendEmail( | ||
EmailMessage.create( | ||
"BSA refresh did not run: another BSA related task is running", | ||
"", | ||
emailRecipient)); | ||
} | ||
|
||
@Test | ||
void notificationSent_abortedByException() { | ||
RuntimeException throwable = new RuntimeException("Error"); | ||
when(bsaLock.executeWithLock(any())).thenThrow(throwable); | ||
action.run(); | ||
verify(gmailClient, times(1)) | ||
.sendEmail( | ||
EmailMessage.create( | ||
"BSA refresh aborted", getStackTraceAsString(throwable), emailRecipient)); | ||
} | ||
|
||
@Test | ||
void notificationSent_success() { | ||
when(bsaLock.executeWithLock(any())) | ||
.thenAnswer( | ||
args -> { | ||
return true; | ||
}); | ||
action.run(); | ||
verify(gmailClient, times(1)) | ||
.sendEmail(EmailMessage.create("BSA refreshed successfully", "", emailRecipient)); | ||
} | ||
} |
Oops, something went wrong.