Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: DBC and HeeUser requests from TSS #252

Merged
merged 11 commits into from
Jul 12, 2024
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>
<groupId>uk.nhs.tis</groupId>
<artifactId>sync</artifactId>
<version>1.22.0</version>
<version>1.23.0</version>
<packaging>jar</packaging>
<name>sync</name>
<description>Separate Microservice for synchronisation</description>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/uk/nhs/tis/sync/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

@SpringBootApplication
@ComponentScan(basePackages = {"com.transformuk.hee.tis.tcs",
"uk.nhs.tis.sync", "com.transformuk.hee.tis.reference.client"})
"uk.nhs.tis.sync", "com.transformuk.hee.tis.reference.client",
"com.transformuk.hee.tis.profile.client"})
@EnableWebMvc
@EnableSpringDataWebSupport
@PropertySource({"classpath:/config/application.properties",
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/uk/nhs/tis/sync/dto/DmsDtoType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package uk.nhs.tis.sync.dto;

import com.transformuk.hee.tis.profile.service.dto.HeeUserDTO;
import com.transformuk.hee.tis.reference.api.dto.DBCDTO;
import com.transformuk.hee.tis.reference.api.dto.GradeDTO;
import com.transformuk.hee.tis.reference.api.dto.SiteDTO;
import com.transformuk.hee.tis.reference.api.dto.TrustDTO;
Expand All @@ -22,6 +24,7 @@
import uk.nhs.tis.sync.mapper.CurriculumMembershipMapper;
import uk.nhs.tis.sync.mapper.DmsMapper;
import uk.nhs.tis.sync.mapper.GradeMapper;
import uk.nhs.tis.sync.mapper.HeeUserMapper;
import uk.nhs.tis.sync.mapper.PersonMapper;
import uk.nhs.tis.sync.mapper.PlacementSpecialtyMapper;
import uk.nhs.tis.sync.mapper.PlacementSummaryMapper;
Expand All @@ -45,9 +48,11 @@ public enum DmsDtoType {
CURRICULUM(CurriculumDTO.class, "tcs", "Curriculum", CurriculumMapper.class),
CURRICULUM_MEMBERSHIP(CurriculumMembershipWrapperDto.class, "tcs", "CurriculumMembership",
CurriculumMembershipMapper.class),
DBC_DETAILS(DBCDTO.class, "reference", "DBC", null),
GDC_DETAILS(GdcDetailsDTO.class, "tcs", "GdcDetails", null),
GMC_DETAILS(GmcDetailsDTO.class, "tcs", "GmcDetails", null),
GRADE(GradeDTO.class, "reference", "Grade", GradeMapper.class),
HEE_USER(HeeUserDTO.class, "auth", "HeeUser", HeeUserMapper.class),
PERSON(PersonDTO.class, "tcs", "Person", PersonMapper.class),
PERSONAL_DETAILS(PersonalDetailsDTO.class, "tcs", "PersonalDetails", null),
PLACEMENT_DETAILS(PlacementSummaryDTO.class, "tcs", "Placement", PlacementSummaryMapper.class),
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/uk/nhs/tis/sync/dto/HeeUserDmsDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package uk.nhs.tis.sync.dto;

import lombok.Data;

/**
* A DTO for transferring HeeUser data to the DMS.
*/
@Data
public class HeeUserDmsDto {
private final String name;
private final String firstName;
private final String lastName;
private final String gmcId;
private final String phoneNumber;
private final String emailAddress;
private final String active;
}
25 changes: 25 additions & 0 deletions src/main/java/uk/nhs/tis/sync/mapper/HeeUserMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package uk.nhs.tis.sync.mapper;

import com.transformuk.hee.tis.profile.service.dto.HeeUserDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import uk.nhs.tis.sync.dto.HeeUserDmsDto;

/**
* A mapper to map between Profile and DMS DTOs for the HeeUser data type.
*/
@Mapper(componentModel = "spring")
public interface HeeUserMapper extends DmsMapper<HeeUserDTO, HeeUserDmsDto> {

//note that password, roles, etc. in the HeeUserDTO are excluded from HeeUserDmsDto: we simply
//map the 'base' record fields
@Mapping(target = "active", source = "active",
qualifiedByName = "getBooleanAsZeroOrOne")
HeeUserDmsDto toDmsDto(HeeUserDTO heeUserDto);

@Named("getBooleanAsZeroOrOne")
default String getBooleanAsZeroOrOne(boolean bool) {
return bool ? "1" : "0";
}
}
22 changes: 21 additions & 1 deletion src/main/java/uk/nhs/tis/sync/service/DataRequestService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package uk.nhs.tis.sync.service;

import com.transformuk.hee.tis.profile.client.service.impl.ProfileServiceImpl;
import com.transformuk.hee.tis.reference.api.dto.DBCDTO;
import com.transformuk.hee.tis.reference.client.impl.ReferenceServiceImpl;
import com.transformuk.hee.tis.tcs.api.dto.PersonDTO;
import com.transformuk.hee.tis.tcs.api.dto.PlacementDetailsDTO;
Expand All @@ -15,6 +17,7 @@
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import uk.nhs.tis.sync.dto.CurriculumMembershipWrapperDto;

Expand All @@ -24,7 +27,9 @@ public class DataRequestService {

private static final String TABLE_CURRICULUM = "Curriculum";
private static final String TABLE_CURRICULUM_MEMBERSHIP = "CurriculumMembership";
private static final String TABLE_DBC = "DBC";
private static final String TABLE_GRADE = "Grade";
private static final String TABLE_HEE_USER = "HeeUser";
private static final String TABLE_PERSON = "Person";
private static final String TABLE_PLACEMENT = "Placement";
private static final String TABLE_PLACEMENT_SPECIALTY = "PlacementSpecialty";
Expand All @@ -39,9 +44,13 @@ public class DataRequestService {

private final ReferenceServiceImpl referenceServiceImpl;

DataRequestService(TcsServiceImpl tcsServiceImpl, ReferenceServiceImpl referenceServiceImpl) {
private final ProfileServiceImpl profileServiceImpl;

DataRequestService(TcsServiceImpl tcsServiceImpl, ReferenceServiceImpl referenceServiceImpl,
ProfileServiceImpl profileServiceImpl) {
this.tcsServiceImpl = tcsServiceImpl;
this.referenceServiceImpl = referenceServiceImpl;
this.profileServiceImpl = profileServiceImpl;
}

/**
Expand Down Expand Up @@ -74,6 +83,17 @@ public List<Object> retrieveDtos(Map<String, String> message) {
.collect(Collectors.toList());
}

if (table.equals(TABLE_HEE_USER) && message.containsKey("name")) {
String name = message.get("name");
return createNonNullList(profileServiceImpl.getSingleAdminUser(name));
}

if (table.equals(TABLE_DBC) && message.containsKey("dbc")) {
String dbc = message.get("dbc");
ResponseEntity<DBCDTO> responseEntity = referenceServiceImpl.getDBCByCode(dbc);
return createNonNullList(responseEntity.getBody());
}

if (message.containsKey("id")) {
long id = Long.parseLong(message.get("id"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
import org.mapstruct.factory.Mappers;
Expand Down
72 changes: 72 additions & 0 deletions src/test/java/uk/nhs/tis/sync/mapper/HeeUserMapperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package uk.nhs.tis.sync.mapper;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.transformuk.hee.tis.profile.dto.RoleDTO;
import com.transformuk.hee.tis.profile.service.dto.HeeUserDTO;
import com.transformuk.hee.tis.profile.service.dto.UserProgrammeDTO;
import com.transformuk.hee.tis.profile.service.dto.UserTrustDTO;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
JosephKelly marked this conversation as resolved.
Show resolved Hide resolved
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import uk.nhs.tis.sync.dto.HeeUserDmsDto;

class HeeUserMapperTest {

private HeeUserMapper mapper;

private HeeUserDTO heeUserDto;

@BeforeEach
public void setUp() {
mapper = new HeeUserMapperImpl();

heeUserDto = new HeeUserDTO();
heeUserDto.setActive(true);
HashSet<UserProgrammeDTO> assocProgrammes = new HashSet<>();
assocProgrammes.add(new UserProgrammeDTO());
heeUserDto.setAssociatedProgrammes(assocProgrammes);
ReubenRobertsHEE marked this conversation as resolved.
Show resolved Hide resolved
HashSet<UserTrustDTO> userTrusts = new HashSet<>();
userTrusts.add(new UserTrustDTO());
ReubenRobertsHEE marked this conversation as resolved.
Show resolved Hide resolved
heeUserDto.setAssociatedTrusts(userTrusts);
heeUserDto.setFirstName("first name");
heeUserDto.setName("name");
Set<String> dbcs = new HashSet<>();
dbcs.add("dbc1");
heeUserDto.setDesignatedBodyCodes(dbcs);
ReubenRobertsHEE marked this conversation as resolved.
Show resolved Hide resolved
heeUserDto.setEmailAddress("email");
heeUserDto.setGmcId("gmc");
heeUserDto.setLastName("last name");
heeUserDto.setPassword("password");
heeUserDto.setPhoneNumber("phone");
HashSet<RoleDTO> roles = new HashSet<>();
roles.add(new RoleDTO());
heeUserDto.setRoles(roles);
ReubenRobertsHEE marked this conversation as resolved.
Show resolved Hide resolved
heeUserDto.setTemporaryPassword(true);
}

@Test
public void shouldMapAHeeUserDtoToADataDmsDto() {
HeeUserDmsDto heeUserDmsDto = mapper.toDmsDto(heeUserDto);

assertEquals("1", heeUserDmsDto.getActive());
assertEquals("name", heeUserDmsDto.getName());
assertEquals("first name", heeUserDmsDto.getFirstName());
assertEquals("gmc", heeUserDmsDto.getGmcId());
assertEquals("last name", heeUserDmsDto.getLastName());
assertEquals("email", heeUserDmsDto.getEmailAddress());
assertEquals("phone", heeUserDmsDto.getPhoneNumber());
//other HeeUserDto properties are ignored
}

@ParameterizedTest
@ValueSource(booleans = {false, true})
void shouldMapBooleansToZeroOrOneString(boolean bool) {
heeUserDto.setActive(bool);
HeeUserDmsDto heeUserDmsDto = mapper.toDmsDto(heeUserDto);
assertEquals(bool ? "1" : "0", heeUserDmsDto.getActive());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import com.transformuk.hee.tis.profile.client.service.impl.ProfileServiceImpl;
import com.transformuk.hee.tis.profile.service.dto.HeeUserDTO;
import com.transformuk.hee.tis.reference.api.dto.DBCDTO;
import com.transformuk.hee.tis.reference.api.dto.GradeDTO;
import com.transformuk.hee.tis.reference.api.dto.SiteDTO;
import com.transformuk.hee.tis.reference.api.dto.TrustDTO;
Expand Down Expand Up @@ -44,13 +47,16 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import uk.nhs.tis.sync.dto.CurriculumMembershipWrapperDto;

class DataRequestServiceTest {

private static final String FORENAMES = "Joe";
private static final String SURNAME = "Bloggs";
private static final String HEE_USER_NAME = "the user name";
private static final String DBC_VALUE = "theDBC";

private static final String GDC_NUMBER = "gdc123";
private static final String GMC_NUMBER = "gmc123";
Expand All @@ -61,11 +67,14 @@ class DataRequestServiceTest {

private ReferenceServiceImpl referenceService;

private ProfileServiceImpl profileService;

@BeforeEach
void setUp() {
tcsService = mock(TcsServiceImpl.class);
referenceService = mock(ReferenceServiceImpl.class);
service = new DataRequestService(tcsService, referenceService);
profileService = mock(ProfileServiceImpl.class);
service = new DataRequestService(tcsService, referenceService, profileService);
}

@Test
Expand Down Expand Up @@ -791,4 +800,86 @@ void shouldReturnEmptyWhenFindGradesIdInThrowsException() {

assertThat("Unexpected DTO count.", grades.size(), is(0));
}

@Test
void shouldReturnHeeUserWhenHeeUserFound() {
HeeUserDTO expectedDto = new HeeUserDTO();
when(profileService.getSingleAdminUser(HEE_USER_NAME)).thenReturn(expectedDto);

Map<String, String> message = new HashMap<String, String>() {{
put("table", "HeeUser");
put("name", HEE_USER_NAME);
}};
List<Object> retrievedDtos = service.retrieveDtos(message);

assertThat("Unexpected DTO count.", retrievedDtos.size(), is(1));
assertThat("Unexpected DTO.", retrievedDtos.get(0), sameInstance(expectedDto));
}

@Test
void shouldReturnEmptyWhenHeeUserNotFound() {
when(profileService.getSingleAdminUser(HEE_USER_NAME)).thenReturn(null);

Map<String, String> message = new HashMap<String, String>() {{
put("table", "HeeUser");
put("name", HEE_USER_NAME);
}};
List<Object> heeUsers = service.retrieveDtos(message);

assertThat("Unexpected DTO count.", heeUsers.size(), is(0));
}

@Test
void shouldReturnEmptyWhenHeeUserMessageHasWrongKey() {
Map<String, String> message = new HashMap<String, String>() {{
put("table", "HeeUser");
put("another key", HEE_USER_NAME);
}};
List<Object> heeUsers = service.retrieveDtos(message);

assertThat("Unexpected DTO count.", heeUsers.size(), is(0));
verifyNoInteractions(profileService);
}

@Test
void shouldReturnDbcWhenDbcFound() {
DBCDTO expectedDto = new DBCDTO();
ResponseEntity<DBCDTO> responseEntity = new ResponseEntity<>(expectedDto, HttpStatus.OK);
when(referenceService.getDBCByCode(DBC_VALUE)).thenReturn(responseEntity);

Map<String, String> message = new HashMap<String, String>() {{
put("table", "DBC");
put("dbc", DBC_VALUE);
}};
List<Object> retrievedDtos = service.retrieveDtos(message);

assertThat("Unexpected DTO count.", retrievedDtos.size(), is(1));
assertThat("Unexpected DTO.", retrievedDtos.get(0), sameInstance(expectedDto));
}

@Test
void shouldReturnEmptyWhenDbcNotFound() {
ResponseEntity<DBCDTO> responseEntity = new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
when(referenceService.getDBCByCode(DBC_VALUE)).thenReturn(responseEntity);

Map<String, String> message = new HashMap<String, String>() {{
put("table", "DBC");
put("dbc", DBC_VALUE);
}};
List<Object> dbcs = service.retrieveDtos(message);

assertThat("Unexpected DTO count.", dbcs.size(), is(0));
}

@Test
void shouldReturnEmptyWhenDbcMessageHasWrongKey() {
Map<String, String> message = new HashMap<String, String>() {{
put("table", "DBC");
put("another key", DBC_VALUE);
}};
List<Object> dbcs = service.retrieveDtos(message);

assertThat("Unexpected DTO count.", dbcs.size(), is(0));
verifyNoInteractions(referenceService);
}
}
Loading