Skip to content

Commit

Permalink
[SELC-5300] Adding context for log on sendMailTest (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-putzu authored Jul 18, 2024
1 parent bc15c80 commit 99662b9
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public HttpResponseMessage sendTestEmail(
@HttpTrigger(name = "req", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("TestSendEmail trigger processed a request");
completionService.sendTestEmail();
completionService.sendTestEmail(context);
return request.createResponseBuilder(HttpStatus.OK).build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.pagopa.selfcare.onboarding.service;

import com.microsoft.azure.functions.ExecutionContext;
import it.pagopa.selfcare.onboarding.dto.OnboardingAggregateOrchestratorInput;
import it.pagopa.selfcare.onboarding.entity.Onboarding;
import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflow;
Expand All @@ -24,5 +25,5 @@ public interface CompletionService {

String createAggregateOnboardingRequest(OnboardingAggregateOrchestratorInput onboardingAggregateOrchestratorInput);

void sendTestEmail();
void sendTestEmail(ExecutionContext context);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.pagopa.selfcare.onboarding.service;

import com.microsoft.azure.functions.ExecutionContext;
import it.pagopa.selfcare.onboarding.common.InstitutionPaSubunitType;
import it.pagopa.selfcare.onboarding.common.InstitutionType;
import it.pagopa.selfcare.onboarding.common.OnboardingStatus;
Expand Down Expand Up @@ -338,7 +339,7 @@ public String createAggregateOnboardingRequest(OnboardingAggregateOrchestratorIn
}

@Override
public void sendTestEmail() {
notificationService.sendTestEmail();
public void sendTestEmail(ExecutionContext context) {
notificationService.sendTestEmail(context);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.pagopa.selfcare.onboarding.service;


import com.microsoft.azure.functions.ExecutionContext;
import it.pagopa.selfcare.onboarding.common.InstitutionType;
import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflow;
import it.pagopa.selfcare.product.entity.Product;
Expand All @@ -27,5 +28,5 @@ public interface NotificationService {

void sendCompletedEmailAggregate(String institutionName, List<String> destinationMails);

void sendTestEmail();
void sendTestEmail(ExecutionContext context);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.pagopa.selfcare.onboarding.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.azure.functions.ExecutionContext;
import io.quarkus.mailer.Mail;
import io.quarkus.mailer.Mailer;
import it.pagopa.selfcare.azurestorage.AzureBlobClient;
Expand Down Expand Up @@ -237,21 +238,18 @@ static class FileMailData {
}

@Override
public void sendTestEmail() {
public void sendTestEmail(ExecutionContext context) {
try {
log.info("Sending Test email to {}", senderMail);

context.getLogger().info("Sending Test email to " + senderMail);
String html = "TEST EMAIL";

Mail mail = Mail
.withHtml(senderMail, html, html)
.setFrom(senderMail);

mailer.send(mail);

log.info("End of sending mail to {}, with subject {}", senderMail, mail);
context.getLogger().info("End of sending mail to {}, with subject " + senderMail + " with subject " + mail);
} catch (Exception e) {
log.error(String.format("%s: %s", ERROR_DURING_SEND_MAIL, e.getMessage()));
context.getLogger().severe(String.format("%s: %s", ERROR_DURING_SEND_MAIL, e.getMessage()));
throw new GenericOnboardingException(ERROR_DURING_SEND_MAIL.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -778,11 +778,11 @@ void sendTestEmail() {
}).when(req).createResponseBuilder(any(HttpStatus.class));

when(executionContext.getLogger()).thenReturn(Logger.getGlobal());
doNothing().when(completionService).sendTestEmail();
doNothing().when(completionService).sendTestEmail(executionContext);

function.sendTestEmail(req, executionContext);

verify(completionService, times(1))
.sendTestEmail();
.sendTestEmail(executionContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.microsoft.azure.functions.ExecutionContext;
import io.quarkus.mongodb.panache.common.PanacheUpdate;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
Expand Down Expand Up @@ -40,6 +41,7 @@

import java.time.LocalDateTime;
import java.util.*;
import java.util.logging.Logger;

import static it.pagopa.selfcare.onboarding.service.OnboardingService.USERS_FIELD_LIST;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -696,12 +698,15 @@ public static Onboarding createSampleOnboarding() {

@Test
void sendTestEmail() {
doNothing().when(notificationService).sendTestEmail();
ExecutionContext executionContext = mock(ExecutionContext.class);
when(executionContext.getLogger()).thenReturn(Logger.getGlobal());

doNothing().when(notificationService).sendTestEmail(executionContext);

completionServiceDefault.sendTestEmail();
completionServiceDefault.sendTestEmail(executionContext);

Mockito.verify(notificationService, times(1))
.sendTestEmail();
.sendTestEmail(executionContext);
}

}
Expand Down

0 comments on commit 99662b9

Please sign in to comment.