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:44 UTC

[james-project] 07/13: JAMES-2813 Add serialization for ClearMailRepositoryTask

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 3381c4d0bf413235b012331f162054d14c4f9871
Author: Gautier DI FOLCO <gd...@linagora.com>
AuthorDate: Fri Jul 19 14:00:54 2019 +0200

    JAMES-2813 Add serialization for ClearMailRepositoryTask
---
 .../webadmin/service/ClearMailRepositoryTask.java  | 64 ++++++++++++++++++++++
 .../service/ClearMailRepositoryTaskTest.java       | 63 +++++++++++++++++++++
 2 files changed, 127 insertions(+)

diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/ClearMailRepositoryTask.java b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/ClearMailRepositoryTask.java
index 406330f..8fe7e41 100644
--- a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/ClearMailRepositoryTask.java
+++ b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/ClearMailRepositoryTask.java
@@ -21,15 +21,20 @@ package org.apache.james.webadmin.service;
 
 import java.util.List;
 import java.util.Optional;
+import java.util.function.Function;
 import java.util.function.Supplier;
 
 import javax.mail.MessagingException;
 
+import org.apache.james.json.DTOModule;
 import org.apache.james.mailrepository.api.MailRepository;
 import org.apache.james.mailrepository.api.MailRepositoryPath;
+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.JsonProperty;
 import com.github.fge.lambdas.Throwing;
 
 public class ClearMailRepositoryTask implements Task {
@@ -60,6 +65,65 @@ public class ClearMailRepositoryTask implements Task {
         }
     }
 
+    public static class UrlEncodingFailureSerializationException extends RuntimeException {
+
+        public UrlEncodingFailureSerializationException(MailRepositoryPath mailRepositoryPath) {
+            super("Unable to serialize: '" + mailRepositoryPath.asString() + "' 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 ClearMailRepositoryTaskDTO implements TaskDTO {
+
+        public static ClearMailRepositoryTaskDTO toDTO(ClearMailRepositoryTask domainObject, String typeName) {
+            try {
+                return new ClearMailRepositoryTaskDTO(typeName, domainObject.additionalInformation.repositoryPath.urlEncoded());
+            } catch (Exception e) {
+                throw new UrlEncodingFailureSerializationException(domainObject.additionalInformation.repositoryPath);
+            }
+        }
+
+        private final String type;
+        private final String mailRepositoryPath;
+
+        public ClearMailRepositoryTaskDTO(@JsonProperty("type") String type, @JsonProperty("mailRepositoryPath") String mailRepositoryPath) {
+            this.type = type;
+            this.mailRepositoryPath = mailRepositoryPath;
+        }
+
+        public ClearMailRepositoryTask fromDTO(List<MailRepository> mailRepositories) {
+            try {
+                return new ClearMailRepositoryTask(mailRepositories, MailRepositoryPath.fromEncoded(mailRepositoryPath));
+            } catch (Exception e) {
+                throw new InvalidMailRepositoryPathDeserializationException(mailRepositoryPath);
+            }
+        }
+
+        @Override
+        public String getType() {
+            return type;
+        }
+
+        public String getMailRepositoryPath() {
+            return mailRepositoryPath;
+        }
+    }
+
+    public static final Function<List<MailRepository>, TaskDTOModule<ClearMailRepositoryTask, ClearMailRepositoryTaskDTO>> MODULE = (mailRepositories) ->
+        DTOModule
+            .forDomainObject(ClearMailRepositoryTask.class)
+            .convertToDTO(ClearMailRepositoryTaskDTO.class)
+            .toDomainObjectConverter(dto -> dto.fromDTO(mailRepositories))
+            .toDTOConverter(ClearMailRepositoryTaskDTO::toDTO)
+            .typeName(TYPE)
+            .withFactory(TaskDTOModule::new);
+
     private final List<MailRepository> mailRepositories;
     private final AdditionalInformation additionalInformation;
 
diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/service/ClearMailRepositoryTaskTest.java b/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/service/ClearMailRepositoryTaskTest.java
new file mode 100644
index 0000000..fe02c84
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/service/ClearMailRepositoryTaskTest.java
@@ -0,0 +1,63 @@
+/****************************************************************
+ * 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 com.fasterxml.jackson.core.JsonProcessingException;
+import org.apache.james.mailrepository.api.MailRepository;
+import org.apache.james.mailrepository.api.MailRepositoryPath;
+import org.apache.james.server.task.json.JsonTaskSerializer;
+
+import com.google.common.collect.ImmutableList;
+import net.javacrumbs.jsonunit.assertj.JsonAssertions;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+
+class ClearMailRepositoryTaskTest {
+    private static final String SERIALIZED = "{\"type\":\"clearMailRepository\",\"mailRepositoryPath\":\"a\"}";
+    private static final ImmutableList<MailRepository> MAIL_REPOSITORIES = ImmutableList.of();
+    private static final ClearMailRepositoryTask TASK = new ClearMailRepositoryTask(MAIL_REPOSITORIES, MailRepositoryPath.from("a"));
+
+    @Test
+    void taskShouldBeSerializable() throws JsonProcessingException {
+        JsonTaskSerializer testee = new JsonTaskSerializer(ClearMailRepositoryTask.MODULE.apply(MAIL_REPOSITORIES));
+        JsonAssertions.assertThatJson(testee.serialize(TASK))
+            .isEqualTo(SERIALIZED);
+    }
+
+    @Test
+    void taskShouldBeDeserializable() throws IOException {
+        JsonTaskSerializer testee = new JsonTaskSerializer(ClearMailRepositoryTask.MODULE.apply(MAIL_REPOSITORIES));
+
+        assertThat(testee.deserialize(SERIALIZED))
+            .isEqualToComparingFieldByFieldRecursively(TASK);
+    }
+
+    @Test
+    void taskShouldThrowOnDeserializationUrlDecodingError() {
+        JsonTaskSerializer testee = new JsonTaskSerializer(ClearMailRepositoryTask.MODULE.apply(MAIL_REPOSITORIES));
+
+        assertThatThrownBy(() -> testee.deserialize("{\"type\":\"clearMailRepository\",\"mailRepositoryPath\":\"%\"}"))
+            .isInstanceOf(ClearMailRepositoryTask.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