BAEL-3097 pr fix
This commit is contained in:
parent
25e11c23a1
commit
91d071c43e
|
@ -6,6 +6,5 @@ import lombok.Setter;
|
|||
@Getter
|
||||
@Setter
|
||||
public class UrlDTO {
|
||||
private String url;
|
||||
private String subUrl;
|
||||
private String address;
|
||||
}
|
|
@ -6,6 +6,5 @@ import lombok.Setter;
|
|||
@Getter
|
||||
@Setter
|
||||
public class UrlObject {
|
||||
private String url;
|
||||
private String subUrl;
|
||||
private String address;
|
||||
}
|
|
@ -10,4 +10,4 @@ import java.lang.annotation.Target;
|
|||
@Qualifier
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.CLASS)
|
||||
public @interface SuffixRemover {}
|
||||
public @interface ProtocolRemover {}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue