Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -22,6 +22,7 @@ public class WorkflowManagerExceptionCodes extends PnExceptionsCodes {
public static final String ERROR_CODE_WORKFLOWMANAGER_UNKNOWN_EVENT_CODE = "PN_WORKFLOWMANAGER_UNKNOWNEVENTCODE";
public static final String ERROR_CODE_WORKFLOWMANAGER_CHANNEL_TRIGGER_EVENT_NOT_FOUND = "PN_WORKFLOWMANAGER_CHANNELTRIGGEREVENTNOTFOUND";
public static final String ERROR_CODE_WORKFLOWMANAGER_PAYMENT_NOT_FOUND = "PN_WORKFLOWMANAGER_PAYMENTNOTFOUND";
public static final String ERROR_CODE_WORKFLOWMANAGER_SENDSMSNOTIFICATIONFAILED = "PN_WORKFLOWMANAGER_SENDSMSNOTIFICATIONFAILED";

public static final String ERROR_CODE_WORKFLOWMANAGER_SENDEMAILNOTIFICATIONFAILED = "PN_WORKFLOWMANAGER_SENDEMAILNOTIFICATIONFAILED";
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ void sendNotificationEMAIL(
DigitalAddressInt digitalAddress,
List<String> attachmentUrls
);

void sendNotificationSMS(
String requestIdx,
String textMessage,
String senderDigitalAddress
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.externalchannels.api.DigitalCourtesyMessagesApi;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.externalchannels.api.DigitalLegalMessagesApi;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.externalchannels.model.DigitalCourtesyMailRequest;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.externalchannels.model.DigitalCourtesySmsRequest;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.externalchannels.model.DigitalNotificationRequest;
import lombok.CustomLog;
import lombok.RequiredArgsConstructor;
Expand All @@ -22,6 +23,7 @@

import static it.pagopa.pn.workflowmanager.exceptions.WorkflowManagerExceptionCodes.ERROR_CODE_WORKFLOWMANAGER_SENDEMAILNOTIFICATIONFAILED;
import static it.pagopa.pn.workflowmanager.exceptions.WorkflowManagerExceptionCodes.ERROR_CODE_WORKFLOWMANAGER_SENDPECNOTIFICATIONFAILED;
import static it.pagopa.pn.workflowmanager.exceptions.WorkflowManagerExceptionCodes.ERROR_CODE_WORKFLOWMANAGER_SENDSMSNOTIFICATIONFAILED;

@Component
@CustomLog
Expand Down Expand Up @@ -105,4 +107,32 @@ public void sendNotificationEMAIL(String requestId,
digitalNotificationRequest.setAttachmentUrls(attachmentUrls.stream().map(FileUtils::getKeyWithStoragePrefix).toList());
return digitalNotificationRequest;
}

@Override
public void sendNotificationSMS(
String requestIdx,
String textMessage,
String senderDigitalAddress
) {
try {
log.logInvokingAsyncExternalService(CLIENT_NAME, COURTESY_NOTIFICATION_REQUEST + "[SMS]", requestIdx);
log.debug("[enter] sendNotificationSMS requestId={} senderDigitalAddress={}", requestIdx, LogUtils.maskNumber(senderDigitalAddress));

DigitalCourtesySmsRequest digitalNotificationRequest = new DigitalCourtesySmsRequest();
digitalNotificationRequest.setChannel(DigitalCourtesySmsRequest.ChannelEnum.SMS);
digitalNotificationRequest.setRequestId(requestIdx);
digitalNotificationRequest.setCorrelationId(requestIdx);
digitalNotificationRequest.setEventType(EVENT_TYPE_INFORMAL);
digitalNotificationRequest.setQos(DigitalCourtesySmsRequest.QosEnum.BATCH);
digitalNotificationRequest.setReceiverDigitalAddress(senderDigitalAddress);
digitalNotificationRequest.setClientRequestTimeStamp(Instant.now());
digitalNotificationRequest.setMessageText(textMessage);

digitalCourtesyMessagesApi.sendCourtesyShortMessage(requestIdx, cfg.getCxId(), digitalNotificationRequest);
log.debug("[exit] sendNotificationSMS requestId={} senderDigitalAddress={}", requestIdx, LogUtils.maskNumber(senderDigitalAddress));
} catch (Exception e) {
log.error("error sending SMS notification for requestIdx={}", requestIdx);
throw new PnInternalException("error sending SMS notification", ERROR_CODE_WORKFLOWMANAGER_SENDSMSNOTIFICATIONFAILED, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.externalchannels.api.DigitalCourtesyMessagesApi;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.externalchannels.api.DigitalLegalMessagesApi;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.externalchannels.model.DigitalCourtesyMailRequest;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.externalchannels.model.DigitalCourtesySmsRequest;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.externalchannels.model.DigitalNotificationRequest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -256,4 +257,46 @@ void wrapsWhenSendNotificationEmailFailAndRethrowsWhenExternalApiFails() {

assertSame(apiException, thrown.getCause());
}

@Test
void sendsSmsNotificationWithExpectedPayload() {
String requestIdx = "request-idx";
String cxId = "cx-id";
String textMessage = "Hello SMS";
String senderDigitalAddress = "+39123456789";

when(cfg.getCxId()).thenReturn(cxId);

client.sendNotificationSMS(requestIdx, textMessage, senderDigitalAddress);

ArgumentCaptor<DigitalCourtesySmsRequest> requestCaptor = ArgumentCaptor.forClass(DigitalCourtesySmsRequest.class);
verify(digitalCourtesyMessagesApi).sendCourtesyShortMessage(eq(requestIdx), eq(cxId), requestCaptor.capture());

DigitalCourtesySmsRequest sent = requestCaptor.getValue();
assertEquals(DigitalCourtesySmsRequest.ChannelEnum.SMS, sent.getChannel());
assertEquals(requestIdx, sent.getRequestId());
assertEquals(requestIdx, sent.getCorrelationId());
assertEquals("INFORMAL", sent.getEventType());
assertEquals(DigitalCourtesySmsRequest.QosEnum.BATCH, sent.getQos());
assertEquals(senderDigitalAddress, sent.getReceiverDigitalAddress());
assertEquals(textMessage, sent.getMessageText());
assertNotNull(sent.getClientRequestTimeStamp());
}

@Test
void wrapsAndRethrowsWhenSmsApiFails() {
String requestIdx = "request-idx";
RuntimeException apiException = new RuntimeException("boom");

when(cfg.getCxId()).thenReturn("cx-id");
doThrow(apiException).when(digitalCourtesyMessagesApi)
.sendCourtesyShortMessage(anyString(), anyString(), any(DigitalCourtesySmsRequest.class));

PnInternalException thrown = assertThrows(
PnInternalException.class,
() -> client.sendNotificationSMS(requestIdx, "text", "+39123456789")
);

assertSame(apiException, thrown.getCause());
}
}
Loading