forked from folio-org/mod-sender
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from indexdata/MODSENDER-66
Modsender 66
- Loading branch information
Showing
6 changed files
with
206 additions
and
16 deletions.
There are no files selected for viewing
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
64 changes: 64 additions & 0 deletions
64
src/main/java/org/folio/sender/delivery/MailDeliveryChannel.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,64 @@ | ||
package org.folio.sender.delivery; | ||
|
||
import io.vertx.core.Vertx; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.ext.web.client.HttpRequest; | ||
import io.vertx.ext.web.client.WebClient; | ||
import org.apache.http.HttpStatus; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.folio.rest.jaxrs.model.EmailEntity; | ||
import org.folio.rest.jaxrs.model.User; | ||
|
||
public class MailDeliveryChannel implements DeliveryChannel { | ||
|
||
private static final Logger LOG = LogManager.getLogger(MailDeliveryChannel.class); | ||
|
||
private final WebClient webClient; | ||
private final String mailUrlPath; | ||
|
||
public MailDeliveryChannel(Vertx vertx, String mailUrlPath) { | ||
this.webClient = WebClient.create(vertx); | ||
this.mailUrlPath = mailUrlPath; | ||
} | ||
|
||
@Override | ||
public void deliverMessage(String notificationId, JsonObject recipientJson, | ||
JsonObject message, JsonObject okapiHeadersJson) { | ||
LOG.debug("deliverMessage:: Sending message to recipient {} with message {}", | ||
recipientJson, message); | ||
try { | ||
User recipient = recipientJson.mapTo(User.class); | ||
EmailEntity emailEntity = message.mapTo(EmailEntity.class); | ||
emailEntity.setNotificationId(notificationId); | ||
emailEntity.setTo(getTo(recipient)); | ||
|
||
HttpRequest<Buffer> request = EmailDeliveryChannel.createRequestAndPrepareHeaders( | ||
okapiHeadersJson, mailUrlPath, webClient); | ||
|
||
request.sendJson(emailEntity, response -> { | ||
if (response.failed()) { | ||
LOG.error("deliverMessage:: Error from Mail module {} ", response.cause().getMessage()); | ||
} else if (response.result().statusCode() != HttpStatus.SC_OK) { | ||
String errorMessage = String.format("deliverMessage:: Mail module responded with " | ||
+ "status '%s' and body '%s'", | ||
response.result().statusCode(), response.result().bodyAsString()); | ||
LOG.error(errorMessage); | ||
} | ||
}); | ||
} catch (Exception e) { | ||
LOG.error("deliverMessage:: Error while attempting to " | ||
+ "deliver message to recipient {} ", recipientJson, e); | ||
} | ||
} | ||
|
||
private String getTo(User recipient) { | ||
if (recipient.getPersonal() != null) { | ||
return recipient.getPersonal().getLastName() + "," | ||
+ recipient.getPersonal().getFirstName() + "," + recipient.getPersonal().getEmail(); | ||
} else { | ||
return recipient.getId(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -79,7 +79,6 @@ public static void tearDown(TestContext context) { | |
vertx.close(context.asyncAssertSuccess()); | ||
} | ||
|
||
|
||
@Test | ||
public void shouldReturn422WhenInvalidBody() { | ||
JsonObject emptyBody = new JsonObject(); | ||
|
@@ -155,6 +154,116 @@ public void shouldReturnNoContentAndSendEmailWhenRequestIsValid() { | |
WireMock.urlMatching("/users/" + mockRecipient.getId()))); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNoContentAndSendMailWhenRequestIsValid() { | ||
User mockRecipient = new User() | ||
.withId(UUID.randomUUID().toString()) | ||
.withPersonal(new Personal().withEmail("[email protected]")); | ||
|
||
mockUserModule(mockRecipient.getId(), mockRecipient); | ||
mockMailModule(); | ||
|
||
Message mailChannel1 = new Message() | ||
.withDeliveryChannel("mail") | ||
.withHeader("header1") | ||
.withBody("body1") | ||
.withOutputFormat(MediaType.TEXT_PLAIN); | ||
Message mailChannel2 = new Message() | ||
.withDeliveryChannel("mail") | ||
.withHeader("header2") | ||
.withBody("body2") | ||
.withOutputFormat(MediaType.TEXT_HTML); | ||
|
||
Notification notification = new Notification() | ||
.withNotificationId(UUID.randomUUID().toString()) | ||
.withRecipientUserId(mockRecipient.getId()) | ||
.withMessages(Arrays.asList(mailChannel1, mailChannel2)); | ||
|
||
RestAssured.given() | ||
.spec(spec) | ||
.header(mockUrlHeader) | ||
.body(toJson(notification)) | ||
.when() | ||
.post(MESSAGE_DELIVERY_PATH) | ||
.then() | ||
.statusCode(HttpStatus.SC_NO_CONTENT); | ||
|
||
WireMock.verify(1, WireMock.getRequestedFor( | ||
WireMock.urlMatching("/users/" + mockRecipient.getId()))); | ||
WireMock.verify(2, WireMock.postRequestedFor( | ||
WireMock.urlMatching("/mail"))); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNoContentWhenFailedToSend() { | ||
User mockRecipient = new User() | ||
.withId(UUID.randomUUID().toString()) | ||
.withPersonal(new Personal().withEmail("[email protected]")); | ||
|
||
mockUserModule(mockRecipient.getId(), mockRecipient); | ||
WireMock.stubFor(WireMock.post("/mail").willReturn(WireMock.forbidden())); | ||
|
||
Message mailChannel1 = new Message() | ||
.withDeliveryChannel("mail") | ||
.withHeader("header1") | ||
.withBody("body1") | ||
.withOutputFormat(MediaType.TEXT_PLAIN); | ||
|
||
Notification notification = new Notification() | ||
.withNotificationId(UUID.randomUUID().toString()) | ||
.withRecipientUserId(mockRecipient.getId()) | ||
.withMessages(Collections.singletonList(mailChannel1)); | ||
|
||
RestAssured.given() | ||
.spec(spec) | ||
.header(mockUrlHeader) | ||
.body(toJson(notification)) | ||
.when() | ||
.post(MESSAGE_DELIVERY_PATH) | ||
.then() | ||
.statusCode(HttpStatus.SC_NO_CONTENT); | ||
|
||
WireMock.verify(1, WireMock.getRequestedFor( | ||
WireMock.urlMatching("/users/" + mockRecipient.getId()))); | ||
WireMock.verify(1, WireMock.postRequestedFor( | ||
WireMock.urlMatching("/mail"))); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNoContentWhenUnexpectedReturnCode() { | ||
User mockRecipient = new User() | ||
.withId(UUID.randomUUID().toString()) | ||
.withPersonal(new Personal().withEmail("[email protected]")); | ||
|
||
mockUserModule(mockRecipient.getId(), mockRecipient); | ||
WireMock.stubFor(WireMock.post("/mail").willReturn(WireMock.created())); | ||
|
||
Message mailChannel1 = new Message() | ||
.withDeliveryChannel("mail") | ||
.withHeader("header1") | ||
.withBody("body1") | ||
.withOutputFormat(MediaType.TEXT_PLAIN); | ||
|
||
Notification notification = new Notification() | ||
.withNotificationId(UUID.randomUUID().toString()) | ||
.withRecipientUserId(mockRecipient.getId()) | ||
.withMessages(Collections.singletonList(mailChannel1)); | ||
|
||
RestAssured.given() | ||
.spec(spec) | ||
.header(mockUrlHeader) | ||
.body(toJson(notification)) | ||
.when() | ||
.post(MESSAGE_DELIVERY_PATH) | ||
.then() | ||
.statusCode(HttpStatus.SC_NO_CONTENT); | ||
|
||
WireMock.verify(1, WireMock.getRequestedFor( | ||
WireMock.urlMatching("/users/" + mockRecipient.getId()))); | ||
WireMock.verify(1, WireMock.postRequestedFor( | ||
WireMock.urlMatching("/mail"))); | ||
} | ||
|
||
@Test | ||
public void shouldNotFailWhenUserContainsAdditionalProperties() { | ||
User mockRecipient = new User() | ||
|
@@ -260,6 +369,11 @@ private void mockEmailModule() { | |
.willReturn(WireMock.ok())); | ||
} | ||
|
||
private void mockMailModule() { | ||
WireMock.stubFor(WireMock.post("/mail") | ||
.willReturn(WireMock.ok())); | ||
} | ||
|
||
private void mockUserModule(String userId, User response) { | ||
WireMock.stubFor(WireMock.get("/users/" + userId) | ||
.willReturn(WireMock.okJson(JsonObject.mapFrom(response).toString()))); | ||
|