Skip to content

Commit

Permalink
Merge pull request #574 from bcgov/GRAD2-2136
Browse files Browse the repository at this point in the history
GRAD2-2136
  • Loading branch information
kamal-mohammed authored Jan 9, 2024
2 parents 5ea93b6 + 1fec1a2 commit 29afaa3
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import ca.bc.gov.educ.api.gradstudent.constant.Topics;
import ca.bc.gov.educ.api.gradstudent.exception.EntityNotFoundException;
import ca.bc.gov.educ.api.gradstudent.model.dc.Event;
import ca.bc.gov.educ.api.gradstudent.model.dc.GradStatusPayload;
import ca.bc.gov.educ.api.gradstudent.model.dto.GraduationStudentRecord;
import ca.bc.gov.educ.api.gradstudent.service.GraduationStatusService;
import ca.bc.gov.educ.api.gradstudent.util.EducGradStudentApiConstants;
import ca.bc.gov.educ.api.gradstudent.util.JsonUtil;
import ca.bc.gov.educ.api.gradstudent.util.LogHelper;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.nats.client.*;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -46,13 +49,33 @@ public void onMessage(Message message) {
try {
Event event = JsonUtil.getJsonObjectFromString(Event.class, eventString);
UUID stdId = UUID.fromString(event.getEventPayload());
boolean hasStudentGraduated = graduationStatusService.hasStudentGraduated(stdId);
response = String.valueOf(hasStudentGraduated);
GraduationStudentRecord graduationStatus = graduationStatusService.getGraduationStatus(stdId);
response = getResponse(graduationStatus);
} catch (Exception e) {
response = (e instanceof EntityNotFoundException) ? "not found" : "error";
response = getErrorResponse(e);
LogHelper.logMessagingEventDetails("NATS message exception at FetchGradStatusSubscriber: " + response + " when processing: " + eventString, constants.isSplunkLogHelperEnabled());
}
this.natsConnection.publish(message.getReplyTo(), response.getBytes());
}

private String getResponse(GraduationStudentRecord graduationStudentRecord) throws JsonProcessingException {
GradStatusPayload gradStatusPayload = GradStatusPayload.builder()
.program(graduationStudentRecord.getProgram())
.programCompletionDate(graduationStudentRecord.getProgramCompletionDate())
.build();
return JsonUtil.getJsonStringFromObject(gradStatusPayload);
}

