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 870c67d9cd0fc921a1d558eacb7168563133242c
Author: Khanh Le <b...@linagora.com>
AuthorDate: Mon Aug 19 15:19:05 2019 +0700

    JAMES-2862 Implement Remove address mapping in AddressMappingRoutes + Tests
---
 .../webadmin/routes/AddressMappingRoutes.java      | 28 ++++++-
 .../webadmin/routes/AddressMappingRoutesTest.java  | 96 ++++++++++++++++++----
 2 files changed, 107 insertions(+), 17 deletions(-)

diff --git 
a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/AddressMappingRoutes.java
 
b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/AddressMappingRoutes.java
index faeb2cc..1c0ff7e 100644
--- 
a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/AddressMappingRoutes.java
+++ 
b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/AddressMappingRoutes.java
@@ -24,6 +24,7 @@ import static spark.Spark.halt;
 import java.util.List;
 
 import javax.inject.Inject;
+import javax.ws.rs.DELETE;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
@@ -56,7 +57,7 @@ import spark.Service;
 public class AddressMappingRoutes implements Routes {
 
     static final String BASE_PATH = "/mappings/address/";
-    static final String ADD_ADDRESS_MAPPING_PATH = 
"/mappings/address/:mappingSource/targets/:destinationAddress";
+    static final String ADDRESS_MAPPING_PATH = 
"/mappings/address/:mappingSource/targets/:destinationAddress";
 
     private final RecipientRewriteTable recipientRewriteTable;
 
@@ -72,11 +73,12 @@ public class AddressMappingRoutes implements Routes {
 
     @Override
     public void define(Service service) {
-        service.post(ADD_ADDRESS_MAPPING_PATH, this::addAddressMapping);
+        service.post(ADDRESS_MAPPING_PATH, this::addAddressMapping);
+        service.delete(ADDRESS_MAPPING_PATH, this::removeAddressMapping);
     }
 
     @POST
-    @Path(ADD_ADDRESS_MAPPING_PATH)
+    @Path(ADDRESS_MAPPING_PATH)
     @ApiOperation(value = "Getting all user mappings in RecipientRewriteTable")
     @ApiResponses(value = {
         @ApiResponse(code = HttpStatus.NO_CONTENT_204, message = "No body on 
created", response = List.class),
@@ -105,4 +107,24 @@ public class AddressMappingRoutes implements Routes {
         }
     }
 
+    @DELETE
+    @Path(ADDRESS_MAPPING_PATH)
+    @ApiOperation(value = "Remove a mapping from a mapping source in 
RecipientRewriteTable")
+    @ApiResponses(value = {
+        @ApiResponse(code = HttpStatus.NO_CONTENT_204, message = "No body on 
deleted", response = List.class),
+        @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Invalid 
parameter values.")
+    })
+    public HaltException removeAddressMapping(Request request, Response 
response) throws RecipientRewriteTableException {
+        MailAddress source = MailAddressParser.parseMailAddress(
+            request.params("mappingSource"),"address");
+        MailAddress destinationAddress = MailAddressParser.parseMailAddress(
+            request.params("destinationAddress"), "address");
+        removeAddressMapping(MappingSource.fromMailAddress(source), 
destinationAddress);
+        return halt(HttpStatus.NO_CONTENT_204);
+    }
+
+    private void removeAddressMapping(MappingSource source, MailAddress 
destination) throws RecipientRewriteTableException {
+        recipientRewriteTable.removeAddressMapping(source, 
destination.asString());
+    }
+
 }
diff --git 
a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/AddressMappingRoutesTest.java
 
b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/AddressMappingRoutesTest.java
index 048f17e..c29ab9a 100644
--- 
a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/AddressMappingRoutesTest.java
+++ 
b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/AddressMappingRoutesTest.java
@@ -43,6 +43,9 @@ import io.restassured.RestAssured;
 import io.restassured.filter.log.LogDetail;
 
 class AddressMappingRoutesTest {
+    private static String MAPPING_SOURCE = "sou...@domain.tld";
+    private static String ALICE_ADDRESS = "al...@domain.tld";
+    private static String BOB_ADDRESS = "b...@domain.tld";
 
     private WebAdminServer webAdminServer;
     private MemoryRecipientRewriteTable recipientRewriteTable;
@@ -74,16 +77,16 @@ class AddressMappingRoutesTest {
     @Test
     void addAddressMappingShouldAddMappingOnRecipientRewriteTable() {
         when()
-            .post("sou...@domain.tld/targets/u...@domain.tld");
+            .post(MAPPING_SOURCE + "/targets/" + ALICE_ADDRESS);
 
-        
assertThat(recipientRewriteTable.getStoredMappings(MappingSource.parse("sou...@domain.tld")))
-            .containsAnyOf(Mapping.of("u...@domain.tld"));
+        
assertThat(recipientRewriteTable.getStoredMappings(MappingSource.parse(MAPPING_SOURCE)))
+            .containsAnyOf(Mapping.of(ALICE_ADDRESS));
     }
 
     @Test
     void addAddressMappingShouldReturnNotFoundWhenOneParameterIsEmpty() {
         when()
-            .post("sou...@domain.tld/targets/" )
+            .post(MAPPING_SOURCE + "/targets/" )
         .then()
             .statusCode(HttpStatus.NOT_FOUND_404);
     }
@@ -91,7 +94,7 @@ class AddressMappingRoutesTest {
     @Test
     void addAddressMappingShouldReturnNoContentWhenValidParameter() {
         when()
-            .post("sou...@domain.tld/targets/al...@domain.tld")
+            .post(MAPPING_SOURCE + "/targets/" + ALICE_ADDRESS)
         .then()
             .statusCode(HttpStatus.NO_CONTENT_204);
     }
@@ -99,7 +102,7 @@ class AddressMappingRoutesTest {
     @Test
     void addAddressMappingShouldReturnBadRequestWhenInvalidMappingSource() {
         when()
-            .post("source/targets/al...@domain.tld")
+            .post("source@domain@domain/targets/" + ALICE_ADDRESS)
         .then()
             .statusCode(HttpStatus.BAD_REQUEST_400);
     }
@@ -107,20 +110,20 @@ class AddressMappingRoutesTest {
     @Test
     void 
addAddressMappingShouldReturnBadRequestWhenInvalidDestinationAddress() {
         when()
-            .post("sou...@domain.tld/targets/alice")
+            .post(MAPPING_SOURCE + "/targets/alice")
         .then()
             .statusCode(HttpStatus.BAD_REQUEST_400);
     }
 
     @Test
     void addAddressMappingShouldReturnNoContentWithDuplicatedAddress() throws 
Exception {
-        MappingSource mappingSource =  MappingSource.fromMailAddress(new 
MailAddress("sou...@domain.tld"));
+        MappingSource mappingSource =  MappingSource.fromMailAddress(new 
MailAddress(MAPPING_SOURCE));
 
-        recipientRewriteTable.addAddressMapping(mappingSource, 
"al...@domain.tld");
-        recipientRewriteTable.addAddressMapping(mappingSource, 
"b...@domain.tld");
+        recipientRewriteTable.addAddressMapping(mappingSource, ALICE_ADDRESS);
+        recipientRewriteTable.addAddressMapping(mappingSource, BOB_ADDRESS);
 
         when()
-            .post("sou...@domain.tld/targets/al...@domain.tld")
+            .post(MAPPING_SOURCE + "/targets/" + ALICE_ADDRESS)
         .then()
             .statusCode(HttpStatus.NO_CONTENT_204);
     }
@@ -128,16 +131,81 @@ class AddressMappingRoutesTest {
     @Test
     void 
addAddressMappingShouldReturnBadRequestWhenSourceAndDestinationIsTheSame() {
         when()
-            .post("sou...@domain.tld/targets/sou...@domain.tld")
+            .post(MAPPING_SOURCE + "/targets/" + MAPPING_SOURCE)
         .then()
             .statusCode(HttpStatus.BAD_REQUEST_400);
     }
 
     @Test
-    void addressMappingSHouldReturnBadRequestWhenSourceDomainNotInDomainList() 
{
+    void 
addAddressMappingShouldReturnBadRequestWhenSourceDomainNotInDomainList() {
         when()
-            .post("source@dexample/targets/al...@domain.tld")
+            .post("source@example/targets/" + ALICE_ADDRESS)
         .then()
             .statusCode(HttpStatus.BAD_REQUEST_400);
     }
+
+    @Test
+    void removeAddressMappingShouldRemoveDestinationAddress() {
+        when()
+            .post(MAPPING_SOURCE + "/targets/" + ALICE_ADDRESS);
+
+        when()
+            .delete(MAPPING_SOURCE + "/targets/" + ALICE_ADDRESS);
+
+        
assertThat(recipientRewriteTable.getStoredMappings(MappingSource.parse(MAPPING_SOURCE)))
+            .doesNotContain(Mapping.of(ALICE_ADDRESS));
+    }
+
+    @Test
+    void removeAddressMappingShouldReturnNoContentWhenValidParameter() {
+        when()
+            .post(MAPPING_SOURCE + "/targets/" + ALICE_ADDRESS);
+
+        when()
+            .delete(MAPPING_SOURCE + "/targets/" + ALICE_ADDRESS)
+        .then()
+            .statusCode(HttpStatus.NO_CONTENT_204);
+    }
+
+    @Test
+    void 
removeAddressMappingShouldReturnNoContentWhenDestinationAddressIsNotFound() {
+        when()
+            .post(MAPPING_SOURCE + "/targets/" + ALICE_ADDRESS);
+
+        when()
+            .delete(MAPPING_SOURCE + "/targets/" + BOB_ADDRESS)
+        .then()
+            .statusCode(HttpStatus.NO_CONTENT_204);
+    }
+
+    @Test
+    void removeAddressMappingShouldBeIdempotent() {
+        when()
+            .post(MAPPING_SOURCE + "/targets/" + ALICE_ADDRESS);
+
+        when()
+            .delete(MAPPING_SOURCE + "/targets/" + ALICE_ADDRESS);
+
+        when()
+            .delete(MAPPING_SOURCE + "/targets/" + ALICE_ADDRESS)
+        .then()
+            .statusCode(HttpStatus.NO_CONTENT_204);
+    }
+
+    @Test
+    void 
removeAddressMappingShouldReturnBadRequestWhenMappingSourceIsInvalid() {
+        when()
+            .delete( "random@domain@domain/targets/" + ALICE_ADDRESS)
+        .then()
+            .statusCode(HttpStatus.BAD_REQUEST_400);
+    }
+
+    @Test
+    void removeAddressMappingShouldReturnNotFoundWhenOneParameterIsEmpty() {
+        when()
+            .delete( MAPPING_SOURCE + "/targets/")
+        .then()
+            .statusCode(HttpStatus.NOT_FOUND_404);
+    }
+
 }
\ No newline at end of file


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

Reply via email to