From 3b1d8fd8feaea4bc3d47763dec1596f76c655d93 Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Fri, 27 Jun 2014 11:11:33 +0100 Subject: [PATCH] Added mail server to test tools --- testsuite/tools/pom.xml | 13 ++ .../keycloak/test/tools/DestroyListener.java | 21 +++ .../test/tools/KeycloakTestApplication.java | 5 + .../java/org/keycloak/test/tools/Mail.java | 131 ++++++++++++++++++ .../org/keycloak/test/tools/PerfTools.java | 4 +- .../tools/src/main/webapp/WEB-INF/web.xml | 7 +- testsuite/tools/src/main/webapp/index.html | 1 + testsuite/tools/src/main/webapp/js/app.js | 25 ++++ .../tools/src/main/webapp/pages/mail.html | 23 +++ 9 files changed, 226 insertions(+), 4 deletions(-) create mode 100644 testsuite/tools/src/main/java/org/keycloak/test/tools/DestroyListener.java create mode 100644 testsuite/tools/src/main/java/org/keycloak/test/tools/Mail.java create mode 100644 testsuite/tools/src/main/webapp/pages/mail.html diff --git a/testsuite/tools/pom.xml b/testsuite/tools/pom.xml index e6e0f79002..3d3d2c3256 100755 --- a/testsuite/tools/pom.xml +++ b/testsuite/tools/pom.xml @@ -15,6 +15,19 @@ + + + com.icegreen + greenmail + + + org.slf4j + slf4j-api + + + + + org.keycloak keycloak-core diff --git a/testsuite/tools/src/main/java/org/keycloak/test/tools/DestroyListener.java b/testsuite/tools/src/main/java/org/keycloak/test/tools/DestroyListener.java new file mode 100644 index 0000000000..99807dcdbf --- /dev/null +++ b/testsuite/tools/src/main/java/org/keycloak/test/tools/DestroyListener.java @@ -0,0 +1,21 @@ +package org.keycloak.test.tools; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +/** + * @author Stian Thorgersen + */ +public class DestroyListener implements ServletContextListener { + @Override + public void contextInitialized(ServletContextEvent sce) { + + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + if (KeycloakTestApplication.mail != null) { + KeycloakTestApplication.mail.stop(); + } + } +} diff --git a/testsuite/tools/src/main/java/org/keycloak/test/tools/KeycloakTestApplication.java b/testsuite/tools/src/main/java/org/keycloak/test/tools/KeycloakTestApplication.java index 52e7811007..7184978d64 100644 --- a/testsuite/tools/src/main/java/org/keycloak/test/tools/KeycloakTestApplication.java +++ b/testsuite/tools/src/main/java/org/keycloak/test/tools/KeycloakTestApplication.java @@ -5,6 +5,8 @@ import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.services.resources.KeycloakApplication; import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; import javax.ws.rs.core.Application; import javax.ws.rs.core.Context; import java.util.HashSet; @@ -19,6 +21,8 @@ public class KeycloakTestApplication extends Application { protected Set> classes = new HashSet>(); protected Set singletons = new HashSet(); + static Mail mail = new Mail(); + public KeycloakTestApplication(@Context ServletContext context, @Context Dispatcher dispatcher) { KeycloakApplication.loadConfig(); @@ -27,6 +31,7 @@ public class KeycloakTestApplication extends Application { context.setAttribute(KeycloakSessionFactory.class.getName(), this.sessionFactory); singletons.add(new PerfTools(sessionFactory)); + singletons.add(mail); } @Override diff --git a/testsuite/tools/src/main/java/org/keycloak/test/tools/Mail.java b/testsuite/tools/src/main/java/org/keycloak/test/tools/Mail.java new file mode 100644 index 0000000000..ddbfe33a38 --- /dev/null +++ b/testsuite/tools/src/main/java/org/keycloak/test/tools/Mail.java @@ -0,0 +1,131 @@ +package org.keycloak.test.tools; + +import com.icegreen.greenmail.util.GreenMail; +import com.icegreen.greenmail.util.ServerSetup; + +import javax.mail.internet.MimeMessage; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import java.util.LinkedList; +import java.util.List; + +/** + * @author Stian Thorgersen + */ +@Path("mail") +public class Mail { + + private GreenMail greenMail; + + @GET + @Path("status") + @Produces("application/json") + public synchronized Status status() { + return new Status(greenMail != null); + } + + @GET + @Path("start") + @Produces("application/json") + public synchronized Status start() { + if (greenMail == null) { + ServerSetup setup = new ServerSetup(3025, "localhost", "smtp"); + + greenMail = new GreenMail(setup); + try { + greenMail.start(); + } catch (Throwable t) { + greenMail = null; + return new Status(false); + } + } + + return new Status(true); + } + + @GET + @Path("stop") + @Produces("application/json") + public synchronized Status stop() { + if (greenMail != null) { + greenMail.stop(); + greenMail = null; + } + + return new Status(false); + } + + @GET + @Path("messages") + @Produces("application/json") + public synchronized List getMessages() throws Exception { + List messages = new LinkedList(); + if (greenMail != null) { + for (MimeMessage m : greenMail.getReceivedMessages()) { + messages.add(new Message(m)); + } + } + return messages; + } + + @Override + protected void finalize() throws Throwable { + if (greenMail != null) { + greenMail.stop(); + } + } + + public static class Status { + + private boolean started; + + public Status(boolean started) { + this.started = started; + } + + public boolean isStarted() { + return started; + } + + } + + public static class Message { + + private String from; + private String to; + private String subject; + private String body; + private Long date; + + public Message(MimeMessage m) throws Exception { + from = m.getFrom()[0].toString(); + to = m.getRecipients(MimeMessage.RecipientType.TO)[0].toString(); + subject = m.getSubject(); + body = m.getContent().toString(); + date = m.getSentDate() != null ? m.getSentDate().getTime() : null; + } + + public String getFrom() { + return from; + } + + public String getTo() { + return to; + } + + public String getSubject() { + return subject; + } + + public String getBody() { + return body; + } + + public Long getDate() { + return date; + } + + } + +} diff --git a/testsuite/tools/src/main/java/org/keycloak/test/tools/PerfTools.java b/testsuite/tools/src/main/java/org/keycloak/test/tools/PerfTools.java index 638e064909..2e08ed77e9 100644 --- a/testsuite/tools/src/main/java/org/keycloak/test/tools/PerfTools.java +++ b/testsuite/tools/src/main/java/org/keycloak/test/tools/PerfTools.java @@ -65,7 +65,7 @@ public class PerfTools { @GET @Path("{realm}/create-users") - public Response createUsers(@PathParam("realm") String realmName, @QueryParam("count") Integer count, @QueryParam("batch") Integer batch, @QueryParam("start") Integer start, @QueryParam("prefix") String prefix, @QueryParam("roles") String roles) throws InterruptedException { + public void createUsers(@PathParam("realm") String realmName, @QueryParam("count") Integer count, @QueryParam("batch") Integer batch, @QueryParam("start") Integer start, @QueryParam("prefix") String prefix, @QueryParam("roles") String roles) throws InterruptedException { if (count == null) { count = 1; } @@ -88,8 +88,6 @@ public class PerfTools { int c = s + batch <= (start + count) ? batch : (start + count) - s; executor.submit(new CreateUsers(job, sessionFactory, realmName, s, c, prefix, rolesArray)); } - - return Response.noContent().build(); } @GET diff --git a/testsuite/tools/src/main/webapp/WEB-INF/web.xml b/testsuite/tools/src/main/webapp/WEB-INF/web.xml index aea2f9c7fe..f899f63945 100755 --- a/testsuite/tools/src/main/webapp/WEB-INF/web.xml +++ b/testsuite/tools/src/main/webapp/WEB-INF/web.xml @@ -26,7 +26,7 @@ - org.keycloak.services.listeners.KeycloakSessionDestroyListener + org.keycloak.test.tools.DestroyListener @@ -54,6 +54,11 @@ /perf/* + + Keycloak REST Interface + /mail/* + +