diff --git a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/helpers/HeadcountHelper.java b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/helpers/HeadcountHelper.java index c73bbe7f4..f742278e4 100644 --- a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/helpers/HeadcountHelper.java +++ b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/helpers/HeadcountHelper.java @@ -145,8 +145,11 @@ public void setResultsTableComparisonValuesDynamicNested(HeadcountResultsTable c public UUID getPreviousCollectionID(SdcSchoolCollectionEntity sdcSchoolCollectionEntity) { Optional collection; String collectionType = sdcSchoolCollectionEntity.getCollectionEntity().getCollectionTypeCode(); - if (collectionType.equals(CollectionTypeCodes.JULY.getTypeCode())) collection = sdcSchoolCollectionRepository.findLastCollectionBySchoolIDAndType(sdcSchoolCollectionEntity.getSchoolID(), collectionType, sdcSchoolCollectionEntity.getSdcSchoolCollectionID()); - else collection = sdcSchoolCollectionRepository.findLastCollectionBySchoolIDAndType(sdcSchoolCollectionEntity.getSchoolID(), CollectionTypeCodes.SEPTEMBER.getTypeCode(), sdcSchoolCollectionEntity.getSdcSchoolCollectionID()); + if (collectionType.equals(CollectionTypeCodes.JULY.getTypeCode())) { + collection = sdcSchoolCollectionRepository.findLastCollectionBySchoolIDAndType(sdcSchoolCollectionEntity.getSchoolID(), collectionType, sdcSchoolCollectionEntity.getSdcSchoolCollectionID(), sdcSchoolCollectionEntity.getCollectionEntity().getSnapshotDate()); + } else { + collection = sdcSchoolCollectionRepository.findLastCollectionBySchoolIDAndType(sdcSchoolCollectionEntity.getSchoolID(), CollectionTypeCodes.SEPTEMBER.getTypeCode(), sdcSchoolCollectionEntity.getSdcSchoolCollectionID(), sdcSchoolCollectionEntity.getCollectionEntity().getSnapshotDate()); + } return collection.map(SdcSchoolCollectionEntity::getSdcSchoolCollectionID).orElse(null); } diff --git a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/repository/v1/SdcSchoolCollectionRepository.java b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/repository/v1/SdcSchoolCollectionRepository.java index 07577e02a..d246e7577 100644 --- a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/repository/v1/SdcSchoolCollectionRepository.java +++ b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/repository/v1/SdcSchoolCollectionRepository.java @@ -151,18 +151,20 @@ public interface SdcSchoolCollectionRepository extends JpaRepository findLastCollectionBySchoolIDAndType(UUID schoolID, String collectionTypeCode, UUID currentSdcCollectionID); + Optional findLastCollectionBySchoolIDAndType(UUID schoolID, String collectionTypeCode, UUID currentSdcCollectionID, LocalDate snapshotDate); @Query(value=""" SELECT C FROM CollectionEntity C WHERE C.collectionID != :currentCollectionID AND C.collectionTypeCode = :collectionTypeCode + AND C.snapshotDate < :snapshotDate ORDER BY C.snapshotDate desc LIMIT 1""") - Optional findLastCollectionByType(String collectionTypeCode, UUID currentCollectionID); + Optional findLastCollectionByType(String collectionTypeCode, UUID currentCollectionID, LocalDate snapshotDate); List findAllBySchoolID(UUID schoolID); diff --git a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/repository/v1/SdcSchoolCollectionStudentRepository.java b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/repository/v1/SdcSchoolCollectionStudentRepository.java index 71274daef..dbbf5a059 100644 --- a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/repository/v1/SdcSchoolCollectionStudentRepository.java +++ b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/repository/v1/SdcSchoolCollectionStudentRepository.java @@ -756,6 +756,30 @@ long countAllByAssignedStudentIdInAndSdcSchoolCollection_SdcSchoolCollectionIDIn "ORDER BY s.enrolledGradeCode") List getSpecialEdHeadcountsBySdcDistrictCollectionId(@Param("sdcDistrictCollectionID") UUID sdcDistrictCollectionID); + @Query("SELECT " + + "COUNT(CASE WHEN s.specialEducationCategoryCode IN ('A', 'B') AND s.fte > 0 THEN 1 END) AS levelOnes, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode = 'A' AND s.fte > 0 THEN 1 END) AS specialEdACodes, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode = 'B' AND s.fte > 0 THEN 1 END) AS specialEdBCodes, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode IN ('C', 'D', 'E', 'F', 'G') AND s.fte > 0 THEN 1 END) AS levelTwos, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode = 'C' AND s.fte > 0 THEN 1 END) AS specialEdCCodes, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode = 'D' AND s.fte > 0 THEN 1 END) AS specialEdDCodes, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode = 'E' AND s.fte > 0 THEN 1 END) AS specialEdECodes, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode = 'F' AND s.fte > 0 THEN 1 END) AS specialEdFCodes, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode = 'G' AND s.fte > 0 THEN 1 END) AS specialEdGCodes, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode IN ('H') AND s.fte > 0 THEN 1 END) AS levelThrees, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode = 'H' AND s.fte > 0 THEN 1 END) AS specialEdHCodes, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode IN ('K', 'P', 'Q', 'R') AND s.fte > 0 THEN 1 END) AS otherLevels, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode = 'K' AND s.fte > 0 THEN 1 END) AS specialEdKCodes, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode = 'P' AND s.fte > 0 THEN 1 END) AS specialEdPCodes, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode = 'Q' AND s.fte > 0 THEN 1 END) AS specialEdQCodes, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode = 'R' AND s.fte > 0 THEN 1 END) AS specialEdRCodes, " + + "COUNT(CASE WHEN s.specialEducationCategoryCode IN ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K', 'P', 'Q', 'R') AND s.fte > 0 THEN 1 END) AS allLevels " + + "FROM SdcSchoolCollectionStudentEntity s " + + "WHERE s.sdcSchoolCollection.sdcDistrictCollectionID = :sdcDistrictCollectionID " + + "AND s.sdcSchoolCollectionStudentStatusCode NOT IN ('ERROR', 'DELETED') " + + "AND s.specialEducationNonEligReasonCode IS NULL ") + SpecialEdHeadcountResult getSpecialEdHeadcountsSimpleBySdcDistrictCollectionId(@Param("sdcDistrictCollectionID") UUID sdcDistrictCollectionID); + @Query("SELECT " + "COUNT(CASE WHEN s.specialEducationCategoryCode IN ('A', 'B') THEN 1 END) AS levelOnes, " + "COUNT(CASE WHEN s.specialEducationCategoryCode = 'A' THEN 1 END) AS specialEdACodes, " + @@ -778,7 +802,7 @@ long countAllByAssignedStudentIdInAndSdcSchoolCollection_SdcSchoolCollectionIDIn "WHERE s.sdcSchoolCollection.sdcDistrictCollectionID = :sdcDistrictCollectionID " + "AND s.sdcSchoolCollectionStudentStatusCode NOT IN ('ERROR', 'DELETED') " + "AND s.specialEducationNonEligReasonCode IS NULL ") - SpecialEdHeadcountResult getSpecialEdHeadcountsSimpleBySdcDistrictCollectionId(@Param("sdcDistrictCollectionID") UUID sdcDistrictCollectionID); + SpecialEdHeadcountResult getSpecialEdHeadcountsSimpleFebruaryBySdcDistrictCollectionId(@Param("sdcDistrictCollectionID") UUID sdcDistrictCollectionID); @Query("SELECT " + "COUNT(CASE WHEN s.specialEducationCategoryCode = 'A' AND s.specialEducationNonEligReasonCode IS NULL THEN 1 END) AS totalEligibleA, " + diff --git a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionService.java b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionService.java index 1f81c9fcc..f7cecdd9b 100644 --- a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionService.java +++ b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionService.java @@ -277,7 +277,7 @@ public void startSDCCollectionFromLastSDCCollectionDataSet(UUID sdcSchoolCollect Optional sdcSchoolCollectionOptional = sdcSchoolCollectionRepository.findById(sdcSchoolCollectionID); SdcSchoolCollectionEntity currentSchoolCollectionEntity = sdcSchoolCollectionOptional.orElseThrow(() -> new EntityNotFoundException(SdcSchoolCollectionEntity.class, SDC_SCHOOL_COLLECTION_ID_KEY, sdcSchoolCollectionID.toString())); sdcSchoolCollectionStudentHistoryRepository.deleteAllBySdcSchoolCollectionID(sdcSchoolCollectionID); - var septemberCollectionOptional = sdcSchoolCollectionRepository.findLastCollectionBySchoolIDAndType(currentSchoolCollectionEntity.getSchoolID(), CollectionTypeCodes.SEPTEMBER.getTypeCode(), currentSchoolCollectionEntity.getSdcSchoolCollectionID()); + var septemberCollectionOptional = sdcSchoolCollectionRepository.findLastCollectionBySchoolIDAndType(currentSchoolCollectionEntity.getSchoolID(), CollectionTypeCodes.SEPTEMBER.getTypeCode(), currentSchoolCollectionEntity.getSdcSchoolCollectionID(), currentSchoolCollectionEntity.getCollectionEntity().getSnapshotDate()); SdcSchoolCollectionEntity septemberCollection = septemberCollectionOptional.orElseThrow(() -> new EntityNotFoundException(SdcSchoolCollectionEntity.class, SDC_SCHOOL_COLLECTION_ID_KEY, sdcSchoolCollectionID.toString())); currentSchoolCollectionEntity.getSDCSchoolStudentEntities().clear(); diff --git a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/reports/CSVReportService.java b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/reports/CSVReportService.java index bca812829..c45b823a6 100644 --- a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/reports/CSVReportService.java +++ b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/reports/CSVReportService.java @@ -472,7 +472,11 @@ public DownloadableReportResponse generateIndySpecialEducationHeadcounts(UUID co // Independent School Inclusive Education Funding Headcounts report public DownloadableReportResponse generateIndySpecialEducationFundingHeadcounts(UUID collectionID) { List collectionRawData = sdcSchoolCollectionStudentRepository.getSpecialEdHeadcountsFebruaryByCollectionId(collectionID); - var mappedSeptData = getLastSeptCollectionSchoolMap(collectionID); + + var collection = collectionRepository.findById(collectionID).orElseThrow(() -> + new EntityNotFoundException(Collection.class, COLLECTION_ID, collectionID.toString())); + + var mappedSeptData = getLastSeptCollectionSchoolMap(collection); CSVFormat csvFormat = CSVFormat.DEFAULT.builder() .setHeader(DISTRICT_NUMBER.getCode(), IndySpecialEducationFundingHeadcountHeader.DISTRICT_NAME.getCode(), IndySpecialEducationFundingHeadcountHeader.AUTHORITY_NUMBER.getCode(), IndySpecialEducationFundingHeadcountHeader.AUTHORITY_NAME.getCode(), MINCODE.getCode(), SCHOOL_NAME.getCode(), @@ -618,10 +622,10 @@ public DownloadableReportResponse generateOffshoreSchoolsHeadcounts(UUID collect } } - private Map getLastSeptCollectionSchoolMap(UUID collectionID){ - var lastSeptCollectionOpt = sdcSchoolCollectionRepository.findLastCollectionByType(CollectionTypeCodes.SEPTEMBER.getTypeCode(), collectionID); + private Map getLastSeptCollectionSchoolMap(CollectionEntity collection){ + var lastSeptCollectionOpt = sdcSchoolCollectionRepository.findLastCollectionByType(CollectionTypeCodes.SEPTEMBER.getTypeCode(), collection.getCollectionID(), collection.getSnapshotDate()); if(lastSeptCollectionOpt.isEmpty()) { - throw new EntityNotFoundException(CollectionEntity.class, COLLECTION_ID, collectionID.toString()); + throw new EntityNotFoundException(CollectionEntity.class, COLLECTION_ID, collection.getCollectionID().toString()); } List lastSeptCollectionRawData = sdcSchoolCollectionStudentRepository.getSpecialEdHeadcountsByCollectionId(lastSeptCollectionOpt.get().getCollectionID()); return lastSeptCollectionRawData.stream().collect(Collectors.toMap(SpecialEdHeadcountResult::getSchoolID, item -> item)); diff --git a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/reports/MinistryHeadcountService.java b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/reports/MinistryHeadcountService.java index bab48798b..e18535212 100644 --- a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/reports/MinistryHeadcountService.java +++ b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/reports/MinistryHeadcountService.java @@ -228,7 +228,9 @@ public SimpleHeadcountResultsTable getSpecialEducationHeadcountsForIndependentsB // Independent School Inclusive Education Funding Headcounts report public SimpleHeadcountResultsTable getSpecialEducationFundingHeadcountsForIndependentsByCollectionID(UUID collectionID) { List collectionRawData = sdcSchoolCollectionStudentRepository.getSpecialEdHeadcountsFebruaryByCollectionId(collectionID); - var mappedSeptData = getLastSeptCollectionSchoolMap(collectionID); + var collection = collectionRepository.findById(collectionID).orElseThrow(() -> + new EntityNotFoundException(Collection.class, COLLECTION_ID, collectionID.toString())); + var mappedSeptData = getLastSeptCollectionSchoolMap(collection); SimpleHeadcountResultsTable resultsTable = new SimpleHeadcountResultsTable(); var headerList = new ArrayList(); @@ -316,10 +318,10 @@ public SimpleHeadcountResultsTable getSpecialEducationFundingHeadcountsForIndepe return resultsTable; } - private Map getLastSeptCollectionSchoolMap(UUID collectionID){ - var lastSeptCollectionOpt = sdcSchoolCollectionRepository.findLastCollectionByType(CollectionTypeCodes.SEPTEMBER.getTypeCode(), collectionID); + private Map getLastSeptCollectionSchoolMap(CollectionEntity collection){ + var lastSeptCollectionOpt = sdcSchoolCollectionRepository.findLastCollectionByType(CollectionTypeCodes.SEPTEMBER.getTypeCode(), collection.getCollectionID(), collection.getSnapshotDate()); if(lastSeptCollectionOpt.isEmpty()) { - throw new EntityNotFoundException(CollectionEntity.class, COLLECTION_ID, collectionID.toString()); + throw new EntityNotFoundException(CollectionEntity.class, COLLECTION_ID, collection.getCollectionID().toString()); } List lastSeptCollectionRawData = sdcSchoolCollectionStudentRepository.getSpecialEdHeadcountsByCollectionId(lastSeptCollectionOpt.get().getCollectionID()); return lastSeptCollectionRawData.stream().collect(Collectors.toMap(SpecialEdHeadcountResult::getSchoolID, item -> item)); diff --git a/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/MinistryReportsControllerTest.java b/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/MinistryReportsControllerTest.java index 56f9bc6d3..543533115 100644 --- a/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/MinistryReportsControllerTest.java +++ b/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/MinistryReportsControllerTest.java @@ -521,6 +521,7 @@ void testGetMinistryReportSpecialEdFunding_ValidIndySchoolType_ShouldReturnRepor CollectionEntity collectionSept = createMockCollectionEntity(); collectionSept.setCloseDate(LocalDateTime.now().minusDays(20)); + collectionSept.setSnapshotDate(LocalDate.now().minusWeeks(4)); collectionSept.setCollectionTypeCode(CollectionTypeCodes.SEPTEMBER.getTypeCode()); var savedSeptColl = collectionRepository.save(collectionSept); @@ -736,10 +737,17 @@ void testGetMinistryReportCSV_ValidIndySpecialEducationFundingType_ShouldReturnR final GrantedAuthority grantedAuthority = () -> "SCOPE_READ_SDC_MINISTRY_REPORTS"; final OidcLoginRequestPostProcessor mockAuthority = oidcLogin().authorities(grantedAuthority); - CollectionEntity collectionSept = createMockCollectionEntity(); - collectionSept.setCloseDate(LocalDateTime.now().minusDays(20)); - collectionSept.setCollectionTypeCode(CollectionTypeCodes.SEPTEMBER.getTypeCode()); - collectionRepository.save(collectionSept); + CollectionEntity collection1 = createMockCollectionEntity(); + collection1.setCloseDate(LocalDateTime.now().minusDays(2)); + collection1.setSnapshotDate(LocalDate.now().minusMonths(3)); + collection1.setCollectionTypeCode(CollectionTypeCodes.SEPTEMBER.getTypeCode()); + collection1 = collectionRepository.save(collection1); + + CollectionEntity collection2 = createMockCollectionEntity(); + collection2.setCloseDate(LocalDateTime.now().plusDays(2)); + collection2.setCollectionTypeCode(CollectionTypeCodes.FEBRUARY.getTypeCode()); + collection2.setSnapshotDate(LocalDate.now().plusDays(2)); + collection2 = collectionRepository.save(collection2); var district = this.createMockDistrict(); var authority = this.createMockAuthority(); @@ -751,23 +759,19 @@ void testGetMinistryReportCSV_ValidIndySpecialEducationFundingType_ShouldReturnR when(this.restUtils.getDistrictByDistrictID(any())).thenReturn(Optional.of(district)); when(this.restUtils.getAuthorityByAuthorityID(any())).thenReturn(Optional.of(authority)); - CollectionEntity collection = createMockCollectionEntity(); - collection.setCloseDate(LocalDateTime.now().plusDays(2)); - collection = collectionRepository.save(collection); - - SdcDistrictCollectionEntity sdcMockDistrict = createMockSdcDistrictCollectionEntity(collection, null); + SdcDistrictCollectionEntity sdcMockDistrict = createMockSdcDistrictCollectionEntity(collection1, null); sdcDistrictCollectionRepository.save(sdcMockDistrict).getSdcDistrictCollectionID(); - SdcDistrictCollectionEntity sdcMockDistrict2 = createMockSdcDistrictCollectionEntity(collection, null); + SdcDistrictCollectionEntity sdcMockDistrict2 = createMockSdcDistrictCollectionEntity(collection2, null); sdcDistrictCollectionRepository.save(sdcMockDistrict2).getSdcDistrictCollectionID(); SchoolTombstone school1 = createMockSchool(); school1.setDistrictId(sdcMockDistrict.getDistrictID().toString()); - SdcSchoolCollectionEntity sdcSchoolCollectionEntity1 = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school1.getSchoolId())); + SdcSchoolCollectionEntity sdcSchoolCollectionEntity1 = createMockSdcSchoolCollectionEntity(collection1, UUID.fromString(school1.getSchoolId())); SchoolTombstone school2 = createMockSchool(); school2.setDistrictId(sdcMockDistrict2.getDistrictID().toString()); - SdcSchoolCollectionEntity sdcSchoolCollectionEntity2 = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school2.getSchoolId())); + SdcSchoolCollectionEntity sdcSchoolCollectionEntity2 = createMockSdcSchoolCollectionEntity(collection2, UUID.fromString(school2.getSchoolId())); sdcSchoolCollectionRepository.saveAll(List.of(sdcSchoolCollectionEntity1, sdcSchoolCollectionEntity2)); @@ -776,7 +780,7 @@ void testGetMinistryReportCSV_ValidIndySpecialEducationFundingType_ShouldReturnR sdcSchoolCollectionStudentRepository.saveAll(List.of(sdcSchoolCollectionStudent1, sdcSchoolCollectionStudent2)); var resultActions1 = this.mockMvc.perform( - get(URL.BASE_MINISTRY_HEADCOUNTS + "/" + collection.getCollectionID() + "/indy-inclusive-ed-funding-headcounts/download").with(mockAuthority)) + get(URL.BASE_MINISTRY_HEADCOUNTS + "/" + collection2.getCollectionID() + "/indy-inclusive-ed-funding-headcounts/download").with(mockAuthority)) .andDo(print()).andExpect(status().isOk()); val summary1 = objectMapper.readValue(resultActions1.andReturn().getResponse().getContentAsByteArray(), new TypeReference() { diff --git a/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/SdcSchoolCollectionControllerTest.java b/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/SdcSchoolCollectionControllerTest.java index 55c2dcfe7..062e8e939 100644 --- a/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/SdcSchoolCollectionControllerTest.java +++ b/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/SdcSchoolCollectionControllerTest.java @@ -31,6 +31,7 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.util.CollectionUtils; +import java.time.LocalDate; import java.time.LocalDateTime; import java.util.*; @@ -660,6 +661,7 @@ void testStartCollectionFromLastSept_ShouldReturnOk() throws Exception { CollectionEntity oldCollection = createMockCollectionEntity(); oldCollection.setCollectionTypeCode(CollectionTypeCodes.SEPTEMBER.getTypeCode()); oldCollection.setCloseDate(LocalDateTime.now().minusDays(2)); + oldCollection.setSnapshotDate(LocalDate.now().minusDays(20)); collectionRepository.save(oldCollection); District district = createMockDistrict(); diff --git a/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/SdcSchoolCollectionStudentControllerTest.java b/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/SdcSchoolCollectionStudentControllerTest.java index 6a0631cb8..b114a279d 100644 --- a/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/SdcSchoolCollectionStudentControllerTest.java +++ b/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/SdcSchoolCollectionStudentControllerTest.java @@ -1130,13 +1130,18 @@ void testDeleteMultiSdcSchoolCollectionStudent_WithValidPayload_ShouldReturnOkay @Test void testGetSdcSchoolCollectionStudentHeadcounts_enrollmentHeadcounts() throws Exception { - var collection = collectionRepository.save(createMockCollectionEntity()); + var collection1 = createMockCollectionEntity(); + collection1.setSnapshotDate(LocalDate.now().minusWeeks(1)); + collection1 = collectionRepository.save(collection1); var school = this.createMockSchool(); when(this.restUtils.getSchoolBySchoolID(anyString())).thenReturn(Optional.of(school)); - var firstSchool = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school.getSchoolId())); + var firstSchool = createMockSdcSchoolCollectionEntity(collection1, UUID.fromString(school.getSchoolId())); firstSchool.setUploadDate(null); firstSchool.setUploadFileName(null); - var secondSchool = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school.getSchoolId())); + var collection2 = createMockCollectionEntity(); + collection2.setSnapshotDate(LocalDate.now().minusWeeks(10)); + collection2 = collectionRepository.save(collection2); + var secondSchool = createMockSdcSchoolCollectionEntity(collection2, UUID.fromString(school.getSchoolId())); secondSchool.setUploadDate(null); secondSchool.setUploadFileName(null); secondSchool.setCreateDate(LocalDateTime.of(Year.now().getValue() - 1, Month.SEPTEMBER, 7, 0, 0)); @@ -1416,13 +1421,18 @@ void testGetSdcSchoolCollectionStudentHeadcounts_csfFrenchHeadcounts() throws Ex @Test void testGetSdcSchoolCollectionStudentHeadcounts_careerHeadcounts() throws Exception { - var collection = collectionRepository.save(createMockCollectionEntity()); + var collection1 = createMockCollectionEntity(); + collection1.setSnapshotDate(LocalDate.now().minusWeeks(1)); + collection1 = collectionRepository.save(collection1); var school = this.createMockSchool(); when(this.restUtils.getSchoolBySchoolID(anyString())).thenReturn(Optional.of(school)); - var firstSchool = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school.getSchoolId())); + var firstSchool = createMockSdcSchoolCollectionEntity(collection1, UUID.fromString(school.getSchoolId())); firstSchool.setUploadDate(null); firstSchool.setUploadFileName(null); - var secondSchool = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school.getSchoolId())); + var collection2 = createMockCollectionEntity(); + collection2.setSnapshotDate(LocalDate.now().minusWeeks(10)); + collection2 = collectionRepository.save(collection2); + var secondSchool = createMockSdcSchoolCollectionEntity(collection2, UUID.fromString(school.getSchoolId())); secondSchool.setUploadDate(null); secondSchool.setUploadFileName(null); secondSchool.setCreateDate(LocalDateTime.of(Year.now().getValue() - 1, Month.SEPTEMBER, 7, 0, 0)); @@ -1974,13 +1984,18 @@ void testReadSdcSchoolCollectionStudentPaginated_withEllYearsGreaterThanEqual_Sh @Test void testGetSdcSchoolCollectionStudentHeadcounts_indigenousHeadcounts() throws Exception { - var collection = collectionRepository.save(createMockCollectionEntity()); + var collection1 = createMockCollectionEntity(); + collection1.setSnapshotDate(LocalDate.now().minusWeeks(1)); + collection1 = collectionRepository.save(collection1); var school = this.createMockSchool(); when(this.restUtils.getSchoolBySchoolID(anyString())).thenReturn(Optional.of(school)); - var firstSchool = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school.getSchoolId())); + var firstSchool = createMockSdcSchoolCollectionEntity(collection1, UUID.fromString(school.getSchoolId())); firstSchool.setUploadDate(null); firstSchool.setUploadFileName(null); - var secondSchool = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school.getSchoolId())); + var collection2 = createMockCollectionEntity(); + collection2.setSnapshotDate(LocalDate.now().minusWeeks(10)); + collection2 = collectionRepository.save(collection2); + var secondSchool = createMockSdcSchoolCollectionEntity(collection2, UUID.fromString(school.getSchoolId())); secondSchool.setUploadDate(null); secondSchool.setUploadFileName(null); secondSchool.setCreateDate(LocalDateTime.of(Year.now().getValue() - 1, Month.SEPTEMBER, 7, 0, 0)); @@ -2121,13 +2136,19 @@ void testCreateYearsInEll_whenEllAlreadyExist_shouldReturnExistingElls() throws @Test void testGetSdcSchoolCollectionStudentHeadcounts_specialEdHeadcounts() throws Exception { - var collection = collectionRepository.save(createMockCollectionEntity()); + var collection1 = createMockCollectionEntity(); + collection1.setSnapshotDate(LocalDate.now().minusWeeks(1)); + collection1 = collectionRepository.save(collection1); var school = this.createMockSchool(); when(this.restUtils.getSchoolBySchoolID(anyString())).thenReturn(Optional.of(school)); - var firstSchool = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school.getSchoolId())); + var firstSchool = createMockSdcSchoolCollectionEntity(collection1, UUID.fromString(school.getSchoolId())); firstSchool.setUploadDate(null); firstSchool.setUploadFileName(null); - var secondSchool = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school.getSchoolId())); + var collection2 = createMockCollectionEntity(); + collection2.setSnapshotDate(LocalDate.now().minusWeeks(10)); + collection2 = collectionRepository.save(collection2); + + var secondSchool = createMockSdcSchoolCollectionEntity(collection2, UUID.fromString(school.getSchoolId())); secondSchool.setUploadDate(null); secondSchool.setUploadFileName(null); secondSchool.setCreateDate(LocalDateTime.of(Year.now().getValue() - 1, Month.SEPTEMBER, 7, 0, 0)); @@ -3450,12 +3471,17 @@ void testGetSdcDistrictCollectionStudentHeadcounts_bandResidenceTable() throws E @Test void testGetSdcSchoolCollectionStudentHeadcounts_bandHeadcounts_WithCompare() throws Exception { - CollectionEntity collection = collectionRepository.save(createMockCollectionEntity()); + var collection = createMockCollectionEntity(); collection.setCollectionTypeCode(CollectionTypeCodes.FEBRUARY.getTypeCode()); + collection.setSnapshotDate(LocalDate.now().minusWeeks(1)); collection.setCreateDate(LocalDateTime.of(Year.now().getValue(), Month.JANUARY, 1, 0, 0)); - CollectionEntity collection2 = collectionRepository.save(createMockCollectionEntity()); + collection = collectionRepository.save(collection); + + var collection2 = createMockCollectionEntity(); collection2.setCollectionTypeCode(CollectionTypeCodes.SEPTEMBER.getTypeCode()); + collection2.setSnapshotDate(LocalDate.now().minusWeeks(10)); collection2.setCreateDate(LocalDateTime.of(Year.now().getValue() - 1, Month.SEPTEMBER, 7, 0, 0)); + collection2 = collectionRepository.save(collection2); var districtID = UUID.randomUUID(); var mockDistrictCollectionEntity = sdcDistrictCollectionRepository.save(createMockSdcDistrictCollectionEntity(collection, districtID)); @@ -3523,15 +3549,20 @@ void testGetSdcSchoolCollectionStudentHeadcounts_bandHeadcounts_WithCompare() th @Test void testGetSdcSchoolCollectionStudentHeadcounts_bandHeadcountsManyPrevBands_WithCompare() throws Exception { - CollectionEntity collection = collectionRepository.save(createMockCollectionEntity()); + var collection = createMockCollectionEntity(); collection.setCollectionTypeCode(CollectionTypeCodes.FEBRUARY.getTypeCode()); + collection.setSnapshotDate(LocalDate.now().minusWeeks(1)); collection.setCreateDate(LocalDateTime.of(Year.now().getValue(), Month.JANUARY, 1, 0, 0)); - CollectionEntity collection2 = collectionRepository.save(createMockCollectionEntity()); + collection = collectionRepository.save(collection); + var collection2 = createMockCollectionEntity(); collection2.setCollectionTypeCode(CollectionTypeCodes.SEPTEMBER.getTypeCode()); + collection2.setSnapshotDate(LocalDate.now().minusWeeks(10)); collection2.setCreateDate(LocalDateTime.of(Year.now().getValue() - 1, Month.SEPTEMBER, 7, 0, 0)); + collection2 = collectionRepository.save(collection2); var districtID = UUID.randomUUID(); - var mockDistrictCollectionEntity = sdcDistrictCollectionRepository.save(createMockSdcDistrictCollectionEntity(collection, districtID)); + var mockDistrictCollectionEntity1 = sdcDistrictCollectionRepository.save(createMockSdcDistrictCollectionEntity(collection, districtID)); + var mockDistrictCollectionEntity2 = sdcDistrictCollectionRepository.save(createMockSdcDistrictCollectionEntity(collection2, districtID)); var school1 = createMockSchool(); school1.setDisplayName("School1"); @@ -3543,13 +3574,13 @@ void testGetSdcSchoolCollectionStudentHeadcounts_bandHeadcountsManyPrevBands_Wit var firstSchool = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school1.getSchoolId())); firstSchool.setUploadDate(null); firstSchool.setUploadFileName(null); - firstSchool.setSdcDistrictCollectionID(mockDistrictCollectionEntity.getSdcDistrictCollectionID()); + firstSchool.setSdcDistrictCollectionID(mockDistrictCollectionEntity1.getSdcDistrictCollectionID()); firstSchool.setCollectionEntity(collection); firstSchool.setCreateDate(LocalDateTime.of(Year.now().getValue(), Month.JANUARY, 1, 0, 0)); - var secondSchool = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school1.getSchoolId())); + var secondSchool = createMockSdcSchoolCollectionEntity(collection2, UUID.fromString(school1.getSchoolId())); secondSchool.setUploadDate(null); secondSchool.setUploadFileName(null); - secondSchool.setSdcDistrictCollectionID(mockDistrictCollectionEntity.getSdcDistrictCollectionID()); + secondSchool.setSdcDistrictCollectionID(mockDistrictCollectionEntity2.getSdcDistrictCollectionID()); secondSchool.setCollectionEntity(collection2); secondSchool.setCreateDate(LocalDateTime.of(Year.now().getValue() - 1, Month.SEPTEMBER, 7, 0, 0)); sdcSchoolCollectionRepository.saveAll(Arrays.asList(firstSchool, secondSchool)); diff --git a/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/helpers/HeadcountHelperTest.java b/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/helpers/HeadcountHelperTest.java index 8957bf9bb..cba892ba6 100644 --- a/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/helpers/HeadcountHelperTest.java +++ b/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/helpers/HeadcountHelperTest.java @@ -14,6 +14,7 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import java.time.LocalDate; import java.time.LocalDateTime; import java.time.Month; import java.util.*; @@ -91,6 +92,7 @@ void testGetPreviousCollectionID_givenPreviousCollection_shouldCallRepositoryWit LocalDateTime createDate = LocalDateTime.of(2023, month, day, 12, 0); CollectionEntity collectionEntity = new CollectionEntity(); collectionEntity.setCollectionTypeCode(CollectionTypeCodes.SEPTEMBER.getTypeCode()); + collectionEntity.setSnapshotDate(LocalDate.now().minusWeeks(2)); SdcSchoolCollectionEntity schoolCollectionEntity = new SdcSchoolCollectionEntity(); schoolCollectionEntity.setSdcSchoolCollectionID(schoolCollectionId); schoolCollectionEntity.setSchoolID(schoolId); @@ -99,7 +101,7 @@ void testGetPreviousCollectionID_givenPreviousCollection_shouldCallRepositoryWit Optional septemberCollections = Optional.of(schoolCollectionEntity); - when(schoolCollectionRepository.findLastCollectionBySchoolIDAndType(eq(schoolId), any(), any())) + when(schoolCollectionRepository.findLastCollectionBySchoolIDAndType(eq(schoolId), any(), any(), any())) .thenReturn(septemberCollections); // When @@ -110,7 +112,7 @@ void testGetPreviousCollectionID_givenPreviousCollection_shouldCallRepositoryWit verify(schoolCollectionRepository).findLastCollectionBySchoolIDAndType( schoolId, CollectionTypeCodes.SEPTEMBER.getTypeCode(), - schoolCollectionEntity.getSdcSchoolCollectionID()); + schoolCollectionEntity.getSdcSchoolCollectionID(), collectionEntity.getSnapshotDate()); } @ParameterizedTest @@ -132,6 +134,7 @@ void testGetPreviousCollectionIDJuly_givenPreviousJulyCollection_shouldCallRepos LocalDateTime createDate = LocalDateTime.of(2023, month, day, 12, 0); CollectionEntity collectionEntity = new CollectionEntity(); collectionEntity.setCollectionTypeCode(CollectionTypeCodes.JULY.getTypeCode()); + collectionEntity.setSnapshotDate(LocalDate.now().minusWeeks(2)); SdcSchoolCollectionEntity schoolCollectionEntity = new SdcSchoolCollectionEntity(); schoolCollectionEntity.setSdcSchoolCollectionID(schoolCollectionId); schoolCollectionEntity.setSchoolID(schoolId); @@ -140,7 +143,7 @@ void testGetPreviousCollectionIDJuly_givenPreviousJulyCollection_shouldCallRepos Optional septemberCollections = Optional.of(schoolCollectionEntity); - when(schoolCollectionRepository.findLastCollectionBySchoolIDAndType(eq(schoolId), any(), any())) + when(schoolCollectionRepository.findLastCollectionBySchoolIDAndType(eq(schoolId), any(), any(), any())) .thenReturn(septemberCollections); // When @@ -151,7 +154,7 @@ void testGetPreviousCollectionIDJuly_givenPreviousJulyCollection_shouldCallRepos verify(schoolCollectionRepository).findLastCollectionBySchoolIDAndType( schoolId, CollectionTypeCodes.JULY.getTypeCode(), - schoolCollectionEntity.getSdcSchoolCollectionID()); + schoolCollectionEntity.getSdcSchoolCollectionID(), collectionEntity.getSnapshotDate()); } @Test diff --git a/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionStudentHeadcountServiceTest.java b/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionStudentHeadcountServiceTest.java index f8f96a415..bc9651675 100644 --- a/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionStudentHeadcountServiceTest.java +++ b/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionStudentHeadcountServiceTest.java @@ -17,6 +17,7 @@ import java.io.File; import java.io.IOException; +import java.time.LocalDate; import java.time.LocalDateTime; import java.time.Month; import java.time.Year; @@ -42,14 +43,19 @@ class SdcSchoolCollectionStudentHeadcountServiceTest extends BaseStudentDataColl @BeforeEach void setUp() throws IOException { - var collection = collectionRepository.save(createMockCollectionEntity()); + var collection1 = createMockCollectionEntity(); + collection1.setSnapshotDate(LocalDate.now().minusWeeks(10)); + collection1 = collectionRepository.save(collection1); var school = this.createMockSchool(); when(this.restUtils.getSchoolBySchoolID(anyString())).thenReturn(Optional.of(school)); - firstSchoolCollection = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school.getSchoolId())); + firstSchoolCollection = createMockSdcSchoolCollectionEntity(collection1, UUID.fromString(school.getSchoolId())); firstSchoolCollection.setUploadDate(null); firstSchoolCollection.setUploadFileName(null); - secondSchoolCollection = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school.getSchoolId())); + var collection2 = createMockCollectionEntity(); + collection2.setSnapshotDate(LocalDate.now().minusWeeks(1)); + collection2 = collectionRepository.save(collection2); + secondSchoolCollection = createMockSdcSchoolCollectionEntity(collection2, UUID.fromString(school.getSchoolId())); secondSchoolCollection.setUploadDate(null); secondSchoolCollection.setUploadFileName(null); secondSchoolCollection.setCreateDate(LocalDateTime.of(Year.now().getValue() - 1, Month.SEPTEMBER, 7, 0, 0)); @@ -84,15 +90,15 @@ void cleanup(){ @Test void testGetEnrollmentReportValues_WhenCompareIsTrue(){ - var resultsTableWithoutCompare = service.getEnrollmentHeadcounts(firstSchoolCollection, false); + var resultsTableWithoutCompare = service.getEnrollmentHeadcounts(secondSchoolCollection, false); var allStudentsSection = resultsTableWithoutCompare.getHeadcountResultsTable().getRows().stream().filter(val -> val.get("section").getCurrentValue().equals("All Students") && val.get("title").getCurrentValue().equals("FTE Total")).findAny(); - assertEquals("2.46", allStudentsSection.get().get("Total").getCurrentValue()); + assertEquals("2.72", allStudentsSection.get().get("Total").getCurrentValue()); assertNull(allStudentsSection.get().get("Total").getComparisonValue()); - var resultsTableWithCompare = service.getEnrollmentHeadcounts(firstSchoolCollection, true); + var resultsTableWithCompare = service.getEnrollmentHeadcounts(secondSchoolCollection, true); var allStudentsWithCompareSection = resultsTableWithCompare.getHeadcountResultsTable().getRows().stream().filter(val -> val.get("section").getCurrentValue().equals("All Students") && val.get("title").getCurrentValue().equals("FTE Total")).findAny(); - assertEquals("2.46", allStudentsWithCompareSection.get().get("Total").getCurrentValue()); - assertEquals("2.72", allStudentsWithCompareSection.get().get("Total").getComparisonValue()); + assertEquals("2.72", allStudentsWithCompareSection.get().get("Total").getCurrentValue()); + assertEquals("2.46", allStudentsWithCompareSection.get().get("Total").getComparisonValue()); } @Test @@ -113,15 +119,15 @@ void testGetCareerProgramReportValues_WhenCompareIsTrue(){ sdcSchoolCollectionStudentEnrolledProgramRepository.saveAll(enrolledPrograms); - var resultsTableWithoutCompare = service.getCareerHeadcounts(firstSchoolCollection, false); + var resultsTableWithoutCompare = service.getCareerHeadcounts(secondSchoolCollection, false); var allStudentsSection = resultsTableWithoutCompare.getHeadcountResultsTable().getRows().stream().filter(val -> val.get("section").getCurrentValue().equals("Career Preparation") && val.get("title").getCurrentValue().equals("Career Preparation")).findAny(); - assertEquals("1", allStudentsSection.get().get("Total").getCurrentValue()); + assertEquals("2", allStudentsSection.get().get("Total").getCurrentValue()); assertNull(allStudentsSection.get().get("Total").getComparisonValue()); - var resultsTableWithCompare = service.getCareerHeadcounts(firstSchoolCollection, true); + var resultsTableWithCompare = service.getCareerHeadcounts(secondSchoolCollection, true); var allStudentsWithCompareSection = resultsTableWithCompare.getHeadcountResultsTable().getRows().stream().filter(val -> val.get("section").getCurrentValue().equals("Career Preparation") && val.get("title").getCurrentValue().equals("Career Preparation")).findAny(); - assertEquals("1", allStudentsWithCompareSection.get().get("Total").getCurrentValue()); - assertEquals("2", allStudentsWithCompareSection.get().get("Total").getComparisonValue()); + assertEquals("2", allStudentsWithCompareSection.get().get("Total").getCurrentValue()); + assertEquals("1", allStudentsWithCompareSection.get().get("Total").getComparisonValue()); } @Test @@ -142,31 +148,31 @@ void testGetIndigenousProgramReportValues_WhenCompareIsTrue(){ sdcSchoolCollectionStudentEnrolledProgramRepository.saveAll(enrolledPrograms); - var resultsTableWithoutCompare = service.getIndigenousHeadcounts(firstSchoolCollection, false); + var resultsTableWithoutCompare = service.getIndigenousHeadcounts(secondSchoolCollection, false); var allStudentsSection = resultsTableWithoutCompare.getHeadcountResultsTable().getRows().stream().filter(val -> val.get("section").getCurrentValue().equals("All Indigenous Support Programs") && val.get("title").getCurrentValue().equals("All Indigenous Support Programs")).findAny(); - assertEquals("2", allStudentsSection.get().get("Total").getCurrentValue()); + assertEquals("4", allStudentsSection.get().get("Total").getCurrentValue()); assertNull(allStudentsSection.get().get("Total").getComparisonValue()); - var resultsTableWithCompare = service.getIndigenousHeadcounts(firstSchoolCollection, true); + var resultsTableWithCompare = service.getIndigenousHeadcounts(secondSchoolCollection, true); var allStudentsWithCompareSection = resultsTableWithCompare.getHeadcountResultsTable().getRows().stream().filter(val -> val.get("section").getCurrentValue().equals("All Indigenous Support Programs") && val.get("title").getCurrentValue().equals("All Indigenous Support Programs")).findAny(); - assertEquals("2", allStudentsWithCompareSection.get().get("Total").getCurrentValue()); - assertEquals("4", allStudentsWithCompareSection.get().get("Total").getComparisonValue()); + assertEquals("4", allStudentsWithCompareSection.get().get("Total").getCurrentValue()); + assertEquals("2", allStudentsWithCompareSection.get().get("Total").getComparisonValue()); } @Test void testGetBandOfResidenceValues_WhenCompareIsTrue(){ - var resultsTableWithoutCompare = service.getBandResidenceHeadcounts(firstSchoolCollection, false); - assertEquals(3, resultsTableWithoutCompare.getHeadcountResultsTable().getRows().size()); + var resultsTableWithoutCompare = service.getBandResidenceHeadcounts(secondSchoolCollection, false); + assertEquals(2, resultsTableWithoutCompare.getHeadcountResultsTable().getRows().size()); var allStudentsWithoutCompareRow = resultsTableWithoutCompare.getHeadcountResultsTable().getRows().stream().filter(val -> val.get("title").getCurrentValue().equals("All Bands & Students")).findAny(); - assertEquals("2.46", allStudentsWithoutCompareRow.get().get("FTE").getCurrentValue()); + assertEquals("1.14", allStudentsWithoutCompareRow.get().get("FTE").getCurrentValue()); assertEquals("2", allStudentsWithoutCompareRow.get().get("Headcount").getCurrentValue()); - var resultsTableWithCompare = service.getBandResidenceHeadcounts(firstSchoolCollection, true); + var resultsTableWithCompare = service.getBandResidenceHeadcounts(secondSchoolCollection, true); var allStudentsWithCompareRow = resultsTableWithCompare.getHeadcountResultsTable().getRows().stream().filter(val -> val.get("title").getCurrentValue().equals("All Bands & Students")).findAny(); assertEquals(4, resultsTableWithCompare.getHeadcountResultsTable().getRows().size()); - assertEquals("2.46", allStudentsWithCompareRow.get().get("FTE").getCurrentValue()); + assertEquals("1.14", allStudentsWithCompareRow.get().get("FTE").getCurrentValue()); assertEquals("2", allStudentsWithCompareRow.get().get("Headcount").getCurrentValue()); - assertEquals("1.14", allStudentsWithCompareRow.get().get("FTE").getComparisonValue()); + assertEquals("2.46", allStudentsWithCompareRow.get().get("FTE").getComparisonValue()); assertEquals("2", allStudentsWithCompareRow.get().get("Headcount").getComparisonValue()); }