BAEL-6572: Added examples and test for conditional mapping using mapstruct

This commit is contained in:
balasr3 2023-08-19 07:22:33 +01:00
parent 5874d1fd15
commit 801bdd95d8
4 changed files with 182 additions and 0 deletions

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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();
});
}
}