Search in sources :

Example 1 with PenRequestBatchArchiveAndReturnSagaData

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());
    }
}
Also used : ApiError(ca.bc.gov.educ.penreg.api.exception.errors.ApiError) PenRequestBatchUnmatchSagaData(ca.bc.gov.educ.penreg.api.struct.PenRequestBatchUnmatchSagaData) java.util(java.util) Getter(lombok.Getter) PenRequestBatchSagaEndpoint(ca.bc.gov.educ.penreg.api.endpoint.v1.PenRequestBatchSagaEndpoint) InvalidParameterException(ca.bc.gov.educ.penreg.api.exception.InvalidParameterException) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) CompletableFuture(java.util.concurrent.CompletableFuture) JsonUtil(ca.bc.gov.educ.penreg.api.util.JsonUtil) StringUtils(org.apache.commons.lang3.StringUtils) SagaMapper(ca.bc.gov.educ.penreg.api.mappers.v1.SagaMapper) SagaEnum(ca.bc.gov.educ.penreg.api.constants.SagaEnum) ca.bc.gov.educ.penreg.api.struct.v1(ca.bc.gov.educ.penreg.api.struct.v1) Pair(org.springframework.data.util.Pair) Orchestrator(ca.bc.gov.educ.penreg.api.orchestrator.base.Orchestrator) Sort(org.springframework.data.domain.Sort) TypeReference(com.fasterxml.jackson.core.type.TypeReference) InvalidPayloadException(ca.bc.gov.educ.penreg.api.exception.InvalidPayloadException) BAD_REQUEST(org.springframework.http.HttpStatus.BAD_REQUEST) PenRequestBatchUserActionsSagaData(ca.bc.gov.educ.penreg.api.struct.PenRequestBatchUserActionsSagaData) BasePenRequestBatchStudentSagaData(ca.bc.gov.educ.penreg.api.struct.BasePenRequestBatchStudentSagaData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) lombok.val(lombok.val) SagaService(ca.bc.gov.educ.penreg.api.service.SagaService) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SagaFilterSpecs(ca.bc.gov.educ.penreg.api.filter.SagaFilterSpecs) Page(org.springframework.data.domain.Page) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) SagaRuntimeException(ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException) HttpStatus(org.springframework.http.HttpStatus) Slf4j(lombok.extern.slf4j.Slf4j) Specification(org.springframework.data.jpa.domain.Specification) ArchiveAndReturnSagaResponseMapper(ca.bc.gov.educ.penreg.api.mappers.v1.ArchiveAndReturnSagaResponseMapper) ResponseEntity(org.springframework.http.ResponseEntity) SagaStatusEnum(ca.bc.gov.educ.penreg.api.constants.SagaStatusEnum) Transactional(org.springframework.transaction.annotation.Transactional) PRIVATE(lombok.AccessLevel.PRIVATE) lombok.val(lombok.val) InvalidParameterException(ca.bc.gov.educ.penreg.api.exception.InvalidParameterException) SagaRuntimeException(ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) InvalidParameterException(ca.bc.gov.educ.penreg.api.exception.InvalidParameterException) InvalidPayloadException(ca.bc.gov.educ.penreg.api.exception.InvalidPayloadException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SagaRuntimeException(ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException)

Example 2 with PenRequestBatchArchiveAndReturnSagaData

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);
}
Also used : lombok.val(lombok.val) PenRequestBatch(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatch) PenRegAPIRuntimeException(ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException) Student(ca.bc.gov.educ.penreg.api.struct.Student) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 3 with PenRequestBatchArchiveAndReturnSagaData

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();
}
Also used : PenRequestBatchArchiveAndReturnSagaData(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchArchiveAndReturnSagaData) Test(org.junit.Test)

Example 4 with PenRequestBatchArchiveAndReturnSagaData

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());
}
Also used : PenRequestBatchEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEntity) PenRequestBatchArchiveAndReturnSagaData(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchArchiveAndReturnSagaData) Test(org.junit.Test)

Example 5 with PenRequestBatchArchiveAndReturnSagaData

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();
}
Also used : PenRequestBatchArchiveAndReturnSagaData(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchArchiveAndReturnSagaData) Test(org.junit.Test)

Aggregations

PenRequestBatchArchiveAndReturnSagaData (ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchArchiveAndReturnSagaData)14 Test (org.junit.Test)13 PenRequestBatchEntity (ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEntity)10 lombok.val (lombok.val)3 PenRegAPIRuntimeException (ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException)2 SagaEvent (ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)2 Student (ca.bc.gov.educ.penreg.api.struct.Student)2 PenRequestBatch (ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatch)2 SagaEnum (ca.bc.gov.educ.penreg.api.constants.SagaEnum)1 SagaStatusEnum (ca.bc.gov.educ.penreg.api.constants.SagaStatusEnum)1 PenRequestBatchSagaEndpoint (ca.bc.gov.educ.penreg.api.endpoint.v1.PenRequestBatchSagaEndpoint)1 InvalidParameterException (ca.bc.gov.educ.penreg.api.exception.InvalidParameterException)1 InvalidPayloadException (ca.bc.gov.educ.penreg.api.exception.InvalidPayloadException)1 SagaRuntimeException (ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException)1 ApiError (ca.bc.gov.educ.penreg.api.exception.errors.ApiError)1 SagaFilterSpecs (ca.bc.gov.educ.penreg.api.filter.SagaFilterSpecs)1 ArchiveAndReturnSagaResponseMapper (ca.bc.gov.educ.penreg.api.mappers.v1.ArchiveAndReturnSagaResponseMapper)1 SagaMapper (ca.bc.gov.educ.penreg.api.mappers.v1.SagaMapper)1 Orchestrator (ca.bc.gov.educ.penreg.api.orchestrator.base.Orchestrator)1 SagaService (ca.bc.gov.educ.penreg.api.service.SagaService)1