use of ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchArchiveAndReturnSagaData in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRequestBatchSagaController method processBatchRequest.
private ResponseEntity<List<ArchiveAndReturnSagaResponse>> processBatchRequest(final SagaEnum sagaName, final PenRequestBatchArchiveAndReturnAllSagaData penRequestBatchArchiveAndReturnAllSagaData) {
final var penRequestBatchIDs = penRequestBatchArchiveAndReturnAllSagaData.getPenRequestBatchArchiveAndReturnSagaData().stream().map(PenRequestBatchArchiveAndReturnSagaData::getPenRequestBatchID).collect(Collectors.toList());
final var sagaInProgress = !this.getSagaService().findAllByPenRequestBatchIDInAndStatusIn(penRequestBatchIDs, this.getStatusesFilter()).isEmpty();
if (sagaInProgress) {
return ResponseEntity.status(HttpStatus.CONFLICT).build();
}
try {
final var updateUser = penRequestBatchArchiveAndReturnAllSagaData.getUpdateUser();
final var payloads = penRequestBatchArchiveAndReturnAllSagaData.getPenRequestBatchArchiveAndReturnSagaData().stream().map(sagaData -> {
sagaData.setUpdateUser(updateUser);
try {
val payload = JsonUtil.getJsonStringFromObject(sagaData);
return Pair.of(sagaData.getPenRequestBatchID(), payload);
} catch (final JsonProcessingException e) {
throw new InvalidParameterException(e.getMessage());
}
}).collect(Collectors.toList());
final var sagas = this.getOrchestratorMap().get(sagaName.toString()).saveMultipleSagas(payloads, penRequestBatchArchiveAndReturnAllSagaData.getCreateUser());
for (val saga : sagas) {
this.getOrchestratorMap().get(sagaName.toString()).startSaga(saga);
}
return ResponseEntity.ok(sagas.stream().map(archiveAndReturnSagaResponseMapper::toStruct).collect(Collectors.toList()));
} catch (final Exception e) {
throw new SagaRuntimeException(e.getMessage());
}
}
use of ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchArchiveAndReturnSagaData in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRequestBatchArchiveAndReturnOrchestrator method saveReportsWithoutPDF.
private void saveReportsWithoutPDF(Event event, Saga saga, PenRequestBatchArchiveAndReturnSagaData penRequestBatchArchiveAndReturnSagaData) throws IOException, InterruptedException, TimeoutException {
SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
saga.setSagaState(SAVE_REPORTS.toString());
penRequestBatchArchiveAndReturnSagaData.setPenRequestBatch(JsonUtil.getJsonObjectFromString(PenRequestBatch.class, event.getEventPayload()));
// save the updated payload to DB...
saga.setPayload(JsonUtil.getJsonStringFromObject(penRequestBatchArchiveAndReturnSagaData));
this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
if (penRequestBatchArchiveAndReturnSagaData.getStudents() == null) {
log.info("students in saga data is null or empty for batch id :: {} and saga id :: {}, setting it from event states table", penRequestBatchArchiveAndReturnSagaData.getPenRequestBatchID(), saga.getSagaId());
SagaEvent sagaEvent = SagaEvent.builder().sagaEventState(GET_STUDENTS.toString()).sagaEventOutcome(STUDENTS_FOUND.toString()).sagaStepNumber(3).build();
val sagaEventOptional = this.getSagaService().findSagaEvent(saga, sagaEvent);
if (sagaEventOptional.isPresent()) {
List<Student> students = obMapper.readValue(sagaEventOptional.get().getSagaEventResponse(), new TypeReference<>() {
});
penRequestBatchArchiveAndReturnSagaData.setStudents(event, students);
} else {
throw new PenRegAPIRuntimeException("students not found in event states table for saga id :: " + saga.getSagaId());
}
}
this.getResponseFileGeneratorService().saveReports(mapper.toModel(penRequestBatchArchiveAndReturnSagaData.getPenRequestBatch()), penRequestBatchArchiveAndReturnSagaData.getPenRequestBatchStudents(), penRequestBatchArchiveAndReturnSagaData.getStudents(), reportMapper.toReportData(penRequestBatchArchiveAndReturnSagaData));
val nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(SAVE_REPORTS).eventOutcome(REPORTS_SAVED).build();
this.handleEvent(nextEvent);
}
use of ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchArchiveAndReturnSagaData in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRequestBatchArchiveAndReturnOrchestratorTest method testHandleEvent_givenBatchInSagaDataExistsAndSysNewPenStudent_shouldGatherReportDataAndBeMarkedREPORT_DATA_GATHERED.
@Test
public void testHandleEvent_givenBatchInSagaDataExistsAndSysNewPenStudent_shouldGatherReportDataAndBeMarkedREPORT_DATA_GATHERED() throws IOException, InterruptedException, TimeoutException {
when(this.restUtils.getPenCoordinator(anyString())).thenReturn(Optional.of(PenCoordinator.builder().penCoordinatorEmail("test@test.com").penCoordinatorName("Joe Blow").build()));
this.saga = penRequestBatchTestUtils.createSaga("19337120", "12345679", PenRequestBatchStudentStatusCodes.SYS_NEW_PEN.getCode(), TEST_PEN);
final var event = Event.builder().eventType(EventType.INITIATED).eventOutcome(EventOutcome.INITIATE_SUCCESS).sagaId(this.saga.get(0).getSagaId()).build();
this.orchestrator.handleEvent(event);
final var sagaFromDB = this.sagaService.findSagaById(this.saga.get(0).getSagaId());
assertThat(sagaFromDB).isPresent();
assertThat(sagaFromDB.get().getSagaState()).isEqualTo(GET_STUDENTS.toString());
final var sagaStates = this.sagaService.findAllSagaStates(sagaFromDB.get());
assertThat(sagaStates.size()).isEqualTo(2);
assertThat(sagaStates.get(0).getSagaEventState()).isEqualTo(INITIATED.toString());
assertThat(sagaStates.get(0).getSagaEventOutcome()).isEqualTo(EventOutcome.INITIATE_SUCCESS.toString());
assertThat(sagaStates.get(1).getSagaEventState()).isEqualTo(GATHER_REPORT_DATA.toString());
assertThat(sagaStates.get(1).getSagaEventOutcome()).isEqualTo(EventOutcome.REPORT_DATA_GATHERED.toString());
final PenRequestBatchArchiveAndReturnSagaData payload = JsonUtil.getJsonObjectFromString(PenRequestBatchArchiveAndReturnSagaData.class, sagaFromDB.get().getPayload());
assertThat(payload.getFacsimile()).isNotEmpty();
assertThat(payload.getFromEmail()).isNotEmpty();
assertThat(payload.getTelephone()).isNotEmpty();
assertThat(payload.getMailingAddress()).isNotEmpty();
assertThat(payload.getPenCoordinator().getPenCoordinatorEmail()).isNotEmpty();
assertThat(payload.getPenCoordinator().getPenCoordinatorName()).isNotEmpty();
assertThat(payload.getPenRequestBatchStudents()).isNotEmpty();
assertThat(payload.getPenRequestBatch()).isNotNull();
}
use of ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchArchiveAndReturnSagaData in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRequestBatchArchiveAndReturnOrchestratorTest method testSendHasCoordinatorEmail_givenEventAndSagaDataNoPenCoordinatorEmail_shouldBeMarkedNOTIFY_PEN_REQUEST_BATCH_ARCHIVE_HAS_NO_SCHOOL_CONTACT.
@Test
public void testSendHasCoordinatorEmail_givenEventAndSagaDataNoPenCoordinatorEmail_shouldBeMarkedNOTIFY_PEN_REQUEST_BATCH_ARCHIVE_HAS_NO_SCHOOL_CONTACT() throws InterruptedException, TimeoutException, IOException {
final var invocations = mockingDetails(this.messagePublisher).getInvocations().size();
PenRequestBatchEntity penRequestBatchEntity = penRequestBatchTestUtils.createBatchEntity("19337120", "12345679", PenRequestBatchStudentStatusCodes.SYS_NEW_PEN.getCode(), TEST_PEN);
PenRequestBatchArchiveAndReturnSagaData payload = PenRequestBatchArchiveAndReturnSagaData.builder().penRequestBatch(batchMapper.toStructure(penRequestBatchEntity)).penRequestBatchStudents(penRequestBatchEntity.getPenRequestBatchStudentEntities().stream().map(batchStudentMapper::toStructure).collect(Collectors.toList())).mailingAddress("123 st").fromEmail("test@email.com").facsimile("5555555555").telephone("2222222222").students(PenRequestBatchTestUtils.createStudents(penRequestBatchEntity)).penRequestBatchID(penRequestBatchEntity.getPenRequestBatchID()).build();
this.saga.get(0).setPayload(JsonUtil.getJsonStringFromObject(payload));
this.sagaService.updateAttachedEntityDuringSagaProcess(this.saga.get(0));
final var event = Event.builder().eventType(EventType.GENERATE_PEN_REQUEST_BATCH_REPORTS).eventOutcome(EventOutcome.ARCHIVE_PEN_REQUEST_BATCH_REPORTS_GENERATED).eventPayload(Base64.getEncoder().encodeToString("Heres a pdf report".getBytes())).sagaId(this.saga.get(0).getSagaId()).build();
this.orchestrator.handleEvent(event);
verify(this.messagePublisher, atMost(invocations + 1)).dispatchMessage(eq(SagaTopicsEnum.PROFILE_REQUEST_EMAIL_API_TOPIC.toString()), this.eventCaptor.capture());
final var newEvent = JsonUtil.getJsonObjectFromString(Event.class, new String(this.eventCaptor.getValue()));
assertThat(newEvent.getEventType()).isEqualTo(NOTIFY_PEN_REQUEST_BATCH_ARCHIVE_HAS_NO_SCHOOL_CONTACT);
assertThat(newEvent.getEventPayload()).isNotEmpty();
// from application.properties
assertThat(newEvent.getEventPayload()).contains("test@abc.com");
final var sagaFromDB = this.sagaService.findSagaById(this.saga.get(0).getSagaId());
assertThat(sagaFromDB).isPresent();
assertThat(sagaFromDB.get().getSagaState()).isEqualTo(NOTIFY_PEN_REQUEST_BATCH_ARCHIVE_HAS_NO_SCHOOL_CONTACT.toString());
final var sagaStates = this.sagaService.findAllSagaStates(this.saga.get(0));
assertThat(sagaStates.size()).isEqualTo(2);
assertThat(sagaStates.get(0).getSagaEventState()).isEqualTo(GENERATE_PEN_REQUEST_BATCH_REPORTS.toString());
assertThat(sagaStates.get(0).getSagaEventOutcome()).isEqualTo(EventOutcome.ARCHIVE_PEN_REQUEST_BATCH_REPORTS_GENERATED.toString());
assertThat(sagaStates.get(1).getSagaEventState()).isEqualTo(EventType.SAVE_REPORTS.toString());
assertThat(sagaStates.get(1).getSagaEventOutcome()).isEqualTo(EventOutcome.REPORTS_SAVED.toString());
}
use of ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchArchiveAndReturnSagaData in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRequestBatchArchiveAndReturnOrchestratorTest method testHandleEvent_givenBatchInSagaDataExistsAndUsrMtchStudent_shouldArchivePenRequestBatchAndBeMarkedSTUDENTS_FOUND.
@Test
public void testHandleEvent_givenBatchInSagaDataExistsAndUsrMtchStudent_shouldArchivePenRequestBatchAndBeMarkedSTUDENTS_FOUND() throws IOException, InterruptedException, TimeoutException {
when(this.restUtils.getPenCoordinator(anyString())).thenReturn(Optional.of(PenCoordinator.builder().penCoordinatorEmail("test@test.com").penCoordinatorName("Joe Blow").build()));
this.saga = penRequestBatchTestUtils.createSaga("19337120", "12345679", PenRequestBatchStudentStatusCodes.USR_MATCHED.getCode(), TEST_PEN);
final var event = Event.builder().eventType(EventType.INITIATED).eventOutcome(EventOutcome.INITIATE_SUCCESS).sagaId(this.saga.get(0).getSagaId()).build();
this.orchestrator.handleEvent(event);
final var sagaFromDB = this.sagaService.findSagaById(this.saga.get(0).getSagaId());
assertThat(sagaFromDB).isPresent();
assertThat(sagaFromDB.get().getSagaState()).isEqualTo(ARCHIVE_PEN_REQUEST_BATCH.toString());
final var sagaStates = this.sagaService.findAllSagaStates(sagaFromDB.get());
assertThat(sagaStates.size()).isEqualTo(3);
assertThat(sagaStates.get(0).getSagaEventState()).isEqualTo(INITIATED.toString());
assertThat(sagaStates.get(0).getSagaEventOutcome()).isEqualTo(EventOutcome.INITIATE_SUCCESS.toString());
assertThat(sagaStates.get(1).getSagaEventState()).isEqualTo(GATHER_REPORT_DATA.toString());
assertThat(sagaStates.get(1).getSagaEventOutcome()).isEqualTo(EventOutcome.REPORT_DATA_GATHERED.toString());
assertThat(sagaStates.get(2).getSagaEventState()).isEqualTo(GET_STUDENTS.toString());
assertThat(sagaStates.get(2).getSagaEventOutcome()).isEqualTo(EventOutcome.STUDENTS_FOUND.toString());
final PenRequestBatchArchiveAndReturnSagaData payload = JsonUtil.getJsonObjectFromString(PenRequestBatchArchiveAndReturnSagaData.class, sagaFromDB.get().getPayload());
assertThat(payload.getFacsimile()).isNotEmpty();
assertThat(payload.getFromEmail()).isNotEmpty();
assertThat(payload.getTelephone()).isNotEmpty();
assertThat(payload.getMailingAddress()).isNotEmpty();
assertThat(payload.getPenCoordinator().getPenCoordinatorEmail()).isNotEmpty();
assertThat(payload.getPenCoordinator().getPenCoordinatorName()).isNotEmpty();
assertThat(payload.getPenRequestBatchStudents()).isNotEmpty();
assertThat(payload.getPenRequestBatch()).isNotNull();
}
Aggregations