Skip to content

Commit

Permalink
Added rule to shouldCreateHistory method to check if a school's isCan…
Browse files Browse the repository at this point in the history
…IssueTranscripts property has changed since the last cache. (#393)

* Added rule to shouldCreateHistory method to check if a school's isCanIssueTranscripts property has changed since the last cache.

* Updated ordering of methods for adding to history
  • Loading branch information
cditcher authored Jan 10, 2025
1 parent 1455af9 commit 8c269aa
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public SchoolCreatedService(SchoolService schoolService) {
public void processEvent(final School school, EventEntity eventEntity) {
log.debug("Processing School Created");
try{
boolean shouldCreateHistory = this.shouldCreateHistory(school);
schoolService.updateSchoolCache(school.getSchoolId());
this.updateEvent(eventEntity, this.shouldCreateHistory(school));
this.updateEvent(eventEntity, shouldCreateHistory);
} catch (ServiceException e) {
log.error(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package ca.bc.gov.educ.api.trax.service;

import ca.bc.gov.educ.api.trax.model.dto.institute.School;
import ca.bc.gov.educ.api.trax.model.dto.institute.SchoolDetail;
import ca.bc.gov.educ.api.trax.service.institute.SchoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
@Slf4j
public abstract class SchoolEventBaseService<T> extends EventBaseService<T> {
Expand All @@ -23,6 +26,11 @@ protected SchoolEventBaseService(@Qualifier("instituteSchoolService") SchoolServ
*/
protected boolean shouldCreateHistory(School school) {
// currently only schools that can issue transcripts qualify
SchoolDetail schoolDetail = this.schoolService.getSchoolDetailBySchoolId(UUID.fromString(school.getSchoolId()));
// if the school's ability to issue transcripts has changed, return true
if (schoolDetail != null && (schoolDetail.isCanIssueTranscripts() != school.isCanIssueTranscripts())){
return true;
}
return school.isCanIssueTranscripts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public SchoolMovedService(@Qualifier("instituteSchoolService") SchoolService sch
public void processEvent(final MoveSchoolData moveSchoolData, EventEntity eventEntity) {
log.debug("Processing School Moved");
try{
schoolService.updateSchoolCache(Arrays.asList(moveSchoolData.getFromSchoolId(), moveSchoolData.getToSchool().getSchoolId()));
// have to check event history eligibility on from and to schools for move.
// if one can issue transcripts, set history eligibility
SchoolDetail schoolDetail = this.schoolService.getSchoolDetailByIdFromInstituteApi(moveSchoolData.getFromSchoolId());
boolean shouldCreateHistory = (schoolDetail.isCanIssueTranscripts() || this.shouldCreateHistory(moveSchoolData.getToSchool()));
boolean shouldCreateHistory = (moveSchoolData.getToSchool().isCanIssueTranscripts() || this.shouldCreateHistory(schoolDetail));
schoolService.updateSchoolCache(Arrays.asList(moveSchoolData.getFromSchoolId(), moveSchoolData.getToSchool().getSchoolId()));
this.updateEvent(eventEntity, shouldCreateHistory);
} catch (ServiceException e) {
log.error(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ca.bc.gov.educ.api.trax.service;

import ca.bc.gov.educ.api.trax.model.dto.institute.School;
import ca.bc.gov.educ.api.trax.model.dto.institute.SchoolDetail;
import ca.bc.gov.educ.api.trax.service.institute.SchoolService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.openMocks;

class SchoolEventBaseServiceTest {

private static final String SCHOOL_ID = UUID.randomUUID().toString();

@Mock
private SchoolService schoolService;
private AutoCloseable closeable;

@InjectMocks
private SchoolEventBaseService<School> schoolEventBaseService = new SchoolCreatedService(schoolService) {};

@BeforeEach
public void setUp() {
closeable = openMocks(this);
}

@AfterEach
public void finish() throws Exception {
closeable.close();
}

@Test
void shouldCreateHistory_whenSchoolCanIssueTranscriptsChanged() {
School school = new School();
school.setSchoolId(SCHOOL_ID);
school.setCanIssueTranscripts(false);
SchoolDetail schoolDetail = new SchoolDetail();
schoolDetail.setCanIssueTranscripts(true);
when(schoolService.getSchoolDetailBySchoolId(UUID.fromString(SCHOOL_ID))).thenReturn(schoolDetail);
assertTrue(schoolEventBaseService.shouldCreateHistory(school));
}

@Test
void shouldCreateHistory_whenSchoolCanIssueTranscriptsIsTrue() {
School school = new School();
school.setSchoolId(SCHOOL_ID);
school.setCanIssueTranscripts(true);
SchoolDetail schoolDetail = new SchoolDetail();
schoolDetail.setCanIssueTranscripts(true);
when(schoolService.getSchoolDetailBySchoolId(UUID.fromString(SCHOOL_ID))).thenReturn(schoolDetail);
assertTrue(schoolEventBaseService.shouldCreateHistory(school));
}

@Test
void shouldNotCreateHistory_whenSchoolCanIssueTranscriptsIsFalse() {
School school = new School();
school.setSchoolId(SCHOOL_ID);
school.setCanIssueTranscripts(false);
SchoolDetail schoolDetail = new SchoolDetail();
schoolDetail.setCanIssueTranscripts(false);
when(schoolService.getSchoolDetailBySchoolId(UUID.fromString(SCHOOL_ID))).thenReturn(schoolDetail);
assertFalse(schoolEventBaseService.shouldCreateHistory(school));
}
}

0 comments on commit 8c269aa

Please sign in to comment.