You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ro...@apache.org on 2019/07/31 07:49:42 UTC

[james-project] 05/13: JAMES-2813 Add serialization for ReprocessingAllMailsTask

This is an automated email from the ASF dual-hosted git repository.

rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 9cb758861e5c8e2e6a373489d3ca03ceca68882d
Author: Gautier DI FOLCO <gd...@linagora.com>
AuthorDate: Fri Jul 19 13:59:13 2019 +0200

    JAMES-2813 Add serialization for ReprocessingAllMailsTask
---
 .../webadmin/service/ReprocessingAllMailsTask.java | 98 ++++++++++++++++++++++
 .../service/ReprocessingAllMailsTaskTest.java      | 93 ++++++++++++++++++++
 2 files changed, 191 insertions(+)

diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/ReprocessingAllMailsTask.java b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/ReprocessingAllMailsTask.java
index 05b8ea1..b8f9409 100644
--- a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/ReprocessingAllMailsTask.java
+++ b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/ReprocessingAllMailsTask.java
@@ -21,16 +21,21 @@ package org.apache.james.webadmin.service;
 
 import java.util.Optional;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Function;
 
 import javax.mail.MessagingException;
 
+import org.apache.james.json.DTOModule;
 import org.apache.james.mailrepository.api.MailKey;
 import org.apache.james.mailrepository.api.MailRepositoryPath;
 import org.apache.james.mailrepository.api.MailRepositoryStore;
+import org.apache.james.server.task.json.dto.TaskDTO;
+import org.apache.james.server.task.json.dto.TaskDTOModule;
 import org.apache.james.task.Task;
 import org.apache.james.task.TaskExecutionDetails;
 
 import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
 
 public class ReprocessingAllMailsTask implements Task {
 
@@ -77,6 +82,99 @@ public class ReprocessingAllMailsTask implements Task {
         }
     }
 
+    public static class UrlEncodingFailureSerializationException extends RuntimeException {
+
+        public UrlEncodingFailureSerializationException(MailRepositoryPath mailRepositoryPath) {
+            super("Unable to serialize: '" + mailRepositoryPath + "' can not be url encoded");
+        }
+    }
+
+    public static class InvalidMailRepositoryPathDeserializationException extends RuntimeException {
+
+        public InvalidMailRepositoryPathDeserializationException(String mailRepositoryPath) {
+            super("Unable to deserialize: '" + mailRepositoryPath + "' can not be url decoded");
+        }
+    }
+
+    private static class ReprocessingAllMailsTaskDTO implements TaskDTO {
+
+        public static ReprocessingAllMailsTaskDTO toDTO(ReprocessingAllMailsTask domainObject, String typeName) {
+            try {
+                return new ReprocessingAllMailsTaskDTO(
+                    typeName,
+                    domainObject.additionalInformation.initialCount,
+                    domainObject.repositoryPath.urlEncoded(),
+                    domainObject.targetQueue,
+                    domainObject.targetProcessor
+                );
+            } catch (Exception e) {
+                throw new UrlEncodingFailureSerializationException(domainObject.repositoryPath);
+            }
+        }
+
+        private final String type;
+        private final long repositorySize;
+        private final String repositoryPath;
+        private final String targetQueue;
+        private final Optional<String> targetProcessor;
+
+        public ReprocessingAllMailsTaskDTO(@JsonProperty("type") String type,
+                                           @JsonProperty("repositorySize") long repositorySize,
+                                           @JsonProperty("repositoryPath") String repositoryPath,
+                                           @JsonProperty("targetQueue") String targetQueue,
+                                           @JsonProperty("targetProcessor") Optional<String> targetProcessor) {
+            this.type = type;
+            this.repositorySize = repositorySize;
+            this.repositoryPath = repositoryPath;
+            this.targetQueue = targetQueue;
+            this.targetProcessor = targetProcessor;
+        }
+
+        public ReprocessingAllMailsTask fromDTO(ReprocessingService reprocessingService) {
+            try {
+                return new ReprocessingAllMailsTask(
+                    reprocessingService,
+                    repositorySize,
+                    MailRepositoryPath.fromEncoded(repositoryPath),
+                    targetQueue,
+                    targetProcessor
+                );
+            } catch (Exception e) {
+                throw new InvalidMailRepositoryPathDeserializationException(repositoryPath);
+            }
+        }
+
+        @Override
+        public String getType() {
+            return type;
+        }
+
+        public long getRepositorySize() {
+            return repositorySize;
+        }
+
+        public String getRepositoryPath() {
+            return repositoryPath;
+        }
+
+        public String getTargetQueue() {
+            return targetQueue;
+        }
+
+        public Optional<String> getTargetProcessor() {
+            return targetProcessor;
+        }
+    }
+
+    public static final Function<ReprocessingService, TaskDTOModule<ReprocessingAllMailsTask, ReprocessingAllMailsTaskDTO>> MODULE = (reprocessingService) ->
+        DTOModule
+            .forDomainObject(ReprocessingAllMailsTask.class)
+            .convertToDTO(ReprocessingAllMailsTaskDTO.class)
+            .toDomainObjectConverter(dto -> dto.fromDTO(reprocessingService))
+            .toDTOConverter(ReprocessingAllMailsTaskDTO::toDTO)
+            .typeName(TYPE)
+            .withFactory(TaskDTOModule::new);
+
     private final ReprocessingService reprocessingService;
     private final MailRepositoryPath repositoryPath;
     private final String targetQueue;
diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/service/ReprocessingAllMailsTaskTest.java b/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/service/ReprocessingAllMailsTaskTest.java
new file mode 100644
index 0000000..1c401d4
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/service/ReprocessingAllMailsTaskTest.java
@@ -0,0 +1,93 @@
+/****************************************************************
+ * 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.service;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.Mockito.mock;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import java.io.IOException;
+import java.util.Optional;
+import java.util.stream.Stream;
+
+import org.apache.james.mailrepository.api.MailRepositoryPath;
+import org.apache.james.server.task.json.JsonTaskSerializer;
+
+import net.javacrumbs.jsonunit.assertj.JsonAssertions;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+class ReprocessingAllMailsTaskTest {
+    private static final ReprocessingService REPROCESSING_SERVICE = mock(ReprocessingService.class);
+
+    @ParameterizedTest
+    @MethodSource
+    void taskShouldBeSerializable(long repositorySize,
+                                  MailRepositoryPath repositoryPath,
+                                  String targetQueue,
+                                  Optional<String> targetProcessor,
+                                  String serialized) throws JsonProcessingException {
+        JsonTaskSerializer testee = new JsonTaskSerializer(ReprocessingAllMailsTask.MODULE.apply(REPROCESSING_SERVICE));
+        ReprocessingAllMailsTask task = new ReprocessingAllMailsTask(REPROCESSING_SERVICE, repositorySize, repositoryPath, targetQueue, targetProcessor);
+        JsonAssertions.assertThatJson(testee.serialize(task))
+            .isEqualTo(serialized);
+    }
+
+    private static Stream<Arguments> taskShouldBeSerializable() {
+        return allValidTasks();
+    }
+
+    @ParameterizedTest
+    @MethodSource
+    void taskShouldBeDeserializable(long repositorySize,
+                                    MailRepositoryPath repositoryPath,
+                                    String targetQueue,
+                                    Optional<String> targetProcessor,
+                                    String serialized) throws IOException {
+        JsonTaskSerializer testee = new JsonTaskSerializer(ReprocessingAllMailsTask.MODULE.apply(REPROCESSING_SERVICE));
+        ReprocessingAllMailsTask task = new ReprocessingAllMailsTask(REPROCESSING_SERVICE, repositorySize, repositoryPath, targetQueue, targetProcessor);
+
+        assertThat(testee.deserialize(serialized))
+            .isEqualToComparingFieldByFieldRecursively(task);
+    }
+
+    private static Stream<Arguments> taskShouldBeDeserializable() {
+        return allValidTasks();
+    }
+
+    private static Stream<Arguments> allValidTasks() {
+        return Stream.of(
+            Arguments.of(5L, MailRepositoryPath.from("a"), "queue", Optional.of("targetProcessor"), "{\"type\":\"reprocessingAllTask\",\"repositorySize\":5,\"repositoryPath\":\"a\",\"targetQueue\":\"queue\",\"targetProcessor\":\"targetProcessor\"}"),
+            Arguments.of(5L, MailRepositoryPath.from("a"), "queue", Optional.empty(), "{\"type\":\"reprocessingAllTask\",\"repositorySize\":5,\"repositoryPath\":\"a\",\"targetQueue\":\"queue\"}")
+        );
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"{\"type\":\"reprocessingAllTask\",\"repositorySize\":5,\"repositoryPath\":\"%\",\"targetQueue\":\"queue\",\"targetProcessor\":\"targetProcessor\"}", "{\"type\":\"reprocessingAllTask\",\"repositorySize\":5,\"repositoryPath\":\"%\",\"targetQueue\":\"queue\"}"})
+    void taskShouldThrowOnDeserializationUrlDecodingError(String serialized) {
+        JsonTaskSerializer testee = new JsonTaskSerializer(ReprocessingAllMailsTask.MODULE.apply(REPROCESSING_SERVICE));
+
+        assertThatThrownBy(() -> testee.deserialize(serialized))
+                .isInstanceOf(ReprocessingAllMailsTask.InvalidMailRepositoryPathDeserializationException.class);
+    }
+}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org