Skip to content

Commit

Permalink
GRAD2-2949: replaced minCode with schoolId for algorithm school data …
Browse files Browse the repository at this point in the history
…with enhancements.

GRAD2-2949: replaced minCode with schoolId for algorithm school data with enhancements.
  • Loading branch information
infstar committed Nov 18, 2024
1 parent 77aee6d commit e776a43
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 51 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 @@ -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 @@ -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 @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -50,29 +51,32 @@ 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()));
}

@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());
}

@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);
Expand All @@ -81,9 +85,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));

Expand All @@ -100,12 +105,13 @@ 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());
Expand All @@ -114,7 +120,7 @@ public void testGetSchoolClob() {
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);
Expand All @@ -125,7 +131,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());
Expand All @@ -151,10 +157,11 @@ 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading

0 comments on commit e776a43

Please sign in to comment.