Merge pull request #14888 from balasr21/BAEL-7032-MapStruct

BAEL-7032: Improvement to conditional mapping by using @Condition
This commit is contained in:
davidmartinezbarua 2023-10-09 13:32:56 -03:00 committed by GitHub
commit c3bcd6ec1e
4 changed files with 64 additions and 3 deletions

View File

@ -11,6 +11,8 @@ public class LicenseDto {
private LocalDateTime endDate; private LocalDateTime endDate;
private String licenseType;
public UUID getId() { public UUID getId() {
return id; return id;
} }
@ -35,4 +37,12 @@ public class LicenseDto {
this.endDate = endDate; this.endDate = endDate;
} }
public String getLicenseType() {
return licenseType;
}
public void setLicenseType(String licenseType) {
this.licenseType = licenseType;
}
} }

View File

@ -6,6 +6,7 @@ import java.time.OffsetDateTime;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import org.mapstruct.AfterMapping; import org.mapstruct.AfterMapping;
import org.mapstruct.Condition;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mapping; import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget; import org.mapstruct.MappingTarget;
@ -40,4 +41,13 @@ public interface LicenseMapper {
.toDays() <= 14; .toDays() <= 14;
} }
} @Condition
default boolean mapsToExpectedLicenseType(String licenseType) {
try {
return licenseType != null && License.LicenseType.valueOf(licenseType) != null;
} catch (IllegalArgumentException e) {
return false;
}
}
}

View File

@ -3,7 +3,6 @@ package com.baeldung.expression.model;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.util.UUID; import java.util.UUID;
public class License { public class License {
private UUID id; private UUID id;
@ -16,6 +15,8 @@ public class License {
private boolean renewalRequired; private boolean renewalRequired;
private LicenseType licenseType;
public UUID getId() { public UUID getId() {
return id; return id;
} }
@ -55,4 +56,16 @@ public class License {
public void setRenewalRequired(boolean renewalRequired) { public void setRenewalRequired(boolean renewalRequired) {
this.renewalRequired = renewalRequired; this.renewalRequired = renewalRequired;
} }
}
public enum LicenseType {
INDIVIDUAL, FAMILY
}
public LicenseType getLicenseType() {
return licenseType;
}
public void setLicenseType(LicenseType licenseType) {
this.licenseType = licenseType;
}
}

View File

@ -6,6 +6,7 @@ import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.UUID; import java.util.UUID;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@ -74,4 +75,31 @@ class LicenseMapperUnitTest {
assertThat(license.getId()).isSameAs(id); assertThat(license.getId()).isSameAs(id);
} }
@Test
void givenLicenseDtoWithoutLicenseTypeString_whenMapperMethodIsInvoked_thenLicenseShouldBePopulatedWithoutLicenseType() {
LicenseDto licenseDto = new LicenseDto();
License license = licenseMapper.toLicense(licenseDto);
assertThat(license).isNotNull();
Assertions.assertNull(license.getLicenseType());
}
@Test
void givenLicenseDtoWithInvalidLicenseTypeString_whenMapperMethodIsInvoked_thenLicenseShouldBePopulatedWithoutLicenseType() {
LicenseDto licenseDto = new LicenseDto();
licenseDto.setLicenseType("invalid_license_type");
License license = licenseMapper.toLicense(licenseDto);
assertThat(license).isNotNull();
Assertions.assertNull(license.getLicenseType());
}
@Test
void givenLicenseDtoWithValidLicenseTypeString_whenMapperMethodIsInvoked_thenLicenseShouldBePopulatedWithMatchingLicenseType() {
LicenseDto licenseDto = new LicenseDto();
licenseDto.setLicenseType("INDIVIDUAL");
License license = licenseMapper.toLicense(licenseDto);
assertThat(license).isNotNull();
Assertions.assertNotNull(license.getLicenseType());
assertThat(license.getLicenseType()).isEqualTo(License.LicenseType.INDIVIDUAL);
}
} }