Skip to content

Commit

Permalink
Creates event to handle get merges in date range
Browse files Browse the repository at this point in the history
  • Loading branch information
eckermania committed Oct 23, 2024
1 parent 06499e1 commit 1e96ab9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public void handleEvent(final Event event, final Message message) {
response = this.getEventHandlerService().handleGetMergeEvent(event);
this.publishToNATS(event, message, isSynchronous, response);
break;
case GET_MERGES_IN_DATE_RANGE:
log.debug("received get merge in date range data :: {}", event.getSagaId());
log.trace(PAYLOAD_LOG, event.getEventPayload());
response = this.getEventHandlerService().handleGetMergeInDateRangeEvent(event);
this.publishToNATS(event, message, isSynchronous, response);
break;
default:
log.info("silently ignoring other event :: {}", event);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,34 @@ public byte[] handleGetMergeEvent(@NonNull final Event event) throws JsonProcess
return this.obMapper.writeValueAsBytes(newEvent);
}

@Transactional(propagation = REQUIRES_NEW)
public byte[] handleGetMergeInDateRangeEvent(@NonNull final Event event) throws JsonProcessingException {
String payload = event.getEventPayload();
String[] keyValuePairs = payload.split("&");

LocalDateTime createDateStart = null;
LocalDateTime createDateEnd = null;

for (String pair : keyValuePairs) {
String[] parts = pair.split("=");
if (parts[0].equals("createDateStart")) {
createDateStart = LocalDateTime.parse(parts[1], DateTimeFormatter.ISO_DATE_TIME);
} else if (parts[0].equals("createDateEnd")) {
createDateEnd = LocalDateTime.parse(parts[1], DateTimeFormatter.ISO_DATE_TIME);
}
}

final List<StudentMergeEntity> mergeEntities = this.getStudentMergeService().findStudentMerges(createDateStart, createDateEnd, StudentMergeDirectionCodes.FROM.getCode());

final Event newEvent = Event.builder()
.sagaId(event.getSagaId())
.eventType(event.getEventType())
.eventOutcome(MERGE_FOUND)
.eventPayload(JsonUtil.getJsonStringFromObject(mergeEntities.stream().map(StudentMergeMapper.mapper::toStructure).toList())).build();
if (log.isDebugEnabled()) {
log.debug(RESPONDING_BACK, newEvent);
}
return this.obMapper.writeValueAsBytes(newEvent);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,33 @@ public void testHandleGetMergeEvent_givenStudentIdPayload_whenSuccessfullyProces
assertThat(addedStudentMerges).hasSize(1);
}

@Test
public void testHandleGetMergeInDateRangeEvent_givenDateRangePayload_whenSuccessfullyProcessed_shouldHaveEventOutcomeMERGE_FOUND() throws JsonProcessingException {
final var studentMerge = this.createStudentMergePayload();
studentMerge.setStudentMergeDirectionCode(StudentMergeDirectionCodes.FROM.getCode());
this.studentMergeRepository.save(mapper.toModel(studentMerge));

final String eventPayload = "createDateStart=2023-01-01T00:00:00&createDateEnd=2024-12-31T23:59:59";
final var event = Event.builder()
.eventType(GET_MERGES_IN_DATE_RANGE)
.replyTo(PEN_SERVICES_API_TOPIC.toString())
.eventPayload(eventPayload)
.build();

final var rawResponse = this.eventHandlerServiceUnderTest.handleGetMergeInDateRangeEvent(event);

assertThat(rawResponse).hasSizeGreaterThan(0);
final var response = JsonUtil.getJsonObjectFromString(Event.class, new String(rawResponse));
assertThat(response.getEventOutcome()).isEqualTo(MERGE_FOUND);


final ObjectMapper objectMapper = new ObjectMapper();
final JavaType type = objectMapper.getTypeFactory().constructCollectionType(List.class, StudentMerge.class);

final List<StudentMerge> addedStudentMerges = objectMapper.readValue(response.getEventPayload(), type);
assertThat(addedStudentMerges).hasSize(1);
}

private PenRequestStudentValidationPayload createValidationPayload() {
return PenRequestStudentValidationPayload.builder()
.isInteractive(false)
Expand Down

0 comments on commit 1e96ab9

Please sign in to comment.