Skip to content

Commit 46f515c

Browse files
Merge pull request #636 from bcgov/fix/npeOnCreateTranscript
fix/npeOnCreateTranscript
2 parents 3a2c8da + 224425e commit 46f515c

2 files changed

Lines changed: 57 additions & 6 deletions

File tree

api/src/main/java/ca/bc/gov/educ/api/graduation/service/ReportService.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class ReportService {
3939
private static final String GRAD_REPORT_API_DOWN = "GRAD-REPORT-API IS DOWN";
4040
private static final String GRAD_GRADUATION_REPORT_API_DOWN = "GRAD-GRADUATION-REPORT-API IS DOWN";
4141
private static final String DOCUMENT_STATUS_COMPLETED = "COMPL";
42+
private static final String REPORT_NOT_GENERATED = "UNABLE TO GENERATE REPORT DATA";
4243

4344
JsonTransformer jsonTransformer;
4445
EducGraduationApiConstants educGraduationApiConstants;
@@ -191,7 +192,8 @@ public ReportData prepareTranscriptData(ca.bc.gov.educ.api.graduation.model.dto.
191192
data.getStudent().setOtherProgramParticipation(otherPrograms);
192193
return data;
193194
} catch (Exception e) {
194-
exception.setExceptionName("UNABLE TO GENERATE REPORT DATA");
195+
log.error("{} for student: {} due to:", REPORT_NOT_GENERATED, gradResponse != null ? gradResponse.getStudentID() : "unknown", e);
196+
exception.setExceptionName(REPORT_NOT_GENERATED);
195197
exception.setExceptionDetails(e.getCause() == null ? e.getLocalizedMessage() : e.getCause().getLocalizedMessage());
196198
}
197199
ReportData errorData = new ReportData();
@@ -367,14 +369,14 @@ private boolean isValidCutOffCourse(List<StudentCourse> studentCourseList, Stude
367369
private void addIntoTranscriptList(TranscriptResult transcriptResult, List<TranscriptResult> tList) {
368370
List<TranscriptResult> dups = tList.stream().filter(tr -> tr.getCourse().isDuplicate(transcriptResult.getCourse()) &&
369371
!tr.getCourse().equals(transcriptResult.getCourse())
370-
).sorted(Comparator.comparing(TranscriptResult::getCompletedPercentage, Comparator.nullsLast(Double::compareTo)).reversed()).toList();
372+
).sorted(Comparator.comparing(TranscriptResult::getCompletedPercentage, Comparator.nullsLast(Comparator.reverseOrder()))).toList();
371373

372374

373375
// Handling duplicates
374376
if (!dups.isEmpty()) {
375377
TranscriptResult tr = dups.get(0);
376378
// GRAD2-2394: only if a course taken previously was not used for grad(= requirementMet is blank), then the highest course will be taken
377-
if (StringUtils.isBlank(tr.getRequirement()) && tr.getCompletedPercentage() < transcriptResult.getCompletedPercentage()) {
379+
if (StringUtils.isBlank(tr.getRequirement()) && isHigherCompletedPercentage(tr, transcriptResult)) {
378380
// replace
379381
tList.remove(tr);
380382
tList.add(transcriptResult);
@@ -384,6 +386,21 @@ private void addIntoTranscriptList(TranscriptResult transcriptResult, List<Trans
384386
tList.add(transcriptResult);
385387
}
386388

389+
private boolean isHigherCompletedPercentage(TranscriptResult existing, TranscriptResult candidate) {
390+
if (existing == null || candidate == null) {
391+
return false;
392+
}
393+
Double existingPercentage = existing.getCompletedPercentage();
394+
Double candidatePercentage = candidate.getCompletedPercentage();
395+
if (candidatePercentage == null) {
396+
return false;
397+
}
398+
if (existingPercentage == null) {
399+
return true;
400+
}
401+
return candidatePercentage > existingPercentage;
402+
}
403+
387404
private TranscriptCourse setCourseObjForTranscript(StudentCourse sc, ca.bc.gov.educ.api.graduation.model.dto.GraduationData graduationDataStatus) {
388405
TranscriptCourse crse = new TranscriptCourse();
389406
crse.setCode(sc.getCourseCode());

api/src/test/java/ca/bc/gov/educ/api/graduation/service/ReportServiceTest.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import ca.bc.gov.educ.api.graduation.exception.EntityNotFoundException;
44
import ca.bc.gov.educ.api.graduation.exception.ServiceException;
55
import ca.bc.gov.educ.api.graduation.model.dto.*;
6+
import ca.bc.gov.educ.api.graduation.model.dto.GraduationData;
67
import ca.bc.gov.educ.api.graduation.model.dto.institute.YearEndReportRequest;
7-
import ca.bc.gov.educ.api.graduation.model.report.Code;
8-
import ca.bc.gov.educ.api.graduation.model.report.ReportData;
9-
import ca.bc.gov.educ.api.graduation.model.report.Transcript;
8+
import ca.bc.gov.educ.api.graduation.model.report.*;
109
import ca.bc.gov.educ.api.graduation.util.*;
1110
import com.fasterxml.jackson.core.type.TypeReference;
1211
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -149,6 +148,41 @@ public void testIsValidCutOffCoursePrefersHighestNonNullPercentage() {
149148
assertFalse(nullResult);
150149
}
151150

151+
@Test
152+
public void testIsHigherCompletedPercentageHandlesNullsAndComparison() {
153+
TranscriptResult existing = new TranscriptResult();
154+
Mark existingMark = new Mark();
155+
existingMark.setCompletedCoursePercentage(70.0);
156+
existing.setMark(existingMark);
157+
158+
TranscriptResult higherCandidate = new TranscriptResult();
159+
Mark higherCandidateMark = new Mark();
160+
higherCandidateMark.setCompletedCoursePercentage(85.0);
161+
higherCandidate.setMark(higherCandidateMark);
162+
163+
Boolean higherResult = ReflectionTestUtils.invokeMethod(reportService, "isHigherCompletedPercentage", existing, higherCandidate);
164+
assertTrue(higherResult);
165+
166+
TranscriptResult lowerCandidate = new TranscriptResult();
167+
Mark lowerCandidateMark = new Mark();
168+
lowerCandidateMark.setCompletedCoursePercentage(65.0);
169+
lowerCandidate.setMark(lowerCandidateMark);
170+
171+
Boolean lowerResult = ReflectionTestUtils.invokeMethod(reportService, "isHigherCompletedPercentage", existing, lowerCandidate);
172+
assertFalse(lowerResult);
173+
174+
TranscriptResult nullCandidate = new TranscriptResult();
175+
Boolean nullResult = ReflectionTestUtils.invokeMethod(reportService, "isHigherCompletedPercentage", existing, nullCandidate);
176+
assertFalse(nullResult);
177+
178+
TranscriptResult nullExisting = new TranscriptResult();
179+
Boolean nullExistingResult = ReflectionTestUtils.invokeMethod(reportService, "isHigherCompletedPercentage", nullExisting, higherCandidate);
180+
assertTrue(nullExistingResult);
181+
182+
Boolean bothNullResult = ReflectionTestUtils.invokeMethod(reportService, "isHigherCompletedPercentage", nullExisting, nullCandidate);
183+
assertFalse(bothNullResult);
184+
}
185+
152186
@Test
153187
public void testGetStudentsForSchoolYearEndNonGradReportWithMincode() {
154188
List<ReportGradStudentData> gradStudentDataList = createStudentSchoolYearEndData("json/studentSchoolYearEndResponse.json");

0 commit comments

Comments
 (0)