Skip to content

Commit

Permalink
add student id and graduated status to grad stud messaging (#706)
Browse files Browse the repository at this point in the history
* add student id and graduated status to grad stud messaging

* test parseGraduationStatus
  • Loading branch information
alexmcdermid authored Nov 28, 2024
1 parent fd2b9bf commit 38f1f56
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ public void onMessage(Message message) {

private String getResponse(GradStudentRecord studentRecord) throws JsonProcessingException {
GradStudentRecordPayload gradStudentRecordPayload = GradStudentRecordPayload.builder()
.studentID(String.valueOf(studentRecord.getStudentID()))
.program(studentRecord.getProgram())
.programCompletionDate(studentRecord.getProgramCompletionDate() != null ? EducGradStudentApiUtils.formatDate(studentRecord.getProgramCompletionDate()) : null)
.schoolOfRecord(studentRecord.getSchoolOfRecord())
.studentStatusCode(studentRecord.getStudentStatusCode())
.graduated(studentRecord.getGraduated().toString())
.build();
return JsonUtil.getJsonStringFromObject(gradStudentRecordPayload);
}
Expand All @@ -83,7 +85,7 @@ private String getErrorResponse(Exception e) {
return JsonUtil.getJsonStringFromObject(gradStudentRecordPayload);
} catch (JsonProcessingException exc) {
log.error("Error while serializing error response", exc);
return "{\"program\": \"\", \"programCompletionDate\": \"\", \"schoolOfRecord\": \"\", \"studentStatusCode\": \"\", \"exception\": \"JSON Parsing exception\"}";
return "{\"studentID\": \"\", \"program\": \"\", \"programCompletionDate\": \"\", \"schoolOfRecord\": \"\", \"studentStatusCode\": \"\", \"graduated\": \"\", \"exception\": \"JSON Parsing exception\"}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
@Builder
public class GradStudentRecordPayload {

private String studentID;
private String exception;
private String program;
private String programCompletionDate;
private String schoolOfRecord;
private String studentStatusCode;
private String graduated;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public class GradStudentRecord {
private Date programCompletionDate;
private String schoolOfRecord;
private String studentStatusCode;
private String studentProjectedGradData;
private Boolean graduated;

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import ca.bc.gov.educ.api.gradstudent.repository.GraduationStudentRecordRepository;
import ca.bc.gov.educ.api.gradstudent.util.EducGradStudentApiConstants;
import ca.bc.gov.educ.api.gradstudent.util.ThreadLocalStateUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.resilience4j.retry.annotation.Retry;
import jakarta.transaction.Transactional;
Expand Down Expand Up @@ -432,8 +434,22 @@ public List<UUID> getStudentIDsBySearchCriteriaOrAll(StudentSearchRequest search
public GradStudentRecord getGraduationStudentRecord(UUID studentID) {
GradStudentRecord response = graduationStatusRepository.findByStudentID(studentID, GradStudentRecord.class);
if (response != null) {
response.setGraduated(parseGraduationStatus(response.getStudentProjectedGradData()));
return response;
}
throw new EntityNotFoundException(String.format(STD_NOT_FOUND_MSG, studentID));
}

public Boolean parseGraduationStatus(String studentProjectedGradData) {
if (studentProjectedGradData == null || studentProjectedGradData.isEmpty()) {
return false;
}
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(studentProjectedGradData);
return jsonNode.get("graduated").asBoolean();
} catch (JsonProcessingException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
import java.util.function.Function;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -863,7 +862,7 @@ public void testGetGraduationStudentRecord_GivenValidProgramCompletionDate_Expec
UUID studentID = UUID.randomUUID();
GraduationStudentRecordEntity graduationStudentRecordEntity = new GraduationStudentRecordEntity();
graduationStudentRecordEntity.setProgramCompletionDate(new java.util.Date());
when(graduationStatusRepository.findByStudentID(studentID, GradStudentRecord.class)).thenReturn(new GradStudentRecord(studentID, "2018-EN", new java.util.Date(), "schoolOfRecord", "studentStatusCode"));
when(graduationStatusRepository.findByStudentID(studentID, GradStudentRecord.class)).thenReturn(new GradStudentRecord(studentID, "2018-EN", new java.util.Date(), "schoolOfRecord", "studentStatusCode", "{\"nonGradReasons\":null,\"graduated\":true}", Boolean.TRUE));
GradStudentRecord result = gradStudentService.getGraduationStudentRecord(studentID);
assertNotNull(result);
}
Expand All @@ -877,6 +876,95 @@ public void testGetGraduationStudentRecord_givenNotFound_ExpectEntityNotFoundExc
});
}

@Test
public void testGetGraduationStudentRecord_GivenGraduatedTrue_ExpectGraduated() {
UUID studentID = UUID.randomUUID();
GradStudentRecord mockRecord = new GradStudentRecord(
studentID,
"2018-EN",
new java.util.Date(),
"schoolOfRecord",
"studentStatusCode",
"{\"nonGradReasons\":null,\"graduated\":true}",
true
);
when(graduationStatusRepository.findByStudentID(studentID, GradStudentRecord.class)).thenReturn(mockRecord);

GradStudentRecord result = gradStudentService.getGraduationStudentRecord(studentID);

assertNotNull(result);
assertTrue(result.getGraduated());
}

@Test
public void testGetGraduationStudentRecord_GivenGraduatedFalse_ExpectNotGraduated() {
UUID studentID = UUID.randomUUID();
GradStudentRecord mockRecord = new GradStudentRecord(
studentID,
"2018-EN",
new java.util.Date(),
"schoolOfRecord",
"studentStatusCode",
"{\"nonGradReasons\":[],\"graduated\":false}",
false
);
when(graduationStatusRepository.findByStudentID(studentID, GradStudentRecord.class)).thenReturn(mockRecord);

GradStudentRecord result = gradStudentService.getGraduationStudentRecord(studentID);

assertNotNull(result);
assertFalse(result.getGraduated());
}

@Test
public void testGetGraduationStudentRecord_GivenNullCLOBData_ExpectNotGraduated() {
UUID studentID = UUID.randomUUID();
GradStudentRecord mockRecord = new GradStudentRecord(
studentID,
"2018-EN",
new java.util.Date(),
"schoolOfRecord",
"studentStatusCode",
null,
false
);
when(graduationStatusRepository.findByStudentID(studentID, GradStudentRecord.class)).thenReturn(mockRecord);

GradStudentRecord result = gradStudentService.getGraduationStudentRecord(studentID);

assertNotNull(result);
assertFalse(result.getGraduated());
}

@Test
public void testGetGraduationStudentRecord_GivenRecordNotFound_ExpectEntityNotFoundException() {
UUID studentID = UUID.randomUUID();
when(graduationStatusRepository.findByStudentID(studentID, GradStudentRecord.class)).thenReturn(null);

assertThrows(EntityNotFoundException.class, () -> gradStudentService.getGraduationStudentRecord(studentID));
}

@Test
public void testParseGraduationStatus_GivenNullInput_ExpectFalse() {
String studentProjectedGradData = null;
Boolean result = gradStudentService.parseGraduationStatus(studentProjectedGradData);
assertFalse("Expected false for null input", result);
}

@Test
public void testParseGraduationStatus_GivenEmptyInput_ExpectFalse() {
String studentProjectedGradData = "";
Boolean result = gradStudentService.parseGraduationStatus(studentProjectedGradData);
assertFalse("Expected false for empty input", result);
}

@Test
public void testParseGraduationStatus_GivenMalformedJson_ExpectFalse() {
String malformedJson = "{invalid-json}";
Boolean result = gradStudentService.parseGraduationStatus(malformedJson);
assertFalse("Expected false for malformed JSON", result);
}

@SneakyThrows
protected Object createDataObjectFromJson(String jsonPath, Class<?> clazz) {
String json = readFile(jsonPath);
Expand Down

0 comments on commit 38f1f56

Please sign in to comment.