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 7027d59c8e7c82112d48bb739b38b2a98983d8cb Author: Tran Tien Duc <[email protected]> AuthorDate: Fri Apr 5 10:23:57 2019 +0700 JAMES-2715 little syntax refactoring & code indentation --- .../webadmin/routes/DomainMappingsRoutes.java | 112 ++++++++++----------- .../webadmin/routes/DomainMappingsRoutesTest.java | 107 ++++++++++---------- 2 files changed, 106 insertions(+), 113 deletions(-) diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainMappingsRoutes.java b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainMappingsRoutes.java index 800d25f..5344b5c 100644 --- a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainMappingsRoutes.java +++ b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainMappingsRoutes.java @@ -26,7 +26,6 @@ import static spark.Spark.halt; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.function.Function; import javax.inject.Inject; import javax.ws.rs.DELETE; @@ -48,6 +47,7 @@ import org.eclipse.jetty.http.HttpStatus; import com.github.fge.lambdas.consumers.ThrowingBiConsumer; import com.github.steveash.guavate.Guavate; +import com.google.common.annotations.VisibleForTesting; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; @@ -73,7 +73,8 @@ public class DomainMappingsRoutes implements Routes { private final JsonTransformer jsonTransformer; @Inject - public DomainMappingsRoutes(final RecipientRewriteTable recipientRewriteTable, final JsonTransformer jsonTransformer) { + @VisibleForTesting + DomainMappingsRoutes(final RecipientRewriteTable recipientRewriteTable, final JsonTransformer jsonTransformer) { this.recipientRewriteTable = recipientRewriteTable; this.jsonTransformer = jsonTransformer; } @@ -95,13 +96,13 @@ public class DomainMappingsRoutes implements Routes { @Path(SPECIFIC_MAPPING_PATH) @ApiOperation(value = "Creating domain mapping between source and destination domains.") @ApiImplicitParams({ - @ApiImplicitParam(required = true, dataType = "string", name = FROM_DOMAIN, paramType = "path") + @ApiImplicitParam(required = true, dataType = "string", name = FROM_DOMAIN, paramType = "path") }) @ApiResponses(value = { - @ApiResponse(code = HttpStatus.NO_CONTENT_204, message = "Ok"), - @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Domain name is invalid"), - @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, - message = "Internal server error - Something went bad on the server side.") + @ApiResponse(code = HttpStatus.NO_CONTENT_204, message = "Ok"), + @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Domain name is invalid"), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, + message = "Internal server error - Something went bad on the server side.") }) public HaltException addDomainMapping(Request request, Response response) { doMapping(request, recipientRewriteTable::addAliasDomainMapping); @@ -112,13 +113,13 @@ public class DomainMappingsRoutes implements Routes { @Path(SPECIFIC_MAPPING_PATH) @ApiOperation(value = "Removes domain mapping between source and destination domains.") @ApiImplicitParams({ - @ApiImplicitParam(required = true, dataType = "string", name = FROM_DOMAIN, paramType = "path") + @ApiImplicitParam(required = true, dataType = "string", name = FROM_DOMAIN, paramType = "path") }) @ApiResponses(value = { - @ApiResponse(code = HttpStatus.NO_CONTENT_204, message = "Ok"), - @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Domain name is invalid"), - @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, - message = "Internal server error - Something went bad on the server side.") + @ApiResponse(code = HttpStatus.NO_CONTENT_204, message = "Ok"), + @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Domain name is invalid"), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, + message = "Internal server error - Something went bad on the server side.") }) public HaltException removeDomainMapping(Request request, Response response) { doMapping(request, recipientRewriteTable::removeAliasDomainMapping); @@ -129,83 +130,78 @@ public class DomainMappingsRoutes implements Routes { @Path(DOMAIN_MAPPINGS) @ApiOperation(value = "Lists all domain mappings.") @ApiResponses(value = { - @ApiResponse(code = HttpStatus.OK_200, message = "Domain mappings.", responseContainer = "Map"), - @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, - message = "Internal server error - Something went bad on the server side.") + @ApiResponse(code = HttpStatus.OK_200, message = "Domain mappings.", responseContainer = "Map"), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, + message = "Internal server error - Something went bad on the server side.") }) public Map<String, List<String>> getAllMappings(Request request, Response response) throws RecipientRewriteTableException { return recipientRewriteTable.getAllMappings() - .entrySet() - .stream() - .filter(mappingsEntry -> !mappingsEntry.getValue().isEmpty()) - .filter(mappingsEntry -> mappingsEntry.getValue().contains(Mapping.Type.Domain)) - .collect(Guavate.toImmutableMap( - mappingsEntry -> mappingsEntry.getKey().getFixedDomain(), - mappingsEntry -> toDomainList(mappingsEntry.getValue()) - )); + .entrySet() + .stream() + .filter(mappingsEntry -> mappingsEntry.getValue().contains(Mapping.Type.Domain)) + .collect(Guavate.toImmutableMap( + mappingsEntry -> mappingsEntry.getKey().getFixedDomain(), + mappingsEntry -> toDomainList(mappingsEntry.getValue()) + )); } @GET @Path(SPECIFIC_MAPPING_PATH) @ApiOperation(value = "Lists mappings for specific domain.") @ApiImplicitParams({ - @ApiImplicitParam(required = true, dataType = "string", name = FROM_DOMAIN, paramType = "path") + @ApiImplicitParam(required = true, dataType = "string", name = FROM_DOMAIN, paramType = "path") }) @ApiResponses(value = { - @ApiResponse(code = HttpStatus.OK_200, message = "Domain mappings.", responseContainer = "List"), - @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "Not existing mappings."), - @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, - message = "Internal server error - Something went bad on the server side.") + @ApiResponse(code = HttpStatus.OK_200, message = "Domain mappings.", responseContainer = "List"), + @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "Not existing mappings."), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, + message = "Internal server error - Something went bad on the server side.") }) public List<String> getMapping(Request request, Response response) throws RecipientRewriteTableException { MappingSource mappingSource = mappingSourceFrom(request); return Optional.of(recipientRewriteTable.getStoredMappings(mappingSource).select(Mapping.Type.Domain)) - .filter(mappings -> !mappings.isEmpty()) - .filter(mappings -> mappings.contains(Mapping.Type.Domain)) - .map(this::toDomainList) - .orElseThrow(() -> ErrorResponder.builder() - .statusCode(HttpStatus.NOT_FOUND_404) - .type(ErrorResponder.ErrorType.NOT_FOUND) - .message(String.format("Cannot find mappings for %s", mappingSource.getFixedDomain())) - .haltError()); + .filter(mappings -> mappings.contains(Mapping.Type.Domain)) + .map(this::toDomainList) + .orElseThrow(() -> ErrorResponder.builder() + .statusCode(HttpStatus.NOT_FOUND_404) + .type(ErrorResponder.ErrorType.NOT_FOUND) + .message(String.format("Cannot find mappings for %s", mappingSource.getFixedDomain())) + .haltError()); } private MappingSource mappingSourceFrom(final Request request) { - return createDomainOrThrow() - .andThen(MappingSource::fromDomain) - .apply(request.params(FROM_DOMAIN)); + Domain fromDomain = extractDomain(request.params(FROM_DOMAIN)); + return MappingSource.fromDomain(fromDomain); } private void doMapping(Request request, ThrowingBiConsumer<MappingSource, Domain> mappingOperation) { MappingSource fromDomain = mappingSourceFrom(request); - Domain toDomain = createDomainOrThrow().apply(request.body()); + Domain toDomain = extractDomain(request.body()); mappingOperation.accept(fromDomain, toDomain); } - private Function<String, Domain> createDomainOrThrow() { - return candidate -> { - try { - return Domain.of(candidate.trim()); - } catch (IllegalArgumentException e) { - throw ErrorResponder.builder() - .statusCode(HttpStatus.BAD_REQUEST_400) - .type(ErrorResponder.ErrorType.INVALID_ARGUMENT) - .message(String.format("The domain %s is invalid.", candidate)) - .cause(e) - .haltError(); - } - }; + private Domain extractDomain(String domainAsString) { + try { + return Domain.of(domainAsString.trim()); + } catch (IllegalArgumentException e) { + throw ErrorResponder.builder() + .statusCode(HttpStatus.BAD_REQUEST_400) + .type(ErrorResponder.ErrorType.INVALID_ARGUMENT) + .message(String.format("The domain %s is invalid.", domainAsString)) + .cause(e) + .haltError(); + } } private List<String> toDomainList(Mappings mappings) { return mappings - .select(Mapping.Type.Domain) - .asStream() - .map(Mapping::asString) - .map(Mapping.Type.Domain::withoutPrefix) - .collect(Guavate.toImmutableList()); + .select(Mapping.Type.Domain) + .asStream() + .map(Mapping::asString) + .map(Mapping.Type.Domain::withoutPrefix) + .collect(Guavate.toImmutableList()); } } diff --git a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainMappingsRoutesTest.java b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainMappingsRoutesTest.java index 833efa8..16f8bd7 100644 --- a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainMappingsRoutesTest.java +++ b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainMappingsRoutesTest.java @@ -25,6 +25,7 @@ import static io.restassured.RestAssured.put; import static io.restassured.RestAssured.when; import static io.restassured.RestAssured.with; import static org.apache.james.webadmin.WebAdminServer.NO_CONFIGURATION; +import static org.apache.james.webadmin.routes.DomainMappingsRoutes.DOMAIN_MAPPINGS; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; import static org.hamcrest.CoreMatchers.is; @@ -72,15 +73,15 @@ class DomainMappingsRoutesTest { private void createServer(DomainMappingsRoutes domainMappingsRoutes) throws Exception { webAdminServer = WebAdminUtils.createWebAdminServer( - new DefaultMetricFactory(), - domainMappingsRoutes); + new DefaultMetricFactory(), + domainMappingsRoutes); webAdminServer.configure(NO_CONFIGURATION); webAdminServer.await(); RestAssured.requestSpecification = WebAdminUtils.buildRequestSpecification(webAdminServer) - .setBasePath("/domainMappings") - .log(LogDetail.METHOD) - .build(); + .setBasePath(DOMAIN_MAPPINGS) + .log(LogDetail.METHOD) + .build(); } @BeforeEach @@ -96,10 +97,6 @@ class DomainMappingsRoutesTest { } @Nested - class Authentication { - } - - @Nested class NormalBehaviour { @Test @@ -142,18 +139,18 @@ class DomainMappingsRoutesTest { recipientRewriteTable.addAliasDomainMapping(mappingSource, Domain.of(alias3)); Map<String, List<String>> map = - when() - .get() - .then() - .contentType(ContentType.JSON) - .statusCode(HttpStatus.OK_200) - .extract() - .body() - .jsonPath() - .getMap("."); + when() + .get() + .then() + .contentType(ContentType.JSON) + .statusCode(HttpStatus.OK_200) + .extract() + .body() + .jsonPath() + .getMap("."); assertThat(map) - .containsOnly(entry(expectedDomain.name(), ImmutableList.of(alias1, alias2, alias3))); + .containsOnly(entry(expectedDomain.name(), ImmutableList.of(alias1, alias2, alias3))); } @Test @@ -162,26 +159,26 @@ class DomainMappingsRoutesTest { MappingSource emptyMapping = MappingSource.fromDomain(Domain.of("def.com")); Map<MappingSource, Mappings> mappings = ImmutableMap.of( - nonEmptyMapping, MappingsImpl.fromRawString("domain:a.com"), - emptyMapping, MappingsImpl.empty() + nonEmptyMapping, MappingsImpl.fromRawString("domain:a.com"), + emptyMapping, MappingsImpl.empty() ); when(recipientRewriteTable.getAllMappings()).thenReturn(mappings); Map<String, List<String>> map = - when() - .get() - .then() - .contentType(ContentType.JSON) - .statusCode(HttpStatus.OK_200) - .extract() - .body() - .jsonPath() - .getMap("."); + when() + .get() + .then() + .contentType(ContentType.JSON) + .statusCode(HttpStatus.OK_200) + .extract() + .body() + .jsonPath() + .getMap("."); assertThat(map) - .containsKey(nonEmptyMapping.asString()) - .doesNotContainKey(emptyMapping.asString()); + .containsKey(nonEmptyMapping.asString()) + .doesNotContainKey(emptyMapping.asString()); } @Test @@ -288,14 +285,14 @@ class DomainMappingsRoutesTest { recipientRewriteTable.addAliasDomainMapping(mappingSource, Domain.of(aliasDomain)); List<String> body = - when() - .get(domain) - .then() - .contentType(ContentType.JSON) - .statusCode(HttpStatus.OK_200) - .extract() - .jsonPath() - .getList("."); + when() + .get(domain) + .then() + .contentType(ContentType.JSON) + .statusCode(HttpStatus.OK_200) + .extract() + .jsonPath() + .getList("."); assertThat(body).containsOnly(aliasDomain); } @@ -385,25 +382,25 @@ class DomainMappingsRoutesTest { .getMap("."); assertThat(errors) - .containsEntry("statusCode", HttpStatus.BAD_REQUEST_400) - .containsEntry("type", "InvalidArgument") - .hasEntrySatisfying("message", o -> assertThat((String) o).matches("^The domain .* is invalid\\.$")); + .containsEntry("statusCode", HttpStatus.BAD_REQUEST_400) + .containsEntry("type", "InvalidArgument") + .hasEntrySatisfying("message", o -> assertThat((String) o).matches("^The domain .* is invalid\\.$")); } - private void assertBadRequest(String toDomain, Function<RequestSpecification, Response> op) { - Map<String, Object> errors = op.apply(given().body(toDomain).when()) - .then() - .statusCode(HttpStatus.BAD_REQUEST_400) - .contentType(ContentType.JSON) - .extract() - .body() - .jsonPath() - .getMap("."); + private void assertBadRequest(String toDomain, Function<RequestSpecification, Response> requestingFunction) { + Map<String, Object> errors = requestingFunction.apply(given().body(toDomain).when()) + .then() + .statusCode(HttpStatus.BAD_REQUEST_400) + .contentType(ContentType.JSON) + .extract() + .body() + .jsonPath() + .getMap("."); assertThat(errors) - .containsEntry("statusCode", HttpStatus.BAD_REQUEST_400) - .containsEntry("type", "InvalidArgument") - .hasEntrySatisfying("message", o -> assertThat((String) o).matches("^The domain .* is invalid\\.$")); + .containsEntry("statusCode", HttpStatus.BAD_REQUEST_400) + .containsEntry("type", "InvalidArgument") + .hasEntrySatisfying("message", o -> assertThat((String) o).matches("^The domain .* is invalid\\.$")); } } } \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
