Search in sources :

Example 16 with SagaEvent

use of ca.bc.gov.educ.penreg.api.model.v1.SagaEvent in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenRequestBatchArchiveAndReturnOrchestrator method archivePenRequestBatch.

private void archivePenRequestBatch(Event event, Saga saga, PenRequestBatchArchiveAndReturnSagaData penRequestBatchArchiveAndReturnSagaData) throws IOException {
    SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    saga.setSagaState(ARCHIVE_PEN_REQUEST_BATCH.toString());
    List<Student> students = obMapper.readValue(event.getEventPayload(), new TypeReference<>() {
    });
    penRequestBatchArchiveAndReturnSagaData.setStudents(event, students);
    // save the updated payload to DB...
    saga.setPayload(JsonUtil.getJsonStringFromObject(penRequestBatchArchiveAndReturnSagaData));
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    var penRequestBatchArchive = PenRequestBatchArchive.builder().penRequestBatchID(penRequestBatchArchiveAndReturnSagaData.getPenRequestBatchID()).updateUser(penRequestBatchArchiveAndReturnSagaData.getUpdateUser()).build();
    Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(EventType.ARCHIVE_PEN_REQUEST_BATCH).replyTo(this.getTopicToSubscribe()).eventPayload(JsonUtil.getJsonStringFromObject(penRequestBatchArchive)).build();
    this.postMessageToTopic(PEN_REQUEST_BATCH_API_TOPIC.toString(), nextEvent);
    log.info("message sent to PEN_REQUEST_BATCH_API_TOPIC for ARCHIVE_PEN_REQUEST_BATCH Event. :: {}", saga.getSagaId());
}
Also used : SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent) Event(ca.bc.gov.educ.penreg.api.struct.Event) Student(ca.bc.gov.educ.penreg.api.struct.Student) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 17 with SagaEvent

use of ca.bc.gov.educ.penreg.api.model.v1.SagaEvent in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenRequestBatchRepostReportsOrchestrator method generatePDFReport.

private void generatePDFReport(Event event, Saga saga, PenRequestBatchRepostReportsFilesSagaData penRequestBatchRepostReportsFilesSagaData) throws IOException {
    SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    saga.setSagaState(GENERATE_PEN_REQUEST_BATCH_REPORTS.toString());
    List<Student> students = obMapper.readValue(event.getEventPayload(), new TypeReference<>() {
    });
    penRequestBatchRepostReportsFilesSagaData.setStudents(event, students);
    // save the updated payload to DB...
    saga.setPayload(JsonUtil.getJsonStringFromObject(penRequestBatchRepostReportsFilesSagaData));
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(GENERATE_PEN_REQUEST_BATCH_REPORTS).replyTo(this.getTopicToSubscribe()).eventPayload(JsonUtil.getJsonStringFromObject(ReportGenerationEventPayload.builder().reportType("PEN_REG_BATCH_RESPONSE_REPORT").reportExtension("pdf").reportName(penRequestBatchRepostReportsFilesSagaData.getPenRequestBatch().getSubmissionNumber()).data(reportMapper.toReportData(penRequestBatchRepostReportsFilesSagaData)).build())).build();
    this.postMessageToTopic(SagaTopicsEnum.PEN_REPORT_GENERATION_API_TOPIC.toString(), nextEvent);
    log.info("message sent to PEN_REPORT_GENERATION_API_TOPIC for {} Event. :: {}", GENERATE_PEN_REQUEST_BATCH_REPORTS.toString(), saga.getSagaId());
}
Also used : SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent) Event(ca.bc.gov.educ.penreg.api.struct.Event) Student(ca.bc.gov.educ.penreg.api.struct.Student) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 18 with SagaEvent

use of ca.bc.gov.educ.penreg.api.model.v1.SagaEvent in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenRequestBatchSagaControllerTest method testGetSagaEventsBySagaID_whenSagaIDIsValid_shouldReturnStatusOk.

