BAEL-3097 pr fix

This commit is contained in:
anilkivilcim.eray 2019-08-24 00:30:29 +03:00
parent 25e11c23a1
commit 91d071c43e
5 changed files with 32 additions and 21 deletions

View File

@ -6,6 +6,5 @@ import lombok.Setter;
@Getter @Getter
@Setter @Setter
public class UrlDTO { public class UrlDTO {
private String url; private String address;
private String subUrl;
} }

View File

@ -6,6 +6,5 @@ import lombok.Setter;
@Getter @Getter
@Setter @Setter
public class UrlObject { public class UrlObject {
private String url; private String address;
private String subUrl;
} }

View File

@ -10,4 +10,4 @@ import java.lang.annotation.Target;
@Qualifier @Qualifier
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS) @Retention(RetentionPolicy.CLASS)
public @interface SuffixRemover {} public @interface ProtocolRemover {}

View File

@ -9,6 +9,7 @@ import org.mapstruct.factory.Mappers;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL;
@Mapper @Mapper
public interface UrlMapper { public interface UrlMapper {
@ -16,19 +17,23 @@ public interface UrlMapper {
UrlMapper INSTANCE = Mappers.getMapper(UrlMapper.class); UrlMapper INSTANCE = Mappers.getMapper(UrlMapper.class);
@Mapping(source = "subUrl", target = "subUrl", qualifiedByName = "protocolRemover") @Mapping(source = "address", target = "address", qualifiedByName = "protocolRemover")
@Mapping(source = "url", target = "url", qualifiedBy = SuffixRemover.class) public UrlObject urlObjectNamedMapper(UrlDTO urlDTO);
public UrlObject urlObjectDomainMapper(UrlDTO urlDTO);
@Mapping(source = "address", target = "address", qualifiedBy = ProtocolRemover.class)
public UrlObject urlObjectAnnotatedMapper(UrlDTO urlDTO);
@Named("protocolRemover") @Named("protocolRemover")
public static String protocolRemoverWithCustomMethod(String domain) throws URISyntaxException { public static String protocolRemoverWithCustomMethod(String address) throws URISyntaxException {
URI uri = new URI(domain); URI uri = new URI(address);
return uri.getHost();
return uri.getHost() + uri.getPath();
} }
@SuffixRemover @ProtocolRemover
public static String protocolRemoverMethod(String domain) throws URISyntaxException { public static String protocolRemoverMethod(String address) throws URISyntaxException {
URI uri = new URI(domain); URI uri = new URI(address);
return uri.getHost();
return uri.getHost() + uri.getPath();
} }
} }

View File

@ -9,14 +9,22 @@ import static org.junit.Assert.assertEquals;
public class UrlMapperUnitTest { public class UrlMapperUnitTest {
@Test @Test
public void givenDomains_whenCallDomainMapper_thenReturnDomainsWithoutProtocols() { public void givenDomain_whenCallNamedMapper_thenReturnDomainWithoutProtocol() {
UrlDTO dto = new UrlDTO(); UrlDTO dto = new UrlDTO();
dto.setUrl("http://www.baeldung.com"); dto.setAddress("http://www.baeldung.com/mapstruct");
dto.setSubUrl("https://www.baeldung.com");
UrlObject urlObject = UrlMapper.INSTANCE.urlObjectDomainMapper(dto); UrlObject urlObject = UrlMapper.INSTANCE.urlObjectNamedMapper(dto);
assertEquals(urlObject.getUrl(), "www.baeldung.com"); assertEquals(urlObject.getAddress(), "www.baeldung.com/mapstruct");
assertEquals(urlObject.getSubUrl(), "www.baeldung.com"); }
@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");
} }
} }