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 f1cf25d3d58bff429002890d1f4d980309665193 Author: Benoit Tellier <[email protected]> AuthorDate: Tue Nov 17 17:06:27 2020 +0700 JAMES-3440 Tasks wrapper to populate EmailQueryView --- .../data/jmap/PopulateEmailQueryViewTask.java | 160 +++++++++++++++++++++ ...EmailQueryViewTaskAdditionalInformationDTO.java | 119 +++++++++++++++ ...ctionItemsTaskAdditionalInformationDTOTest.java | 45 ++++++ ...opulateEmailQueryViewTaskSerializationTest.java | 45 ++++++ .../json/populateAll.additionalInformation.json | 11 ++ .../src/test/resources/json/populateAll.task.json | 6 + 6 files changed, 386 insertions(+) diff --git a/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/PopulateEmailQueryViewTask.java b/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/PopulateEmailQueryViewTask.java new file mode 100644 index 0000000..cd25741 --- /dev/null +++ b/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/PopulateEmailQueryViewTask.java @@ -0,0 +1,160 @@ +/**************************************************************** + * 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.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 PopulateEmailQueryViewTask implements Task { + static final TaskType TASK_TYPE = TaskType.of("PopulateEmailQueryViewTask"); + + public static class AdditionalInformation implements TaskExecutionDetails.AdditionalInformation { + private static AdditionalInformation from(EmailQueryViewPopulator.Progress progress, + RunningOptions runningOptions) { + return new AdditionalInformation(runningOptions, + progress.getProcessedUserCount(), + progress.getProcessedMessageCount(), + progress.getFailedUserCount(), + progress.getFailedMessageCount(), + Clock.systemUTC().instant()); + } + + private final RunningOptions runningOptions; + private final long processedUserCount; + private final long processedMessageCount; + private final long failedUserCount; + private final long failedMessageCount; + private final Instant timestamp; + + public AdditionalInformation(RunningOptions runningOptions, long processedUserCount, long processedMessageCount, long failedUserCount, long failedMessageCount, Instant timestamp) { + this.runningOptions = runningOptions; + this.processedUserCount = processedUserCount; + this.processedMessageCount = processedMessageCount; + this.failedUserCount = failedUserCount; + this.failedMessageCount = failedMessageCount; + this.timestamp = timestamp; + } + + public long getProcessedUserCount() { + return processedUserCount; + } + + public long getProcessedMessageCount() { + return processedMessageCount; + } + + public long getFailedUserCount() { + return failedUserCount; + } + + public long getFailedMessageCount() { + return failedMessageCount; + } + + public RunningOptions getRunningOptions() { + return runningOptions; + } + + @Override + public Instant timestamp() { + return timestamp; + } + } + + public static class PopulateEmailQueryViewTaskDTO implements TaskDTO { + private final String type; + private final Optional<RunningOptionsDTO> runningOptions; + + public PopulateEmailQueryViewTaskDTO(@JsonProperty("type") String type, + @JsonProperty("runningOptions") Optional<RunningOptionsDTO> runningOptions) { + this.type = type; + this.runningOptions = runningOptions; + } + + @Override + public String getType() { + return type; + } + + public Optional<RunningOptionsDTO> getRunningOptions() { + return runningOptions; + } + } + + public static TaskDTOModule<PopulateEmailQueryViewTask, PopulateEmailQueryViewTaskDTO> module(EmailQueryViewPopulator populator) { + return DTOModule + .forDomainObject(PopulateEmailQueryViewTask.class) + .convertToDTO(PopulateEmailQueryViewTaskDTO.class) + .toDomainObjectConverter(dto -> asTask(populator, dto)) + .toDTOConverter(PopulateEmailQueryViewTask::asDTO) + .typeName(TASK_TYPE.asString()) + .withFactory(TaskDTOModule::new); + } + + private static PopulateEmailQueryViewTaskDTO asDTO(PopulateEmailQueryViewTask task, String type) { + return new PopulateEmailQueryViewTaskDTO(type, Optional.of(RunningOptionsDTO.asDTO(task.runningOptions))); + } + + private static PopulateEmailQueryViewTask asTask(EmailQueryViewPopulator populator, PopulateEmailQueryViewTaskDTO dto) { + return new PopulateEmailQueryViewTask(populator, + dto.getRunningOptions() + .map(RunningOptionsDTO::asDomainObject) + .orElse(RunningOptions.DEFAULT)); + } + + private final EmailQueryViewPopulator populator; + private final EmailQueryViewPopulator.Progress progress; + private final RunningOptions runningOptions; + + PopulateEmailQueryViewTask(EmailQueryViewPopulator populator, RunningOptions runningOptions) { + this.populator = populator; + this.runningOptions = runningOptions; + this.progress = new EmailQueryViewPopulator.Progress(); + } + + @Override + public Result run() { + return populator.populateView(progress, runningOptions) + .subscribeOn(Schedulers.elastic()) + .block(); + } + + @Override + public TaskType type() { + return TASK_TYPE; + } + + @Override + public Optional<TaskExecutionDetails.AdditionalInformation> details() { + return Optional.of(AdditionalInformation.from(progress, runningOptions)); + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/PopulateEmailQueryViewTaskAdditionalInformationDTO.java b/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/PopulateEmailQueryViewTaskAdditionalInformationDTO.java new file mode 100644 index 0000000..6e2c484 --- /dev/null +++ b/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/PopulateEmailQueryViewTaskAdditionalInformationDTO.java @@ -0,0 +1,119 @@ +/**************************************************************** + * 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 java.util.Optional; + +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; +import com.google.common.annotations.VisibleForTesting; + +public class PopulateEmailQueryViewTaskAdditionalInformationDTO implements AdditionalInformationDTO { + public static AdditionalInformationDTOModule<PopulateEmailQueryViewTask.AdditionalInformation, PopulateEmailQueryViewTaskAdditionalInformationDTO> module() { + return DTOModule.forDomainObject(PopulateEmailQueryViewTask.AdditionalInformation.class) + .convertToDTO(PopulateEmailQueryViewTaskAdditionalInformationDTO.class) + .toDomainObjectConverter(PopulateEmailQueryViewTaskAdditionalInformationDTO::toDomainObject) + .toDTOConverter(PopulateEmailQueryViewTaskAdditionalInformationDTO::toDTO) + .typeName(PopulateEmailQueryViewTask.TASK_TYPE.asString()) + .withFactory(AdditionalInformationDTOModule::new); + } + + private static PopulateEmailQueryViewTask.AdditionalInformation toDomainObject(PopulateEmailQueryViewTaskAdditionalInformationDTO dto) { + return new PopulateEmailQueryViewTask.AdditionalInformation( + dto.getRunningOptions() + .map(RunningOptionsDTO::asDomainObject) + .orElse(RunningOptions.DEFAULT), + dto.getProcessedUserCount(), + dto.getProcessedMessageCount(), + dto.getFailedUserCount(), + dto.getFailedMessageCount(), + dto.timestamp); + } + + private static PopulateEmailQueryViewTaskAdditionalInformationDTO toDTO(PopulateEmailQueryViewTask.AdditionalInformation details, String type) { + return new PopulateEmailQueryViewTaskAdditionalInformationDTO( + type, + details.timestamp(), + details.getProcessedUserCount(), + details.getProcessedMessageCount(), + details.getFailedUserCount(), + details.getFailedMessageCount(), + Optional.of(RunningOptionsDTO.asDTO(details.getRunningOptions()))); + } + + private final String type; + private final Instant timestamp; + private final long processedUserCount; + private final long processedMessageCount; + private final long failedUserCount; + private final long failedMessageCount; + private final Optional<RunningOptionsDTO> runningOptions; + + @VisibleForTesting + PopulateEmailQueryViewTaskAdditionalInformationDTO(@JsonProperty("type") String type, + @JsonProperty("timestamp") Instant timestamp, + @JsonProperty("processedUserCount") long processedUserCount, + @JsonProperty("processedMessageCount") long processedMessageCount, + @JsonProperty("failedUserCount") long failedUserCount, + @JsonProperty("failedMessageCount") long failedMessageCount, + @JsonProperty("runningOptions") Optional<RunningOptionsDTO> runningOptions) { + this.type = type; + this.timestamp = timestamp; + this.processedUserCount = processedUserCount; + this.processedMessageCount = processedMessageCount; + this.failedUserCount = failedUserCount; + this.failedMessageCount = failedMessageCount; + this.runningOptions = runningOptions; + } + + @Override + public String getType() { + return type; + } + + @Override + public Instant getTimestamp() { + return timestamp; + } + + public long getProcessedUserCount() { + return processedUserCount; + } + + public long getProcessedMessageCount() { + return processedMessageCount; + } + + public long getFailedUserCount() { + return failedUserCount; + } + + public long getFailedMessageCount() { + return failedMessageCount; + } + + public Optional<RunningOptionsDTO> getRunningOptions() { + return runningOptions; + } +} diff --git a/server/protocols/webadmin/webadmin-jmap/src/test/java/org/apache/james/webadmin/data/jmap/PopulateEmailQueryViewProjectionItemsTaskAdditionalInformationDTOTest.java b/server/protocols/webadmin/webadmin-jmap/src/test/java/org/apache/james/webadmin/data/jmap/PopulateEmailQueryViewProjectionItemsTaskAdditionalInformationDTOTest.java new file mode 100644 index 0000000..09de56b --- /dev/null +++ b/server/protocols/webadmin/webadmin-jmap/src/test/java/org/apache/james/webadmin/data/jmap/PopulateEmailQueryViewProjectionItemsTaskAdditionalInformationDTOTest.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 java.time.Instant; + +import org.apache.james.JsonSerializationVerifier; +import org.apache.james.util.ClassLoaderUtils; +import org.junit.jupiter.api.Test; + +class PopulateEmailQueryViewProjectionItemsTaskAdditionalInformationDTOTest { + private static final Instant INSTANT = Instant.parse("2007-12-03T10:15:30.00Z"); + private static final PopulateEmailQueryViewTask.AdditionalInformation DOMAIN_OBJECT = new PopulateEmailQueryViewTask.AdditionalInformation( + RunningOptions.withMessageRatePerSecond(20), + 1, + 2, + 3, + 4, + INSTANT); + + @Test + void shouldMatchJsonSerializationContract() throws Exception { + JsonSerializationVerifier.dtoModule(PopulateEmailQueryViewTaskAdditionalInformationDTO.module()) + .bean(DOMAIN_OBJECT) + .json(ClassLoaderUtils.getSystemResourceAsString("json/populateAll.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/PopulateEmailQueryViewTaskSerializationTest.java b/server/protocols/webadmin/webadmin-jmap/src/test/java/org/apache/james/webadmin/data/jmap/PopulateEmailQueryViewTaskSerializationTest.java new file mode 100644 index 0000000..97b5ddc --- /dev/null +++ b/server/protocols/webadmin/webadmin-jmap/src/test/java/org/apache/james/webadmin/data/jmap/PopulateEmailQueryViewTaskSerializationTest.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.util.ClassLoaderUtils; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class PopulateEmailQueryViewTaskSerializationTest { + EmailQueryViewPopulator populator; + + @BeforeEach + void setUp() { + populator = mock(EmailQueryViewPopulator.class); + } + + @Test + void shouldMatchJsonSerializationContract() throws Exception { + JsonSerializationVerifier.dtoModule(PopulateEmailQueryViewTask.module(populator)) + .bean(new PopulateEmailQueryViewTask(populator, + RunningOptions.withMessageRatePerSecond(2))) + .json(ClassLoaderUtils.getSystemResourceAsString("json/populateAll.task.json")) + .verify(); + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-jmap/src/test/resources/json/populateAll.additionalInformation.json b/server/protocols/webadmin/webadmin-jmap/src/test/resources/json/populateAll.additionalInformation.json new file mode 100644 index 0000000..eb99605 --- /dev/null +++ b/server/protocols/webadmin/webadmin-jmap/src/test/resources/json/populateAll.additionalInformation.json @@ -0,0 +1,11 @@ +{ + "type":"PopulateEmailQueryViewTask", + "timestamp":"2007-12-03T10:15:30Z", + "processedUserCount":1, + "processedMessageCount":2, + "failedUserCount":3, + "failedMessageCount":4, + "runningOptions": { + "messagesPerSecond":20 + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-jmap/src/test/resources/json/populateAll.task.json b/server/protocols/webadmin/webadmin-jmap/src/test/resources/json/populateAll.task.json new file mode 100644 index 0000000..ca9a5ee --- /dev/null +++ b/server/protocols/webadmin/webadmin-jmap/src/test/resources/json/populateAll.task.json @@ -0,0 +1,6 @@ +{ + "type":"PopulateEmailQueryViewTask", + "runningOptions": { + "messagesPerSecond":2 + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
