diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/v2/CommonController.java b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/v2/CommonController.java index e5161588..158a40cd 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/v2/CommonController.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/v2/CommonController.java @@ -20,6 +20,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.UUID; @CrossOrigin @RestController @@ -47,22 +48,22 @@ public ResponseEntity> getAllSchoolsForClobData() { return response.GET(commonService.getSchoolsForClobDataFromRedisCache()); } - @GetMapping(EducGradTraxApiConstants.GRAD_SCHOOL_CLOB_URL_MAPPING_V2 + EducGradTraxApiConstants.GET_SCHOOL_BY_CODE_MAPPING) + @GetMapping(EducGradTraxApiConstants.GRAD_SCHOOL_CLOB_URL_MAPPING_V2 + EducGradTraxApiConstants.GET_SCHOOL_BY_SCHOOL_ID) @PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA) - @Operation(summary = "Find a School Clob data by MinCode for GRAD Algorithm Data from cache", description = "Get a School Clob data by MinCode for GRAD Algorithm Data from cache", tags = { "Algorithm Data" }) + @Operation(summary = "Find a School Clob data by SchoolId for GRAD Algorithm Data from cache", description = "Get a School Clob data by SchoolId for GRAD Algorithm Data from cache", tags = { "Algorithm Data" }) @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "400", description = "BAD REQUEST"), @ApiResponse(responseCode = "422", description = "UNPROCESSABLE CONTENT"), @ApiResponse(responseCode = "204", description = "NO CONTENT")}) - public ResponseEntity getSchoolForClobDataByMinCode(@PathVariable String minCode) { - log.debug("getSchoolClobData by minCode: {}", minCode); - validation.requiredField(minCode, "minCode"); + public ResponseEntity getSchoolForClobDataBySchoolId(@PathVariable UUID schoolId) { + log.debug("getSchoolClobData by schoolId: {}", schoolId); + validation.requiredField(schoolId, "schoolId"); if (validation.hasErrors()) { validation.stopOnErrors(); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } - School schoolResponse = commonService.getSchoolForClobDataByMinCodeFromRedisCache(minCode); + School schoolResponse = commonService.getSchoolForClobDataBySchoolIdFromRedisCache(schoolId); if (schoolResponse != null) { return response.GET(schoolResponse); } else { diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/v2/DistrictController.java b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/v2/DistrictController.java index 0116fa40..2b1f5003 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/v2/DistrictController.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/v2/DistrictController.java @@ -14,6 +14,7 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; @@ -53,18 +54,30 @@ public ResponseEntity reloadDistrictsIntoCache() { return ResponseEntity.ok("Districts loaded into cache!"); } - @GetMapping(EducGradTraxApiConstants.GRAD_DISTRICT_URL_MAPPING_V2 + EducGradTraxApiConstants.GET_DISTRICT_BY_DISTNO_MAPPING) + @GetMapping(EducGradTraxApiConstants.GRAD_DISTRICT_URL_MAPPING_V2) @PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA) @Operation(summary = "Find a District by District Number V2", description = "Get District by District Number V2", tags = { "District" }) @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")}) - public ResponseEntity getDistrictDetailsByDistNo(@PathVariable String distNo) { + public ResponseEntity getDistrictDetailsByDistNo(@RequestParam(required = true) String distNo) { if(distNo.length() <=3) { District distResponse = districtService.getDistrictByDistNoFromRedisCache(distNo); if (distResponse != null) { return response.GET(distResponse); } } - return null; + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + @GetMapping(EducGradTraxApiConstants.GRAD_DISTRICT_URL_MAPPING_V2 + EducGradTraxApiConstants.GET_DISTRICT_BY_ID_MAPPING) + @PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA) + @Operation(summary = "Find a District by ID V2", description = "Get District by ID V2", tags = { "District" }) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")}) + public ResponseEntity getDistrictDetailsById(@PathVariable String districtId) { + District distResponse = districtService.getDistrictByIdFromRedisCache(districtId); + if (distResponse != null) { + return response.GET(distResponse); + } + return new ResponseEntity<>(HttpStatus.NOT_FOUND); } @GetMapping(EducGradTraxApiConstants.GRAD_DISTRICT_URL_MAPPING_V2 + EducGradTraxApiConstants.GET_DISTRICTS_BY_SCHOOL_CATEGORY_MAPPING) diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/School.java b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/School.java index 37662f2e..0cc622af 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/School.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/School.java @@ -23,7 +23,7 @@ public class School implements Comparable { private String postal; private String openFlag; private String schoolCategoryCode; - private String schoolCategoryCodeInstitute; + private String schoolCategoryLegacyCode; public String getSchoolName() { return schoolName != null ? schoolName.trim(): ""; @@ -69,7 +69,7 @@ public int hashCode() { @Override public String toString() { - return "School [minCode=" + minCode + ", schoolId=" + schoolId + ", schoolCategoryCode=" + schoolCategoryCode + ", schoolCategoryCodeInstitute=" + schoolCategoryCodeInstitute + return "School [minCode=" + minCode + ", schoolId=" + schoolId + ", schoolCategoryCode=" + schoolCategoryCode + ", schoolCategoryLegacyCode=" + schoolCategoryLegacyCode + ", schoolName=" + schoolName + ", districtName=" + districtName + ", transcriptEligibility=" + transcriptEligibility + ", certificateEligibility=" + certificateEligibility + ", address1=" + address1 + ", address2=" + address2 + ", city=" + city + ", provCode=" + provCode + ", countryCode=" + countryCode + ", postal=" + postal + ", openFlag=" + openFlag + "]"; diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/repository/redis/DistrictRedisRepository.java b/api/src/main/java/ca/bc/gov/educ/api/trax/repository/redis/DistrictRedisRepository.java index 5b42704c..b38fcbb4 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/repository/redis/DistrictRedisRepository.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/repository/redis/DistrictRedisRepository.java @@ -4,9 +4,11 @@ import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface DistrictRedisRepository extends CrudRepository { String HASH_KEY = "District"; - DistrictEntity findByDistrictNumber(String districtNumber); + Optional findByDistrictNumber(String districtNumber); } diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/repository/redis/SchoolDetailRedisRepository.java b/api/src/main/java/ca/bc/gov/educ/api/trax/repository/redis/SchoolDetailRedisRepository.java index 9b788dc5..b851e7ef 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/repository/redis/SchoolDetailRedisRepository.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/repository/redis/SchoolDetailRedisRepository.java @@ -5,6 +5,7 @@ import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface SchoolDetailRedisRepository extends CrudRepository { @@ -14,5 +15,5 @@ public interface SchoolDetailRedisRepository extends CrudRepository findByDistrictId(String districtId); - SchoolDetailEntity findByMincode(String mincode); + Optional findByMincode(String mincode); } \ No newline at end of file diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/repository/redis/SchoolRedisRepository.java b/api/src/main/java/ca/bc/gov/educ/api/trax/repository/redis/SchoolRedisRepository.java index c0bec73f..e4f4684c 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/repository/redis/SchoolRedisRepository.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/repository/redis/SchoolRedisRepository.java @@ -5,12 +5,13 @@ import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface SchoolRedisRepository extends CrudRepository { String HASH_KEY = "School"; - SchoolEntity findByMincode(String mincode); + Optional findByMincode(String mincode); List findAllByDistrictIdAndMincode(String districtId, String mincode); List findAllByDistrictId(String districtId); } \ No newline at end of file diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/CommonService.java b/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/CommonService.java index 03b81492..ea87c80a 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/CommonService.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/CommonService.java @@ -43,7 +43,14 @@ public List getSchoolsForClobDataFromR public ca.bc.gov.educ.api.trax.model.dto.School getSchoolForClobDataByMinCodeFromRedisCache(String minCode) { log.debug("Get a School Clob data by MinCode from Redis Cache: {}", minCode); SchoolDetail schoolDetail = schoolService.getSchoolDetailByMincodeFromRedisCache(minCode); - return schoolDetail != null? convertSchoolDetailIntoSchoolClob(schoolDetail) : null; + return schoolDetail != null? convertSchoolDetailIntoSchoolClob((schoolDetail)) : null; + } + + // School Clob data for Algorithm Data by schoolId from RedisCache + public ca.bc.gov.educ.api.trax.model.dto.School getSchoolForClobDataBySchoolIdFromRedisCache(UUID schoolId) { + log.debug("Get a School Clob data by SchoolId from Redis Cache: {}", schoolId); + SchoolDetail schoolDetail = schoolService.getSchoolDetailBySchoolId(schoolId); + return schoolDetail != null? convertSchoolDetailIntoSchoolClob((schoolDetail)) : null; } public UUID getSchoolIdFromRedisCache(String minCode) { @@ -81,8 +88,8 @@ private School convertSchoolDetailIntoSchoolClob(ca.bc.gov.educ.api.trax.model.d school.setDistrictName(district.getDisplayName()); } - // School Category Code - school.setSchoolCategoryCodeInstitute(schoolDetail.getSchoolCategoryCode()); + // School Category + school.setSchoolCategoryCode(schoolDetail.getSchoolCategoryCode()); populateSchoolCategoryLegacyCode(school, schoolDetail.getSchoolCategoryCode()); // Address @@ -112,7 +119,7 @@ private void populateSchoolCategoryLegacyCode(School school, String schoolCatego if (StringUtils.isNotBlank(schoolCategoryCode)) { SchoolCategoryCode schoolCategoryCodeObj = codeService.getSchoolCategoryCodeFromRedisCache(schoolCategoryCode); if (schoolCategoryCodeObj != null) { - school.setSchoolCategoryCode(schoolCategoryCodeObj.getLegacyCode()); + school.setSchoolCategoryLegacyCode(schoolCategoryCodeObj.getLegacyCode()); } } } diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/DistrictService.java b/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/DistrictService.java index 1e284b7f..b79cd393 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/DistrictService.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/DistrictService.java @@ -90,12 +90,12 @@ public void initializeDistrictCache(boolean force) { public District getDistrictByDistNoFromRedisCache(String districtNumber) { log.debug("**** Getting district by district no. from Redis Cache."); - return districtTransformer.transformToDTO(districtRedisRepository.findByDistrictNumber(districtNumber)); + return districtRedisRepository.findByDistrictNumber(districtNumber).map(districtTransformer::transformToDTO).orElse(null); } public District getDistrictByIdFromRedisCache(String districtId) { log.debug("**** Getting district by ID from Redis Cache."); - return districtTransformer.transformToDTO(districtRedisRepository.findById(districtId)); + return districtRedisRepository.findById(districtId).map(districtTransformer::transformToDTO).orElse(null); } public List getDistrictsBySchoolCategoryCode(String schoolCategoryCode) { diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/SchoolService.java b/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/SchoolService.java index d44085d2..0cb19d37 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/SchoolService.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/SchoolService.java @@ -72,12 +72,12 @@ public List getSchoolsFromRedisCache() { public School getSchoolByMinCodeFromRedisCache(String minCode) { log.debug("Get School by Mincode from Redis Cache: {}", minCode); - return schoolTransformer.transformToDTO(schoolRedisRepository.findByMincode(minCode)); + return schoolRedisRepository.findByMincode(minCode).map(schoolTransformer::transformToDTO).orElse(null); } public boolean checkIfSchoolExists(String minCode) { - SchoolEntity schoolEntity = schoolRedisRepository.findByMincode(minCode); - return schoolEntity != null; + Optional schoolOptional = schoolRedisRepository.findByMincode(minCode); + return schoolOptional.isPresent(); } public void initializeSchoolCache(boolean force) { @@ -121,7 +121,12 @@ public List getSchoolDetailsFromRedisCache() { public SchoolDetail getSchoolDetailByMincodeFromRedisCache(String mincode) { log.debug("**** Getting school Details By Mincode from Redis Cache."); - return schoolDetailTransformer.transformToDTO(schoolDetailRedisRepository.findByMincode(mincode)); + return schoolDetailRedisRepository.findByMincode(mincode).map(schoolDetailTransformer::transformToDTO).orElse(null); + } + + public SchoolDetail getSchoolDetailBySchoolId(UUID schoolId) { + log.debug("**** Getting school Details By SchoolId from Redis Cache."); + return schoolDetailRedisRepository.findById(String.valueOf(schoolId)).map(schoolDetailTransformer::transformToDTO).orElse(null); } public void initializeSchoolDetailCache(boolean force) { @@ -183,8 +188,8 @@ public List getSchoolsByParams(UUID districtId, String mincode) { } else if (mincode == null) { return schoolTransformer.transformToDTO(schoolRedisRepository.findAllByDistrictId(String.valueOf(districtId))); } else if(districtId == null) { - SchoolEntity schoolEntity = schoolRedisRepository.findByMincode(mincode); - return schoolEntity != null ? List.of(schoolTransformer.transformToDTO(schoolEntity)) : Collections.emptyList(); + Optional schoolOptional = schoolRedisRepository.findByMincode(mincode); + return schoolOptional.map(schoolEntity -> List.of(schoolTransformer.transformToDTO(schoolEntity))).orElse(Collections.emptyList()); } else { return schoolTransformer.transformToDTO(schoolRedisRepository.findAllByDistrictIdAndMincode(String.valueOf(districtId), mincode)); } diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/util/EducGradTraxApiConstants.java b/api/src/main/java/ca/bc/gov/educ/api/trax/util/EducGradTraxApiConstants.java index a237596f..222411a6 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/util/EducGradTraxApiConstants.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/util/EducGradTraxApiConstants.java @@ -69,6 +69,7 @@ public class EducGradTraxApiConstants { public static final String GET_STUDENT_PSI_BY_CODE_MAPPING = "/student"; + public static final String GET_DISTRICT_BY_ID_MAPPING = "/{districtId}"; public static final String GET_DISTRICT_BY_DISTNO_MAPPING = "/{distNo}"; public static final String GET_DISTRICTS_BY_SCHOOL_CATEGORY_MAPPING = "/schoolCategories"; public static final String GET_SCHOOLS_BY_SCHOOL_CATEGORY_MAPPING = "/schoolCategories"; diff --git a/api/src/test/java/ca/bc/gov/educ/api/trax/controller/CommonControllerTest.java b/api/src/test/java/ca/bc/gov/educ/api/trax/controller/CommonControllerTest.java index 5abd62a8..208fdae2 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/trax/controller/CommonControllerTest.java +++ b/api/src/test/java/ca/bc/gov/educ/api/trax/controller/CommonControllerTest.java @@ -71,28 +71,29 @@ public void testGetSchoolsForClobDataByDistrictNumber() { @Test public void testGetSchoolClobData() { School school = new School(); - school.setSchoolId(UUID.randomUUID().toString()); + UUID schoolId = UUID.randomUUID(); + school.setSchoolId(schoolId.toString()); school.setSchoolName("Test School"); school.setMinCode("12345678"); - Mockito.when(commonService.getSchoolForClobDataByMinCodeFromRedisCache(school.getMinCode())).thenReturn(school); - commonController.getSchoolForClobDataByMinCode(school.getMinCode()); - Mockito.verify(commonService).getSchoolForClobDataByMinCodeFromRedisCache(school.getMinCode()); + Mockito.when(commonService.getSchoolForClobDataBySchoolIdFromRedisCache(schoolId)).thenReturn(school); + commonController.getSchoolForClobDataBySchoolId(schoolId); + Mockito.verify(commonService).getSchoolForClobDataBySchoolIdFromRedisCache(schoolId); } @Test public void testGetSchoolClobData_whenMinCode_isNot_Provided() { Mockito.when(validation.hasErrors()).thenReturn(true); - commonController.getSchoolForClobDataByMinCode(""); + commonController.getSchoolForClobDataBySchoolId(null); Mockito.verify(validation).stopOnErrors(); } @Test public void testGetSchoolClobData_whenMinCode_isNot_Found() { - String minCode = "12345678"; - Mockito.when(commonService.getSchoolForClobDataByMinCodeFromRedisCache(minCode)).thenReturn(null); - commonController.getSchoolForClobDataByMinCode(minCode); - Mockito.verify(commonService).getSchoolForClobDataByMinCodeFromRedisCache(minCode); + UUID schoolId = UUID.randomUUID(); + Mockito.when(commonService.getSchoolForClobDataBySchoolIdFromRedisCache(schoolId)).thenReturn(null); + commonController.getSchoolForClobDataBySchoolId(schoolId); + Mockito.verify(commonService).getSchoolForClobDataBySchoolIdFromRedisCache(schoolId); } } diff --git a/api/src/test/java/ca/bc/gov/educ/api/trax/controller/DistrictControllerTest.java b/api/src/test/java/ca/bc/gov/educ/api/trax/controller/DistrictControllerTest.java index 8926ed5f..8e59fbf4 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/trax/controller/DistrictControllerTest.java +++ b/api/src/test/java/ca/bc/gov/educ/api/trax/controller/DistrictControllerTest.java @@ -13,6 +13,7 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import java.util.ArrayList; @@ -90,7 +91,7 @@ public void whenGetDistrictDetailsByDistNo_ReturnNULL() { districtControllerV2.getDistrictDetailsByDistNo(distNo); Mockito.verify(districtServiceV2, never()).getDistrictByDistNoFromRedisCache(distNo); - Assertions.assertEquals(null, districtControllerV2.getDistrictDetailsByDistNo(distNo)); + Assertions.assertEquals(new ResponseEntity<>(HttpStatus.NOT_FOUND), districtControllerV2.getDistrictDetailsByDistNo(distNo)); } @Test diff --git a/api/src/test/java/ca/bc/gov/educ/api/trax/controller/v2/DistrictControllerV2Test.java b/api/src/test/java/ca/bc/gov/educ/api/trax/controller/v2/DistrictControllerV2Test.java index 3598608c..23e5a4e5 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/trax/controller/v2/DistrictControllerV2Test.java +++ b/api/src/test/java/ca/bc/gov/educ/api/trax/controller/v2/DistrictControllerV2Test.java @@ -1,16 +1,19 @@ package ca.bc.gov.educ.api.trax.controller.v2; +import ca.bc.gov.educ.api.trax.model.dto.institute.District; import ca.bc.gov.educ.api.trax.service.institute.DistrictService; +import ca.bc.gov.educ.api.trax.util.ResponseHelper; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.ResponseEntity; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) class DistrictControllerV2Test { @@ -21,18 +24,45 @@ class DistrictControllerV2Test { @InjectMocks private DistrictController districtController; + @Mock + private ResponseHelper responseHelperMock; + @Test - void testReloadSchoolCategoryCodesIntoCache_shouldReturnOK() { + void whenReloadSchoolCategoryCodesIntoCache_shouldReturnOK() { doNothing().when(districtService).initializeDistrictCache(true); districtController.reloadDistrictsIntoCache(); Mockito.verify(districtService).initializeDistrictCache(true); } @Test - void testReloadSchoolCategoryCodesIntoCache_shouldThrowException() { + void whenReloadSchoolCategoryCodesIntoCache_shouldThrowException() { doThrow(new RuntimeException()).when(districtService).initializeDistrictCache(true); districtController.reloadDistrictsIntoCache(); assertThrows(RuntimeException.class, () -> districtService.initializeDistrictCache(true)); } + @Test + void whenGetDistrictDetailsById_shouldReturnDistrict() { + District district = new District(); + district.setDistrictId("1a-2b-3c"); + district.setDistrictNumber("01"); + district.setDistrictRegionCode("reg-code"); + ResponseEntity resp = ResponseEntity.ok().body(district); + when(districtService.getDistrictByIdFromRedisCache("1a-2b-3c")).thenReturn(district); + when(responseHelperMock.GET(district)).thenReturn(resp); + ResponseEntity actual = districtController.getDistrictDetailsById("1a-2b-3c"); + Mockito.verify(districtService).getDistrictByIdFromRedisCache(district.getDistrictId()); + assertEquals(resp, actual); + } + + @Test + void whenGetDistrictDetailsById_shouldReturn_NOT_FOUND() { + String districtId = "1a-2b-3c"; + ResponseEntity resp = ResponseEntity.notFound().build(); + when(districtService.getDistrictByIdFromRedisCache(districtId)).thenReturn(null); + ResponseEntity actual = districtController.getDistrictDetailsById(districtId); + Mockito.verify(districtService).getDistrictByIdFromRedisCache(districtId); + assertEquals(resp, actual); + } + } diff --git a/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/CommonServiceTest.java b/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/CommonServiceTest.java index 21dfa551..7a4f2657 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/CommonServiceTest.java +++ b/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/CommonServiceTest.java @@ -17,9 +17,12 @@ import redis.clients.jedis.JedisCluster; import java.util.List; +import java.util.Optional; import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.mockito.Mockito.when; @RunWith(SpringRunner.class) @@ -50,9 +53,10 @@ public class CommonServiceTest { @Test public void testGetSchoolIdFromRedisCache() { + UUID schoolId = UUID.randomUUID(); String minCode = "12345678"; String distNo = "123"; - SchoolDetail schoolDetail = mockInstituteData(minCode, distNo, "PUBLIC", "01"); + SchoolDetail schoolDetail = mockInstituteData(schoolId, minCode, distNo, "PUBLIC", "01"); var result = commonService.getSchoolIdFromRedisCache(minCode); assertThat(result).isEqualTo(UUID.fromString(schoolDetail.getSchoolId())); @@ -60,9 +64,10 @@ public void testGetSchoolIdFromRedisCache() { @Test public void testGetSchoolIdStrFromRedisCache() { + UUID schoolId = UUID.randomUUID(); String minCode = "12345678"; String distNo = "123"; - SchoolDetail schoolDetail = mockInstituteData(minCode, distNo, "PUBLIC", "01"); + SchoolDetail schoolDetail = mockInstituteData(schoolId, minCode, distNo, "PUBLIC", "01"); var result = commonService.getSchoolIdStrFromRedisCache(minCode); assertThat(result).isEqualTo(schoolDetail.getSchoolId()); @@ -70,9 +75,10 @@ public void testGetSchoolIdStrFromRedisCache() { @Test public void testGetAllSchoolClobs() { + UUID schoolId = UUID.randomUUID(); String minCode = "12345678"; String distNo = "123"; - SchoolDetail schoolDetail = mockInstituteData(minCode, distNo, "PUBLIC", "01"); + SchoolDetail schoolDetail = mockInstituteData(schoolId, minCode, distNo, "PUBLIC", "01"); var result = commonService.getSchoolsForClobDataFromRedisCache(); assertThat(result).hasSize(1); @@ -81,9 +87,10 @@ public void testGetAllSchoolClobs() { @Test public void testGetSchoolsByDistrictNumber() { + UUID schoolId = UUID.randomUUID(); String minCode = "12345678"; String distNo = "123"; - SchoolDetail schoolDetail = mockInstituteData(minCode, distNo, "PUBLIC", "01"); + SchoolDetail schoolDetail = mockInstituteData(schoolId, minCode, distNo, "PUBLIC", "01"); when(schoolService.getSchoolDetailsByDistrictFromRedisCache(schoolDetail.getDistrictId())).thenReturn(List.of(schoolDetail)); @@ -100,21 +107,22 @@ public void testGetSchoolsByDistrictNumber_returnEmptyList() { @Test public void testGetSchoolClob() { + UUID schoolId = UUID.randomUUID(); String minCode = "12345678"; String distNo = "123"; String schoolCategoryLegacyCode = "01"; - SchoolDetail schoolDetail = mockInstituteData(minCode, distNo, "PUBLIC", schoolCategoryLegacyCode); + SchoolDetail schoolDetail = mockInstituteData(schoolId, minCode, distNo, "PUBLIC", schoolCategoryLegacyCode); - var result = commonService.getSchoolForClobDataByMinCodeFromRedisCache(minCode); + var result = commonService.getSchoolForClobDataBySchoolIdFromRedisCache(schoolId); assertThat(result).isNotNull(); assertThat(result.getSchoolId()).isEqualTo(schoolDetail.getSchoolId()); assertThat(result.getMinCode()).isEqualTo(schoolDetail.getMincode()); - assertThat(result.getSchoolCategoryCode()).isEqualTo(schoolCategoryLegacyCode); + assertThat(result.getSchoolCategoryLegacyCode()).isEqualTo(schoolCategoryLegacyCode); assertThat(result.getDistrictName()).isNotNull(); assertThat(result.getAddress1()).isNotNull(); } - public SchoolDetail mockInstituteData(String minCode, String distNo, String schoolCategoryCode, String schoolCategoryLegacyCode) { + public SchoolDetail mockInstituteData(UUID schoolId, String minCode, String distNo, String schoolCategoryCode, String schoolCategoryLegacyCode) { District district = new District(); district.setDistrictId(UUID.randomUUID().toString()); district.setDistrictNumber(distNo); @@ -125,7 +133,7 @@ public SchoolDetail mockInstituteData(String minCode, String distNo, String scho schoolCategory.setSchoolCategoryCode(schoolCategoryCode); SchoolDetail schoolDetail = new SchoolDetail(); - schoolDetail.setSchoolId(UUID.randomUUID().toString()); + schoolDetail.setSchoolId(schoolId.toString()); schoolDetail.setMincode(minCode); schoolDetail.setDisplayName("Test School"); schoolDetail.setDistrictId(district.getDistrictId()); @@ -151,12 +159,62 @@ public SchoolDetail mockInstituteData(String minCode, String distNo, String scho when(this.codeService.getSchoolCategoryCodeFromRedisCache(schoolCategoryCode)).thenReturn(schoolCategory); when(this.districtService.getDistrictByIdFromRedisCache(district.getDistrictId())).thenReturn(district); when(this.districtService.getDistrictByDistNoFromRedisCache(district.getDistrictNumber())).thenReturn(district); + when(this.schoolService.getSchoolDetailBySchoolId(schoolId)).thenReturn(schoolDetail); when(this.schoolService.getSchoolDetailByMincodeFromRedisCache(minCode)).thenReturn(schoolDetail); when(this.schoolService.getSchoolDetailsFromRedisCache()).thenReturn(List.of(schoolDetail)); - when(this.schoolService.getSchoolDetailByMincodeFromRedisCache(minCode)).thenReturn(schoolDetail); when(this.schoolService.getSchoolByMinCodeFromRedisCache(minCode)).thenReturn(school); + when(this.schoolService.getSchoolBySchoolId(schoolId)).thenReturn(Optional.of(school)); return schoolDetail; } + @Test + public void whenGetSchoolForClobDataBySchoolIdFromRedisCache_shouldReturnSchool() { + UUID schoolId = UUID.randomUUID(); + String schoolIdString = schoolId.toString(); + SchoolDetail schoolDetail = new SchoolDetail(); + schoolDetail.setSchoolId(schoolIdString); + schoolDetail.setMincode("12345"); + schoolDetail.setDisplayName("My School"); + ca.bc.gov.educ.api.trax.model.dto.School expected = new ca.bc.gov.educ.api.trax.model.dto.School(); + expected.setSchoolId(schoolIdString); + expected.setMinCode("12345"); + expected.setSchoolName("My School"); + when(schoolService.getSchoolDetailBySchoolId(schoolId)).thenReturn(schoolDetail); + ca.bc.gov.educ.api.trax.model.dto.School actual = commonService.getSchoolForClobDataBySchoolIdFromRedisCache(schoolId); + assertEquals(expected, actual); + } + + @Test + public void whenGetSchoolForClobDataBySchoolIdFromRedisCache_shouldReturnNull() { + UUID schoolId = UUID.randomUUID(); + when(schoolService.getSchoolDetailBySchoolId(schoolId)).thenReturn(null); + ca.bc.gov.educ.api.trax.model.dto.School actual = commonService.getSchoolForClobDataBySchoolIdFromRedisCache(schoolId); + assertNull(actual); + } + + @Test + public void whenGetSchoolForClobDataByMinCodeFromRedisCache_shouldReturnSchool() { + UUID schoolId = UUID.randomUUID(); + String schoolIdString = schoolId.toString(); + SchoolDetail schoolDetail = new SchoolDetail(); + schoolDetail.setSchoolId(schoolIdString); + schoolDetail.setMincode("12345"); + schoolDetail.setDisplayName("My School"); + ca.bc.gov.educ.api.trax.model.dto.School expected = new ca.bc.gov.educ.api.trax.model.dto.School(); + expected.setSchoolId(schoolIdString); + expected.setMinCode("12345"); + expected.setSchoolName("My School"); + when(schoolService.getSchoolDetailByMincodeFromRedisCache(schoolDetail.getMincode())).thenReturn(schoolDetail); + ca.bc.gov.educ.api.trax.model.dto.School actual = commonService.getSchoolForClobDataByMinCodeFromRedisCache(schoolDetail.getMincode()); + assertEquals(expected, actual); + } + + @Test + public void whenGetSchoolForClobDataByMinCodeFromRedisCache_shouldReturnNull() { + String mincode = "12345"; + when(schoolService.getSchoolDetailByMincodeFromRedisCache(mincode)).thenReturn(null); + ca.bc.gov.educ.api.trax.model.dto.School actual = commonService.getSchoolForClobDataByMinCodeFromRedisCache(mincode); + assertNull(actual); + } } diff --git a/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/InstituteDistrictServiceTest.java b/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/InstituteDistrictServiceTest.java index 53c70f24..701b793c 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/InstituteDistrictServiceTest.java +++ b/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/InstituteDistrictServiceTest.java @@ -309,7 +309,7 @@ public void whenGetDistrictByDistNoFromRedisCache_ReturnDistrict() { districtEntity.setContacts(Arrays.asList(new DistrictContactEntity(), new DistrictContactEntity())); when(this.districtRedisRepository.findByDistrictNumber(distNo)) - .thenReturn(districtEntity); + .thenReturn(Optional.of(districtEntity)); when(this.districtTransformerMock.transformToDTO(districtEntity)) .thenReturn(district); assertEquals(district, districtService.getDistrictByDistNoFromRedisCache(distNo)); @@ -333,7 +333,7 @@ public void whenGetDistrictByIdFromRedisCache_ReturnDistrict() { when(this.districtRedisRepository.findById("ID")) .thenReturn(Optional.of(districtEntity)); - when(this.districtTransformerMock.transformToDTO(Optional.of(districtEntity))) + when(this.districtTransformerMock.transformToDTO(districtEntity)) .thenReturn(district); assertEquals(district, districtService.getDistrictByIdFromRedisCache("ID")); } @@ -390,7 +390,7 @@ public void whenGetDistrictsBySchoolCategoryCode_ReturnDistricts() { schoolDetail.setSchoolId("ID"); schoolDetail.setDistrictId("DistID"); schoolDetail.setSchoolNumber("12345"); - schoolDetail.setSchoolCategoryCode("SCC"); + schoolDetail.setSchoolCategoryCode(schoolCategoryCode); schoolDetail.setEmail("abc@xyz.ca"); schoolDetails.add(schoolDetail); @@ -398,7 +398,7 @@ public void whenGetDistrictsBySchoolCategoryCode_ReturnDistricts() { schoolDetail.setSchoolId("ID"); schoolDetail.setDistrictId("DistID"); schoolDetail.setSchoolNumber("12345"); - schoolDetail.setSchoolCategoryCode("SCC"); + schoolDetail.setSchoolCategoryCode(schoolCategoryCode); schoolDetail.setEmail("abc@xyz.ca"); schoolDetails.add(schoolDetail); @@ -407,7 +407,7 @@ public void whenGetDistrictsBySchoolCategoryCode_ReturnDistricts() { schoolDetailEntity.setSchoolId("ID"); schoolDetailEntity.setDistrictId("DistID"); schoolDetailEntity.setSchoolNumber("12345"); - schoolDetailEntity.setSchoolCategoryCode("SCC"); + schoolDetailEntity.setSchoolCategoryCode(schoolCategoryCode); schoolDetailEntity.setEmail("abc@xyz.ca"); schoolDetailEntities.add(schoolDetailEntity); @@ -415,7 +415,7 @@ public void whenGetDistrictsBySchoolCategoryCode_ReturnDistricts() { schoolDetailEntity.setSchoolId("ID"); schoolDetailEntity.setDistrictId("DistID"); schoolDetailEntity.setSchoolNumber("12345"); - schoolDetailEntity.setSchoolCategoryCode("SCC"); + schoolDetailEntity.setSchoolCategoryCode(schoolCategoryCode); schoolDetailEntity.setEmail("abc@xyz.ca"); schoolDetailEntities.add(schoolDetailEntity); @@ -424,7 +424,9 @@ public void whenGetDistrictsBySchoolCategoryCode_ReturnDistricts() { .thenReturn(schoolDetailEntities); when(this.schoolDetailTransformer.transformToDTO(schoolDetailEntities)) .thenReturn(schoolDetails); - when(this.districtService.getDistrictByIdFromRedisCache("ID")) + when(this.districtRedisRepository.findById("DistID")) + .thenReturn(Optional.of(districtEntity)); + when(this.districtService.getDistrictByIdFromRedisCache("DistID")) .thenReturn(district); when(this.districtTransformerMock.transformToDTO(districtEntities)) .thenReturn(districts); diff --git a/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/InstituteSchoolServiceTest.java b/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/InstituteSchoolServiceTest.java index e74bd550..8da741b8 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/InstituteSchoolServiceTest.java +++ b/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/InstituteSchoolServiceTest.java @@ -1,9 +1,6 @@ package ca.bc.gov.educ.api.trax.service.institute; import ca.bc.gov.educ.api.trax.constant.CacheKey; -import ca.bc.gov.educ.api.trax.messaging.NatsConnection; -import ca.bc.gov.educ.api.trax.messaging.jetstream.Publisher; -import ca.bc.gov.educ.api.trax.messaging.jetstream.Subscriber; import ca.bc.gov.educ.api.trax.model.dto.ResponseObj; import ca.bc.gov.educ.api.trax.model.dto.institute.School; import ca.bc.gov.educ.api.trax.model.dto.institute.SchoolDetail; @@ -211,7 +208,7 @@ void whenGetSchoolByMincodeFromRedisCache_ReturnSchool() { schoolEntity.setEmail("abc@xyz.ca"); when(this.schoolRedisRepository.findByMincode(mincode)) - .thenReturn(schoolEntity); + .thenReturn(Optional.of(schoolEntity)); when(this.schoolTransformer.transformToDTO(schoolEntity)) .thenReturn(school); assertEquals(school, schoolService.getSchoolByMinCodeFromRedisCache(mincode)); @@ -261,14 +258,14 @@ void whenCheckIfSchoolExists_returnTrue() { schoolEntity.setSchoolCategoryCode("SCC"); schoolEntity.setEmail("abc@xyz.ca"); - when(schoolRedisRepository.findByMincode(minCode)).thenReturn(schoolEntity); + when(schoolRedisRepository.findByMincode(minCode)).thenReturn(Optional.of(schoolEntity)); assertEquals(true, schoolService.checkIfSchoolExists(minCode)); } @Test void whenCheckIfSchoolExists_returnFalse() { String minCode = "12345678"; - when(schoolRedisRepository.findByMincode(minCode)).thenReturn(null); + when(schoolRedisRepository.findByMincode(minCode)).thenReturn(Optional.empty()); assertEquals(false, schoolService.checkIfSchoolExists(minCode)); } @@ -440,7 +437,7 @@ void whenGetSchoolDetailByMincodeFromRedisCache_ReturnSchoolDetail() { schoolDetailEntity.setEmail("abc@xyz.ca"); when(this.schoolDetailRedisRepository.findByMincode(mincode)) - .thenReturn(schoolDetailEntity); + .thenReturn(Optional.of(schoolDetailEntity)); when(this.schoolDetailTransformer.transformToDTO(schoolDetailEntity)) .thenReturn(schoolDetail); assertEquals(schoolDetail, schoolService.getSchoolDetailByMincodeFromRedisCache(mincode)); @@ -536,7 +533,7 @@ void testGetSchoolsByParams() { Mockito.when(schoolRedisRepository.findAll()).thenReturn(List.of(schoolEntity)); Mockito.when(schoolRedisRepository.findAllByDistrictId(districtId.toString())).thenReturn(List.of(schoolEntity)); - Mockito.when(schoolRedisRepository.findByMincode(mincode)).thenReturn(schoolEntity); + Mockito.when(schoolRedisRepository.findByMincode(mincode)).thenReturn(Optional.of(schoolEntity)); Mockito.when(schoolRedisRepository.findAllByDistrictIdAndMincode(districtId.toString(), mincode)).thenReturn(List.of(schoolEntity)); Mockito.when(schoolTransformer.transformToDTO(List.of(schoolEntity))).thenReturn(List.of(school)); Mockito.when(schoolTransformer.transformToDTO(schoolEntity)).thenReturn(school); @@ -569,7 +566,7 @@ void testGetSchoolsByParams_EmptyResults() { Mockito.when(schoolRedisRepository.findAll()).thenReturn(Collections.emptyList()); Mockito.when(schoolRedisRepository.findAllByDistrictId(districtId.toString())).thenReturn(Collections.emptyList()); - Mockito.when(schoolRedisRepository.findByMincode(mincode)).thenReturn(null); + Mockito.when(schoolRedisRepository.findByMincode(mincode)).thenReturn(Optional.empty()); Mockito.when(schoolRedisRepository.findAllByDistrictIdAndMincode(districtId.toString(), mincode)).thenReturn(Collections.emptyList()); // Test case when both districtId and mincode are null