Search in sources :

Example 1 with PenRequestBatchStudentEntity

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

the class PenRequestBatchAPIController method findAllStudents.

/**
 * Find all students completable future.
 *
 * @param pageNumber             the page number
 * @param pageSize               the page size
 * @param sortCriteriaJson       the sort criteria json
 * @param searchCriteriaListJson the search criteria list json
 * @return the completable future
 */
@Override
public CompletableFuture<Page<PenRequestBatchStudent>> findAllStudents(final Integer pageNumber, final Integer pageSize, final String sortCriteriaJson, final String searchCriteriaListJson) {
    final List<Sort.Order> sorts = new ArrayList<>();
    Specification<PenRequestBatchStudentEntity> penRequestBatchStudentEntitySpecification = null;
    try {
        final var associationNames = this.getSortCriteria(sortCriteriaJson, JsonUtil.mapper, sorts);
        if (StringUtils.isNotBlank(searchCriteriaListJson)) {
            final List<Search> searches = JsonUtil.mapper.readValue(searchCriteriaListJson, new TypeReference<>() {
            });
            this.getAssociationNamesFromSearchCriterias(associationNames, searches);
            int i = 0;
            for (final var search : searches) {
                penRequestBatchStudentEntitySpecification = this.getSpecifications(penRequestBatchStudentEntitySpecification, i, search, associationNames, this.penRegBatchStudentFilterSpecs);
                i++;
            }
        }
    } catch (final JsonProcessingException e) {
        throw new InvalidParameterException(e.getMessage());
    }
    return this.getStudentService().findAll(penRequestBatchStudentEntitySpecification, pageNumber, pageSize, sorts).thenApplyAsync(penRegBatchEntities -> penRegBatchEntities.map(studentMapper::toStructure));
}
Also used : InvalidParameterException(ca.bc.gov.educ.penreg.api.exception.InvalidParameterException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) PenRequestBatchAPIEndpoint(ca.bc.gov.educ.penreg.api.endpoint.v1.PenRequestBatchAPIEndpoint) PenRequestBatchStudentEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity)

Example 2 with PenRequestBatchStudentEntity

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

the class PenRequestBatchFileService method checkBatchForRepeatRequests.

private void checkBatchForRepeatRequests(String guid, PenRequestBatchEntity penRequestBatchEntity, Set<PenRequestBatchStudentEntity> studentEntities, Set<PenRequestBatchStudentEntity> filteredStudentEntities) {
    long numRepeats = 0;
    final Map<String, List<PenRequestBatchStudentEntity>> repeatCheckMap = this.penRequestBatchStudentService.populateRepeatCheckMap(penRequestBatchEntity);
    if (repeatCheckMap.size() > 0) {
        for (final PenRequestBatchStudentEntity penRequestBatchStudent : studentEntities) {
            if (PenRequestBatchStudentStatusCodes.DUPLICATE.getCode().equals(penRequestBatchStudent.getPenRequestBatchStudentStatusCode())) {
                // no need to do any checking for DUPLICATE student requests, continue further.
                continue;
            }
            final String repeatCheckKey = this.penRequestBatchStudentService.constructKeyGivenBatchStudent(penRequestBatchStudent);
            if (repeatCheckMap.containsKey(repeatCheckKey)) {
                // if it is a repeat remove it from the list to be further processed.
                filteredStudentEntities.remove(penRequestBatchStudent);
                this.updatePenRequestBatchStudentRequest(repeatCheckMap.get(repeatCheckKey), penRequestBatchStudent);
                numRepeats++;
            }
        }
    }
    log.debug("{} :: Found {} total repeats", guid, numRepeats);
    penRequestBatchEntity.setRepeatCount(numRepeats);
}
Also used : PenRequestBatchStudentEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity)

Example 3 with PenRequestBatchStudentEntity

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

the class PenRegBatchProcessorTest method testProcessPenRegBatchFileFromTSW_Given30RowValidFile_ShouldCreateRecordsInDB.

/**
 * Test process pen reg batch file from tsw given 30 row valid file should create records in db.
 *
 * @throws IOException the io exception
 */
