Skip to content

Commit

Permalink
split logging to its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
Rwolfe-Nava committed Jun 18, 2024
1 parent a7b063e commit 4e8b44a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
22 changes: 13 additions & 9 deletions optout/src/main/java/gov/cms/ab2d/optout/OptOutHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,25 @@ public void processSQSMessage(SQSEvent.SQSMessage msg, Context context) {

var optOutProcessing = processorInit(logger);
var optOutResults = optOutProcessing.process(getFileName(notification), getBucketName(notification), ENDPOINT);
if (optOutResults != null) {
logger.log("OptOut Lambda completed. Total records processed today is: totaltoday=" + optOutResults.getTotalToday()
+ " Number of opt in today is: todayin=" + optOutResults.getOptInToday()
+ " Number of opt out today is: todayout=" + optOutResults.getOptOutToday()
+ " Total records processed to date is: totaltodate=" + optOutResults.getTotalAllTime()
+ " Total number of opt in is: totalin=" + optOutResults.getOptInTotal()
+ " Total number of opt out is: totalout=" + optOutResults.getOptOutTotal()
);
}
logResults(optOutResults, logger);
} catch (Exception ex) {
logger.log("An error occurred");
throw new OptOutException("An error occurred", ex);
}
}

public void logResults(OptOutResults optOutResults, LambdaLogger logger) {
if (optOutResults != null) {
logger.log("OptOut Lambda completed. Total records processed today is: totaltoday=" + optOutResults.getTotalToday()
+ " Number of opt in today is: todayin=" + optOutResults.getOptInToday()
+ " Number of opt out today is: todayout=" + optOutResults.getOptOutToday()
+ " Total records processed to date is: totaltodate=" + optOutResults.getTotalAllTime()
+ " Total number of opt in is: totalin=" + optOutResults.getOptInTotal()
+ " Total number of opt out is: totalout=" + optOutResults.getOptOutTotal()
);
}
}

public OptOutProcessor processorInit(LambdaLogger logger) {
return new OptOutProcessor(logger);
}
Expand Down
22 changes: 17 additions & 5 deletions optout/src/test/java/gov/cms/ab2d/optout/OptOutHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,29 @@ void optOutHandlerInvoke() {
verify(logger, times(1)).log(anyString());
}

@Test
void itLogsResults() {
LambdaLogger logger = mock(LambdaLogger.class);
OptOutResults optOutResults = new OptOutResults(1, 1, 2, 2);

String loggedMessage = "OptOut Lambda completed. Total records processed today is: totaltoday=" + optOutResults.getTotalToday()
+ " Number of opt in today is: todayin=" + optOutResults.getOptInToday()
+ " Number of opt out today is: todayout=" + optOutResults.getOptOutToday()
+ " Total records processed to date is: totaltodate=" + optOutResults.getTotalAllTime()
+ " Total number of opt in is: totalin=" + optOutResults.getOptInTotal()
+ " Total number of opt out is: totalout=" + optOutResults.getOptOutTotal();

handler.logResults(optOutResults, logger);
verify(logger, times(1)).log(loggedMessage);
}

@Test
void itDoesNotLogWhenResultsAreNull() throws URISyntaxException {
Context context = mock(Context.class);
LambdaLogger logger = mock(LambdaLogger.class);
OptOutProcessor processor = mock(OptOutProcessor.class);

when(context.getLogger()).thenReturn(logger);
when(handler.processorInit(any())).thenReturn(processor);
when(processor.process(anyString(), anyString(), anyString())).thenReturn(null);

handler.handleRequest(sqsEvent, context);
handler.logResults(null, logger);
verify(logger, times(0)).log(anyString());
}

Expand Down

0 comments on commit 4e8b44a

Please sign in to comment.