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 a1ef4ba4c1
commit 671b53094f
4 changed files with 182 additions and 0 deletions

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