@Test
@Transactional
public void testProcessPenRegBatchFileFromTSW_Given30RowValidFile_ShouldCreateRecordsInDB() throws IOException {
    when(this.restUtils.getSchoolByMincode(anyString())).thenReturn(Optional.of(this.createMockSchool()));
    final File file = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource("sample_30_records_OK.txt")).getFile());
    final byte[] bFile = Files.readAllBytes(file.toPath());
    final var randomNum = (new Random().nextLong() * (MAX - MIN + 1) + MIN);
    var tsw = PENWebBlobEntity.builder().penWebBlobId(1L).mincode("66510518").sourceApplication("MYED").tswAccount((randomNum + "").substring(0, 8)).fileName("sample_30_records_OK").fileType("PEN").fileContents(bFile).insertDateTime(LocalDateTime.now()).submissionNumber(("T" + randomNum).substring(0, 8)).build();
    tsw = this.penRequestBatchTestUtils.savePenWebBlob(tsw);
    this.penRegBatchProcessor.processPenRegBatchFileFromPenWebBlob(tsw);
    final var result = this.repository.findAll();
    assertThat(result.size()).isEqualTo(1);
    final var entity = result.get(0);
    assertThat(entity.getPenRequestBatchID()).isNotNull();
    assertThat(entity.getPenRequestBatchStatusCode()).isEqualTo(REPEATS_CHECKED.getCode());
    assertThat(entity.getSchoolGroupCode()).isEqualTo(K12.getCode());
    assertThat(entity.getPenRequestBatchStatusReason()).isNull();
    assertThat(entity.getRepeatCount()).isZero();
    final var students = this.studentRepository.findAllByPenRequestBatchEntity(result.get(0));
    assertThat(entity.getPenRequestBatchHistoryEntities().size()).isEqualTo(2);
    final Optional<PenRequestBatchHistoryEntity> penRequestBatchHistoryEntityOptional = entity.getPenRequestBatchHistoryEntities().stream().min(new PenRequestBatchHistoryComparator());
    assertThat(penRequestBatchHistoryEntityOptional).isPresent();
    assertThat(penRequestBatchHistoryEntityOptional.get().getPenRequestBatchEventCode()).isEqualTo(STATUS_CHANGED.getCode());
    assertThat(penRequestBatchHistoryEntityOptional.get().getPenRequestBatchStatusReason()).isNull();
    assertThat(students.size()).isEqualTo(30);
    students.sort(Comparator.comparing(PenRequestBatchStudentEntity::getRecordNumber));
    log.error("students {}", students);
    var counter = 1;
    for (final PenRequestBatchStudentEntity student : students) {
        assertThat(counter++).isEqualTo(student.getRecordNumber());
    }
}
Also used : PenRequestBatchHistoryEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchHistoryEntity) PenRequestBatchHistoryComparator(ca.bc.gov.educ.penreg.api.compare.PenRequestBatchHistoryComparator) Random(java.util.Random) File(java.io.File) PenRequestBatchStudentEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity) BasePenRegAPITest(ca.bc.gov.educ.penreg.api.BasePenRegAPITest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with PenRequestBatchStudentEntity

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

the class PenRegBatchProcessorTest method testProcessPenRegBatchFileFromTSW_Given30RowValidFileAndExistingRecords_ShouldShowRepeats.

/**
 * Test process pen reg batch file from tsw given 30 row valid file should create records in db.
 *
 * @throws IOException the io exception
 */
@Test
@Transactional
public void testProcessPenRegBatchFileFromTSW_Given30RowValidFileAndExistingRecords_ShouldShowRepeats() throws IOException {
    when(this.restUtils.getSchoolByMincode(anyString())).thenReturn(Optional.of(this.createMockSchool()));
    this.penRequestBatchTestUtils.createBatchStudentsInSingleTransaction(this.repository, "mock_pen_req_batch_repeat.json", "mock_pen_req_batch_student_repeat.json", 1, (batch) -> batch.setProcessDate(LocalDateTime.now().minusDays(3)));
    final File file = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource("sample_5_K12_OK.txt")).getFile());
    final byte[] bFile = Files.readAllBytes(file.toPath());
    final var randomNum = (new Random().nextLong() * (MAX - MIN + 1) + MIN);
    var tsw = PENWebBlobEntity.builder().penWebBlobId(1L).mincode("66510518").sourceApplication("MYED").tswAccount((randomNum + "").substring(0, 8)).fileName("sample_5_K12_OK").fileType("PEN").fileContents(bFile).insertDateTime(LocalDateTime.now()).submissionNumber(("T" + randomNum).substring(0, 8)).build();
    tsw = this.penRequestBatchTestUtils.savePenWebBlob(tsw);
    this.penRegBatchProcessor.processPenRegBatchFileFromPenWebBlob(tsw);
    final var result = this.repository.findAll();
    assertThat(result.size()).isEqualTo(2);
    final var entity = result.get(1);
    assertThat(entity.getPenRequestBatchID()).isNotNull();
    assertThat(entity.getPenRequestBatchStatusCode()).isEqualTo(REPEATS_CHECKED.getCode());
    assertThat(entity.getSchoolGroupCode()).isEqualTo(K12.getCode());
    assertThat(entity.getPenRequestBatchHistoryEntities().size()).isEqualTo(2);
    assertThat(entity.getRepeatCount()).isEqualTo(1);
    final Optional<PenRequestBatchHistoryEntity> penRequestBatchHistoryEntityOptional = entity.getPenRequestBatchHistoryEntities().stream().min(new PenRequestBatchHistoryComparator());
    assertThat(penRequestBatchHistoryEntityOptional).isPresent();
    assertThat(penRequestBatchHistoryEntityOptional.get().getPenRequestBatchEventCode()).isEqualTo(STATUS_CHANGED.getCode());
    assertThat(penRequestBatchHistoryEntityOptional.get().getPenRequestBatchStatusReason()).isNull();
    final var students = this.studentRepository.findAllByPenRequestBatchEntity(result.get(1));
    assertThat(students.stream().filter(s -> PenRequestBatchStudentStatusCodes.REPEAT.getCode().equals(s.getPenRequestBatchStudentStatusCode())).count()).isEqualTo(1);
    assertThat(students.size()).isEqualTo(5);
    students.sort(Comparator.comparing(PenRequestBatchStudentEntity::getRecordNumber));
    log.error("students {}", students);
    var counter = 1;
    for (final PenRequestBatchStudentEntity student : students) {
        assertThat(counter++).isEqualTo(student.getRecordNumber());
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BasePenRegAPITest(ca.bc.gov.educ.penreg.api.BasePenRegAPITest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LocalDateTime(java.time.LocalDateTime) DUPLICATE(ca.bc.gov.educ.penreg.api.constants.PenRequestBatchStudentStatusCodes.DUPLICATE) Autowired(org.springframework.beans.factory.annotation.Autowired) PenRequestBatchTestUtils(ca.bc.gov.educ.penreg.api.support.PenRequestBatchTestUtils) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) PenRequestBatchStudentRepository(ca.bc.gov.educ.penreg.api.repository.PenRequestBatchStudentRepository) JsonUtil(ca.bc.gov.educ.penreg.api.util.JsonUtil) PenRequestBatchStudentEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity) Message(io.nats.client.Message) Subscription(io.nats.client.Subscription) PENWebBlobEntity(ca.bc.gov.educ.penreg.api.model.v1.PENWebBlobEntity) PSI(ca.bc.gov.educ.penreg.api.constants.SchoolGroupCodes.PSI) Faker(com.github.javafaker.Faker) Connection(io.nats.client.Connection) Duration(java.time.Duration) PenRequestBatchHistoryComparator(ca.bc.gov.educ.penreg.api.compare.PenRequestBatchHistoryComparator) K12(ca.bc.gov.educ.penreg.api.constants.SchoolGroupCodes.K12) PenRequestBatchHistoryEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchHistoryEntity) STATUS_CHANGED(ca.bc.gov.educ.penreg.api.constants.PenRequestBatchEventCodes.STATUS_CHANGED) SchoolGroupCodes(ca.bc.gov.educ.penreg.api.constants.SchoolGroupCodes) PenRequestBatchRepository(ca.bc.gov.educ.penreg.api.repository.PenRequestBatchRepository) RestUtils(ca.bc.gov.educ.penreg.api.rest.RestUtils) Before(org.junit.Before) Status(io.nats.client.support.Status) PenRequestBatchStatusCodes(ca.bc.gov.educ.penreg.api.constants.PenRequestBatchStatusCodes) PenCoordinator(ca.bc.gov.educ.penreg.api.struct.v1.PenCoordinator) PenRequestBatchStudentStatusCodes(ca.bc.gov.educ.penreg.api.constants.PenRequestBatchStudentStatusCodes) Files(java.nio.file.Files) School(ca.bc.gov.educ.penreg.api.struct.School) Headers(io.nats.client.impl.Headers) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) File(java.io.File) SchoolGroupCode(ca.bc.gov.educ.penreg.api.struct.v1.SchoolGroupCode) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) PenWebBlobRepository(ca.bc.gov.educ.penreg.api.repository.PenWebBlobRepository) Optional(java.util.Optional) MessagePublisher(ca.bc.gov.educ.penreg.api.messaging.MessagePublisher) Comparator(java.util.Comparator) NatsJetStreamMetaData(io.nats.client.impl.NatsJetStreamMetaData) Transactional(org.springframework.transaction.annotation.Transactional) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PenRequestBatchHistoryEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchHistoryEntity) PenRequestBatchHistoryComparator(ca.bc.gov.educ.penreg.api.compare.PenRequestBatchHistoryComparator) Random(java.util.Random) File(java.io.File) PenRequestBatchStudentEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity) BasePenRegAPITest(ca.bc.gov.educ.penreg.api.BasePenRegAPITest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with PenRequestBatchStudentEntity

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

the class PenRequestBatchEventService method updatePenRequestBatchStudent.

@Transactional(propagation = Propagation.REQUIRES_NEW)
public PenRequestBatchEvent updatePenRequestBatchStudent(final Event event) throws JsonProcessingException {
    val penRequestBatchEventOptional = this.getPenRequestBatchEventRepository().findBySagaIdAndEventType(event.getSagaId(), event.getEventType().toString());
    final PenRequestBatchEvent penRequestBatchEvent;
    if (penRequestBatchEventOptional.isEmpty()) {
        log.info(NO_RECORD_SAGA_ID_EVENT_TYPE);
        log.trace(EVENT_PAYLOAD, event);
        final var prbStudent = JsonUtil.getJsonObjectFromString(PenRequestBatchStudent.class, event.getEventPayload());
        final PenRequestBatchStudentEntity entity = prbStudentMapper.toModel(prbStudent);
        this.populateAuditColumnsForUpdateStudent(entity);
        try {
            this.prbStudentService.updateStudent(entity, UUID.fromString(prbStudent.getPenRequestBatchID()), UUID.fromString(prbStudent.getPenRequestBatchStudentID()));
            // need to convert to structure MANDATORY otherwise jackson will break.
            event.setEventPayload(JsonUtil.getJsonStringFromObject(prbStudentMapper.toStructure(entity)));
            event.setEventOutcome(EventOutcome.PEN_REQUEST_BATCH_STUDENT_UPDATED);
        } catch (final EntityNotFoundException ex) {
            log.error("PenRequestBatchStudent not found while trying to update it", ex);
            event.setEventOutcome(EventOutcome.PEN_REQUEST_BATCH_STUDENT_NOT_FOUND);
        }
        penRequestBatchEvent = this.createPenRequestBatchEventRecord(event);
    } else {
        log.info(RECORD_FOUND_FOR_SAGA_ID_EVENT_TYPE);
        log.trace(EVENT_PAYLOAD, event);
        penRequestBatchEvent = penRequestBatchEventOptional.get();
        penRequestBatchEvent.setEventStatus(MESSAGE_PUBLISHED.toString());
    }
    this.getPenRequestBatchEventRepository().save(penRequestBatchEvent);
    return penRequestBatchEvent;
}
Also used : lombok.val(lombok.val) PenRequestBatchEvent(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEvent) EntityNotFoundException(ca.bc.gov.educ.penreg.api.exception.EntityNotFoundException) PenRequestBatchStudentEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

PenRequestBatchStudentEntity (ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity)26 Test (org.junit.Test)17 PenRequestBatchEntity (ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEntity)16 Transactional (org.springframework.transaction.annotation.Transactional)12 PenMatchResult (ca.bc.gov.educ.penreg.api.struct.PenMatchResult)11 PenMatchRecord (ca.bc.gov.educ.penreg.api.struct.PenMatchRecord)10 lombok.val (lombok.val)10 BaseOrchestratorTest (ca.bc.gov.educ.penreg.api.orchestrator.BaseOrchestratorTest)9 BasePenRegAPITest (ca.bc.gov.educ.penreg.api.BasePenRegAPITest)8 File (java.io.File)8 PenRequestBatchStudentStatusCodes (ca.bc.gov.educ.penreg.api.constants.PenRequestBatchStudentStatusCodes)7 LocalDateTime (java.time.LocalDateTime)7 Autowired (org.springframework.beans.factory.annotation.Autowired)7 PenRequestBatchHistoryComparator (ca.bc.gov.educ.penreg.api.compare.PenRequestBatchHistoryComparator)6 PenRequestBatchHistoryEntity (ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchHistoryEntity)6 Student (ca.bc.gov.educ.penreg.api.struct.Student)6 Before (org.junit.Before)6 PENWebBlobEntity (ca.bc.gov.educ.penreg.api.model.v1.PENWebBlobEntity)5 RestUtils (ca.bc.gov.educ.penreg.api.rest.RestUtils)5 JsonUtil (ca.bc.gov.educ.penreg.api.util.JsonUtil)5