diff --git a/mapstruct/pom.xml b/mapstruct/pom.xml index 2613dcf083..e98c4a318b 100644 --- a/mapstruct/pom.xml +++ b/mapstruct/pom.xml @@ -35,6 +35,12 @@ lombok ${org.projectlombok.version} + + org.assertj + assertj-core + ${assertj.version} + test + @@ -70,6 +76,7 @@ 1.8 1.8 1.18.4 + 3.11.1 diff --git a/mapstruct/src/main/java/com/baeldung/unmappedproperties/dto/CarDTO.java b/mapstruct/src/main/java/com/baeldung/unmappedproperties/dto/CarDTO.java new file mode 100644 index 0000000000..8f324f5a15 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/unmappedproperties/dto/CarDTO.java @@ -0,0 +1,23 @@ +package com.baeldung.unmappedproperties.dto; + +public class CarDTO { + private int id; + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} \ No newline at end of file diff --git a/mapstruct/src/main/java/com/baeldung/unmappedproperties/dto/DocumentDTO.java b/mapstruct/src/main/java/com/baeldung/unmappedproperties/dto/DocumentDTO.java new file mode 100644 index 0000000000..8df306eb13 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/unmappedproperties/dto/DocumentDTO.java @@ -0,0 +1,52 @@ +package com.baeldung.unmappedproperties.dto; + +import java.util.List; + +public class DocumentDTO { + private int id; + private String title; + private String text; + private List comments; + private String author; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public List getComments() { + return comments; + } + + public void setComments(List comments) { + this.comments = comments; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + +} \ No newline at end of file diff --git a/mapstruct/src/main/java/com/baeldung/unmappedproperties/entity/Car.java b/mapstruct/src/main/java/com/baeldung/unmappedproperties/entity/Car.java new file mode 100644 index 0000000000..c23ced3a6a --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/unmappedproperties/entity/Car.java @@ -0,0 +1,23 @@ +package com.baeldung.unmappedproperties.entity; + +public class Car { + private int id; + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} \ No newline at end of file diff --git a/mapstruct/src/main/java/com/baeldung/unmappedproperties/entity/Document.java b/mapstruct/src/main/java/com/baeldung/unmappedproperties/entity/Document.java new file mode 100644 index 0000000000..89457133a3 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/unmappedproperties/entity/Document.java @@ -0,0 +1,42 @@ +package com.baeldung.unmappedproperties.entity; + +import java.util.Date; + +public class Document { + private int id; + private String title; + private String text; + private Date modificationTime; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public Date getModificationTime() { + return modificationTime; + } + + public void setModificationTime(Date modificationTime) { + this.modificationTime = modificationTime; + } +} \ No newline at end of file diff --git a/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/CarMapper.java b/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/CarMapper.java new file mode 100644 index 0000000000..714301b811 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/CarMapper.java @@ -0,0 +1,13 @@ +package com.baeldung.unmappedproperties.mapper; + +import com.baeldung.unmappedproperties.dto.CarDTO; +import com.baeldung.unmappedproperties.entity.Car; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface CarMapper { + CarMapper INSTANCE = Mappers.getMapper(CarMapper.class); + + CarDTO carToCarDTO(Car car); +} \ No newline at end of file diff --git a/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/DocumentMapper.java b/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/DocumentMapper.java new file mode 100644 index 0000000000..fe233ed172 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/DocumentMapper.java @@ -0,0 +1,16 @@ +package com.baeldung.unmappedproperties.mapper; + +import com.baeldung.unmappedproperties.dto.DocumentDTO; +import com.baeldung.unmappedproperties.entity.Document; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface DocumentMapper { + + DocumentMapper INSTANCE = Mappers.getMapper(DocumentMapper.class); + + DocumentDTO documentToDocumentDTO(Document entity); + + Document documentDTOToDocument(DocumentDTO dto); +} \ No newline at end of file diff --git a/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/DocumentMapperMappingIgnore.java b/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/DocumentMapperMappingIgnore.java new file mode 100644 index 0000000000..bf3b0b49c4 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/DocumentMapperMappingIgnore.java @@ -0,0 +1,20 @@ +package com.baeldung.unmappedproperties.mapper; + +import com.baeldung.unmappedproperties.dto.DocumentDTO; +import com.baeldung.unmappedproperties.entity.Document; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface DocumentMapperMappingIgnore { + + DocumentMapperMappingIgnore INSTANCE = Mappers.getMapper(DocumentMapperMappingIgnore.class); + + @Mapping(target = "comments", ignore = true) + DocumentDTO documentToDocumentDTO(Document entity); + + @Mapping(target = "modificationTime", ignore = true) + Document documentDTOToDocument(DocumentDTO dto); + +} \ No newline at end of file diff --git a/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/DocumentMapperUnmappedPolicy.java b/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/DocumentMapperUnmappedPolicy.java new file mode 100644 index 0000000000..b44f714774 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/DocumentMapperUnmappedPolicy.java @@ -0,0 +1,17 @@ +package com.baeldung.unmappedproperties.mapper; + +import com.baeldung.unmappedproperties.dto.DocumentDTO; +import com.baeldung.unmappedproperties.entity.Document; +import org.mapstruct.Mapper; +import org.mapstruct.ReportingPolicy; +import org.mapstruct.factory.Mappers; + +@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) +public interface DocumentMapperUnmappedPolicy { + + DocumentMapperUnmappedPolicy INSTANCE = Mappers.getMapper(DocumentMapperUnmappedPolicy.class); + + DocumentDTO documentToDocumentDTO(Document entity); + + Document documentDTOToDocument(DocumentDTO dto); +} \ No newline at end of file diff --git a/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/DocumentMapperWithConfig.java b/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/DocumentMapperWithConfig.java new file mode 100644 index 0000000000..dfaab7e1a1 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/DocumentMapperWithConfig.java @@ -0,0 +1,16 @@ +package com.baeldung.unmappedproperties.mapper; + +import com.baeldung.unmappedproperties.dto.DocumentDTO; +import com.baeldung.unmappedproperties.entity.Document; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +@Mapper(config = IgnoreUnmappedMapperConfig.class) +public interface DocumentMapperWithConfig { + + DocumentMapperWithConfig INSTANCE = Mappers.getMapper(DocumentMapperWithConfig.class); + + DocumentDTO documentToDocumentDTO(Document entity); + + Document documentDTOToDocument(DocumentDTO dto); +} \ No newline at end of file diff --git a/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/IgnoreUnmappedMapperConfig.java b/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/IgnoreUnmappedMapperConfig.java new file mode 100644 index 0000000000..804ce9c03a --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/unmappedproperties/mapper/IgnoreUnmappedMapperConfig.java @@ -0,0 +1,8 @@ +package com.baeldung.unmappedproperties.mapper; + +import org.mapstruct.MapperConfig; +import org.mapstruct.ReportingPolicy; + +@MapperConfig(unmappedTargetPolicy = ReportingPolicy.IGNORE) +public interface IgnoreUnmappedMapperConfig { +} \ No newline at end of file diff --git a/mapstruct/src/test/java/com/baeldung/mapper/unmappedproperties/CarMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapper/unmappedproperties/CarMapperUnitTest.java new file mode 100644 index 0000000000..4ce04015f1 --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/mapper/unmappedproperties/CarMapperUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.mapper.unmappedproperties; + +import com.baeldung.unmappedproperties.dto.CarDTO; +import com.baeldung.unmappedproperties.entity.Car; +import com.baeldung.unmappedproperties.mapper.CarMapper; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CarMapperUnitTest { + + @Test + public void givenCarEntitytoCar_whenMaps_thenCorrect() { + Car entity = new Car(); + entity.setId(1); + entity.setName("Toyota"); + + CarDTO carDto = CarMapper.INSTANCE.carToCarDTO(entity); + + assertThat(carDto.getId()).isEqualTo(entity.getId()); + assertThat(carDto.getName()).isEqualTo(entity.getName()); + } +} diff --git a/mapstruct/src/test/java/com/baeldung/mapper/unmappedproperties/DocumentMapperMappingIgnoreUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapper/unmappedproperties/DocumentMapperMappingIgnoreUnitTest.java new file mode 100644 index 0000000000..493c7f8686 --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/mapper/unmappedproperties/DocumentMapperMappingIgnoreUnitTest.java @@ -0,0 +1,45 @@ +package com.baeldung.mapper.unmappedproperties; + +import com.baeldung.unmappedproperties.dto.DocumentDTO; +import com.baeldung.unmappedproperties.entity.Document; +import com.baeldung.unmappedproperties.mapper.DocumentMapperMappingIgnore; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Date; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DocumentMapperMappingIgnoreUnitTest { + + @Test + public void givenDocumentEntityToDocumentDto_whenMaps_thenCorrect() { + Document entity = new Document(); + entity.setId(1); + entity.setTitle("Price 13-42"); + entity.setText("List of positions......."); + entity.setModificationTime(new Date()); + + DocumentDTO dto = DocumentMapperMappingIgnore.INSTANCE.documentToDocumentDTO(entity); + + assertThat(dto.getId()).isEqualTo(entity.getId()); + assertThat(dto.getTitle()).isEqualTo(entity.getTitle()); + assertThat(dto.getText()).isEqualTo(entity.getText()); + } + + @Test + public void givenDocumentDtoToDocumentEntity_whenMaps_thenCorrect() { + DocumentDTO dto = new DocumentDTO(); + dto.setId(1); + dto.setTitle("Price 13-42"); + dto.setText("List of positions......."); + dto.setComments(Arrays.asList("Not all positions", "Wrong price values")); + dto.setAuthor("Author1"); + + Document entity = DocumentMapperMappingIgnore.INSTANCE.documentDTOToDocument(dto); + + assertThat(entity.getId()).isEqualTo(dto.getId()); + assertThat(entity.getTitle()).isEqualTo(dto.getTitle()); + assertThat(entity.getText()).isEqualTo(dto.getText()); + } +} \ No newline at end of file diff --git a/mapstruct/src/test/java/com/baeldung/mapper/unmappedproperties/DocumentMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapper/unmappedproperties/DocumentMapperUnitTest.java new file mode 100644 index 0000000000..1d3645ca96 --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/mapper/unmappedproperties/DocumentMapperUnitTest.java @@ -0,0 +1,45 @@ +package com.baeldung.mapper.unmappedproperties; + +import com.baeldung.unmappedproperties.dto.DocumentDTO; +import com.baeldung.unmappedproperties.entity.Document; +import com.baeldung.unmappedproperties.mapper.DocumentMapper; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Date; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DocumentMapperUnitTest { + + @Test + public void givenDocumentEntityToDocumentDto_whenMaps_thenCorrect() { + Document entity = new Document(); + entity.setId(1); + entity.setTitle("Price 13-42"); + entity.setText("List of positions......."); + entity.setModificationTime(new Date()); + + DocumentDTO dto = DocumentMapper.INSTANCE.documentToDocumentDTO(entity); + + assertThat(dto.getId()).isEqualTo(entity.getId()); + assertThat(dto.getTitle()).isEqualTo(entity.getTitle()); + assertThat(dto.getText()).isEqualTo(entity.getText()); + } + + @Test + public void givenDocumentDtoToDocumentEntity_whenMaps_thenCorrect() { + DocumentDTO dto = new DocumentDTO(); + dto.setId(1); + dto.setTitle("Price 13-42"); + dto.setText("List of positions......."); + dto.setComments(Arrays.asList("Not all positions", "Wrong price values")); + dto.setAuthor("Author1"); + + Document entity = DocumentMapper.INSTANCE.documentDTOToDocument(dto); + + assertThat(entity.getId()).isEqualTo(dto.getId()); + assertThat(entity.getTitle()).isEqualTo(dto.getTitle()); + assertThat(entity.getText()).isEqualTo(dto.getText()); + } +} \ No newline at end of file diff --git a/mapstruct/src/test/java/com/baeldung/mapper/unmappedproperties/DocumentMapperUnmappedPolicyUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapper/unmappedproperties/DocumentMapperUnmappedPolicyUnitTest.java new file mode 100644 index 0000000000..f6666a52ec --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/mapper/unmappedproperties/DocumentMapperUnmappedPolicyUnitTest.java @@ -0,0 +1,45 @@ +package com.baeldung.mapper.unmappedproperties; + +import com.baeldung.unmappedproperties.dto.DocumentDTO; +import com.baeldung.unmappedproperties.entity.Document; +import com.baeldung.unmappedproperties.mapper.DocumentMapperUnmappedPolicy; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Date; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DocumentMapperUnmappedPolicyUnitTest { + + @Test + public void givenDocumentEntityToDocumentDto_whenMaps_thenCorrect() { + Document entity = new Document(); + entity.setId(1); + entity.setTitle("Price 13-42"); + entity.setText("List of positions......."); + entity.setModificationTime(new Date()); + + DocumentDTO dto = DocumentMapperUnmappedPolicy.INSTANCE.documentToDocumentDTO(entity); + + assertThat(dto.getId()).isEqualTo(entity.getId()); + assertThat(dto.getTitle()).isEqualTo(entity.getTitle()); + assertThat(dto.getText()).isEqualTo(entity.getText()); + } + + @Test + public void givenDocumentDtoToDocumentEntity_whenMaps_thenCorrect() { + DocumentDTO dto = new DocumentDTO(); + dto.setId(1); + dto.setTitle("Price 13-42"); + dto.setText("List of positions......."); + dto.setComments(Arrays.asList("Not all positions", "Wrong price values")); + dto.setAuthor("Author1"); + + Document entity = DocumentMapperUnmappedPolicy.INSTANCE.documentDTOToDocument(dto); + + assertThat(entity.getId()).isEqualTo(dto.getId()); + assertThat(entity.getTitle()).isEqualTo(dto.getTitle()); + assertThat(entity.getText()).isEqualTo(dto.getText()); + } +} \ No newline at end of file diff --git a/mapstruct/src/test/java/com/baeldung/mapper/unmappedproperties/DocumentMapperWithConfigUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapper/unmappedproperties/DocumentMapperWithConfigUnitTest.java new file mode 100644 index 0000000000..c9409225f5 --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/mapper/unmappedproperties/DocumentMapperWithConfigUnitTest.java @@ -0,0 +1,45 @@ +package com.baeldung.mapper.unmappedproperties; + +import com.baeldung.unmappedproperties.dto.DocumentDTO; +import com.baeldung.unmappedproperties.entity.Document; +import com.baeldung.unmappedproperties.mapper.DocumentMapperWithConfig; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Date; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DocumentMapperWithConfigUnitTest { + + @Test + public void givenDocumentEntityToDocumentDto_whenMaps_thenCorrect() { + Document entity = new Document(); + entity.setId(1); + entity.setTitle("Price 13-42"); + entity.setText("List of positions......."); + entity.setModificationTime(new Date()); + + DocumentDTO dto = DocumentMapperWithConfig.INSTANCE.documentToDocumentDTO(entity); + + assertThat(dto.getId()).isEqualTo(entity.getId()); + assertThat(dto.getTitle()).isEqualTo(entity.getTitle()); + assertThat(dto.getText()).isEqualTo(entity.getText()); + } + + @Test + public void givenDocumentDtoToDocumentEntity_whenMaps_thenCorrect() { + DocumentDTO dto = new DocumentDTO(); + dto.setId(1); + dto.setTitle("Price 13-42"); + dto.setText("List of positions......."); + dto.setComments(Arrays.asList("Not all positions", "Wrong price values")); + dto.setAuthor("Author1"); + + Document entity = DocumentMapperWithConfig.INSTANCE.documentDTOToDocument(dto); + + assertThat(entity.getId()).isEqualTo(dto.getId()); + assertThat(entity.getTitle()).isEqualTo(dto.getTitle()); + assertThat(entity.getText()).isEqualTo(dto.getText()); + } +} \ No newline at end of file