Skip to content

Commit

Permalink
Merge pull request #371 from bcgov/feature/GRAD2-2949
Browse files Browse the repository at this point in the history
GRAD2-2949: replaced minCode with schoolId for algorithm school data …
  • Loading branch information
infstar authored Nov 19, 2024
2 parents 77aee6d + f496a5c commit fc88dd9
Show file tree
Hide file tree
Showing 16 changed files with 186 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
Expand Down Expand Up @@ -47,22 +48,22 @@ public ResponseEntity<List<School>> 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<School> getSchoolForClobDataByMinCode(@PathVariable String minCode) {
log.debug("getSchoolClobData by minCode: {}", minCode);
validation.requiredField(minCode, "minCode");
public ResponseEntity<School> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -53,18 +54,30 @@ public ResponseEntity<String> 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<District> getDistrictDetailsByDistNo(@PathVariable String distNo) {
public ResponseEntity<District> 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<District> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class School implements Comparable<School> {
private String postal;
private String openFlag;
private String schoolCategoryCode;
private String schoolCategoryCodeInstitute;
private String schoolCategoryLegacyCode;

public String getSchoolName() {
return schoolName != null ? schoolName.trim(): "";
Expand Down Expand Up @@ -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
+ "]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DistrictEntity, String> {
String HASH_KEY = "District";

DistrictEntity findByDistrictNumber(String districtNumber);
Optional<DistrictEntity> findByDistrictNumber(String districtNumber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface SchoolDetailRedisRepository extends CrudRepository<SchoolDetailEntity, String> {
Expand All @@ -14,5 +15,5 @@ public interface SchoolDetailRedisRepository extends CrudRepository<SchoolDetail

List<SchoolDetailEntity> findByDistrictId(String districtId);

SchoolDetailEntity findByMincode(String mincode);
Optional<SchoolDetailEntity> findByMincode(String mincode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface SchoolRedisRepository extends CrudRepository<SchoolEntity, String> {
String HASH_KEY = "School";

SchoolEntity findByMincode(String mincode);
Optional<SchoolEntity> findByMincode(String mincode);
List<SchoolEntity> findAllByDistrictIdAndMincode(String districtId, String mincode);
List<SchoolEntity> findAllByDistrictId(String districtId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ public List<ca.bc.gov.educ.api.trax.model.dto.School> 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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<District> getDistrictsBySchoolCategoryCode(String schoolCategoryCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public List<School> 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<SchoolEntity> schoolOptional = schoolRedisRepository.findByMincode(minCode);
return schoolOptional.isPresent();
}

public void initializeSchoolCache(boolean force) {
Expand Down Expand Up @@ -121,7 +121,12 @@ public List<SchoolDetail> 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) {
Expand Down Expand Up @@ -183,8 +188,8 @@ public List<School> 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<SchoolEntity> 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<District> resp = ResponseEntity.ok().body(district);
when(districtService.getDistrictByIdFromRedisCache("1a-2b-3c")).thenReturn(district);
when(responseHelperMock.GET(district)).thenReturn(resp);
ResponseEntity<District> 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<District> resp = ResponseEntity.notFound().build();
when(districtService.getDistrictByIdFromRedisCache(districtId)).thenReturn(null);
ResponseEntity<District> actual = districtController.getDistrictDetailsById(districtId);
Mockito.verify(districtService).getDistrictByIdFromRedisCache(districtId);
assertEquals(resp, actual);
}

}
Loading

0 comments on commit fc88dd9

Please sign in to comment.