private String getErrorResponse(Exception e) {
String ex = (e instanceof EntityNotFoundException) ? "not found" : "error";
GradStatusPayload gradStatusPayload = GradStatusPayload.builder()
.exception(ex)
.build();
try {
return JsonUtil.getJsonStringFromObject(gradStatusPayload);
} catch (JsonProcessingException exc) {
return "{\"program\": \"\", \"programCompletionDate\": \"\", \"exception\": \"JSON Parsing exception\"}";
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ca.bc.gov.educ.api.gradstudent.model.dc;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class GradStatusPayload {

private String program;
private String programCompletionDate;
private String exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,21 @@ public class GraduationStatusTransformer {
@Autowired
GradValidation validation;

public GraduationStudentRecord transformToDTO (GraduationStudentRecordEntity gradStatusEntity) {
public GraduationStudentRecord transformToDTOWithModifiedProgramCompletionDate(GraduationStudentRecordEntity gradStatusEntity) {
GraduationStudentRecord gradStatus = modelMapper.map(gradStatusEntity, GraduationStudentRecord.class);
gradStatus.setProgramCompletionDate(EducGradStudentApiUtils.parseDateFromString(gradStatusEntity.getProgramCompletionDate() != null ?
EducGradStudentApiUtils.formatDate(gradStatusEntity.getProgramCompletionDate()) : null));
return gradStatus;
}

public GraduationStudentRecord transformToDTO ( Optional<GraduationStudentRecordEntity> gradStatusEntity ) {
public GraduationStudentRecord transformToDTO(GraduationStudentRecordEntity gradStatusEntity) {
GraduationStudentRecord gradStatus = modelMapper.map(gradStatusEntity, GraduationStudentRecord.class);
gradStatus.setProgramCompletionDate(gradStatusEntity.getProgramCompletionDate() != null ?
EducGradStudentApiUtils.formatDate(gradStatusEntity.getProgramCompletionDate()) : null);
return gradStatus;
}

public GraduationStudentRecord transformToDTOWithModifiedProgramCompletionDate(Optional<GraduationStudentRecordEntity> gradStatusEntity ) {
GraduationStudentRecordEntity cae = new GraduationStudentRecordEntity();
if (gradStatusEntity.isPresent())
cae = gradStatusEntity.get();
Expand All @@ -50,7 +57,7 @@ public GraduationStudentRecord transformToDTO ( Optional<GraduationStudentRecord
return gradStatus;
}

public List<GraduationStudentRecord> transformToDTO (Iterable<GraduationStudentRecordEntity> gradStatusEntities ) {
public List<GraduationStudentRecord> transformToDTOWithModifiedProgramCompletionDate(Iterable<GraduationStudentRecordEntity> gradStatusEntities ) {
List<GraduationStudentRecord> gradStatusList = new ArrayList<>();
for (GraduationStudentRecordEntity gradStatusEntity : gradStatusEntities) {
GraduationStudentRecord gradStatus = modelMapper.map(gradStatusEntity, GraduationStudentRecord.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ public GraduationStudentRecord saveGraduationStudentRecord(UUID studentID, Gradu
if (gradStatusOptional.isPresent()) {
GraduationStudentRecordEntity gradEntity = gradStatusOptional.get();
gradEntity = handleExistingGraduationStatus(sourceObject, gradEntity, graduationStatus.getPen(), ongoingUpdate);
return graduationStatusTransformer.transformToDTO(gradEntity);
return graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(gradEntity);
} else {
sourceObject = handleNewGraduationStatus(sourceObject, graduationStatus.getPen(), ongoingUpdate);
return graduationStatusTransformer.transformToDTO(sourceObject);
return graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(sourceObject);
}
}

Expand All @@ -116,7 +116,7 @@ public GraduationStudentRecord updateGraduationStatusByFields(OngoingUpdateReque
if (constants.isStudentGuidPenXrefEnabled() && StringUtils.isNotBlank(requestDTO.getPen())) {
saveStudentGuidPenXref(gradEntity.getStudentID(), requestDTO.getPen());
}
return graduationStatusTransformer.transformToDTO(gradEntity);
return graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(gradEntity);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private GradSearchStudent populateGradSearchStudent(Student student, String acce
BeanUtils.copyProperties(student, gradStu);
GraduationStudentRecordEntity graduationStatusEntity = graduationStatusRepository.findByStudentID(UUID.fromString(student.getStudentID()));
if(graduationStatusEntity != null) {
GraduationStudentRecord gradObj = graduationStatusTransformer.transformToDTO(graduationStatusEntity);
GraduationStudentRecord gradObj = graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(graduationStatusEntity);
gradStu.setProgram(gradObj.getProgram());
gradStu.setStudentGrade(gradObj.getStudentGrade());
gradStu.setStudentStatus(gradObj.getStudentStatus());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public GraduationStudentRecord getGraduationStatusForAlgorithm(UUID studentID) {
logger.debug("getGraduationStatusForAlgorithm");
Optional<GraduationStudentRecordEntity> responseOptional = graduationStatusRepository.findById(studentID);
return responseOptional.map(gs -> {
GraduationStudentRecord gradStatus = graduationStatusTransformer.transformToDTO(gs);
GraduationStudentRecord gradStatus = graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(gs);
List<StudentCareerProgramEntity> studentCareerProgramEntities = gradStudentCareerProgramRepository.findByStudentID(studentID);
gradStatus.setCareerPrograms(gradStudentCareerProgramTransformer.transformToDTO(studentCareerProgramEntities));
return gradStatus;
Expand All @@ -131,7 +131,7 @@ public GraduationStudentRecord getGraduationStatus(UUID studentID, String access
logger.debug("getGraduationStatus");
Optional<GraduationStudentRecordEntity> responseOptional = graduationStatusRepository.findById(studentID);
if (responseOptional.isPresent()) {
GraduationStudentRecord gradStatus = graduationStatusTransformer.transformToDTO(responseOptional.get());
GraduationStudentRecord gradStatus = graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(responseOptional.get());
if (gradStatus.getProgram() != null) {
gradStatus.setProgramName(getProgramName(gradStatus.getProgram(), accessToken));
}
Expand All @@ -154,6 +154,14 @@ public GraduationStudentRecord getGraduationStatus(UUID studentID, String access

}

public GraduationStudentRecord getGraduationStatus(UUID studentID) throws EntityNotFoundException {
Optional<GraduationStudentRecordEntity> responseOptional = graduationStatusRepository.findById(studentID);
if (responseOptional.isPresent()) {
return graduationStatusTransformer.transformToDTO(responseOptional.get());
}
throw new EntityNotFoundException(String.format("Student with ID: %s not found", studentID));
}

@Transactional
@Retry(name = "generalpostcall")
public Pair<GraduationStudentRecord, GradStatusEvent> saveGraduationStatus(UUID studentID, GraduationStudentRecord graduationStatus, Long batchId, String accessToken) throws JsonProcessingException {
Expand Down Expand Up @@ -181,15 +189,15 @@ public Pair<GraduationStudentRecord, GradStatusEvent> saveGraduationStatus(UUID
if (gradStatusEvent != null) {
gradStatusEventRepository.save(gradStatusEvent);
}
return Pair.of(graduationStatusTransformer.transformToDTO(gradEntity), gradStatusEvent);
return Pair.of(graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(gradEntity), gradStatusEvent);
} else {
sourceObject = graduationStatusRepository.saveAndFlush(sourceObject);
final GradStatusEvent gradStatusEvent = createGradStatusEvent(sourceObject.getUpdateUser(), sourceObject,
EventType.GRAD_STUDENT_GRADUATED, EventOutcome.GRAD_STATUS_UPDATED, GRAD_ALG, accessToken);
if (gradStatusEvent != null) {
gradStatusEventRepository.save(gradStatusEvent);
}
return Pair.of(graduationStatusTransformer.transformToDTO(sourceObject), gradStatusEvent);
return Pair.of(graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(sourceObject), gradStatusEvent);
}
}

Expand Down Expand Up @@ -250,7 +258,7 @@ public Pair<GraduationStudentRecord, GradStatusEvent> updateGraduationStatus(UUI
if (gradStatusEvent != null) {
gradStatusEventRepository.save(gradStatusEvent);
}
return Pair.of(graduationStatusTransformer.transformToDTO(gradEntity), gradStatusEvent);
return Pair.of(graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(gradEntity), gradStatusEvent);
} else {
validation.addErrorAndStop(String.format("Student ID [%s] does not exists", studentID));
return Pair.of(graduationStatus, null);
Expand Down Expand Up @@ -885,7 +893,7 @@ public Pair<GraduationStudentRecord, GradStatusEvent> undoCompletionStudent(UUID
if (gradStatusEvent != null) {
gradStatusEventRepository.save(gradStatusEvent);
}
return Pair.of(graduationStatusTransformer.transformToDTO(gradEntity), gradStatusEvent);
return Pair.of(graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(gradEntity), gradStatusEvent);
} else {
validation.addErrorAndStop(String.format("Student ID [%s] does not exists", studentID));
return Pair.of(null, null);
Expand Down Expand Up @@ -997,7 +1005,7 @@ public GraduationStudentRecord saveStudentRecordDistributionRun(UUID studentID,
gradEntity.setBatchId(batchId);
gradEntity = graduationStatusRepository.saveAndFlush(gradEntity);
historyService.createStudentHistory(gradEntity, activityCode);
return graduationStatusTransformer.transformToDTO(gradEntity);
return graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(gradEntity);
}
return null;
}
Expand All @@ -1022,7 +1030,7 @@ public GraduationStudentRecord saveStudentRecordProjectedTVRRun(UUID studentID,
gradEntity.setStudentProjectedGradData(projectedClob);
gradEntity = graduationStatusRepository.saveAndFlush(gradEntity);
historyService.createStudentHistory(gradEntity, "GRADPROJECTED");
return graduationStatusTransformer.transformToDTO(gradEntity);
return graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(gradEntity);
}
return null;
}
Expand All @@ -1038,7 +1046,7 @@ public List<UUID> getStudentsForYearlyDistribution() {
}

public GraduationStudentRecord getDataForBatch(UUID studentID,String accessToken) {
GraduationStudentRecord ent = graduationStatusTransformer.transformToDTO(graduationStatusRepository.findByStudentID(studentID));
GraduationStudentRecord ent = graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(graduationStatusRepository.findByStudentID(studentID));
return processReceivedStudent(ent,accessToken);
}

Expand Down Expand Up @@ -1168,7 +1176,7 @@ private GraduationStudentRecord saveBatchFlagsOfGraduationStudentRecord(Graduati
}
if (isUpdated) {
graduationStatusRepository.save(entity);
return graduationStatusTransformer.transformToDTO(entity);
return graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(entity);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void testGetStudentFromStudentAPI() {
graduationStatusEntity.setSchoolOfRecord(mincode);

when(this.graduationStatusRepository.findByStudentID(studentID)).thenReturn(graduationStatusEntity);
when(this.graduationStatusTransformer.transformToDTO(graduationStatusEntity)).thenReturn(graduationStatus);
when(this.graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(graduationStatusEntity)).thenReturn(graduationStatus);

// School
final School school = new School();
Expand Down Expand Up @@ -244,7 +244,7 @@ public void testGetGRADStudents() {
graduationStatusEntity.setSchoolOfRecord(mincode);

when(this.graduationStatusRepository.findByStudentID(studentID)).thenReturn(graduationStatusEntity);
when(this.graduationStatusTransformer.transformToDTO(graduationStatusEntity)).thenReturn(graduationStatus);
when(this.graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(graduationStatusEntity)).thenReturn(graduationStatus);

org.springframework.data.domain.Page<GraduationStudentRecordEntity> pagedResult = new PageImpl<>(List.of(graduationStatusEntity));
when(this.graduationStatusRepository.findAll(any(), any(Pageable.class))).thenReturn(pagedResult);
Expand Down Expand Up @@ -345,7 +345,7 @@ public void testGetStudentFromStudentAPIGradOnly() {


when(this.graduationStatusRepository.findByStudentID(studentID)).thenReturn(graduationStatusEntity);
when(this.graduationStatusTransformer.transformToDTO(graduationStatusEntity)).thenReturn(graduationStatus);
when(this.graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(graduationStatusEntity)).thenReturn(graduationStatus);
when(this.graduationStatusRepository.findByStudentIDIn(studentSubList)).thenReturn(List.of(graduationStatusEntity));
RestResponsePage<Student> response = new RestResponsePage<>(List.of(student));
final ParameterizedTypeReference<RestResponsePage<Student>> studentResponseType = new ParameterizedTypeReference<>() {
Expand Down Expand Up @@ -505,7 +505,7 @@ public void testGetStudentByPenFromStudentAPI() {
graduationStatusEntity.setSchoolOfRecord(mincode);

when(this.graduationStatusRepository.findByStudentID(studentID)).thenReturn(graduationStatusEntity);
when(this.graduationStatusTransformer.transformToDTO(graduationStatusEntity)).thenReturn(graduationStatus);
when(this.graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(graduationStatusEntity)).thenReturn(graduationStatus);

// School
final School school = new School();
Expand Down Expand Up @@ -597,7 +597,7 @@ public void testGetStudentByStudentIDFromStudentAPI() {
graduationStatusEntity.setSchoolOfRecord(mincode);

when(this.graduationStatusRepository.findByStudentID(studentID)).thenReturn(graduationStatusEntity);
when(this.graduationStatusTransformer.transformToDTO(graduationStatusEntity)).thenReturn(graduationStatus);
when(this.graduationStatusTransformer.transformToDTOWithModifiedProgramCompletionDate(graduationStatusEntity)).thenReturn(graduationStatus);

// School
final School school = new School();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ public void testHasStudentGraduated_GivenNotFound_ExpectEntityNotFoundException(
});
}

@Test
public void testGetGraduationStatus_GivenValidProgramCompletionDate_ExpectTrue() throws EntityNotFoundException {
UUID studentID = UUID.randomUUID();
GraduationStudentRecordEntity graduationStatusEntity = new GraduationStudentRecordEntity();
graduationStatusEntity.setProgramCompletionDate(new java.util.Date());
when(graduationStatusRepository.findById(studentID)).thenReturn(Optional.of(graduationStatusEntity));
GraduationStudentRecord result = graduationStatusService.getGraduationStatus(studentID);
assertNotNull(result);
}

@Test
public void testGetGraduationStatus_givenNotFound_ExpectEntityNotFoundExcetpion() {
UUID studentID = UUID.randomUUID();
when(graduationStatusRepository.findById(studentID)).thenReturn(Optional.empty());
assertThrows(EntityNotFoundException.class, () -> {
graduationStatusService.getGraduationStatus(studentID);
});
}

@Test
public void testGetGraduationStatusForAlgorithm() {
// ID
Expand Down

0 comments on commit 29afaa3

Please sign in to comment.