@Test
@SuppressWarnings("java:S100")
public void testGetSagaEventsBySagaID_whenSagaIDIsValid_shouldReturnStatusOk() throws Exception {
    final File sagEventsFile = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource("mock-saga-events.json")).getFile());
    final File sagFile = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource("mock-saga.json")).getFile());
    val sagaEvents = Arrays.asList(JsonUtil.mapper.readValue(sagEventsFile, ca.bc.gov.educ.penreg.api.model.v1.SagaEvent[].class));
    val saga = JsonUtil.mapper.readValue(sagFile, ca.bc.gov.educ.penreg.api.model.v1.Saga.class);
    saga.setSagaId(null);
    saga.setCreateDate(LocalDateTime.now());
    saga.setUpdateDate(LocalDateTime.now());
    this.repository.save(saga);
    for (val sagaEvent : sagaEvents) {
        sagaEvent.setSaga(saga);
        sagaEvent.setCreateDate(LocalDateTime.now());
        sagaEvent.setUpdateDate(LocalDateTime.now());
    }
    this.sagaEventRepository.saveAll(sagaEvents);
    this.mockMvc.perform(get("/api/v1/pen-request-batch-saga/{sagaId}/saga-events", saga.getSagaId()).with(jwt().jwt((jwt) -> jwt.claim("scope", "PEN_REQUEST_BATCH_READ_SAGA")))).andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$", hasSize(8)));
}
Also used : lombok.val(lombok.val) BasePenRegAPITest(ca.bc.gov.educ.penreg.api.BasePenRegAPITest) java.util(java.util) MockMvcResultMatchers.jsonPath(org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath) LocalDateTime(java.time.LocalDateTime) AND(ca.bc.gov.educ.penreg.api.struct.v1.Condition.AND) Autowired(org.springframework.beans.factory.annotation.Autowired) PenRequestBatchTestUtils(ca.bc.gov.educ.penreg.api.support.PenRequestBatchTestUtils) SagaRepository(ca.bc.gov.educ.penreg.api.repository.SagaRepository) JsonUtil(ca.bc.gov.educ.penreg.api.util.JsonUtil) SagaMapper(ca.bc.gov.educ.penreg.api.mappers.v1.SagaMapper) PenRequestBatchEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEntity) MockMvc(org.springframework.test.web.servlet.MockMvc) SagaEnum(ca.bc.gov.educ.penreg.api.constants.SagaEnum) MockitoAnnotations(org.mockito.MockitoAnnotations) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) MvcResult(org.springframework.test.web.servlet.MvcResult) Matchers.hasSize(org.hamcrest.Matchers.hasSize) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) TypeReference(com.fasterxml.jackson.core.type.TypeReference) PenRequestBatchRepository(ca.bc.gov.educ.penreg.api.repository.PenRequestBatchRepository) Before(org.junit.Before) ValueType(ca.bc.gov.educ.penreg.api.struct.v1.ValueType) SecurityMockMvcRequestPostProcessors.jwt(org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt) FilterOperation(ca.bc.gov.educ.penreg.api.filter.FilterOperation) SagaEventRepository(ca.bc.gov.educ.penreg.api.repository.SagaEventRepository) MockMvcResultHandlers.print(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print) MediaType(org.springframework.http.MediaType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) lombok.val(lombok.val) SagaService(ca.bc.gov.educ.penreg.api.service.SagaService) SearchCriteria(ca.bc.gov.educ.penreg.api.struct.v1.SearchCriteria) Test(org.junit.Test) Collectors(java.util.stream.Collectors) MockMvcRequestBuilders(org.springframework.test.web.servlet.request.MockMvcRequestBuilders) Saga(ca.bc.gov.educ.penreg.api.struct.v1.Saga) File(java.io.File) Search(ca.bc.gov.educ.penreg.api.struct.v1.Search) Matchers.is(org.hamcrest.Matchers.is) File(java.io.File) BasePenRegAPITest(ca.bc.gov.educ.penreg.api.BasePenRegAPITest) Test(org.junit.Test)

Example 19 with SagaEvent

use of ca.bc.gov.educ.penreg.api.model.v1.SagaEvent in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenRequestBatchArchiveAndReturnOrchestrator method generatePDFReport.

