This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 1038a909adb3129c9059100d4f54e8d91d8a29e6 Author: Benoit Tellier <btell...@linagora.com> AuthorDate: Thu Jun 6 15:25:04 2019 +0700 JAMES-2526 UnauthorizedEndpoint can be performed on a single James server As all the endpoints are rejected with a 401 unauthorized we can clearly play all 89 the tests on the same James instance, saving 88 restarts --- .../integration/CassandraJmapExtension.java | 103 ++++++++++++++++++++- .../integration/UnauthorizedEndpointsTest.java | 10 +- 2 files changed, 103 insertions(+), 10 deletions(-) diff --git a/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/CassandraJmapExtension.java b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/CassandraJmapExtension.java index 5d7c8b2..90a8dec 100644 --- a/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/CassandraJmapExtension.java +++ b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/CassandraJmapExtension.java @@ -21,6 +21,9 @@ package org.apache.james.webadmin.integration; import static org.apache.james.CassandraJamesServerMain.ALL_BUT_JMX_CASSANDRA_MODULE; import java.io.IOException; +import java.util.Optional; +import java.util.function.Consumer; +import java.util.function.Supplier; import org.apache.james.CleanupTasksPerformer; import org.apache.james.DockerCassandraRule; @@ -31,6 +34,7 @@ import org.apache.james.mailbox.store.search.PDFTextExtractor; import org.apache.james.modules.TestDockerESMetricReporterModule; import org.apache.james.modules.TestJMAPServerModule; import org.apache.james.server.core.configuration.Configuration; +import org.apache.james.util.FunctionalUtils; import org.apache.james.util.Runnables; import org.apache.james.webadmin.WebAdminConfiguration; import org.junit.jupiter.api.extension.AfterAllCallback; @@ -43,19 +47,100 @@ import org.junit.jupiter.api.extension.ParameterResolutionException; import org.junit.jupiter.api.extension.ParameterResolver; import org.junit.rules.TemporaryFolder; +import com.github.fge.lambdas.Throwing; + public class CassandraJmapExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, ParameterResolver { + public interface JamesLifeCyclePolicy { + JamesLifeCyclePolicy FOR_EACH_TEST = serverSupplier -> JamesLifecycleHandler.builder() + .beforeAll(Optional::empty) + .beforeEach(() -> Optional.of(serverSupplier.get())) + .afterEach(GuiceJamesServer::stop) + .afterAll(guiceJamesServer -> { }); + JamesLifeCyclePolicy COMMON_TO_ALL_TESTS = serverSupplier -> JamesLifecycleHandler.builder() + .beforeAll(() -> Optional.of(serverSupplier.get())) + .beforeEach(Optional::empty) + .afterEach(guiceJamesServer -> { }) + .afterAll(GuiceJamesServer::stop); + + JamesLifecycleHandler createHandler(Supplier<GuiceJamesServer> serverSupplier); + } + + public static class JamesLifecycleHandler { + public interface Builder { + @FunctionalInterface + interface RequiresBeforeAll { + RequiresBeforeEach beforeAll(Supplier<Optional<GuiceJamesServer>> beforeAll); + + } + + @FunctionalInterface + interface RequiresBeforeEach { + RequiresAfterEach beforeEach(Supplier<Optional<GuiceJamesServer>> beforeEach); + } + + @FunctionalInterface + interface RequiresAfterEach { + RequiresAfterAll afterEach(Consumer<GuiceJamesServer> afterAll); + } + + @FunctionalInterface + interface RequiresAfterAll { + JamesLifecycleHandler afterAll(Consumer<GuiceJamesServer> afterAll); + } + } + + public static Builder.RequiresBeforeAll builder() { + return beforeAll -> beforeEach -> afterEach -> afterAll -> new JamesLifecycleHandler(beforeAll, beforeEach, afterEach, afterAll); + } + + private final Supplier<Optional<GuiceJamesServer>> beforeAll; + private final Supplier<Optional<GuiceJamesServer>> beforeEach; + private final Consumer<GuiceJamesServer> afterEach; + private final Consumer<GuiceJamesServer> afterAll; + + JamesLifecycleHandler(Supplier<Optional<GuiceJamesServer>> beforeAll, Supplier<Optional<GuiceJamesServer>> beforeEach, Consumer<GuiceJamesServer> afterEach, Consumer<GuiceJamesServer> afterAll) { + this.beforeAll = beforeAll; + this.beforeEach = beforeEach; + this.afterEach = afterEach; + this.afterAll = afterAll; + } + + Optional<GuiceJamesServer> beforeAll() { + return beforeAll.get() + .map(FunctionalUtils.toFunction(Throwing.consumer(GuiceJamesServer::start))); + } + + Optional<GuiceJamesServer> beforeEach() { + return beforeEach.get() + .map(FunctionalUtils.toFunction(Throwing.consumer(GuiceJamesServer::start))); + } + + void afterEach(GuiceJamesServer guiceJamesServer) { + afterEach.accept(guiceJamesServer); + } + + void afterAll(GuiceJamesServer guiceJamesServer) { + afterAll.accept(guiceJamesServer); + } + } private static final int LIMIT_TO_20_MESSAGES = 20; private final TemporaryFolder temporaryFolder; private final DockerCassandraRule cassandra; private final DockerElasticSearchRule elasticSearchRule; + private final JamesLifecycleHandler jamesLifecycleHandler; private GuiceJamesServer james; public CassandraJmapExtension() { + this(JamesLifeCyclePolicy.FOR_EACH_TEST); + } + + public CassandraJmapExtension(JamesLifeCyclePolicy jamesLifeCyclePolicy) { this.temporaryFolder = new TemporaryFolder(); this.cassandra = new DockerCassandraRule(); this.elasticSearchRule = new DockerElasticSearchRule(); + this.jamesLifecycleHandler = jamesLifeCyclePolicy.createHandler(jamesSupplier()); } private GuiceJamesServer james() throws IOException { @@ -75,27 +160,31 @@ public class CassandraJmapExtension implements BeforeAllCallback, AfterAllCallba .overrideWith((binder -> binder.bind(CleanupTasksPerformer.class).asEagerSingleton())); } + private Supplier<GuiceJamesServer> jamesSupplier() { + return Throwing.supplier(this::james); + } + @Override public void beforeAll(ExtensionContext context) throws Exception { temporaryFolder.create(); - Runnables.runParallel(cassandra::start, elasticSearchRule::start); + james = jamesLifecycleHandler.beforeAll().orElse(james); } @Override public void afterAll(ExtensionContext context) { + jamesLifecycleHandler.afterAll(james); Runnables.runParallel(cassandra::stop, elasticSearchRule.getDockerEs()::cleanUpData); } @Override - public void beforeEach(ExtensionContext context) throws Exception { - james = james(); - james.start(); + public void beforeEach(ExtensionContext context) { + james = jamesLifecycleHandler.beforeEach().orElse(james); } @Override public void afterEach(ExtensionContext context) { - james.stop(); + jamesLifecycleHandler.afterEach(james); } @Override @@ -103,6 +192,10 @@ public class CassandraJmapExtension implements BeforeAllCallback, AfterAllCallba return parameterContext.getParameter().getType() == GuiceJamesServer.class; } + public GuiceJamesServer getJames() { + return james; + } + @Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { return james; diff --git a/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/UnauthorizedEndpointsTest.java b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/UnauthorizedEndpointsTest.java index 7d22960..c320895 100644 --- a/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/UnauthorizedEndpointsTest.java +++ b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/UnauthorizedEndpointsTest.java @@ -21,7 +21,6 @@ package org.apache.james.webadmin.integration; import static io.restassured.RestAssured.when; -import org.apache.james.GuiceJamesServer; import org.apache.james.utils.WebAdminGuiceProbe; import org.apache.james.webadmin.WebAdminUtils; import org.apache.james.webadmin.routes.AliasRoutes; @@ -45,18 +44,19 @@ import org.apache.james.webadmin.routes.UserRoutes; import org.apache.james.webadmin.vault.routes.DeletedMessagesVaultRoutes; import org.eclipse.jetty.http.HttpStatus; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import io.restassured.RestAssured; -@ExtendWith(CassandraJmapExtension.class) class UnauthorizedEndpointsTest { + @RegisterExtension + static CassandraJmapExtension cassandraJmapExtension = new CassandraJmapExtension(CassandraJmapExtension.JamesLifeCyclePolicy.COMMON_TO_ALL_TESTS); @BeforeEach - void setup(GuiceJamesServer james) { - WebAdminGuiceProbe webAdminGuiceProbe = james.getProbe(WebAdminGuiceProbe.class); + void setup() { + WebAdminGuiceProbe webAdminGuiceProbe = cassandraJmapExtension.getJames().getProbe(WebAdminGuiceProbe.class); RestAssured.requestSpecification = WebAdminUtils.buildRequestSpecification(webAdminGuiceProbe.getWebAdminPort()) .build(); --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org