From 8cb53c0a9444595026fd9f8d46a9a55ad1d71b93 Mon Sep 17 00:00:00 2001 From: Jinil Sung Date: Fri, 20 Dec 2024 16:37:41 -0800 Subject: [PATCH] Unit tests as unexpected path are fixed to get more coverage. Unit tests as unexpected path are fixed to get more coverage. --- .../api/batchgraduation/rest/RestUtils.java | 27 ++---- .../batchgraduation/util/RestUtilsTest.java | 87 ++++++++++++++----- 2 files changed, 76 insertions(+), 38 deletions(-) diff --git a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/rest/RestUtils.java b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/rest/RestUtils.java index f71994ad..35db2aa5 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/rest/RestUtils.java +++ b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/rest/RestUtils.java @@ -613,8 +613,7 @@ public Long getTotalReportsForProcessing(List finalSchoolDistricts, String Long reportsCount = 0L; ThreadLocalStateUtil.setCorrelationID(UUID.randomUUID().toString()); try { - String accessToken = getAccessToken(); - reportsCount = restService.post(String.format(constants.getGradSchoolReportsCountUrl(), reportType), finalSchoolDistricts, Long.class, accessToken); + reportsCount = restService.post(String.format(constants.getGradSchoolReportsCountUrl(), reportType), finalSchoolDistricts, Long.class); } catch(Exception e) { LOGGER.error("Unable to retrieve school reports counts", e); summaryDTO.setErroredCount(summaryDTO.getErroredCount() + 1); @@ -631,8 +630,7 @@ public Long getTotalReportsForProcessing(List finalSchoolDistricts, String Long reportsCount = 0L; ThreadLocalStateUtil.setCorrelationID(UUID.randomUUID().toString()); try { - String accessToken = getAccessToken(); - reportsCount = restService.post(String.format(constants.getGradSchoolReportsCountUrl(), reportType), finalSchoolDistricts, Long.class, accessToken); + reportsCount = restService.post(String.format(constants.getGradSchoolReportsCountUrl(), reportType), finalSchoolDistricts, Long.class); } catch(Exception e) { LOGGER.error("Unable to retrieve school reports counts", e); summaryDTO.setErroredCount(summaryDTO.getErroredCount() + 1); @@ -649,8 +647,7 @@ public List getSchoolReportsLiteByReportType(String reportType, Sc ThreadLocalStateUtil.setCorrelationID(UUID.randomUUID().toString()); List schoolReportsLite = new ArrayList<>(); try { - String accessToken = getAccessToken(); - var response = restService.get(String.format(constants.getSchoolReportsLiteByReportTypeUrl(), reportType), List.class, accessToken); + var response = restService.get(String.format(constants.getSchoolReportsLiteByReportTypeUrl(), reportType), List.class); if (response != null) { schoolReportsLite = jsonTransformer.convertValue(response, new TypeReference>() { }); @@ -671,8 +668,7 @@ public List getReportStudentIDsByStudentIDsAndReportType(List fina List result = new ArrayList<>(); ThreadLocalStateUtil.setCorrelationID(UUID.randomUUID().toString()); try { - String accessToken = getAccessToken(); - var response = restService.post(String.format(constants.getGradStudentReportsGuidsUrl(), reportType, rowCount), finalSchoolDistricts, List.class, accessToken); + var response = restService.post(String.format(constants.getGradStudentReportsGuidsUrl(), reportType, rowCount), finalSchoolDistricts, List.class); if (response != null) { List guids = jsonTransformer.convertValue(response, new TypeReference<>() {}); result.addAll(guids); @@ -695,8 +691,7 @@ public Integer archiveSchoolReports(Long batchId, List finalSchoolDistrict LOGGER.debug("Archive {} School Reports for Ministry Codes: {}", reportType, !finalSchoolDistricts.isEmpty() ? String.join(",", finalSchoolDistricts.toString()) : summaryDTO.getSchools().stream().map(School::getSchoolId).collect(Collectors.joining(","))); } try { - String accessToken = getAccessToken(); - return restService.post(String.format(constants.getGradArchiveSchoolReportsUrl(), batchId, reportType), finalSchoolDistricts, Integer.class, accessToken); + return restService.post(String.format(constants.getGradArchiveSchoolReportsUrl(), batchId, reportType), finalSchoolDistricts, Integer.class); } catch(Exception e) { LOGGER.error("Unable to archive School Reports", e); summaryDTO.setErroredCount(summaryDTO.getErroredCount() + 1); @@ -710,8 +705,7 @@ public Long getTotalStudentsBySchoolOfRecordIdAndStudentStatus(List finalS Long studentsCount = 0L; ThreadLocalStateUtil.setCorrelationID(UUID.randomUUID().toString()); try { - String accessToken = getAccessToken(); - studentsCount = restService.post(String.format(constants.getGradStudentCountUrl(), studentStatus), finalSchoolDistricts, Long.class, accessToken); + studentsCount = restService.post(String.format(constants.getGradStudentCountUrl(), studentStatus), finalSchoolDistricts, Long.class); } catch(Exception e) { LOGGER.error("Unable to retrieve student counts", e); summaryDTO.setErroredCount(summaryDTO.getErroredCount() + 1); @@ -730,9 +724,8 @@ public Integer archiveStudents(Long batchId, List finalSchoolDistricts, St LOGGER.debug("Archive {} Students for Institute School: {}", studentStatus, String.join(",", finalSchoolDistricts.toString())); } try { - String accessToken = getAccessToken(); String userName = StringUtils.defaultString(summaryDTO.getUserName(), "Batch Archive Process"); - return restService.post(String.format(constants.getGradArchiveStudentsUrl(), batchId, studentStatus, userName), finalSchoolDistricts, Integer.class, accessToken); + return restService.post(String.format(constants.getGradArchiveStudentsUrl(), batchId, studentStatus, userName), finalSchoolDistricts, Integer.class); } catch(Exception e) { LOGGER.error("Unable to archive Students", e); summaryDTO.setErroredCount(summaryDTO.getErroredCount() + 1); @@ -745,8 +738,7 @@ public Integer archiveStudents(Long batchId, List finalSchoolDistricts, St public List getStudentIDsBySearchCriteriaOrAll(StudentSearchRequest searchRequest, DistributionSummaryDTO summaryDTO) { ThreadLocalStateUtil.setCorrelationID(UUID.randomUUID().toString()); try { - String accessToken = getAccessToken(); - var response = restService.post(constants.getGradGetStudentsBySearchCriteriaUrl(), searchRequest, List.class, accessToken); + var response = restService.post(constants.getGradGetStudentsBySearchCriteriaUrl(), searchRequest, List.class); return jsonTransformer.convertValue(response, new TypeReference<>() {}); } catch(Exception e) { LOGGER.error("Unable to retrieve list of Students", e); @@ -761,8 +753,7 @@ public long deleteStudentReports(Long batchId, List uuids, String reportTy Long studentsCount = 0L; ThreadLocalStateUtil.setCorrelationID(UUID.randomUUID().toString()); try { - String accessToken = getAccessToken(); - studentsCount = restService.post(String.format(constants.getDeleteStudentReportsUrl(), batchId, reportType), uuids, Long.class, accessToken); + studentsCount = restService.post(String.format(constants.getDeleteStudentReportsUrl(), batchId, reportType), uuids, Long.class); } catch(Exception e) { LOGGER.error("Unable to delete student reports", e); summaryDTO.setErroredCount(summaryDTO.getErroredCount() + 1); diff --git a/api/src/test/java/ca/bc/gov/educ/api/batchgraduation/util/RestUtilsTest.java b/api/src/test/java/ca/bc/gov/educ/api/batchgraduation/util/RestUtilsTest.java index 2ad3737c..e40c39c2 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/batchgraduation/util/RestUtilsTest.java +++ b/api/src/test/java/ca/bc/gov/educ/api/batchgraduation/util/RestUtilsTest.java @@ -756,15 +756,28 @@ public void whenCreateAndStoreSchoolReports_WithParams_ThenReturnResult() { assertNotNull(result); } - @Test(expected = Exception.class) + @Test + public void whenCreateAndStoreSchoolReports_WithParams_ThenReturnsEmptyList() { + final String type = "TVRRUN"; + + when(LOGGER.isDebugEnabled()).thenReturn(true); + + var result = this.restUtils.createAndStoreSchoolReports(null, type, new SchoolReportsRegenSummaryDTO()); + assertThat(result).isZero(); + } + + @Test public void whenCreateAndStoreSchoolReports_WithParams_ThenThrowException() { final String type = "TVRRUN"; final UUID schoolId = UUID.randomUUID(); - when(this.restService.post(String.format(constants.getCreateAndStoreSchoolReports(),type), List.of(schoolId), Integer.class, "accessToken")).thenThrow(Exception.class); + SchoolReportsRegenSummaryDTO summaryDTO = new SchoolReportsRegenSummaryDTO(); + + when(this.restService.post(String.format(constants.getCreateAndStoreSchoolReports(),type), List.of(schoolId), Integer.class)).thenThrow(RuntimeException.class); when(LOGGER.isDebugEnabled()).thenReturn(true); - var result = this.restUtils.createAndStoreSchoolReports(schoolId, type, new SchoolReportsRegenSummaryDTO()); + val result = this.restUtils.createAndStoreSchoolReports(schoolId, type, summaryDTO); + assertThat(result).isZero(); } @Test @@ -779,14 +792,14 @@ public void testGetSchoolReportsLiteByReportType() { mockTokenResponseObject(); - when(this.restService.get(String.format(constants.getSchoolReportsLiteByReportTypeUrl(), "GRADREG"), List.class, "accessToken")).thenReturn(schoolReportsLite); + when(this.restService.get(String.format(constants.getSchoolReportsLiteByReportTypeUrl(), "GRADREG"), List.class)).thenReturn(schoolReportsLite); when(LOGGER.isDebugEnabled()).thenReturn(true); val result = this.restUtils.getSchoolReportsLiteByReportType("GRADREG", new SchoolReportsRegenSummaryDTO()); assertThat(result).hasSize(1); } - @Test(expected = Exception.class) + @Test public void whenGetSchoolReportsLiteByReportType_ThenThrowException() { UUID uuid = UUID.randomUUID(); SchoolReport sr = new SchoolReport(); @@ -797,10 +810,11 @@ public void whenGetSchoolReportsLiteByReportType_ThenThrowException() { mockTokenResponseObject(); - when(this.restService.get(String.format(constants.getSchoolReportsLiteByReportTypeUrl(), "GRADREG"), List.class, "accessToken")).thenThrow(Exception.class); + when(this.restService.get(String.format(constants.getSchoolReportsLiteByReportTypeUrl(), "GRADREG"), List.class)).thenThrow(new RuntimeException("Test Exception")); when(LOGGER.isDebugEnabled()).thenReturn(true); val result = this.restUtils.getSchoolReportsLiteByReportType("GRADREG", new SchoolReportsRegenSummaryDTO()); + assertThat(result).isEmpty(); } @Test @@ -1464,7 +1478,7 @@ public void testGetTotalSchoolReportsForArchiving() { mockTokenResponseObject(); - when(this.restService.post(String.format(constants.getGradSchoolReportsCountUrl(), "GRADREG"), schools, Long.class, "accessToken")).thenReturn(1L); + when(this.restService.post(String.format(constants.getGradSchoolReportsCountUrl(), "GRADREG"), schools, Long.class)).thenReturn(1L); DistributionSummaryDTO summaryDTO = new DistributionSummaryDTO(); @@ -1472,6 +1486,22 @@ public void testGetTotalSchoolReportsForArchiving() { assertThat(result).isEqualTo(1); } + @Test + public void testGetTotalSchoolReportsRegeneration() { + UUID schoolId1 = UUID.randomUUID(); + UUID schoolId2 = UUID.randomUUID(); + List schools = Arrays.asList(schoolId1, schoolId2); + + mockTokenResponseObject(); + + when(this.restService.post(String.format(constants.getGradSchoolReportsCountUrl(), "GRADREG"), schools, Long.class)).thenReturn(1L); + + SchoolReportsRegenSummaryDTO summaryDTO = new SchoolReportsRegenSummaryDTO(); + + val result = this.restUtils.getTotalReportsForProcessing(schools, "GRADREG", summaryDTO); + assertThat(result).isEqualTo(1); + } + @Test public void testGetReportStudentIDsByStudentIDsAndReportType() { UUID uuid = UUID.randomUUID(); @@ -1480,7 +1510,7 @@ public void testGetReportStudentIDsByStudentIDsAndReportType() { mockTokenResponseObject(); - when(this.restService.post(String.format(constants.getGradStudentReportsGuidsUrl(), "ACHV", 1), studentIDsIn, List.class, "accessToken")).thenReturn(studentIDsOut); + when(this.restService.post(String.format(constants.getGradStudentReportsGuidsUrl(), "ACHV", 1), studentIDsIn, List.class)).thenReturn(studentIDsOut); DistributionSummaryDTO summaryDTO = new DistributionSummaryDTO(); @@ -1496,7 +1526,7 @@ public void testGetReportStudentIDsByStudentIDsAndReportTypeError() { mockTokenResponseObject(); - when(this.restService.post(String.format(constants.getGradStudentReportsGuidsUrl(), "ACHV", 1), studentIDsIn, List.class, "accessToken")).thenThrow(new RuntimeException("Unable to retrieve report student guids")); + when(this.restService.post(String.format(constants.getGradStudentReportsGuidsUrl(), "ACHV", 1), studentIDsIn, List.class)).thenThrow(new RuntimeException("Unable to retrieve report student guids")); DistributionSummaryDTO summaryDTO = new DistributionSummaryDTO(); @@ -1514,7 +1544,7 @@ public void testGetStudentIDsBySearchCriteriaOrAll() { StudentSearchRequest searchRequest = new StudentSearchRequest(); searchRequest.setStudentIDs(studentIDs); - when(this.restService.post(constants.getGradGetStudentsBySearchCriteriaUrl(), searchRequest, List.class, "accessToken")).thenReturn(studentIDs); + when(this.restService.post(constants.getGradGetStudentsBySearchCriteriaUrl(), searchRequest, List.class)).thenReturn(studentIDs); DistributionSummaryDTO summaryDTO = new DistributionSummaryDTO(); @@ -1531,7 +1561,7 @@ public void testGetStudentIDsBySearchCriteriaOrAllError() { StudentSearchRequest searchRequest = new StudentSearchRequest(); searchRequest.setStudentIDs(studentIDs); - when(this.restService.post(constants.getGradGetStudentsBySearchCriteriaUrl(), searchRequest, List.class, "accessToken")).thenThrow(new RuntimeException("Unable to retrieve list of Students")); + when(this.restService.post(constants.getGradGetStudentsBySearchCriteriaUrl(), searchRequest, List.class)).thenThrow(new RuntimeException("Unable to retrieve list of Students")); DistributionSummaryDTO summaryDTO = new DistributionSummaryDTO(); @@ -1548,7 +1578,7 @@ public void testGetTotalSchoolReportsForArchivingError() { mockTokenResponseObject(); - when(this.restService.post(String.format(constants.getGradSchoolReportsCountUrl(), "GRADREG"), schools, Long.class, "accessToken")).thenThrow(new RuntimeException("Unable to retrieve school reports counts")); + when(this.restService.post(String.format(constants.getGradSchoolReportsCountUrl(), "GRADREG"), schools, Long.class)).thenThrow(new RuntimeException("Unable to retrieve school reports counts")); DistributionSummaryDTO summaryDTO = new DistributionSummaryDTO(); @@ -1557,13 +1587,30 @@ public void testGetTotalSchoolReportsForArchivingError() { assertThat(summaryDTO.getErrors()).isNotEmpty(); } + @Test + public void testGetTotalSchoolReportsRegenerationError() { + UUID schoolId1 = UUID.randomUUID(); + UUID schoolId2 = UUID.randomUUID(); + List schools = Arrays.asList(schoolId1, schoolId2); + + mockTokenResponseObject(); + + when(this.restService.post(String.format(constants.getGradSchoolReportsCountUrl(), "GRADREG"), schools, Long.class)).thenThrow(new RuntimeException("Unable to retrieve school reports counts")); + + SchoolReportsRegenSummaryDTO summaryDTO = new SchoolReportsRegenSummaryDTO(); + + val result = this.restUtils.getTotalReportsForProcessing(schools, "GRADREG", summaryDTO); + assertThat(result).isNotNull(); + assertThat(summaryDTO.getErrors()).isNotEmpty(); + } + @Test public void testDeleteStudentReports() { List studentIDs = Arrays.asList(UUID.randomUUID()); mockTokenResponseObject(); - when(this.restService.post(String.format(constants.getDeleteStudentReportsUrl(), 12345678L, "ACHV"), studentIDs, Long.class, "accessToken")).thenReturn(1L); + when(this.restService.post(String.format(constants.getDeleteStudentReportsUrl(), 12345678L, "ACHV"), studentIDs, Long.class)).thenReturn(1L); DistributionSummaryDTO summaryDTO = new DistributionSummaryDTO(); @@ -1577,7 +1624,7 @@ public void testDeleteStudentReportsError() { mockTokenResponseObject(); - when(this.restService.post(String.format(constants.getDeleteStudentReportsUrl(), 12345678L, "ACHV"), studentIDs, Long.class, "accessToken")).thenThrow(new RuntimeException("Unable to delete student reports")); + when(this.restService.post(String.format(constants.getDeleteStudentReportsUrl(), 12345678L, "ACHV"), studentIDs, Long.class)).thenThrow(new RuntimeException("Unable to delete student reports")); DistributionSummaryDTO summaryDTO = new DistributionSummaryDTO(); @@ -1595,7 +1642,7 @@ public void testArchiveSchoolReports() { mockTokenResponseObject(); - when(this.restService.post(String.format(constants.getGradArchiveSchoolReportsUrl(), 12345678L, "GRADREG"), schools, Integer.class, "accessToken")).thenReturn(1); + when(this.restService.post(String.format(constants.getGradArchiveSchoolReportsUrl(), 12345678L, "GRADREG"), schools, Integer.class)).thenReturn(1); DistributionSummaryDTO summaryDTO = new DistributionSummaryDTO(); @@ -1611,7 +1658,7 @@ public void testArchiveSchoolReportsError() { mockTokenResponseObject(); - when(this.restService.post(String.format(constants.getGradArchiveSchoolReportsUrl(), 12345678L, "GRADREG"), schools, Integer.class, "accessToken")).thenThrow(new RuntimeException("Unable to archive School Reports")); + when(this.restService.post(String.format(constants.getGradArchiveSchoolReportsUrl(), 12345678L, "GRADREG"), schools, Integer.class)).thenThrow(new RuntimeException("Unable to archive School Reports")); DistributionSummaryDTO summaryDTO = new DistributionSummaryDTO(); @@ -1630,7 +1677,7 @@ public void testGetTotalStudentsForArchiving() { DistributionSummaryDTO summaryDTO = new DistributionSummaryDTO(); - when(this.restService.post(String.format(constants.getGradStudentCountUrl(), "CUR"), schools, Long.class, "accessToken")).thenReturn(1L); + when(this.restService.post(String.format(constants.getGradStudentCountUrl(), "CUR"), schools, Long.class)).thenReturn(1L); val result = this.restUtils.getTotalStudentsBySchoolOfRecordIdAndStudentStatus(schools, "CUR", summaryDTO); assertThat(result).isEqualTo(1); @@ -1646,7 +1693,7 @@ public void testGetTotalStudentsForArchivingError() { DistributionSummaryDTO summaryDTO = new DistributionSummaryDTO(); - when(this.restService.post(String.format(constants.getGradStudentCountUrl(), "CUR"), schools, Long.class, "accessToken")).thenThrow(new RuntimeException("Unable to retrieve student counts")); + when(this.restService.post(String.format(constants.getGradStudentCountUrl(), "CUR"), schools, Long.class)).thenThrow(new RuntimeException("Unable to retrieve student counts")); val result = this.restUtils.getTotalStudentsBySchoolOfRecordIdAndStudentStatus(schools, "CUR", summaryDTO); assertThat(result).isNotNull(); @@ -1664,7 +1711,7 @@ public void testArchiveStudents() { DistributionSummaryDTO summaryDTO = new DistributionSummaryDTO(); summaryDTO.setUserName("USER"); - when(this.restService.post(String.format(constants.getGradArchiveStudentsUrl(), 12345678L, "CUR", "USER"), schools, Integer.class, "accessToken")).thenReturn(1); + when(this.restService.post(String.format(constants.getGradArchiveStudentsUrl(), 12345678L, "CUR", "USER"), schools, Integer.class)).thenReturn(1); val result = this.restUtils.archiveStudents(12345678L, schools,"CUR", summaryDTO); assertThat(result).isEqualTo(1); @@ -1681,7 +1728,7 @@ public void testArchiveStudentsError() { DistributionSummaryDTO summaryDTO = new DistributionSummaryDTO(); summaryDTO.setUserName("USER"); - when(this.restService.post(String.format(constants.getGradArchiveStudentsUrl(), 12345678L, "CUR", "USER"), schools, Integer.class, "accessToken")).thenThrow(new RuntimeException("Unable to archive Students")); + when(this.restService.post(String.format(constants.getGradArchiveStudentsUrl(), 12345678L, "CUR", "USER"), schools, Integer.class)).thenThrow(new RuntimeException("Unable to archive Students")); val result = this.restUtils.archiveStudents(12345678L, schools,"CUR", summaryDTO); assertThat(result).isNotNull();