Skip to content

Commit

Permalink
SLING-6949 - Create testing utilities for email-enabled applications
Browse files Browse the repository at this point in the history
Implement message removal

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1798232 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
rombert committed Jun 9, 2017
1 parent f5de0a0 commit bc95895
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* a JSON object containing the configuration properties of the {@link SmtpServerWrapper}</li>
* <li><tt>GET /system/sling/testing/email/messages</tt>, which returns the messages
* currently held by the {@link SmtpServerWrapper}</li>
* <li><tt>DELETE /system/sling/testing/email</tt>, which removes all messages.</li>
* </ol>
*/
@Component(service = Servlet.class,
Expand Down Expand Up @@ -103,7 +104,13 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
break;
}

}

@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

wiser.clearMessages();

resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,47 @@ public String getPathInfo() {
String readBody = JsonPath.read(new ByteArrayInputStream(out), "$.messages[0].['-Content-']");
assertThat("body", readBody, equalTo(body1));
}

@Test
public void getMessages_empty() throws ServletException, IOException {

// SLING-6947
MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(ctx.resourceResolver()) {
@Override
public String getPathInfo() {
return "/messages";
}
};

MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
servlet.service(request, response);

assertEquals("response.status", HttpServletResponse.SC_OK, response.getStatus());

// SLING-6948
byte[] out = response.getOutputAsString().getBytes();
int messageCount = JsonPath.read(new ByteArrayInputStream(out), "$.messages.length()");

assertThat("messages.length", messageCount, Matchers.equalTo(0));
}

@Test
public void deleteMessages() throws MessagingException, ServletException, IOException {

// send an email
sendEmail("Test email", "A long message \r\nbody");

// delete all messages
MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(ctx.resourceResolver());
request.setMethod("DELETE");
MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
servlet.service(request, response);

assertEquals("response.status", HttpServletResponse.SC_NO_CONTENT, response.getStatus());

// validate that no messages are stored
getMessages_empty();
}

private void sendEmail(String subject, String body) throws MessagingException, AddressException {

Expand Down

0 comments on commit bc95895

Please sign in to comment.