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 a150fdb2d07e40cc177b6c4115a7c751238a2b16 Author: Tran Tien Duc <[email protected]> AuthorDate: Fri Jan 3 10:16:10 2020 +0700 JAMES-2993 Task DTO & serialization test for user preview recomputation --- .../jmap/MessageFastViewProjectionCorrector.java | 2 +- .../RecomputeUserFastViewProjectionItemsTask.java | 141 +++++++++++++++++++++ ...teUserPreviewsTaskAdditionalInformationDTO.java | 88 +++++++++++++ ...ctionItemsTaskAdditionalInformationDTOTest.java | 41 ++++++ ...stViewProjectionItemsTaskSerializationTest.java | 45 +++++++ .../json/recomputeUser.additionalInformation.json | 7 + .../test/resources/json/recomputeUser.task.json | 4 + 7 files changed, 327 insertions(+), 1 deletion(-) diff --git a/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/MessageFastViewProjectionCorrector.java b/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/MessageFastViewProjectionCorrector.java index 0eb7f08..53a9c21 100644 --- a/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/MessageFastViewProjectionCorrector.java +++ b/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/MessageFastViewProjectionCorrector.java @@ -112,7 +112,7 @@ public class MessageFastViewProjectionCorrector { } } - private Mono<Void> correctUsersProjectionItems(Progress progress, Username username) { + Mono<Void> correctUsersProjectionItems(Progress progress, Username username) { try { MailboxSession session = mailboxManager.createSystemSession(username); return listUsersMailboxes(session) diff --git a/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/RecomputeUserFastViewProjectionItemsTask.java b/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/RecomputeUserFastViewProjectionItemsTask.java new file mode 100644 index 0000000..4f30958 --- /dev/null +++ b/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/RecomputeUserFastViewProjectionItemsTask.java @@ -0,0 +1,141 @@ +/**************************************************************** + * 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.data.jmap; + +import java.time.Clock; +import java.time.Instant; +import java.util.Optional; + +import org.apache.james.core.Username; +import org.apache.james.json.DTOModule; +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 org.apache.james.task.TaskType; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import reactor.core.scheduler.Schedulers; + +public class RecomputeUserFastViewProjectionItemsTask implements Task { + static final TaskType TASK_TYPE = TaskType.of("RecomputeUserFastViewProjectionItemsTask"); + + public static class AdditionalInformation implements TaskExecutionDetails.AdditionalInformation { + private static AdditionalInformation from(MessageFastViewProjectionCorrector.Progress progress, Username username) { + return new AdditionalInformation(username, + progress.getProcessedMessageCount(), + progress.getFailedMessageCount(), + Clock.systemUTC().instant()); + } + + private final Username username; + private final long processedMessageCount; + private final long failedMessageCount; + private final Instant timestamp; + + public AdditionalInformation(Username username, long processedMessageCount, long failedMessageCount, Instant timestamp) { + this.username = username; + this.processedMessageCount = processedMessageCount; + this.failedMessageCount = failedMessageCount; + this.timestamp = timestamp; + } + + public long getProcessedMessageCount() { + return processedMessageCount; + } + + public long getFailedMessageCount() { + return failedMessageCount; + } + + public String getUsername() { + return username.asString(); + } + + @Override + public Instant timestamp() { + return timestamp; + } + } + + public static class RecomputeUserPreviewsTaskDTO implements TaskDTO { + private final String type; + private final String username; + + public RecomputeUserPreviewsTaskDTO( + @JsonProperty("type") String type, + @JsonProperty("username") String username) { + this.type = type; + this.username = username; + } + + @Override + public String getType() { + return type; + } + + public String getUsername() { + return username; + } + } + + public static TaskDTOModule<RecomputeUserFastViewProjectionItemsTask, RecomputeUserPreviewsTaskDTO> module(MessageFastViewProjectionCorrector corrector) { + return DTOModule + .forDomainObject(RecomputeUserFastViewProjectionItemsTask.class) + .convertToDTO(RecomputeUserPreviewsTaskDTO.class) + .toDomainObjectConverter(dto -> new RecomputeUserFastViewProjectionItemsTask(corrector, Username.of(dto.username))) + .toDTOConverter((task, type) -> new RecomputeUserPreviewsTaskDTO(type, task.username.asString())) + .typeName(TASK_TYPE.asString()) + .withFactory(TaskDTOModule::new); + } + + private final MessageFastViewProjectionCorrector corrector; + private final MessageFastViewProjectionCorrector.Progress progress; + private final Username username; + + RecomputeUserFastViewProjectionItemsTask(MessageFastViewProjectionCorrector corrector, Username username) { + this.corrector = corrector; + this.username = username; + this.progress = new MessageFastViewProjectionCorrector.Progress(); + } + + @Override + public Result run() { + corrector.correctUsersProjectionItems(progress, username) + .subscribeOn(Schedulers.elastic()) + .block(); + + if (progress.failed()) { + return Result.PARTIAL; + } + return Result.COMPLETED; + } + + @Override + public TaskType type() { + return TASK_TYPE; + } + + @Override + public Optional<TaskExecutionDetails.AdditionalInformation> details() { + return Optional.of(AdditionalInformation.from(progress, username)); + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/RecomputeUserPreviewsTaskAdditionalInformationDTO.java b/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/RecomputeUserPreviewsTaskAdditionalInformationDTO.java new file mode 100644 index 0000000..030c09c --- /dev/null +++ b/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/RecomputeUserPreviewsTaskAdditionalInformationDTO.java @@ -0,0 +1,88 @@ +/**************************************************************** + * 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.data.jmap; + +import java.time.Instant; + +import org.apache.james.core.Username; +import org.apache.james.json.DTOModule; +import org.apache.james.server.task.json.dto.AdditionalInformationDTO; +import org.apache.james.server.task.json.dto.AdditionalInformationDTOModule; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class RecomputeUserPreviewsTaskAdditionalInformationDTO implements AdditionalInformationDTO { + public static final AdditionalInformationDTOModule<RecomputeUserFastViewProjectionItemsTask.AdditionalInformation, RecomputeUserPreviewsTaskAdditionalInformationDTO> SERIALIZATION_MODULE = + DTOModule.forDomainObject(RecomputeUserFastViewProjectionItemsTask.AdditionalInformation.class) + .convertToDTO(RecomputeUserPreviewsTaskAdditionalInformationDTO.class) + .toDomainObjectConverter(dto -> new RecomputeUserFastViewProjectionItemsTask.AdditionalInformation( + Username.of(dto.username), + dto.getProcessedMessageCount(), + dto.getFailedMessageCount(), + dto.timestamp)) + .toDTOConverter((details, type) -> new RecomputeUserPreviewsTaskAdditionalInformationDTO( + type, + details.timestamp(), + details.getUsername(), + details.getProcessedMessageCount(), + details.getFailedMessageCount())) + .typeName(RecomputeUserFastViewProjectionItemsTask.TASK_TYPE.asString()) + .withFactory(AdditionalInformationDTOModule::new); + + private final String type; + private final Instant timestamp; + private final String username; + private final long processedMessageCount; + private final long failedMessageCount; + + private RecomputeUserPreviewsTaskAdditionalInformationDTO(@JsonProperty("type") String type, + @JsonProperty("timestamp") Instant timestamp, + @JsonProperty("username") String username, + @JsonProperty("processedMessageCount") long processedMessageCount, + @JsonProperty("failedMessageCount") long failedMessageCount) { + this.type = type; + this.timestamp = timestamp; + this.username = username; + this.processedMessageCount = processedMessageCount; + this.failedMessageCount = failedMessageCount; + } + + @Override + public String getType() { + return type; + } + + @Override + public Instant getTimestamp() { + return timestamp; + } + + public long getProcessedMessageCount() { + return processedMessageCount; + } + + public long getFailedMessageCount() { + return failedMessageCount; + } + + public String getUsername() { + return username; + } +} diff --git a/server/protocols/webadmin/webadmin-jmap/src/test/java/org/apache/james/webadmin/data/jmap/RecomputeUserFastViewProjectionItemsTaskAdditionalInformationDTOTest.java b/server/protocols/webadmin/webadmin-jmap/src/test/java/org/apache/james/webadmin/data/jmap/RecomputeUserFastViewProjectionItemsTaskAdditionalInformationDTOTest.java new file mode 100644 index 0000000..394d2b0 --- /dev/null +++ b/server/protocols/webadmin/webadmin-jmap/src/test/java/org/apache/james/webadmin/data/jmap/RecomputeUserFastViewProjectionItemsTaskAdditionalInformationDTOTest.java @@ -0,0 +1,41 @@ +/**************************************************************** + * 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.data.jmap; + +import java.time.Instant; + +import org.apache.james.JsonSerializationVerifier; +import org.apache.james.core.Username; +import org.apache.james.util.ClassLoaderUtils; +import org.junit.jupiter.api.Test; + +class RecomputeUserFastViewProjectionItemsTaskAdditionalInformationDTOTest { + private static final Instant INSTANT = Instant.parse("2007-12-03T10:15:30.00Z"); + private static final RecomputeUserFastViewProjectionItemsTask.AdditionalInformation DOMAIN_OBJECT = new RecomputeUserFastViewProjectionItemsTask.AdditionalInformation( + Username.of("bob"), 2, 3, INSTANT); + + @Test + void shouldMatchJsonSerializationContract() throws Exception { + JsonSerializationVerifier.dtoModule(RecomputeUserPreviewsTaskAdditionalInformationDTO.SERIALIZATION_MODULE) + .bean(DOMAIN_OBJECT) + .json(ClassLoaderUtils.getSystemResourceAsString("json/recomputeUser.additionalInformation.json")) + .verify(); + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-jmap/src/test/java/org/apache/james/webadmin/data/jmap/RecomputeUserFastViewProjectionItemsTaskSerializationTest.java b/server/protocols/webadmin/webadmin-jmap/src/test/java/org/apache/james/webadmin/data/jmap/RecomputeUserFastViewProjectionItemsTaskSerializationTest.java new file mode 100644 index 0000000..2d7d75d --- /dev/null +++ b/server/protocols/webadmin/webadmin-jmap/src/test/java/org/apache/james/webadmin/data/jmap/RecomputeUserFastViewProjectionItemsTaskSerializationTest.java @@ -0,0 +1,45 @@ +/**************************************************************** + * 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.data.jmap; + +import static org.mockito.Mockito.mock; + +import org.apache.james.JsonSerializationVerifier; +import org.apache.james.core.Username; +import org.apache.james.util.ClassLoaderUtils; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class RecomputeUserFastViewProjectionItemsTaskSerializationTest { + MessageFastViewProjectionCorrector corrector; + + @BeforeEach + void setUp() { + corrector = mock(MessageFastViewProjectionCorrector.class); + } + + @Test + void shouldMatchJsonSerializationContract() throws Exception { + JsonSerializationVerifier.dtoModule(RecomputeUserFastViewProjectionItemsTask.module(corrector)) + .bean(new RecomputeUserFastViewProjectionItemsTask(corrector, Username.of("bob"))) + .json(ClassLoaderUtils.getSystemResourceAsString("json/recomputeUser.task.json")) + .verify(); + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-jmap/src/test/resources/json/recomputeUser.additionalInformation.json b/server/protocols/webadmin/webadmin-jmap/src/test/resources/json/recomputeUser.additionalInformation.json new file mode 100644 index 0000000..7e24f49 --- /dev/null +++ b/server/protocols/webadmin/webadmin-jmap/src/test/resources/json/recomputeUser.additionalInformation.json @@ -0,0 +1,7 @@ +{ + "type":"RecomputeUserFastViewProjectionItemsTask", + "timestamp":"2007-12-03T10:15:30Z", + "username": "bob", + "processedMessageCount":2, + "failedMessageCount":3 +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-jmap/src/test/resources/json/recomputeUser.task.json b/server/protocols/webadmin/webadmin-jmap/src/test/resources/json/recomputeUser.task.json new file mode 100644 index 0000000..fcd7712 --- /dev/null +++ b/server/protocols/webadmin/webadmin-jmap/src/test/resources/json/recomputeUser.task.json @@ -0,0 +1,4 @@ +{ + "type":"RecomputeUserFastViewProjectionItemsTask", + "username": "bob" +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
