Search in sources :

Example 1 with PossibleMatch

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

the class PenReqBatchUserUnmatchOrchestrator method deletePossibleMatchesFromStudent.

/**
 * this method expects that the twin ids provided in the payload here is already validated.
 * Delete twin records to student.
 *
 * @param event                          the event
 * @param saga                           the saga
 * @param penRequestBatchUnmatchSagaData the pen request batch user actions saga data
 * @throws JsonProcessingException the json processing exception
 */
protected void deletePossibleMatchesFromStudent(final Event event, final Saga saga, final PenRequestBatchUnmatchSagaData penRequestBatchUnmatchSagaData) throws JsonProcessingException {
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    // set current event as saga state.
    saga.setSagaState(DELETE_POSSIBLE_MATCH.toString());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    final List<PossibleMatch> possibleMatches = new ArrayList<>();
    penRequestBatchUnmatchSagaData.getMatchedStudentIDList().forEach(element -> {
        final PossibleMatch possibleMatch = new PossibleMatch();
        possibleMatch.setStudentID(penRequestBatchUnmatchSagaData.getStudentID());
        possibleMatch.setMatchedStudentID(element);
        possibleMatch.setCreateUser(penRequestBatchUnmatchSagaData.getCreateUser());
        possibleMatch.setUpdateUser(penRequestBatchUnmatchSagaData.getUpdateUser());
        possibleMatches.add(possibleMatch);
    });
    final Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(DELETE_POSSIBLE_MATCH).replyTo(this.getTopicToSubscribe()).eventPayload(JsonUtil.getJsonStringFromObject(possibleMatches)).build();
    this.postMessageToTopic(PEN_MATCH_API_TOPIC.toString(), nextEvent);
    log.info("message sent to PEN_MATCH_API_TOPIC for DELETE_POSSIBLE_MATCH Event.");
}
Also used : PossibleMatch(ca.bc.gov.educ.penreg.api.struct.v1.PossibleMatch) ArrayList(java.util.ArrayList) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent) Event(ca.bc.gov.educ.penreg.api.struct.Event) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 2 with PossibleMatch

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

the class PenReqBatchUserUnmatchOrchestratorTest method testHandleEvent_givenEventAndSagaDataWithTwins_shouldPostEventToPenMatchApi.

@Test
public void testHandleEvent_givenEventAndSagaDataWithTwins_shouldPostEventToPenMatchApi() throws IOException, InterruptedException, TimeoutException {
    final var invocations = mockingDetails(this.messagePublisher).getInvocations().size();
    final var event = Event.builder().eventType(UPDATE_STUDENT).eventOutcome(EventOutcome.STUDENT_UPDATED).sagaId(this.saga.getSagaId()).eventPayload(JsonUtil.getJsonStringFromObject(this.sagaData)).build();
    this.orchestrator.handleEvent(event);
    verify(this.messagePublisher, atMost(invocations + 1)).dispatchMessage(eq(PEN_MATCH_API_TOPIC.toString()), this.eventCaptor.capture());
    final var newEvent = JsonUtil.getJsonObjectFromString(Event.class, new String(this.eventCaptor.getValue()));
    assertThat(newEvent.getEventType()).isEqualTo(DELETE_POSSIBLE_MATCH);
    final List<PossibleMatch> payload = new ObjectMapper().readValue(newEvent.getEventPayload(), new TypeReference<>() {
    });
    assertThat(payload).size().isEqualTo(1);
    assertThat(payload.get(0).getMatchedStudentID()).isEqualTo(this.studentTwinID);
    final var sagaFromDB = this.sagaService.findSagaById(this.saga.getSagaId());
    assertThat(sagaFromDB).isPresent();
    final var currentSaga = sagaFromDB.get();
    assertThat(currentSaga.getSagaState()).isEqualTo(DELETE_POSSIBLE_MATCH.toString());
    final var sagaStates = this.sagaService.findAllSagaStates(this.saga);
    assertThat(sagaStates.size()).isEqualTo(1);
    assertThat(sagaStates.get(0).getSagaEventState()).isEqualTo(UPDATE_STUDENT.toString());
    assertThat(sagaStates.get(0).getSagaEventOutcome()).isEqualTo(EventOutcome.STUDENT_UPDATED.toString());
}
Also used : PossibleMatch(ca.bc.gov.educ.penreg.api.struct.v1.PossibleMatch) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with PossibleMatch

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

the class PenReqBatchUserMatchOrchestratorTest method testHandleEvent_givenEventAndSagaDataWithTwins_shouldPostEventToMatchApi.

@Test
public void testHandleEvent_givenEventAndSagaDataWithTwins_shouldPostEventToMatchApi() throws IOException, InterruptedException, TimeoutException {
    final var studentPayload = Student.builder().studentID(this.studentID).pen(TEST_PEN).legalFirstName("Jack").build();
    final var invocations = mockingDetails(this.messagePublisher).getInvocations().size();
    final var event = Event.builder().eventType(UPDATE_STUDENT).eventOutcome(EventOutcome.STUDENT_UPDATED).sagaId(this.saga.getSagaId()).eventPayload(JsonUtil.getJsonStringFromObject(studentPayload)).build();
    this.orchestrator.handleEvent(event);
    verify(this.messagePublisher, atMost(invocations + 1)).dispatchMessage(eq(PEN_MATCH_API_TOPIC.toString()), this.eventCaptor.capture());
    final var newEvent = JsonUtil.getJsonObjectFromString(Event.class, new String(this.eventCaptor.getValue()));
    assertThat(newEvent.getEventType()).isEqualTo(ADD_POSSIBLE_MATCH);
    final ObjectMapper objectMapper = new ObjectMapper();
    final List<PossibleMatch> possibleMatches = objectMapper.readValue(newEvent.getEventPayload(), new TypeReference<>() {
    });
    assertThat(possibleMatches).size().isEqualTo(1);
    assertThat(possibleMatches.get(0).getStudentID()).isEqualTo(this.studentID);
    assertThat(possibleMatches.get(0).getMatchReasonCode()).isEqualTo(TwinReasonCodes.PEN_MATCH.getCode());
    assertThat(possibleMatches.get(0).getCreateUser()).isEqualTo("test");
    assertThat(possibleMatches.get(0).getUpdateUser()).isEqualTo("test");
    final var sagaFromDB = this.sagaService.findSagaById(this.saga.getSagaId());
    assertThat(sagaFromDB).isPresent();
    final var currentSaga = sagaFromDB.get();
    assertThat(currentSaga.getSagaState()).isEqualTo(ADD_POSSIBLE_MATCH.toString());
    assertThat(this.getPenRequestBatchUserActionsSagaDataFromJsonString(currentSaga.getPayload()).getAssignedPEN()).isEqualTo(TEST_PEN);
    final var sagaStates = this.sagaService.findAllSagaStates(this.saga);
    assertThat(sagaStates.size()).isEqualTo(1);
    assertThat(sagaStates.get(0).getSagaEventState()).isEqualTo(UPDATE_STUDENT.toString());
    assertThat(sagaStates.get(0).getSagaEventOutcome()).isEqualTo(EventOutcome.STUDENT_UPDATED.toString());
}
Also used : PossibleMatch(ca.bc.gov.educ.penreg.api.struct.v1.PossibleMatch) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

PossibleMatch (ca.bc.gov.educ.penreg.api.struct.v1.PossibleMatch)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Test (org.junit.Test)2 SagaEvent (ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)1 Event (ca.bc.gov.educ.penreg.api.struct.Event)1 ArrayList (java.util.ArrayList)1