private void generatePDFReport(Event event, Saga saga, PenRequestBatchArchiveAndReturnSagaData penRequestBatchArchiveAndReturnSagaData) throws JsonProcessingException {
    SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    saga.setSagaState(GENERATE_PEN_REQUEST_BATCH_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());
        }
    }
    Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(GENERATE_PEN_REQUEST_BATCH_REPORTS).replyTo(this.getTopicToSubscribe()).eventPayload(JsonUtil.getJsonStringFromObject(ReportGenerationEventPayload.builder().reportType("PEN_REG_BATCH_RESPONSE_REPORT").reportExtension("pdf").reportName(penRequestBatchArchiveAndReturnSagaData.getPenRequestBatch().getSubmissionNumber()).data(reportMapper.toReportData(penRequestBatchArchiveAndReturnSagaData)).build())).build();
    this.postMessageToTopic(SagaTopicsEnum.PEN_REPORT_GENERATION_API_TOPIC.toString(), nextEvent);
    log.info("message sent to PEN_REPORT_GENERATION_API_TOPIC for {} Event. :: {}", GENERATE_PEN_REQUEST_BATCH_REPORTS.toString(), saga.getSagaId());
}
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) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent) Event(ca.bc.gov.educ.penreg.api.struct.Event) Student(ca.bc.gov.educ.penreg.api.struct.Student) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 20 with SagaEvent

use of ca.bc.gov.educ.penreg.api.model.v1.SagaEvent in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenRequestBatchRepostReportsOrchestrator method saveReportsWithoutPDF.

private void saveReportsWithoutPDF(Event event, Saga saga, PenRequestBatchRepostReportsFilesSagaData penRequestBatchRepostReportsFilesSagaData) throws IOException, TimeoutException, InterruptedException {
    SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    saga.setSagaState(SAVE_REPORTS.toString());
    List<Student> students = obMapper.readValue(event.getEventPayload(), new TypeReference<>() {
    });
    penRequestBatchRepostReportsFilesSagaData.setStudents(event, students);
    // save the updated payload to DB...
    saga.setPayload(JsonUtil.getJsonStringFromObject(penRequestBatchRepostReportsFilesSagaData));
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    this.getResponseFileGeneratorService().saveReports(mapper.toModel(penRequestBatchRepostReportsFilesSagaData.getPenRequestBatch()), penRequestBatchRepostReportsFilesSagaData.getPenRequestBatchStudents(), penRequestBatchRepostReportsFilesSagaData.getStudents(), reportMapper.toReportData(penRequestBatchRepostReportsFilesSagaData));
    val nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(SAVE_REPORTS).eventOutcome(REPORTS_SAVED).build();
    this.handleEvent(nextEvent);
}
Also used : lombok.val(lombok.val) Student(ca.bc.gov.educ.penreg.api.struct.Student) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Aggregations

SagaEvent (ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)24 Event (ca.bc.gov.educ.penreg.api.struct.Event)18 lombok.val (lombok.val)13 Student (ca.bc.gov.educ.penreg.api.struct.Student)11 PenRegAPIRuntimeException (ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException)6 PenRequestBatchStudent (ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent)6 EventOutcome (ca.bc.gov.educ.penreg.api.constants.EventOutcome)3 EventType (ca.bc.gov.educ.penreg.api.constants.EventType)3 SagaService (ca.bc.gov.educ.penreg.api.service.SagaService)3 PossibleMatch (ca.bc.gov.educ.penreg.api.struct.v1.PossibleMatch)3 JsonUtil (ca.bc.gov.educ.penreg.api.util.JsonUtil)3 LocalDateTime (java.time.LocalDateTime)3 Collectors (java.util.stream.Collectors)3 SagaTopicsEnum (ca.bc.gov.educ.penreg.api.constants.SagaTopicsEnum)2 TwinReasonCodes (ca.bc.gov.educ.penreg.api.constants.TwinReasonCodes)2 MessagePublisher (ca.bc.gov.educ.penreg.api.messaging.MessagePublisher)2 Saga (ca.bc.gov.educ.penreg.api.model.v1.Saga)2 RestUtils (ca.bc.gov.educ.penreg.api.rest.RestUtils)2 PenRequestBatchUserActionsSagaData (ca.bc.gov.educ.penreg.api.struct.PenRequestBatchUserActionsSagaData)2 PenRequestBatch (ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatch)2