Skip to content

Commit

Permalink
chore: tidying up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenRobertsHEE committed Sep 15, 2023
1 parent cbc7f1b commit 0291bcb
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 415 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@

import com.amazonaws.xray.spring.aop.XRayEnabled;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Stream;
import org.springframework.stereotype.Service;
import uk.nhs.hee.trainee.details.dto.enumeration.GoldGuideVersion;
import uk.nhs.hee.trainee.details.mapper.ProgrammeMembershipMapper;
import uk.nhs.hee.trainee.details.model.ConditionsOfJoining;
import uk.nhs.hee.trainee.details.model.Curriculum;
import uk.nhs.hee.trainee.details.model.ProgrammeMembership;
import uk.nhs.hee.trainee.details.model.TraineeProfile;
import uk.nhs.hee.trainee.details.repository.TraineeProfileRepository;
Expand All @@ -46,7 +43,7 @@ public class ProgrammeMembershipService {
private final CachingDelegate cachingDelegate;

ProgrammeMembershipService(TraineeProfileRepository repository,
ProgrammeMembershipMapper mapper, CachingDelegate cachingDelegate) {
ProgrammeMembershipMapper mapper, CachingDelegate cachingDelegate) {
this.repository = repository;
this.mapper = mapper;
this.cachingDelegate = cachingDelegate;
Expand All @@ -60,7 +57,7 @@ public class ProgrammeMembershipService {
* @return The updated programme membership or empty if a trainee with the ID was not found.
*/
public Optional<ProgrammeMembership> updateProgrammeMembershipForTrainee(String traineeTisId,
ProgrammeMembership programmeMembership) {
ProgrammeMembership programmeMembership) {
TraineeProfile traineeProfile = repository.findByTraineeTisId(traineeTisId);

if (traineeProfile == null) {
Expand All @@ -73,43 +70,20 @@ public Optional<ProgrammeMembership> updateProgrammeMembershipForTrainee(String
if (programmeMembership.getConditionsOfJoining() == null
|| programmeMembership.getConditionsOfJoining().signedAt() == null) {

// Restore the Conditions of Joining if it exists
// FIXME: push all PMs through tis-trainee-sync to flush 2 and 3 so they can be removed?
// 3 scenarios:
try {
//1. new uuid PM, with CoJ also saved against this PM *THE FUTURE*
UUID uuid = UUID.fromString(programmeMembership.getTisId());
ProgrammeMembership savedProgrammeMembership
= existingProgrammeMemberships.stream()
.filter(i -> i.getTisId().equals(uuid.toString()))
.findAny()
.orElse(null);
if (savedProgrammeMembership == null
|| savedProgrammeMembership.getConditionsOfJoining() == null) {
//2. new uuid PM, but with CoJ saved against old PM with delimited cm ids *THE PRESENT*
for (Curriculum curriculum : programmeMembership.getCurricula()) {
ProgrammeMembership oldProgrammeMembership
= existingProgrammeMemberships.stream()
.filter(i -> Arrays.stream(i.getTisId().split(","))
.anyMatch(id -> id.equals(curriculum.getTisId())))
.findAny()
.orElse(null);
if (oldProgrammeMembership != null
&& oldProgrammeMembership.getConditionsOfJoining() != null) {
ConditionsOfJoining savedCoj = oldProgrammeMembership.getConditionsOfJoining();
programmeMembership.setConditionsOfJoining(savedCoj);
break;
}
}
}
} catch (IllegalArgumentException e) {
//3. old cm-ids PM, with CoJ cached against old delimited cm ids *THE PAST*
for (String id : programmeMembership.getTisId().split(",")) {
Optional<ConditionsOfJoining> conditionsOfJoiningId
= cachingDelegate.getConditionsOfJoining(id);
conditionsOfJoiningId.ifPresent(programmeMembership::setConditionsOfJoining);
// All results should be the same, but iterating through all IDs ensures a clean cache.
}
// Restore the Conditions of Joining if it exists. This covers the (generally short-term)
// case when a CoJ has just been signed, but the data has not yet made the round-trip to TIS
// and tis-trainee-sync, enriching the incoming programme membership with this information.

UUID uuid = UUID.fromString(programmeMembership.getTisId());
ProgrammeMembership savedProgrammeMembership
= existingProgrammeMemberships.stream()
.filter(i -> i.getTisId().equals(uuid.toString()))
.findAny()
.orElse(null);
if (savedProgrammeMembership != null
&& savedProgrammeMembership.getConditionsOfJoining() != null) {
ConditionsOfJoining savedCoj = savedProgrammeMembership.getConditionsOfJoining();
programmeMembership.setConditionsOfJoining(savedCoj);
}
}

Expand Down Expand Up @@ -146,25 +120,10 @@ public boolean deleteProgrammeMembershipsForTrainee(String traineeTisId) {
existingProgrammeMemberships.stream()
.filter(pm -> pm.getConditionsOfJoining() != null
&& pm.getConditionsOfJoining().signedAt() != null)
.flatMap(pm ->
Stream.of(pm.getTisId().split(",")).map(id -> {
ProgrammeMembership newPm = new ProgrammeMembership();
newPm.setTisId(id);
newPm.setConditionsOfJoining(pm.getConditionsOfJoining());
return newPm;
})
)
.forEach(pm -> {
try {
//preferentially cache against new uuid
UUID uuid = UUID.fromString(pm.getTisId());
cachingDelegate.cacheConditionsOfJoining(uuid.toString(),
pm.getConditionsOfJoining());
} catch (IllegalArgumentException e) {
//fallback: cache against delimited ids
cachingDelegate.cacheConditionsOfJoining(pm.getTisId(),
pm.getConditionsOfJoining());
}
UUID uuid = UUID.fromString(pm.getTisId());
cachingDelegate.cacheConditionsOfJoining(uuid.toString(),
pm.getConditionsOfJoining());
});

existingProgrammeMemberships.clear();
Expand All @@ -181,7 +140,7 @@ public boolean deleteProgrammeMembershipsForTrainee(String traineeTisId) {
* @return True, or False if a trainee with the ID was not found.
*/
public boolean deleteProgrammeMembershipForTrainee(String traineeTisId,
String programmeMembershipId) {
String programmeMembershipId) {
TraineeProfile traineeProfile = repository.findByTraineeTisId(traineeTisId);

if (traineeProfile == null) {
Expand Down
Loading

0 comments on commit 0291bcb

Please sign in to comment.