这是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 @@ -15,6 +15,10 @@ public UUID getId() {
return id;
}

public void setId(UUID id) {
this.id = id;
}

public LocalDateTime getStartDate() {
return startDate;
}
Expand Down
41 changes: 39 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,9 +3,7 @@
import java.time.OffsetDateTime;
import java.util.UUID;

import lombok.Data;

@Data
public class License {

private UUID id;
Expand All @@ -18,4 +16,43 @@ public class License {

private boolean renewalRequired;

public UUID getId() {
return id;
}

public void setId(UUID id) {
this.id = id;
}

public OffsetDateTime getStartDate() {
return startDate;
}

public void setStartDate(OffsetDateTime startDate) {
this.startDate = startDate;
}

public OffsetDateTime getEndDate() {
return endDate;
}

public void setEndDate(OffsetDateTime endDate) {
this.endDate = endDate;
}

public boolean isActive() {
return active;
}

public void setActive(boolean active) {
this.active = active;
}

public boolean isRenewalRequired() {
return renewalRequired;
}

public void setRenewalRequired(boolean renewalRequired) {
this.renewalRequired = renewalRequired;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of modifying each existing test, add a new test just for the id field. In that test, set the id in the LicenseDto, then assert the same id is present in the License after mapping.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done but I have just validated that it's not null since the mapping is not based on licenseDto, as per my mapping example its populated with a random UUID as usually this will be decided during persistence. Please let me know if any comments

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which class are you suggesting would have the UUID automatically populated by the persistence layer, both the License and the LicenseDto?

In my mind, I thought that the LicenseDto was (as a Data Transfer Object) was the object that would be persisted, and the License is the domain object that is mapped to/from the LicenseDto.

The flow I would usually expect is:

  1. LicenseDto is saved to persistence layer, including an automatically generated UUID
  2. LicenseDto is later retrieved from the persistence layer
  3. LicenseDto is mapped to domain object License, keeping the same UUID
  4. License might later be modified due to some business rules
  5. License is mapped back to LicenseDto to be saved, keeping the same UUID
  6. LicenseDto is updated in the persistence layer, using the UUID as a key

Did you have something different in mind? What scenario is there when the License and LicenseDto would have a different automatically generated UUID?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@theangrydev Apologies for the confusion, its been a while I wrote this and I thought of something else but now realised mapper is different. Yes you are right, licenseDto is the input and we map it to domain object License so we will get Id from the licenseDto. I have modified the PR accordingly

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.UUID;

import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
Expand Down Expand Up @@ -61,4 +62,16 @@ void givenLicenseDtoWithEndDateInTwoWeeks_WhenMapperMethodIsInvoked_ThenLicenseS
assertThat(license.isRenewalRequired()).isTrue();
}

@Test
void givenLicenseDtoWithoutId_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithValidId() {
LicenseDto licenseDto = new LicenseDto();
UUID id = UUID.randomUUID();
licenseDto.setId(id);
licenseDto.setEndDate(LocalDateTime.now()
.plusDays(10));
License license = licenseMapper.toLicense(licenseDto);
assertThat(license).isNotNull();
assertThat(license.getId()).isSameAs(id);
}

}