-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8cf8686
commit 87e0039
Showing
3 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...r/src/main/java/io/flexwork/modules/usermanagement/service/mapper/OrganizationMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,31 @@ | ||
package io.flexwork.modules.usermanagement.service.mapper; | ||
|
||
import io.flexwork.modules.usermanagement.domain.Organization; | ||
import io.flexwork.modules.usermanagement.domain.Team; | ||
import io.flexwork.modules.usermanagement.service.dto.OrganizationDTO; | ||
import io.flexwork.modules.usermanagement.service.dto.TeamDTO; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface OrganizationMapper { | ||
|
||
@Mapping(target = "teams", expression = "java(mapTeamsForIdOnly(organization.getTeams()))") | ||
OrganizationDTO organizationToOrganizationDTO(Organization organization); | ||
|
||
Organization organizationDTOToOrganization(OrganizationDTO organizationDTO); | ||
|
||
// Custom method to map only the 'id' field from Team to TeamDTO | ||
default Set<TeamDTO> mapTeamsForIdOnly(Set<Team> teams) { | ||
return teams.stream() | ||
.map( | ||
team -> | ||
TeamDTO.builder() | ||
.id(team.getId()) | ||
.build()) // Map only the 'id' field, set other fields as | ||
// null | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...c/test/java/io/flexwork/modules/usermanagement/service/mapper/OrganizationMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.flexwork.modules.usermanagement.service.mapper; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import io.flexwork.modules.usermanagement.domain.Organization; | ||
import io.flexwork.modules.usermanagement.domain.Team; | ||
import io.flexwork.modules.usermanagement.service.dto.OrganizationDTO; | ||
import java.util.Set; | ||
import org.junit.jupiter.api.Test; | ||
import org.mapstruct.factory.Mappers; | ||
|
||
public class OrganizationMapperTest { | ||
private OrganizationMapper organizationMapper = Mappers.getMapper(OrganizationMapper.class); | ||
|
||
@Test | ||
void testOrganizationToOrganizationDTO() { | ||
Organization organization = | ||
Organization.builder() | ||
.id(1L) | ||
.description("description") | ||
.teams( | ||
Set.of( | ||
Team.builder().id(1L).name("Team1").build(), | ||
Team.builder().id(2L).name("Team2").build())) | ||
.build(); | ||
OrganizationDTO organizationDTO = | ||
organizationMapper.organizationToOrganizationDTO(organization); | ||
assertAll( | ||
() -> assertEquals(organization.getId(), organizationDTO.getId()), | ||
() -> assertEquals(organization.getDescription(), organizationDTO.getDescription()), | ||
() -> assertEquals(2, organizationDTO.getTeams().size()), | ||
() -> | ||
assertThat(organizationDTO.getTeams()) | ||
.extracting("id") | ||
.containsExactly(1L, 2L)); | ||
} | ||
} |