Skip to content

Commit

Permalink
Merge pull request #470 from Health-Education-England/fix/ignoreIrrel…
Browse files Browse the repository at this point in the history
…evantDbcSyncs

fix: ignore irrelevant DBCs
  • Loading branch information
ReubenRobertsHEE authored Jul 19, 2024
2 parents ed253ab + d2532b7 commit c631a68
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 41 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
}

group = "uk.nhs.hee.tis.trainee"
version = "1.17.0"
version = "1.17.1"

configurations {
compileOnly {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class DbcEventListener extends AbstractMongoEventListener<Dbc> {

public static final String DBC_NAME = "name";
public static final String DBC_ABBR = "abbr";
public static final String DBC_TYPE = "type";
public static final String DBC_TYPE_RELEVANT = "LETB/Deanery";

private final DbcSyncService dbcSyncService;

Expand Down Expand Up @@ -123,29 +125,35 @@ public void onAfterDelete(AfterDeleteEvent<Dbc> event) {
* @param dbc The DBC to get related programmes for.
*/
private void queueRelatedProgrammes(Dbc dbc) {
String abbr = dbc.getData().get(DBC_ABBR);
Optional<LocalOffice> localOfficeOptional = localOfficeSyncService.findByAbbreviation(abbr);

if (localOfficeOptional.isEmpty()) {
log.info("Local office {} not found, requesting data.", abbr);
localOfficeSyncService.requestByAbbr(abbr);

} else {

Set<Programme> programmes =
programmeSyncService.findByOwner(
localOfficeOptional.get().getData().get(LOCAL_OFFICE_NAME));

for (Programme programme : programmes) {
log.debug("DBC / LocalOffice {} affects programme {}, "
+ "and will require related programme memberships to have RO data amended.",
dbc.getData().get(DBC_ABBR), programme.getTisId());
// Default each message to LOAD.
programme.setOperation(Operation.LOAD);
String deduplicationId = fifoMessagingService
.getUniqueDeduplicationId(Programme.ENTITY_NAME, programme.getTisId());
fifoMessagingService.sendMessageToFifoQueue(programmeQueueUrl, programme, deduplicationId);
String dbcType = dbc.getData().get(DBC_TYPE);
if (dbcType.equalsIgnoreCase(DBC_TYPE_RELEVANT)) {
String abbr = dbc.getData().get(DBC_ABBR);
Optional<LocalOffice> localOfficeOptional = localOfficeSyncService.findByAbbreviation(abbr);

if (localOfficeOptional.isEmpty()) {
log.info("Local office {} not found, requesting data.", abbr);
localOfficeSyncService.requestByAbbr(abbr);

} else {

Set<Programme> programmes =
programmeSyncService.findByOwner(
localOfficeOptional.get().getData().get(LOCAL_OFFICE_NAME));

for (Programme programme : programmes) {
log.debug("DBC / LocalOffice {} affects programme {}, "
+ "and will require related programme memberships to have RO data amended.",
dbc.getData().get(DBC_ABBR), programme.getTisId());
// Default each message to LOAD.
programme.setOperation(Operation.LOAD);
String deduplicationId = fifoMessagingService
.getUniqueDeduplicationId(Programme.ENTITY_NAME, programme.getTisId());
fifoMessagingService.sendMessageToFifoQueue(programmeQueueUrl, programme,
deduplicationId);
}
}
} else {
log.info("Ignoring DBC of irrelevant type {}.", dbcType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static uk.nhs.hee.tis.trainee.sync.event.DbcEventListener.DBC_TYPE;
import static uk.nhs.hee.tis.trainee.sync.event.DbcEventListener.DBC_TYPE_RELEVANT;
import static uk.nhs.hee.tis.trainee.sync.event.LocalOfficeEventListener.LOCAL_OFFICE_NAME;

import java.util.Collections;
Expand Down Expand Up @@ -70,6 +72,8 @@ class DbcEventListenerTest {
private FifoMessagingService fifoMessagingService;
private Cache cache;

private Dbc dbc;

@BeforeEach
void setUp() {
dbcService = mock(DbcSyncService.class);
Expand All @@ -81,24 +85,33 @@ void setUp() {
when(cacheManager.getCache(Dbc.ENTITY_NAME)).thenReturn(cache);
listener = new DbcEventListener(dbcService, programmeService, localOfficeService,
fifoMessagingService, PROGRAMME_QUEUE_URL, cacheManager);

dbc = new Dbc();
dbc.setTisId(DBC_ID);
dbc.setData(Map.of("name", "some name", "abbr", ABBR, DBC_TYPE, DBC_TYPE_RELEVANT));
}

@Test
void shouldCacheAfterSave() {
Dbc dbc = new Dbc();
dbc.setTisId(DBC_ID);
AfterSaveEvent<Dbc> event = new AfterSaveEvent<>(dbc, null, null);

listener.onAfterSave(event);

verify(cache).put(DBC_ID, dbc);
}

@Test
void shouldNotInteractWithProgrammeQueueAfterSaveWhenNotRelevantType() {
dbc.setData(Map.of("name", "some name", "abbr", ABBR, DBC_TYPE, "another type"));
AfterSaveEvent<Dbc> event = new AfterSaveEvent<>(dbc, null, null);

listener.onAfterSave(event);

verifyNoInteractions(fifoMessagingService);
}

@Test
void shouldNotInteractWithProgrammeQueueAfterSaveWhenNoRelatedLocalOffice() {
Dbc dbc = new Dbc();
dbc.setTisId(DBC_ID);
dbc.setData(Map.of("name", "some name", "abbr", ABBR));
AfterSaveEvent<Dbc> event = new AfterSaveEvent<>(dbc, null, null);

when(localOfficeService.findByAbbreviation(ABBR)).thenReturn(Optional.empty());
Expand All @@ -110,9 +123,6 @@ void shouldNotInteractWithProgrammeQueueAfterSaveWhenNoRelatedLocalOffice() {

@Test
void shouldNotInteractWithProgrammeQueueAfterSaveWhenNoRelatedProgrammes() {
Dbc dbc = new Dbc();
dbc.setTisId(DBC_ID);
dbc.setData(Map.of("name", "some name", "abbr", ABBR));
final AfterSaveEvent<Dbc> event = new AfterSaveEvent<>(dbc, null, null);

LocalOffice localOffice = new LocalOffice();
Expand All @@ -128,10 +138,6 @@ void shouldNotInteractWithProgrammeQueueAfterSaveWhenNoRelatedProgrammes() {

@Test
void shouldSendRelatedProgrammesAfterSaveWhenRelatedProgrammes() {
Dbc dbc = new Dbc();
dbc.setTisId(DBC_ID);
dbc.setData(Map.of("name", "some name", "abbr", ABBR));

LocalOffice localOffice = new LocalOffice();
localOffice.setData(Map.of(LOCAL_OFFICE_NAME, OWNER, "abbr", ABBR));

Expand Down Expand Up @@ -162,7 +168,6 @@ void shouldSendRelatedProgrammesAfterSaveWhenRelatedProgrammes() {
void shouldFindAndCacheDbcIfNotInCacheBeforeDelete() {
Document document = new Document();
document.append("_id", "1");
Dbc dbc = new Dbc();
BeforeDeleteEvent<Dbc> event = new BeforeDeleteEvent<>(document, null, null);

when(cache.get("1", Dbc.class)).thenReturn(null);
Expand All @@ -179,7 +184,6 @@ void shouldFindAndCacheDbcIfNotInCacheBeforeDelete() {
void shouldNotFindAndCacheDbcIfInCacheBeforeDelete() {
Document document = new Document();
document.append("_id", "1");
Dbc dbc = new Dbc();
BeforeDeleteEvent<Dbc> event = new BeforeDeleteEvent<>(document, null, null);

when(cache.get("1", Dbc.class)).thenReturn(dbc);
Expand All @@ -192,10 +196,6 @@ void shouldNotFindAndCacheDbcIfInCacheBeforeDelete() {

@Test
void shouldQueueProgrammesAfterDelete() {
Dbc dbc = new Dbc();
dbc.setTisId(DBC_ID);
dbc.setData(Map.of("name", "some name", "abbr", ABBR));

LocalOffice localOffice = new LocalOffice();
localOffice.setData(Map.of(LOCAL_OFFICE_NAME, OWNER, "abbr", ABBR));

Expand Down

0 comments on commit c631a68

Please sign in to comment.