From 91d071c43e5e8ddc232c3a61c27743549b0fd2f6 Mon Sep 17 00:00:00 2001 From: "anilkivilcim.eray" Date: Sat, 24 Aug 2019 00:30:29 +0300 Subject: [PATCH] BAEL-3097 pr fix --- .../main/java/com/baeldung/dto/UrlDTO.java | 3 +-- .../java/com/baeldung/entity/UrlObject.java | 3 +-- ...uffixRemover.java => ProtocolRemover.java} | 2 +- .../java/com/baeldung/mapper/UrlMapper.java | 25 +++++++++++-------- .../baeldung/mapper/UrlMapperUnitTest.java | 20 ++++++++++----- 5 files changed, 32 insertions(+), 21 deletions(-) rename mapstruct/src/main/java/com/baeldung/mapper/{SuffixRemover.java => ProtocolRemover.java} (88%) diff --git a/mapstruct/src/main/java/com/baeldung/dto/UrlDTO.java b/mapstruct/src/main/java/com/baeldung/dto/UrlDTO.java index 405473bbd1..e0d7fd58e9 100644 --- a/mapstruct/src/main/java/com/baeldung/dto/UrlDTO.java +++ b/mapstruct/src/main/java/com/baeldung/dto/UrlDTO.java @@ -6,6 +6,5 @@ import lombok.Setter; @Getter @Setter public class UrlDTO { - private String url; - private String subUrl; + private String address; } \ No newline at end of file diff --git a/mapstruct/src/main/java/com/baeldung/entity/UrlObject.java b/mapstruct/src/main/java/com/baeldung/entity/UrlObject.java index ceded9e18e..9d1f4668c2 100644 --- a/mapstruct/src/main/java/com/baeldung/entity/UrlObject.java +++ b/mapstruct/src/main/java/com/baeldung/entity/UrlObject.java @@ -6,6 +6,5 @@ import lombok.Setter; @Getter @Setter public class UrlObject { - private String url; - private String subUrl; + private String address; } \ No newline at end of file diff --git a/mapstruct/src/main/java/com/baeldung/mapper/SuffixRemover.java b/mapstruct/src/main/java/com/baeldung/mapper/ProtocolRemover.java similarity index 88% rename from mapstruct/src/main/java/com/baeldung/mapper/SuffixRemover.java rename to mapstruct/src/main/java/com/baeldung/mapper/ProtocolRemover.java index 3db963b0e3..4c22ffd9ee 100644 --- a/mapstruct/src/main/java/com/baeldung/mapper/SuffixRemover.java +++ b/mapstruct/src/main/java/com/baeldung/mapper/ProtocolRemover.java @@ -10,4 +10,4 @@ import java.lang.annotation.Target; @Qualifier @Target(ElementType.METHOD) @Retention(RetentionPolicy.CLASS) -public @interface SuffixRemover {} +public @interface ProtocolRemover {} diff --git a/mapstruct/src/main/java/com/baeldung/mapper/UrlMapper.java b/mapstruct/src/main/java/com/baeldung/mapper/UrlMapper.java index b92c134bde..22083fabb2 100644 --- a/mapstruct/src/main/java/com/baeldung/mapper/UrlMapper.java +++ b/mapstruct/src/main/java/com/baeldung/mapper/UrlMapper.java @@ -9,6 +9,7 @@ import org.mapstruct.factory.Mappers; import java.net.URI; import java.net.URISyntaxException; +import java.net.URL; @Mapper public interface UrlMapper { @@ -16,19 +17,23 @@ public interface UrlMapper { UrlMapper INSTANCE = Mappers.getMapper(UrlMapper.class); - @Mapping(source = "subUrl", target = "subUrl", qualifiedByName = "protocolRemover") - @Mapping(source = "url", target = "url", qualifiedBy = SuffixRemover.class) - public UrlObject urlObjectDomainMapper(UrlDTO urlDTO); + @Mapping(source = "address", target = "address", qualifiedByName = "protocolRemover") + public UrlObject urlObjectNamedMapper(UrlDTO urlDTO); + + @Mapping(source = "address", target = "address", qualifiedBy = ProtocolRemover.class) + public UrlObject urlObjectAnnotatedMapper(UrlDTO urlDTO); @Named("protocolRemover") - public static String protocolRemoverWithCustomMethod(String domain) throws URISyntaxException { - URI uri = new URI(domain); - return uri.getHost(); + public static String protocolRemoverWithCustomMethod(String address) throws URISyntaxException { + URI uri = new URI(address); + + return uri.getHost() + uri.getPath(); } - @SuffixRemover - public static String protocolRemoverMethod(String domain) throws URISyntaxException { - URI uri = new URI(domain); - return uri.getHost(); + @ProtocolRemover + public static String protocolRemoverMethod(String address) throws URISyntaxException { + URI uri = new URI(address); + + return uri.getHost() + uri.getPath(); } } \ No newline at end of file diff --git a/mapstruct/src/test/java/com/baeldung/mapper/UrlMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapper/UrlMapperUnitTest.java index 455bfe408f..567d0aefbc 100644 --- a/mapstruct/src/test/java/com/baeldung/mapper/UrlMapperUnitTest.java +++ b/mapstruct/src/test/java/com/baeldung/mapper/UrlMapperUnitTest.java @@ -9,14 +9,22 @@ import static org.junit.Assert.assertEquals; public class UrlMapperUnitTest { @Test - public void givenDomains_whenCallDomainMapper_thenReturnDomainsWithoutProtocols() { + public void givenDomain_whenCallNamedMapper_thenReturnDomainWithoutProtocol() { UrlDTO dto = new UrlDTO(); - dto.setUrl("http://www.baeldung.com"); - dto.setSubUrl("https://www.baeldung.com"); + dto.setAddress("http://www.baeldung.com/mapstruct"); - UrlObject urlObject = UrlMapper.INSTANCE.urlObjectDomainMapper(dto); + UrlObject urlObject = UrlMapper.INSTANCE.urlObjectNamedMapper(dto); - assertEquals(urlObject.getUrl(), "www.baeldung.com"); - assertEquals(urlObject.getSubUrl(), "www.baeldung.com"); + assertEquals(urlObject.getAddress(), "www.baeldung.com/mapstruct"); + } + + @Test + public void givenAddress_whenCallAnnotatedMapper_thenReturnDomainWithoutProtocol() { + UrlDTO dto = new UrlDTO(); + dto.setAddress("http://www.baeldung.com/customMappers"); + + UrlObject urlObject = UrlMapper.INSTANCE.urlObjectAnnotatedMapper(dto); + + assertEquals(urlObject.getAddress(), "www.baeldung.com/customMappers"); } } \ No newline at end of file