From 801bdd95d8b70d1039ece085abc7d43c98d73a1d Mon Sep 17 00:00:00 2001 From: balasr3 Date: Sat, 19 Aug 2023 07:22:33 +0100 Subject: [PATCH 1/5] BAEL-6572: Added examples and test for conditional mapping using mapstruct --- .../baeldung/expression/dto/LicenseDto.java | 21 ++++ .../expression/mapper/LicenseMapper.java | 43 ++++++++ .../baeldung/expression/model/License.java | 21 ++++ .../mapper/LicenseMapperUnitTest.java | 97 +++++++++++++++++++ 4 files changed, 182 insertions(+) create mode 100644 mapstruct/src/main/java/com/baeldung/expression/dto/LicenseDto.java create mode 100644 mapstruct/src/main/java/com/baeldung/expression/mapper/LicenseMapper.java create mode 100644 mapstruct/src/main/java/com/baeldung/expression/model/License.java create mode 100644 mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java diff --git a/mapstruct/src/main/java/com/baeldung/expression/dto/LicenseDto.java b/mapstruct/src/main/java/com/baeldung/expression/dto/LicenseDto.java new file mode 100644 index 0000000000..cd6e9bad68 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/expression/dto/LicenseDto.java @@ -0,0 +1,21 @@ +package com.baeldung.expression.dto; + +import java.time.LocalDateTime; +import java.util.UUID; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +@AllArgsConstructor +@Data +@Builder +public class LicenseDto { + + private UUID id; + + private LocalDateTime startDate; + + private LocalDateTime endDate; + +} diff --git a/mapstruct/src/main/java/com/baeldung/expression/mapper/LicenseMapper.java b/mapstruct/src/main/java/com/baeldung/expression/mapper/LicenseMapper.java new file mode 100644 index 0000000000..100588b45d --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/expression/mapper/LicenseMapper.java @@ -0,0 +1,43 @@ +package com.baeldung.expression.mapper; + +import java.time.Duration; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; + +import org.mapstruct.AfterMapping; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.MappingTarget; + +import com.baeldung.expression.dto.LicenseDto; +import com.baeldung.expression.model.License; + +@Mapper +public interface LicenseMapper { + + @Mapping(target = "startDate", expression = "java(mapStartDate(licenseDto))") + @Mapping(target = "endDate", ignore = true) + @Mapping(target = "active", constant = "true") + @Mapping(target = "renewalRequired", conditionExpression = "java(isEndDateInTwoWeeks(licenseDto))", source = ".") + License toLicense(LicenseDto licenseDto); + + @AfterMapping + default void afterMapping(LicenseDto licenseDto, @MappingTarget License license) { + OffsetDateTime endDate = licenseDto.getEndDate() != null ? licenseDto.getEndDate() + .atOffset(ZoneOffset.UTC) : OffsetDateTime.now() + .plusYears(1); + license.setEndDate(endDate); + } + + default OffsetDateTime mapStartDate(LicenseDto licenseDto) { + return licenseDto.getStartDate() != null ? licenseDto.getStartDate() + .atOffset(ZoneOffset.UTC) : OffsetDateTime.now(); + } + + default boolean isEndDateInTwoWeeks(LicenseDto licenseDto) { + return licenseDto.getEndDate() != null && Duration.between(licenseDto.getEndDate(), LocalDateTime.now()) + .toDays() <= 14; + } + +} diff --git a/mapstruct/src/main/java/com/baeldung/expression/model/License.java b/mapstruct/src/main/java/com/baeldung/expression/model/License.java new file mode 100644 index 0000000000..9e87be03d4 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/expression/model/License.java @@ -0,0 +1,21 @@ +package com.baeldung.expression.model; + +import java.time.OffsetDateTime; +import java.util.UUID; + +import lombok.Data; + +@Data +public class License { + + private UUID id; + + private OffsetDateTime startDate; + + private OffsetDateTime endDate; + + private boolean active; + + private boolean renewalRequired; + +} diff --git a/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java new file mode 100644 index 0000000000..7d33f0185c --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java @@ -0,0 +1,97 @@ +package com.baeldung.expression.mapper; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.LocalDate; +import java.time.LocalDateTime; + +import org.junit.jupiter.api.Test; +import org.mapstruct.factory.Mappers; + +import com.baeldung.expression.dto.LicenseDto; +import com.baeldung.expression.model.License; + +class LicenseMapperUnitTest { + + LicenseMapper licenseMapper = Mappers.getMapper(LicenseMapper.class); + + @Test + void givenLicenseDtoWithStartDateAndWithoutEndDate_ThenLicenseShouldBePopulatedWithDefaultEndDate() { + License license = licenseMapper.toLicense(LicenseDto.builder() + .startDate(LocalDateTime.now()) + .build()); + assertThat(license).isNotNull() + .satisfies(l -> { + assertThat(l.getStartDate() + .toLocalDate()).isEqualTo(LocalDate.now()); + assertThat(l.getEndDate() + .toLocalDate()).isEqualTo(LocalDate.now() + .plusYears(1)); + }); + } + + @Test + void givenLicenseDtoWithEndDateAndWithoutStartDate_ThenLicenseShouldBePopulatedWithDefaultStartDate() { + License license = licenseMapper.toLicense(LicenseDto.builder() + .endDate(LocalDateTime.now() + .plusYears(2)) + .build()); + assertThat(license).isNotNull() + .satisfies(l -> { + assertThat(l.getStartDate() + .toLocalDate()).isEqualTo(LocalDate.now()); + assertThat(l.getEndDate() + .toLocalDate()).isEqualTo(LocalDate.now() + .plusYears(2)); + }); + } + + @Test + void givenLicenseDtoWithoutEndDateAndWithoutStartDate_ThenLicenseShouldBePopulatedWithDefaultStartDateAndEndDate() { + License license = licenseMapper.toLicense(LicenseDto.builder() + .build()); + assertThat(license).isNotNull() + .satisfies(l -> { + assertThat(l.getStartDate() + .toLocalDate()).isEqualTo(LocalDate.now()); + assertThat(l.getEndDate() + .toLocalDate()).isEqualTo(LocalDate.now() + .plusYears(1)); + }); + } + + @Test + void givenLicenseDtoWithoutStartDateAndEndDate_ThenLicenseShouldBePopulatedWithDefaultDetails() { + License license = licenseMapper.toLicense(LicenseDto.builder() + .build()); + assertThat(license).isNotNull() + .satisfies(l -> { + assertThat(l.getStartDate() + .toLocalDate()).isEqualTo(LocalDate.now()); + assertThat(l.getEndDate() + .toLocalDate()).isEqualTo(LocalDate.now() + .plusYears(1)); + assertThat(l.isActive()).isTrue(); + assertThat(l.isRenewalRequired()).isFalse(); + }); + } + + @Test + void givenLicenseDtoWithEndDateInTwoWeeks_ThenLicenseShouldBePopulatedWithReminderSetToTrue() { + License license = licenseMapper.toLicense(LicenseDto.builder() + .endDate(LocalDateTime.now() + .plusDays(10)) + .build()); + assertThat(license).isNotNull() + .satisfies(l -> { + assertThat(l.getStartDate() + .toLocalDate()).isEqualTo(LocalDate.now()); + assertThat(l.getEndDate() + .toLocalDate()).isEqualTo(LocalDate.now() + .plusDays(10)); + assertThat(l.isActive()).isTrue(); + assertThat(l.isRenewalRequired()).isTrue(); + }); + } + +} \ No newline at end of file From 31682be3e3e5f9871f642d398ca561bed0beb7c6 Mon Sep 17 00:00:00 2001 From: balasr3 Date: Sat, 19 Aug 2023 14:40:07 +0100 Subject: [PATCH 2/5] BAEL-6572: Modifed test name as per convention --- .../expression/mapper/LicenseMapperUnitTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java index 7d33f0185c..b741c45e4e 100644 --- a/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java +++ b/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java @@ -16,7 +16,7 @@ class LicenseMapperUnitTest { LicenseMapper licenseMapper = Mappers.getMapper(LicenseMapper.class); @Test - void givenLicenseDtoWithStartDateAndWithoutEndDate_ThenLicenseShouldBePopulatedWithDefaultEndDate() { + void givenLicenseDtoWithStartDateAndWithoutEndDate_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithDefaultEndDate() { License license = licenseMapper.toLicense(LicenseDto.builder() .startDate(LocalDateTime.now()) .build()); @@ -31,7 +31,7 @@ class LicenseMapperUnitTest { } @Test - void givenLicenseDtoWithEndDateAndWithoutStartDate_ThenLicenseShouldBePopulatedWithDefaultStartDate() { + void givenLicenseDtoWithEndDateAndWithoutStartDate_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithDefaultStartDate() { License license = licenseMapper.toLicense(LicenseDto.builder() .endDate(LocalDateTime.now() .plusYears(2)) @@ -47,7 +47,7 @@ class LicenseMapperUnitTest { } @Test - void givenLicenseDtoWithoutEndDateAndWithoutStartDate_ThenLicenseShouldBePopulatedWithDefaultStartDateAndEndDate() { + void givenLicenseDtoWithoutEndDateAndWithoutStartDate_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithDefaultStartDateAndEndDate() { License license = licenseMapper.toLicense(LicenseDto.builder() .build()); assertThat(license).isNotNull() @@ -61,7 +61,7 @@ class LicenseMapperUnitTest { } @Test - void givenLicenseDtoWithoutStartDateAndEndDate_ThenLicenseShouldBePopulatedWithDefaultDetails() { + void givenLicenseDtoWithoutStartDateAndEndDate_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithDefaultDetails() { License license = licenseMapper.toLicense(LicenseDto.builder() .build()); assertThat(license).isNotNull() @@ -77,7 +77,7 @@ class LicenseMapperUnitTest { } @Test - void givenLicenseDtoWithEndDateInTwoWeeks_ThenLicenseShouldBePopulatedWithReminderSetToTrue() { + void givenLicenseDtoWithEndDateInTwoWeeks_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithReminderSetToTrue() { License license = licenseMapper.toLicense(LicenseDto.builder() .endDate(LocalDateTime.now() .plusDays(10)) From eb4a1d040e8aabd9e44d716f8d0b306709c6bc81 Mon Sep 17 00:00:00 2001 From: balasr3 Date: Tue, 22 Aug 2023 20:59:17 +0100 Subject: [PATCH 3/5] BAEL-6572: Modified test based on review comments --- .../mapper/LicenseMapperUnitTest.java | 51 ++++++------------- 1 file changed, 16 insertions(+), 35 deletions(-) diff --git a/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java index b741c45e4e..38bf940325 100644 --- a/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java +++ b/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java @@ -20,14 +20,10 @@ class LicenseMapperUnitTest { License license = licenseMapper.toLicense(LicenseDto.builder() .startDate(LocalDateTime.now()) .build()); - assertThat(license).isNotNull() - .satisfies(l -> { - assertThat(l.getStartDate() - .toLocalDate()).isEqualTo(LocalDate.now()); - assertThat(l.getEndDate() - .toLocalDate()).isEqualTo(LocalDate.now() - .plusYears(1)); - }); + assertThat(license).isNotNull(); + assertThat(license.getEndDate() + .toLocalDate()).isEqualTo(LocalDate.now() + .plusYears(1)); } @Test @@ -36,28 +32,21 @@ class LicenseMapperUnitTest { .endDate(LocalDateTime.now() .plusYears(2)) .build()); - assertThat(license).isNotNull() - .satisfies(l -> { - assertThat(l.getStartDate() - .toLocalDate()).isEqualTo(LocalDate.now()); - assertThat(l.getEndDate() - .toLocalDate()).isEqualTo(LocalDate.now() - .plusYears(2)); - }); + assertThat(license).isNotNull(); + assertThat(license.getStartDate() + .toLocalDate()).isEqualTo(LocalDate.now()); } @Test void givenLicenseDtoWithoutEndDateAndWithoutStartDate_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithDefaultStartDateAndEndDate() { License license = licenseMapper.toLicense(LicenseDto.builder() .build()); - assertThat(license).isNotNull() - .satisfies(l -> { - assertThat(l.getStartDate() - .toLocalDate()).isEqualTo(LocalDate.now()); - assertThat(l.getEndDate() - .toLocalDate()).isEqualTo(LocalDate.now() - .plusYears(1)); - }); + assertThat(license).isNotNull(); + assertThat(license.getStartDate() + .toLocalDate()).isEqualTo(LocalDate.now()); + assertThat(license.getEndDate() + .toLocalDate()).isEqualTo(LocalDate.now() + .plusYears(1)); } @Test @@ -77,21 +66,13 @@ class LicenseMapperUnitTest { } @Test - void givenLicenseDtoWithEndDateInTwoWeeks_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithReminderSetToTrue() { + void givenLicenseDtoWithEndDateInTwoWeeks_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithRenewalRequiredSetToTrue() { License license = licenseMapper.toLicense(LicenseDto.builder() .endDate(LocalDateTime.now() .plusDays(10)) .build()); - assertThat(license).isNotNull() - .satisfies(l -> { - assertThat(l.getStartDate() - .toLocalDate()).isEqualTo(LocalDate.now()); - assertThat(l.getEndDate() - .toLocalDate()).isEqualTo(LocalDate.now() - .plusDays(10)); - assertThat(l.isActive()).isTrue(); - assertThat(l.isRenewalRequired()).isTrue(); - }); + assertThat(license).isNotNull(); + assertThat(license.isRenewalRequired()).isTrue(); } } \ No newline at end of file From 1b1dab20ff026037c5c04ebed020803c8c0048a8 Mon Sep 17 00:00:00 2001 From: balasr3 Date: Tue, 22 Aug 2023 21:07:19 +0100 Subject: [PATCH 4/5] BAEL-6572: Modified test based on review comments --- .../expression/mapper/LicenseMapperUnitTest.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java index 38bf940325..947e6a33c2 100644 --- a/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java +++ b/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java @@ -37,18 +37,6 @@ class LicenseMapperUnitTest { .toLocalDate()).isEqualTo(LocalDate.now()); } - @Test - void givenLicenseDtoWithoutEndDateAndWithoutStartDate_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithDefaultStartDateAndEndDate() { - License license = licenseMapper.toLicense(LicenseDto.builder() - .build()); - assertThat(license).isNotNull(); - assertThat(license.getStartDate() - .toLocalDate()).isEqualTo(LocalDate.now()); - assertThat(license.getEndDate() - .toLocalDate()).isEqualTo(LocalDate.now() - .plusYears(1)); - } - @Test void givenLicenseDtoWithoutStartDateAndEndDate_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithDefaultDetails() { License license = licenseMapper.toLicense(LicenseDto.builder() From a60c2eb94441615b9c428361f6327827b83f44ff Mon Sep 17 00:00:00 2001 From: balasr3 Date: Wed, 23 Aug 2023 07:08:49 +0100 Subject: [PATCH 5/5] BAEL-6572: Modified test based on review comments --- .../mapper/LicenseMapperUnitTest.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java index 947e6a33c2..98f704e61c 100644 --- a/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java +++ b/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java @@ -41,16 +41,14 @@ class LicenseMapperUnitTest { void givenLicenseDtoWithoutStartDateAndEndDate_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithDefaultDetails() { License license = licenseMapper.toLicense(LicenseDto.builder() .build()); - assertThat(license).isNotNull() - .satisfies(l -> { - assertThat(l.getStartDate() - .toLocalDate()).isEqualTo(LocalDate.now()); - assertThat(l.getEndDate() - .toLocalDate()).isEqualTo(LocalDate.now() - .plusYears(1)); - assertThat(l.isActive()).isTrue(); - assertThat(l.isRenewalRequired()).isFalse(); - }); + assertThat(license).isNotNull(); + assertThat(license.getStartDate() + .toLocalDate()).isEqualTo(LocalDate.now()); + assertThat(license.getEndDate() + .toLocalDate()).isEqualTo(LocalDate.now() + .plusYears(1)); + assertThat(license.isActive()).isTrue(); + assertThat(license.isRenewalRequired()).isFalse(); } @Test