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 bt...@apache.org on 2019/12/17 01:55:32 UTC

[james-project] 11/24: JAMES-3006 Use Task factory in cassandra alias projection routes

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 81fbf90179f5dddc0c58e6ebd9a4640e10fb80b1
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Thu Dec 12 08:16:21 2019 +0100

    JAMES-3006 Use Task factory in cassandra alias projection routes
---
 .../apache/james/webadmin/dto/ActionMappings.java  | 36 ----------------
 .../webadmin/routes/CassandraMappingsRoutes.java   | 22 +++++-----
 .../webadmin/service/CassandraMappingsService.java | 12 +-----
 .../james/webadmin/dto/ActionMappingsTest.java     | 49 ----------------------
 .../routes/CassandraMappingsRoutesTest.java        |  4 +-
 5 files changed, 13 insertions(+), 110 deletions(-)

diff --git a/server/protocols/webadmin/webadmin-cassandra-data/src/main/java/org/apache/james/webadmin/dto/ActionMappings.java b/server/protocols/webadmin/webadmin-cassandra-data/src/main/java/org/apache/james/webadmin/dto/ActionMappings.java
deleted file mode 100644
index 7ca05b1..0000000
--- a/server/protocols/webadmin/webadmin-cassandra-data/src/main/java/org/apache/james/webadmin/dto/ActionMappings.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/****************************************************************
- * 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.dto;
-
-import java.util.Arrays;
-
-import com.google.common.base.Preconditions;
-
-public enum ActionMappings {
-    SolveInconsistencies;
-
-    public static ActionMappings parse(String action) {
-        Preconditions.checkArgument(action != null, "'action' url parameter is mandatory");
-        return Arrays.stream(ActionMappings.values())
-            .filter(element -> element.toString().equalsIgnoreCase(action))
-            .findAny()
-            .orElseThrow(() -> new IllegalArgumentException("'" + action + "' is not a valid action query parameter"));
-    }
-}
\ No newline at end of file
diff --git a/server/protocols/webadmin/webadmin-cassandra-data/src/main/java/org/apache/james/webadmin/routes/CassandraMappingsRoutes.java b/server/protocols/webadmin/webadmin-cassandra-data/src/main/java/org/apache/james/webadmin/routes/CassandraMappingsRoutes.java
index 15c3d4e..5a4aa8d 100644
--- a/server/protocols/webadmin/webadmin-cassandra-data/src/main/java/org/apache/james/webadmin/routes/CassandraMappingsRoutes.java
+++ b/server/protocols/webadmin/webadmin-cassandra-data/src/main/java/org/apache/james/webadmin/routes/CassandraMappingsRoutes.java
@@ -24,14 +24,13 @@ import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 
-import org.apache.james.task.Task;
-import org.apache.james.task.TaskId;
 import org.apache.james.task.TaskManager;
 import org.apache.james.webadmin.Constants;
 import org.apache.james.webadmin.Routes;
-import org.apache.james.webadmin.dto.ActionMappings;
-import org.apache.james.webadmin.dto.TaskIdDto;
 import org.apache.james.webadmin.service.CassandraMappingsService;
+import org.apache.james.webadmin.tasks.TaskFactory;
+import org.apache.james.webadmin.tasks.TaskIdDto;
+import org.apache.james.webadmin.tasks.TaskRegistrationKey;
 import org.apache.james.webadmin.utils.JsonTransformer;
 import org.eclipse.jetty.http.HttpStatus;
 
@@ -42,8 +41,7 @@ import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiResponse;
 import io.swagger.annotations.ApiResponses;
 import io.swagger.annotations.ResponseHeader;
-import spark.Request;
-import spark.Response;
+import spark.Route;
 import spark.Service;
 
 @Api(tags = "Cassandra Mappings Operations")
@@ -51,6 +49,7 @@ import spark.Service;
 @Produces(Constants.JSON_CONTENT_TYPE)
 public class CassandraMappingsRoutes implements Routes {
     public static final String ROOT_PATH = "cassandra/mappings";
+    private static final TaskRegistrationKey SOLVE_INCONSISTENCIES = TaskRegistrationKey.of("SolveInconsistencies");
 
     private final CassandraMappingsService cassandraMappingsService;
     private final TaskManager taskManager;
@@ -73,7 +72,7 @@ public class CassandraMappingsRoutes implements Routes {
 
     @Override
     public void define(Service service) {
-        service.post(ROOT_PATH, this::performActionOnMappings, jsonTransformer);
+        service.post(ROOT_PATH, performActionOnMappings(), jsonTransformer);
     }
 
     @POST
@@ -97,10 +96,9 @@ public class CassandraMappingsRoutes implements Routes {
         @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = INVALID_ACTION_ARGUMENT_REQUEST),
         @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = ACTION_REQUEST_CAN_NOT_BE_DONE)
     })
-    public TaskIdDto performActionOnMappings(Request request, Response response) {
-        ActionMappings action = ActionMappings.parse(request.queryParams("action"));
-        Task task = cassandraMappingsService.createActionTask(action);
-        TaskId taskId = taskManager.submit(task);
-        return TaskIdDto.respond(response, taskId);
+    public Route performActionOnMappings() {
+        return TaskFactory.builder()
+            .register(SOLVE_INCONSISTENCIES, request -> cassandraMappingsService.solveMappingsSourcesInconsistencies())
+            .buildAsRoute(taskManager);
     }
 }
diff --git a/server/protocols/webadmin/webadmin-cassandra-data/src/main/java/org/apache/james/webadmin/service/CassandraMappingsService.java b/server/protocols/webadmin/webadmin-cassandra-data/src/main/java/org/apache/james/webadmin/service/CassandraMappingsService.java
index e1733ef..5edcaf9 100644
--- a/server/protocols/webadmin/webadmin-cassandra-data/src/main/java/org/apache/james/webadmin/service/CassandraMappingsService.java
+++ b/server/protocols/webadmin/webadmin-cassandra-data/src/main/java/org/apache/james/webadmin/service/CassandraMappingsService.java
@@ -24,7 +24,6 @@ import javax.inject.Inject;
 import org.apache.james.rrt.cassandra.CassandraMappingsSourcesDAO;
 import org.apache.james.rrt.cassandra.migration.MappingsSourcesMigration;
 import org.apache.james.task.Task;
-import org.apache.james.webadmin.dto.ActionMappings;
 
 public class CassandraMappingsService {
     private final MappingsSourcesMigration mappingsSourcesMigration;
@@ -37,16 +36,7 @@ public class CassandraMappingsService {
         this.cassandraMappingsSourcesDAO = cassandraMappingsSourcesDAO;
     }
 
-    public Task createActionTask(ActionMappings action) {
-        switch (action) {
-            case SolveInconsistencies:
-                return solveMappingsSourcesInconsistencies();
-            default:
-                throw new IllegalArgumentException(action + " is not a supported action");
-        }
-    }
-
-    private Task solveMappingsSourcesInconsistencies() {
+    public Task solveMappingsSourcesInconsistencies() {
         return new CassandraMappingsSolveInconsistenciesTask(mappingsSourcesMigration, cassandraMappingsSourcesDAO);
     }
 }
diff --git a/server/protocols/webadmin/webadmin-cassandra-data/src/test/java/org/apache/james/webadmin/dto/ActionMappingsTest.java b/server/protocols/webadmin/webadmin-cassandra-data/src/test/java/org/apache/james/webadmin/dto/ActionMappingsTest.java
deleted file mode 100644
index 3cfe691..0000000
--- a/server/protocols/webadmin/webadmin-cassandra-data/src/test/java/org/apache/james/webadmin/dto/ActionMappingsTest.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/****************************************************************
- * 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.dto;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-
-import org.junit.jupiter.api.Test;
-
-class ActionMappingsTest {
-    private static final String ACTION = "SolveInconsistencies";
-
-    @Test
-    void parseShouldSucceedWithCorrectActionMappingsArgument() {
-        assertThat(ActionMappings.parse(ACTION)).isEqualTo(ActionMappings.SolveInconsistencies);
-    }
-
-    @Test
-    void parseShouldFailWithIncorrectActionMappingsArgument() {
-        assertThatThrownBy(() -> ActionMappings.parse("incorrect-action"))
-            .isInstanceOf(IllegalArgumentException.class)
-            .hasMessage("'incorrect-action' is not a valid action query parameter");
-    }
-
-    @Test
-    void parseShouldFailWithMissingActionMappingsArgument() {
-        assertThatThrownBy(() -> ActionMappings.parse(null))
-            .isInstanceOf(IllegalArgumentException.class)
-            .hasMessage("'action' url parameter is mandatory");
-    }
-
-}
diff --git a/server/protocols/webadmin/webadmin-cassandra-data/src/test/java/org/apache/james/webadmin/routes/CassandraMappingsRoutesTest.java b/server/protocols/webadmin/webadmin-cassandra-data/src/test/java/org/apache/james/webadmin/routes/CassandraMappingsRoutesTest.java
index 22a0a22..8215091 100644
--- a/server/protocols/webadmin/webadmin-cassandra-data/src/test/java/org/apache/james/webadmin/routes/CassandraMappingsRoutesTest.java
+++ b/server/protocols/webadmin/webadmin-cassandra-data/src/test/java/org/apache/james/webadmin/routes/CassandraMappingsRoutesTest.java
@@ -140,7 +140,7 @@ class CassandraMappingsRoutesTest {
             .body("statusCode", is(400))
             .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType()))
             .body("message", is("Invalid arguments supplied in the user request"))
-            .body("details", is("'invalid-action' is not a valid action query parameter"));
+            .body("details", is("Invalid value supplied for 'action': invalid-action. Supported values are [SolveInconsistencies]"));
     }
 
     @Test
@@ -152,7 +152,7 @@ class CassandraMappingsRoutesTest {
             .body("statusCode", is(400))
             .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType()))
             .body("message", is("Invalid arguments supplied in the user request"))
-            .body("details", is("'action' url parameter is mandatory"));
+            .body("details", is("'action' query parameter is compulsory. Supported values are [SolveInconsistencies]"));
     }
 
     @Test


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