Skip to content

Commit

Permalink
UT
Browse files Browse the repository at this point in the history
  • Loading branch information
SodhiA1 committed Nov 30, 2023
1 parent 4f1c32f commit f8ce5c0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private SchoolEntity createSchoolHelper(School school) {
school.getFacilityTypeCode(), school.getSchoolCategoryCode(),
school.getIndependentAuthorityId()));
} else {
throw new EntityNotFoundException(DistrictTombstoneEntity.class, school.getDistrictId());
throw new EntityNotFoundException(DistrictTombstoneEntity.class, school.getDistrictId(), school.getSchoolId());
}

schoolEntity.getAddresses().stream().forEach(address -> {
Expand Down Expand Up @@ -460,7 +460,7 @@ private SchoolEntity moveSchoolHelper(School school, School existingSchool) {
school.getIndependentAuthorityId()));
}
} else {
throw new EntityNotFoundException(DistrictTombstoneEntity.class, school.getDistrictId());
throw new EntityNotFoundException(DistrictTombstoneEntity.class, school.getDistrictId(), school.getSchoolId());
}

schoolEntity.getAddresses().stream().forEach(address -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,36 @@ public void testHandleEvent_givenEventTypeCREATE_SCHOOL_WITH_NEW_SCHOOL_NUMBER__
assertThat(schoolCreatedEvent.get().getEventOutcome()).isEqualTo(SCHOOL_CREATED.toString());
}

@Test(expected = EntityNotFoundException.class)
public void testHandleEvent_givenEventTypeCREATE_SCHOOL_WithNoDistrict__DoesExistAndSynchronousNatsMessage_shouldRespondWithData() throws IOException, ExecutionException, InterruptedException {
final DistrictTombstoneEntity dist = this.districtTombstoneRepository.save(this.createDistrictData());

var schoolEntity = this.createNewSchoolData(null, "PUBLIC", "DISTONLINE");

SchoolMapper map = SchoolMapper.mapper;

School mappedSchool = map.toStructure(schoolEntity);

mappedSchool.setDistrictId(UUID.randomUUID().toString());
mappedSchool.setCreateDate(null);
mappedSchool.setUpdateDate(null);
mappedSchool.setSchoolNumber(null);
mappedSchool.setGrades(List.of(createSchoolGrade()));
mappedSchool.setNeighborhoodLearning(List.of(createNeighborhoodLearning()));
mappedSchool.setAddresses(List.of(createSchoolAddress()));

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule()).configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

var sagaId = UUID.randomUUID();
final Event event = Event.builder().eventType(CREATE_SCHOOL).sagaId(sagaId).eventPayload(objectMapper.writeValueAsString(mappedSchool)).build();
eventHandlerServiceUnderTest.handleCreateSchoolEvent(event).getLeft();
var schoolCreatedEvent = instituteEventRepository.findBySagaIdAndEventType(sagaId, CREATE_SCHOOL.toString());
assertThat(schoolCreatedEvent).isPresent();
assertThat(schoolCreatedEvent.get().getEventStatus()).isEqualTo(MESSAGE_PUBLISHED.toString());
assertThat(schoolCreatedEvent.get().getEventOutcome()).isEqualTo(SCHOOL_CREATED.toString());
}

@Test
public void testHandleEvent_givenEventTypeCREATE_SCHOOL_WITH_EXISTING_SCHOOL_NUMBER__DoesExistAndSynchronousNatsMessage_shouldRespondWithData() throws IOException, ExecutionException, InterruptedException {
final DistrictTombstoneEntity dist = this.districtTombstoneRepository.save(this.createDistrictData());
Expand Down Expand Up @@ -333,6 +363,43 @@ public void testHandleEvent_givenEventTypeMOVE_SCHOOL__DoesExistAndSynchronousNa
assertThat(toSchoolEntityComplete.getSchoolMoveFrom().stream().toList().get(0).getFromSchoolId()).isEqualTo(fromSchoolEntityComplete.getSchoolId());
}

@Test(expected = EntityNotFoundException.class)
public void testHandleEvent_givenEventTypeMOVE_SCHOOL_DistrictNotFound__DoesExistAndSynchronousNatsMessage_shouldRespondWithDataAndCreateMoveHistory() throws IOException, ExecutionException, InterruptedException {
final DistrictTombstoneEntity dist = this.districtTombstoneRepository.save(this.createDistrictData());
SchoolEntity toSchoolEntity = this.createNewSchoolData("99000", "PUBLIC", "DISTONLINE");
SchoolEntity fromSchoolEntity = this.createNewSchoolData("99000", "PUBLIC", "DISTONLINE");
LocalDateTime moveDate = LocalDateTime.now().plusDays(1).truncatedTo(ChronoUnit.MILLIS);

Set<SchoolAddressEntity> schoolAddressSet = new HashSet<>();
SchoolAddressEntity fromSchoolAddressEntity = SchoolAddressEntity.builder()
.addressTypeCode("MAILING").addressLine1("123 This Street").city("Victoria")
.provinceCode("BC").countryCode("CA").postal("V1V2V3").schoolEntity(fromSchoolEntity).build();
schoolAddressSet.add(fromSchoolAddressEntity);

fromSchoolEntity.setDistrictEntity(dist);
fromSchoolEntity.setAddresses(schoolAddressSet);
schoolRepository.save(fromSchoolEntity);

School toSchool = new School();
toSchool.setGrades(List.of(createSchoolGrade()));
toSchool.setNeighborhoodLearning(List.of(createNeighborhoodLearning()));
toSchool.setAddresses(List.of(createSchoolAddress()));
SchoolMapper map = SchoolMapper.mapper;

BeanUtils.copyProperties(map.toStructure(toSchoolEntity), toSchool);
toSchool.setDistrictId(UUID.randomUUID().toString());

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule()).configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

MoveSchoolData moveSchoolData = createMoveSchoolData(toSchool, fromSchoolEntity.getSchoolId(), moveDate);

UUID sagaId = UUID.randomUUID();
final Event event = Event.builder().eventType(MOVE_SCHOOL).sagaId(sagaId).eventPayload(objectMapper.writeValueAsString(moveSchoolData)).build();

eventHandlerServiceUnderTest.handleMoveSchoolEvent(event).getLeft();
}

@Test
public void testHandleEvent_givenEventTypeMOVE_SCHOOL_whereSchoolNumberInNewDistrictIsAvailable__DoesExistAndSynchronousNatsMessage_shouldRespondWithDataAndCreateMoveHistory() throws IOException, ExecutionException, InterruptedException {
final DistrictTombstoneEntity dist = this.districtTombstoneRepository.save(this.createDistrictData());
Expand Down Expand Up @@ -680,6 +747,7 @@ private DistrictTombstoneEntity createDistrictData() {
return DistrictTombstoneEntity.builder().districtNumber("003").displayName("District Name").districtStatusCode("OPEN").districtRegionCode("KOOTENAYS")
.website("[email protected]").createDate(LocalDateTime.now()).updateDate(LocalDateTime.now()).createUser("TEST").updateUser("TEST").build();
}

private SchoolGrade createSchoolGrade() {
SchoolGrade schoolGrade = new SchoolGrade();
schoolGrade.setSchoolGradeCode("01");
Expand Down

0 comments on commit f8ce5c0

Please sign in to comment.