这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class LicenseDto {

private LocalDateTime endDate;

private String licenseType;

public UUID getId() {
return id;
}
Expand All @@ -35,4 +37,12 @@ public void setEndDate(LocalDateTime endDate) {
this.endDate = endDate;
}

public String getLicenseType() {
return licenseType;
}

public void setLicenseType(String licenseType) {
this.licenseType = licenseType;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.time.ZoneOffset;

import org.mapstruct.AfterMapping;
import org.mapstruct.Condition;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
Expand Down Expand Up @@ -40,4 +41,13 @@ default boolean isEndDateInTwoWeeks(LicenseDto licenseDto) {
.toDays() <= 14;
}

}
@Condition
default boolean mapsToExpectedLicenseType(String licenseType) {
try {
return licenseType != null && License.LicenseType.valueOf(licenseType) != null;
} catch (IllegalArgumentException e) {
return false;
}
}

}
17 changes: 15 additions & 2 deletions mapstruct/src/main/java/com/baeldung/expression/model/License.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.time.OffsetDateTime;
import java.util.UUID;


public class License {

private UUID id;
Expand All @@ -16,6 +15,8 @@ public class License {

private boolean renewalRequired;

private LicenseType licenseType;

public UUID getId() {
return id;
}
Expand Down Expand Up @@ -55,4 +56,16 @@ public boolean isRenewalRequired() {
public void setRenewalRequired(boolean renewalRequired) {
this.renewalRequired = renewalRequired;
}
}

public enum LicenseType {
INDIVIDUAL, FAMILY
}

public LicenseType getLicenseType() {
return licenseType;
}

public void setLicenseType(LicenseType licenseType) {
this.licenseType = licenseType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.time.LocalDateTime;
import java.util.UUID;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;

Expand Down Expand Up @@ -74,4 +75,31 @@ void givenLicenseDtoWithoutId_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopul
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);
}

}