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 a483fae393325339fc09ae126501d3aa9da8ae4d Author: Matthieu Baechler <[email protected]> AuthorDate: Thu Jul 25 11:55:11 2019 +0200 JAMES-2813 implement MigrationTask serialization --- backends-common/cassandra/pom.xml | 9 ++++ .../cassandra/migration/MigrationTask.java | 37 +++++++++++++- .../migration/MigrationTaskSerializationTest.java | 57 ++++++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/backends-common/cassandra/pom.xml b/backends-common/cassandra/pom.xml index 8cdd17c..be63401 100644 --- a/backends-common/cassandra/pom.xml +++ b/backends-common/cassandra/pom.xml @@ -49,6 +49,10 @@ </dependency> <dependency> <groupId>${james.groupId}</groupId> + <artifactId>james-server-task-json</artifactId> + </dependency> + <dependency> + <groupId>${james.groupId}</groupId> <artifactId>james-server-util</artifactId> </dependency> <dependency> @@ -94,6 +98,11 @@ <version>0.3.0</version> </dependency> <dependency> + <groupId>net.javacrumbs.json-unit</groupId> + <artifactId>json-unit-assertj</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>nl.jqno.equalsverifier</groupId> <artifactId>equalsverifier</artifactId> <scope>test</scope> diff --git a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/MigrationTask.java b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/MigrationTask.java index 16c4d81..632fdc0 100644 --- a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/MigrationTask.java +++ b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/MigrationTask.java @@ -22,17 +22,22 @@ package org.apache.james.backends.cassandra.migration; import static org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManager.DEFAULT_VERSION; import java.util.Optional; +import java.util.function.Function; import javax.inject.Inject; import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionDAO; import org.apache.james.backends.cassandra.versions.SchemaTransition; import org.apache.james.backends.cassandra.versions.SchemaVersion; +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.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.annotation.JsonProperty; import com.github.fge.lambdas.Throwing; import com.google.common.annotations.VisibleForTesting; import reactor.util.function.Tuple2; @@ -40,6 +45,35 @@ import reactor.util.function.Tuples; public class MigrationTask implements Task { + @VisibleForTesting + static final Function<Factory, TaskDTOModule<MigrationTask, MigrationTaskDTO>> SERIALIZATION_MODULE = + factory -> DTOModule.forDomainObject(MigrationTask.class) + .convertToDTO(MigrationTaskDTO.class) + .toDomainObjectConverter(dto -> factory.create(new SchemaVersion(dto.targetVersion))) + .toDTOConverter((task, type) -> new MigrationTaskDTO(type, task.target.getValue())) + .typeName("cassandra-migration-task") + .withFactory(TaskDTOModule::new); + + private static class MigrationTaskDTO implements TaskDTO { + + private final String type; + private final int targetVersion; + + MigrationTaskDTO(@JsonProperty("type") String type, @JsonProperty("targetVersion") int targetVersion) { + this.type = type; + this.targetVersion = targetVersion; + } + + @Override + public String getType() { + return type; + } + + public int getTargetVersion() { + return targetVersion; + } + } + public interface Factory { MigrationTask create(SchemaVersion target); } @@ -132,7 +166,7 @@ public class MigrationTask implements Task { private String failureMessage(SchemaVersion newVersion) { return String.format("Migrating to version %d partially done. " + - "Please check logs for cause of failure and re-run this migration.", newVersion.getValue()); + "Please check logs for cause of failure and re-run this migration.", newVersion.getValue()); } @Override @@ -144,4 +178,5 @@ public class MigrationTask implements Task { public Optional<TaskExecutionDetails.AdditionalInformation> details() { return Optional.of(new Details(target)); } + } diff --git a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/migration/MigrationTaskSerializationTest.java b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/migration/MigrationTaskSerializationTest.java new file mode 100644 index 0000000..9fde422 --- /dev/null +++ b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/migration/MigrationTaskSerializationTest.java @@ -0,0 +1,57 @@ +/**************************************************************** + * 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.backends.cassandra.migration; + +import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +import java.io.IOException; + +import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionDAO; +import org.apache.james.backends.cassandra.versions.SchemaVersion; +import org.apache.james.server.task.json.JsonTaskSerializer; +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.core.JsonProcessingException; + +class MigrationTaskSerializationTest { + + private static final int SCHEMA_VERSION = 12; + private static final String SERIALIZED_TASK = "{\"type\": \"cassandra-migration-task\", \"targetVersion\": 12}"; + + private final CassandraSchemaVersionDAO cassandraSchemaVersionDAO = mock(CassandraSchemaVersionDAO.class); + private final CassandraSchemaTransitions transitions = mock(CassandraSchemaTransitions.class); + private final MigrationTask.Factory factory = target -> new MigrationTask(cassandraSchemaVersionDAO, transitions, target); + private final JsonTaskSerializer taskSerializer = new JsonTaskSerializer(MigrationTask.SERIALIZATION_MODULE.apply(factory)); + + @Test + void taskShouldBeSerializable() throws JsonProcessingException { + MigrationTask task = factory.create(new SchemaVersion(SCHEMA_VERSION)); + assertThatJson(taskSerializer.serialize(task)).isEqualTo(SERIALIZED_TASK); + } + + @Test + void taskShouldBeDeserializable() throws IOException { + MigrationTask task = factory.create(new SchemaVersion(SCHEMA_VERSION)); + assertThat(taskSerializer.deserialize(SERIALIZED_TASK)) + .isEqualToComparingFieldByField(task); + } + +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
