Arsnael commented on code in PR #1004: URL: https://github.com/apache/james-project/pull/1004#discussion_r877826226
########## server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/MessageRoutesExpireTest.java: ########## @@ -0,0 +1,284 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.webadmin.routes; + +import com.google.common.collect.ImmutableSet; +import io.restassured.RestAssured; +import org.apache.james.core.Username; +import org.apache.james.domainlist.api.DomainList; +import org.apache.james.json.DTOConverter; +import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.MessageManager; +import org.apache.james.mailbox.inmemory.InMemoryMailboxManager; +import org.apache.james.mailbox.inmemory.InMemoryMessageId; +import org.apache.james.mailbox.inmemory.manager.InMemoryIntegrationResources; +import org.apache.james.mailbox.model.ComposedMessageId; +import org.apache.james.mailbox.model.Mailbox; +import org.apache.james.mailbox.model.MailboxPath; +import org.apache.james.mailbox.store.mail.model.MailboxMessage; +import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex; +import org.apache.james.task.Hostname; +import org.apache.james.task.MemoryTaskManager; +import org.apache.james.user.memory.MemoryUsersRepository; +import org.apache.james.webadmin.WebAdminServer; +import org.apache.james.webadmin.WebAdminUtils; +import org.apache.james.webadmin.service.ExpireInboxesAdditionalInformationDTO; +import org.apache.james.webadmin.service.ExpireInboxesService; +import org.apache.james.webadmin.service.ExpireInboxesService.RunningOptions; +import org.apache.james.webadmin.utils.ErrorResponder; +import org.apache.james.webadmin.utils.JsonTransformer; +import org.apache.mailbox.tools.indexer.MessageIdReIndexerImpl; +import org.apache.mailbox.tools.indexer.MessageIdReIndexingTask; +import org.apache.mailbox.tools.indexer.MessageIdReindexingTaskAdditionalInformationDTO; +import org.apache.mailbox.tools.indexer.ReIndexerPerformer; +import org.eclipse.jetty.http.HttpStatus; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import reactor.core.publisher.Mono; + +import java.util.Date; +import java.util.Optional; + +import static io.restassured.RestAssured.given; +import static io.restassured.RestAssured.when; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +class MessageRoutesExpireTest { + private static final DomainList NO_DOMAIN_LIST = null; + private static final Username USERNAME = Username.of("benwa"); + private static final MailboxPath INBOX = MailboxPath.inbox(USERNAME); + + private WebAdminServer webAdminServer; + private InMemoryMailboxManager mailboxManager; + private MemoryTaskManager taskManager; + private MemoryUsersRepository usersRepository; + + @BeforeEach + void beforeEach() { + taskManager = new MemoryTaskManager(new Hostname("foo")); + mailboxManager = InMemoryIntegrationResources.defaultResources().getMailboxManager(); + usersRepository = MemoryUsersRepository.withoutVirtualHosting(NO_DOMAIN_LIST); + JsonTransformer jsonTransformer = new JsonTransformer(); + + webAdminServer = WebAdminUtils.createWebAdminServer( + new TasksRoutes(taskManager, jsonTransformer, + DTOConverter.of( + ExpireInboxesAdditionalInformationDTO.module())), + new MessagesRoutes(taskManager, + new InMemoryMessageId.Factory(), + null, + new ExpireInboxesService(usersRepository, mailboxManager), + jsonTransformer, + ImmutableSet.of())) + .start(); + + RestAssured.requestSpecification = WebAdminUtils.buildRequestSpecification(webAdminServer).build(); + RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); + } + + @AfterEach + void tearDown() { + webAdminServer.destroy(); + taskManager.stop(); + } + + @Nested + class ExpireInboxes { + @Nested + class Validation { + @Test + void expireInboxesShouldFailWithNoOption() { + when() + .delete("/messages") + .then() + .statusCode(HttpStatus.BAD_REQUEST_400) + .body("statusCode", is(400)) + .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType())) + .body("message", is("Invalid arguments supplied in the user request")) + .body("details", is("Must specify either 'olderThan' or 'byExpiresHeader' parameter")); + } + + @Test + void expireInboxesShouldFailWithBothOptions() { + when() + .delete("/messages?byExpiresHeader&olderThan=30d") + .then() + .statusCode(HttpStatus.BAD_REQUEST_400) + .body("statusCode", is(400)) + .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType())) + .body("message", is("Invalid arguments supplied in the user request")) + .body("details", is("Must specify either 'olderThan' or 'byExpiresHeader' parameter")); + } + + @Test + void expireInboxesShouldFailWithBadOlderThan() { + when() + .delete("/messages?olderThan=bad") + .then() + .statusCode(HttpStatus.BAD_REQUEST_400) + .body("statusCode", is(400)) + .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType())) + .body("message", is("Invalid arguments supplied in the user request")) + .body("details", is("'usersPerSecond' must be numeric")); + } + + @Test + void expireInboxesShouldFailWithNegativeOlderThan() { + when() + .delete("/messages?olderThan=-30d") + .then() + .statusCode(HttpStatus.BAD_REQUEST_400) + .body("statusCode", is(400)) + .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType())) + .body("message", is("Invalid arguments supplied in the user request")) + .body("details", is("Duration amount should be positive")); + } + + @Test + void expireInboxesShouldFailWithBadUsersPerSeconds() { + when() + .delete("/messages?byExpiresHeader&usersPerSecond=bad") + .then() + .statusCode(HttpStatus.BAD_REQUEST_400) + .body("statusCode", is(400)) + .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType())) + .body("message", is("Invalid arguments supplied in the user request")) + .body("details", is("'usersPerSecond' must be numeric")); + } + + @Test + void expireInboxesShouldFailWithNegativeUsersPerSeconds() { + when() + .delete("/messages?byExpiresHeader&usersPerSecond=-10") + .then() + .statusCode(HttpStatus.BAD_REQUEST_400) + .body("statusCode", is(400)) + .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType())) + .body("message", is("Invalid arguments supplied in the user request")) + .body("details", is("'usersPerSecond' must be strictly positive")); + } + + } + + @Nested + class TaskDetails { + @Test + void expireInboxesShouldNotFailWhenNoMailsFound() { + String taskId = when() + .delete("/messages?byExpiresHeader") + .jsonPath() + .get("taskId"); + + given() + .basePath(TasksRoutes.BASE) + .when() + .get(taskId + "/await") + .then() + .body("type", Matchers.is("ExpireInboxesTask")) + .body("status", is("completed")) + .body("taskId", is(notNullValue())) + .body("additionalInformation.type", is("ExpireInboxesTask")) + .body("additionalInformation.runningOptions.usersPerSecond", is(RunningOptions.DEFAULT.getUsersPerSecond())) + .body("additionalInformation.runningOptions.byExpiresHeader", is(true)) + .body("additionalInformation.runningOptions.olderThan", is(nullValue())) + .body("additionalInformation.inboxesExpired", is(0)) + .body("additionalInformation.inboxesFailed", is(0)) + .body("additionalInformation.inboxesProcessed", is(0)) + .body("additionalInformation.messagesDeleted", is(0)); + } + + @Test + void expireInboxesShouldReturnTaskDetailsWhenMail() throws Exception { + usersRepository.addUser(USERNAME, "secret"); + MailboxSession systemSession = mailboxManager.createSystemSession(USERNAME); + mailboxManager.createMailbox(INBOX, systemSession).get(); + mailboxManager.getMailbox(INBOX, systemSession).appendMessage( + MessageManager.AppendCommand.builder() + .withInternalDate(new Date(System.currentTimeMillis() - 5000)) + .build("header: value\r\n\r\nbody"), + systemSession).getId(); + + String taskId = when() + .delete("/messages?olderThan=1s") + .jsonPath() + .get("taskId"); + + given() + .basePath(TasksRoutes.BASE) + .when() + .get(taskId + "/await") + .then() + .body("type", Matchers.is("ExpireInboxesTask")) + .body("status", is("completed")) + .body("taskId", is(notNullValue())) + .body("additionalInformation.type", is("ExpireInboxesTask")) + .body("additionalInformation.runningOptions.usersPerSecond", is(RunningOptions.DEFAULT.getUsersPerSecond())) + .body("additionalInformation.runningOptions.byExpiresHeader", is(false)) + .body("additionalInformation.runningOptions.olderThan", is("1s")) + .body("additionalInformation.inboxesExpired", is(1)) + .body("additionalInformation.inboxesFailed", is(0)) + .body("additionalInformation.inboxesProcessed", is(1)) + .body("additionalInformation.messagesDeleted", is(1)); + } Review Comment: Maybe adding a test to show it does not delete messages not older than the value specified in the task? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
