Merge pull request #7610 from kivicko/BAEL-3097-mapstruct-custom-method
BAEL-3097 init for mapstruct custom mapping method
This commit is contained in:
commit
ad073e5691
@ -65,7 +65,7 @@
|
|||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<org.mapstruct.version>1.3.0.Beta2</org.mapstruct.version>
|
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
|
||||||
<springframework.version>4.3.4.RELEASE</springframework.version>
|
<springframework.version>4.3.4.RELEASE</springframework.version>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung.dto;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class UserBodyImperialValuesDTO {
|
||||||
|
private int inch;
|
||||||
|
private int pound;
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung.entity;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class UserBodyValues {
|
||||||
|
private double kilogram;
|
||||||
|
private double centimeter;
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.mapper;
|
||||||
|
|
||||||
|
import org.mapstruct.Qualifier;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
@Retention(RetentionPolicy.CLASS)
|
||||||
|
public @interface PoundToKilogramMapper {}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.mapper;
|
||||||
|
|
||||||
|
import com.baeldung.dto.UserBodyImperialValuesDTO;
|
||||||
|
import com.baeldung.entity.UserBodyValues;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.Named;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface UserBodyValuesMapper {
|
||||||
|
|
||||||
|
UserBodyValuesMapper INSTANCE = Mappers.getMapper(UserBodyValuesMapper.class);
|
||||||
|
|
||||||
|
@Mapping(source = "pound", target = "kilogram", qualifiedBy = PoundToKilogramMapper.class)
|
||||||
|
@Mapping(source = "inch", target = "centimeter", qualifiedByName = "inchToCentimeter")
|
||||||
|
public UserBodyValues userBodyValuesMapper(UserBodyImperialValuesDTO dto);
|
||||||
|
|
||||||
|
@Named("inchToCentimeter")
|
||||||
|
public static double inchToCentimeter(int inch) {
|
||||||
|
return inch * 2.54;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PoundToKilogramMapper
|
||||||
|
public static double poundToKilogram(int pound) {
|
||||||
|
return pound * 0.4535;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package com.baeldung.mapper;
|
||||||
|
|
||||||
|
import com.baeldung.dto.UserBodyImperialValuesDTO;
|
||||||
|
import com.baeldung.entity.UserBodyValues;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
|
||||||
|
public class UserBodyValuesMapperUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUserBodyImperialValuesDTOToUserBodyValuesObject_whenMaps_thenCorrect() {
|
||||||
|
UserBodyImperialValuesDTO dto = new UserBodyImperialValuesDTO();
|
||||||
|
dto.setInch(10);
|
||||||
|
dto.setPound(100);
|
||||||
|
|
||||||
|
UserBodyValues obj = UserBodyValuesMapper.INSTANCE.userBodyValuesMapper(dto);
|
||||||
|
|
||||||
|
assertNotNull(obj);
|
||||||
|
assertEquals(25.4, obj.getCentimeter(), 0);
|
||||||
|
assertEquals(45.35, obj.getKilogram(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUserBodyImperialValuesDTOWithInchToUserBodyValuesObject_whenMaps_thenCorrect() {
|
||||||
|
UserBodyImperialValuesDTO dto = new UserBodyImperialValuesDTO();
|
||||||
|
dto.setInch(10);
|
||||||
|
|
||||||
|
UserBodyValues obj = UserBodyValuesMapper.INSTANCE.userBodyValuesMapper(dto);
|
||||||
|
|
||||||
|
assertNotNull(obj);
|
||||||
|
assertEquals(25.4, obj.getCentimeter(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUserBodyImperialValuesDTOWithPoundToUserBodyValuesObject_whenMaps_thenCorrect() {
|
||||||
|
UserBodyImperialValuesDTO dto = new UserBodyImperialValuesDTO();
|
||||||
|
dto.setPound(100);
|
||||||
|
|
||||||
|
UserBodyValues obj = UserBodyValuesMapper.INSTANCE.userBodyValuesMapper(dto);
|
||||||
|
|
||||||
|
assertNotNull(obj);
|
||||||
|
assertEquals(45.35, obj.getKilogram(), 0);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user