Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
haiphucnguyen committed Oct 17, 2024
1 parent 8cf8686 commit 87e0039
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ public class OrganizationDTO {
private String logoUrl; // Logo URL
private String slogan; // Organization slogan
private String description; // Description of the organization
private Set<TeamDTO> teamIds; // Set of team IDs
private Set<UserDTO> employeeIds; // Set of employee (User) IDs
private Set<TeamDTO> teams; // Set of team IDs
}
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());
}
}
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));
}
}

0 comments on commit 87e0039

Please sign in to comment.