diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/SchoolController.java b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/SchoolController.java index 7c6bc2af..ba147f53 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/SchoolController.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/SchoolController.java @@ -58,9 +58,9 @@ public List getAllSchools() { @Operation(summary = "Find a School by Mincode", description = "Get a School by Mincode", tags = { "School" }) @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "204", description = "NO CONTENT")}) - public ResponseEntity getSchoolDetails(@PathVariable String minCode, @RequestHeader(name="Authorization") String accessToken) { + public ResponseEntity getSchoolDetails(@PathVariable String minCode) { log.debug("getSchoolDetails : "); - School schoolResponse = schoolService.getSchoolDetails(minCode, accessToken.replace(BEARER, "")); + School schoolResponse = schoolService.getSchoolDetails(minCode); if(schoolResponse != null) { return response.GET(schoolResponse); }else { 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 new file mode 100644 index 00000000..e5161588 --- /dev/null +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/v2/CommonController.java @@ -0,0 +1,82 @@ +package ca.bc.gov.educ.api.trax.controller.v2; + +import ca.bc.gov.educ.api.trax.model.dto.School; +import ca.bc.gov.educ.api.trax.service.institute.CommonService; +import ca.bc.gov.educ.api.trax.util.EducGradTraxApiConstants; +import ca.bc.gov.educ.api.trax.util.GradValidation; +import ca.bc.gov.educ.api.trax.util.PermissionsConstants; +import ca.bc.gov.educ.api.trax.util.ResponseHelper; +import io.swagger.v3.oas.annotations.OpenAPIDefinition; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.info.Info; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +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.*; + +import java.util.List; + +@CrossOrigin +@RestController +@Slf4j +@OpenAPIDefinition(info = @Info(title = "API for School Clob for Graduation Algorithm Data.", description = "This Common API is for retrieving School Clob for Graduation Algorithm Data from Redis Cache.", version = "2"), + security = {@SecurityRequirement(name = "OAUTH2", scopes = {"READ_GRAD_SCHOOL_DATA"})}) +public class CommonController { + CommonService commonService; + GradValidation validation; + ResponseHelper response; + + @Autowired + public CommonController(CommonService commonService, GradValidation validation, ResponseHelper response) { + this.commonService = commonService; + this.validation = validation; + this.response = response; + } + + @GetMapping(EducGradTraxApiConstants.GRAD_SCHOOL_CLOB_URL_MAPPING_V2) + @PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA) + @Operation(summary = "Find All Schools Clob data for GRAD Algorithm Data from cache", description = "Get All Schools Clob data for GRAD Algorithm Data from cache", tags = { "Algorithm Data" }) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")}) + public ResponseEntity> getAllSchoolsForClobData() { + log.debug("getAllSchoolsClob : "); + return response.GET(commonService.getSchoolsForClobDataFromRedisCache()); + } + + @GetMapping(EducGradTraxApiConstants.GRAD_SCHOOL_CLOB_URL_MAPPING_V2 + EducGradTraxApiConstants.GET_SCHOOL_BY_CODE_MAPPING) + @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" }) + @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"); + if (validation.hasErrors()) { + validation.stopOnErrors(); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + School schoolResponse = commonService.getSchoolForClobDataByMinCodeFromRedisCache(minCode); + if (schoolResponse != null) { + return response.GET(schoolResponse); + } else { + return response.NOT_FOUND(); + } + } + + @GetMapping(EducGradTraxApiConstants.GRAD_SCHOOLS_BY_DISTRICT_URL_MAPPING_V2 + EducGradTraxApiConstants.GET_DISTRICT_BY_DISTNO_MAPPING) + @PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA) + @Operation(summary = "Find Schools Clob data by District Number for GRAD Algorithm Data from cache", description = "Get Schools Clob data by District Number for GRAD Algorithm Data from cache", tags = { "Algorithm Data" }) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")}) + public ResponseEntity> getSchoolsForClobDataByDistrictNumber(@PathVariable String distNo) { + log.debug("getSchoolsClob by districtNumber: {}", distNo); + return response.GET(commonService.getSchoolsByDistrictNumberFromRedisCache(distNo)); + } + +} diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/v2/SchoolController.java b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/v2/SchoolController.java index 79ecdfd3..29c1b35a 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/v2/SchoolController.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/v2/SchoolController.java @@ -55,7 +55,7 @@ public List getAllSchools() { @ApiResponse(responseCode = "204", description = "NO CONTENT")}) public ResponseEntity getSchoolByMincode(@PathVariable String minCode) { log.debug("getSchoolByMincode V2 : "); - School schoolResponse = schoolService.getSchoolByMincodeFromRedisCache(minCode); + School schoolResponse = schoolService.getSchoolByMinCodeFromRedisCache(minCode); if(schoolResponse != null) { return response.GET(schoolResponse); }else { 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 747a0b1a..37662f2e 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 @@ -10,33 +10,20 @@ public class School implements Comparable { private String minCode; + private String schoolId; private String schoolName; private String districtName; private String transcriptEligibility; private String certificateEligibility; - private String independentDesignation; - private String mailerType; private String address1; private String address2; private String city; - private String provCode; - private String provinceName; - private String countryCode; - private String countryName; + private String provCode; + private String countryCode; private String postal; - private String independentAffiliation; - private String openFlag; - private String signatureDistrict; - private String newMinCode; - private String schoolOrg; - private String appendTrans; - private String ministryContact; - private String principalName; - private String schoolPhone; - private String schoolFax; - private String schoolEmail; - - private String schoolCategory; + private String openFlag; + private String schoolCategoryCode; + private String schoolCategoryCodeInstitute; public String getSchoolName() { return schoolName != null ? schoolName.trim(): ""; @@ -57,54 +44,10 @@ public String getAddress2() { public String getCity() { return city != null ? city.trim(): ""; } - - public String getProvinceName() { - return provinceName != null ? provinceName.trim(): ""; - } - - public String getCountryName() { - return countryName != null ? countryName.trim(): ""; - } public String getPostal() { return postal != null ? postal.trim(): ""; } - - public String getIndependentDesignation() { - return independentDesignation != null ? independentDesignation.trim(): ""; - } - - public String getIndependentAffiliation() { - return independentAffiliation != null ? independentAffiliation.trim(): ""; - } - - public String getOpenFlag() { - return openFlag != null ? openFlag.trim(): ""; - } - - public String getReportingFlag() { - return getOpenFlag(); - } - - public String getSignatureDistrict() { - return signatureDistrict != null ? signatureDistrict.trim(): ""; - } - - public String getSchoolEmail() { - return schoolEmail != null ? schoolEmail.trim(): ""; - } - - public String getPrincipalName() { - return principalName != null ? principalName.trim(): ""; - } - - public String getAppendTrans() { - return appendTrans != null ? appendTrans.trim(): ""; - } - - public String getMinistryContact() { - return ministryContact != null ? ministryContact.trim(): ""; - } public String getMinCode() { return minCode != null ? minCode.trim(): ""; @@ -126,16 +69,10 @@ public int hashCode() { @Override public String toString() { - return "School [minCode=" + minCode + ", schoolName=" + schoolName + ", districtName=" + districtName - + ", transcriptEligibility=" + transcriptEligibility + ", certificateEligibility=" - + certificateEligibility + ", independentDesignation=" + independentDesignation + ", mailerType=" - + mailerType + ", address1=" + address1 + ", address2=" + address2 + ", city=" + city + ", provCode=" - + provCode + ", provinceName=" + provinceName + ", countryCode=" + countryCode + ", countryName=" - + countryName + ", postal=" + postal + ", independentAffiliation=" + independentAffiliation - + ", openFlag=" + openFlag + ", signatureDistrict=" + signatureDistrict + ", newMinCode=" + newMinCode - + ", schoolOrg=" + schoolOrg + ", appendTrans=" + appendTrans + ", ministryContact=" + ministryContact - + ", principalName=" + principalName + ", schoolPhone=" + schoolPhone + ", schoolFax=" + schoolFax - + ", schoolEmail=" + schoolEmail + "]"; + return "School [minCode=" + minCode + ", schoolId=" + schoolId + ", schoolCategoryCode=" + schoolCategoryCode + ", schoolCategoryCodeInstitute=" + schoolCategoryCodeInstitute + + ", schoolName=" + schoolName + ", districtName=" + districtName + ", transcriptEligibility=" + transcriptEligibility + ", certificateEligibility=" + certificateEligibility + + ", address1=" + address1 + ", address2=" + address2 + ", city=" + city + ", provCode=" + provCode + ", countryCode=" + countryCode + ", postal=" + postal + ", openFlag=" + openFlag + + "]"; } @Override diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/model/transformer/institute/SchoolTransformer.java b/api/src/main/java/ca/bc/gov/educ/api/trax/model/transformer/institute/SchoolTransformer.java index 8a07ad52..e5265740 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/model/transformer/institute/SchoolTransformer.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/model/transformer/institute/SchoolTransformer.java @@ -17,6 +17,7 @@ public class SchoolTransformer { ModelMapper modelMapper; public School transformToDTO (SchoolEntity schoolEntity) { + if (schoolEntity == null) return null; return modelMapper.map(schoolEntity, School.class); } 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 caa1c721..9b788dc5 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 @@ -12,5 +12,7 @@ public interface SchoolDetailRedisRepository extends CrudRepository findBySchoolCategoryCode(String schoolCategoryCode); + List findByDistrictId(String districtId); + SchoolDetailEntity findByMincode(String mincode); } \ No newline at end of file diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/service/SchoolService.java b/api/src/main/java/ca/bc/gov/educ/api/trax/service/SchoolService.java index 5cf27659..ace25224 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/service/SchoolService.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/service/SchoolService.java @@ -8,36 +8,39 @@ import ca.bc.gov.educ.api.trax.repository.SchoolRepository; import ca.bc.gov.educ.api.trax.repository.TraxSchoolSearchCriteria; import ca.bc.gov.educ.api.trax.repository.TraxSchoolSearchSpecification; +import ca.bc.gov.educ.api.trax.service.institute.CommonService; import ca.bc.gov.educ.api.trax.util.CommonSchoolCache; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; @Service +@Slf4j public class SchoolService { private SchoolRepository schoolRepository; private SchoolTransformer schoolTransformer; private DistrictRepository districtRepository; private DistrictTransformer districtTransformer; - private CodeService codeService; + private CommonService commonService; private CommonSchoolCache commonSchoolCache; @Autowired - public SchoolService(SchoolRepository schoolRepository, SchoolTransformer schoolTransformer, DistrictRepository districtRepository, DistrictTransformer districtTransformer, CodeService codeService, CommonSchoolCache commonSchoolCache) { + public SchoolService(SchoolRepository schoolRepository, SchoolTransformer schoolTransformer, DistrictRepository districtRepository, DistrictTransformer districtTransformer, + CommonSchoolCache commonSchoolCache, CommonService commonService) { this.schoolRepository = schoolRepository; this.schoolTransformer = schoolTransformer; this.districtRepository = districtRepository; this.districtTransformer = districtTransformer; - this.codeService = codeService; + this.commonService = commonService; this.commonSchoolCache = commonSchoolCache; } @@ -54,24 +57,16 @@ public List getSchoolList() { if (dist != null) { sL.setDistrictName(dist.getDistrictName()); } - if(StringUtils.isNotBlank(sL.getCountryCode())) { - GradCountry country = codeService.getSpecificCountryCode(sL.getCountryCode()); - if(country != null) { - sL.setCountryName(country.getCountryName()); - } - } - if(StringUtils.isNotBlank(sL.getProvCode())) { - GradProvince province = codeService.getSpecificProvinceCode(sL.getProvCode()); - if(province != null) { - sL.setProvinceName(province.getProvName()); - } + sL.setSchoolId(commonService.getSchoolIdStrFromRedisCache(sL.getMinCode())); // Dev + if (sL.getSchoolId() == null) { + log.debug("Mincode [{}] failed to get School from RedisCache!", sL.getMinCode()); } }); return schoolList; } @Transactional(readOnly = true) - public School getSchoolDetails(String minCode, String accessToken) { + public School getSchoolDetails(String minCode) { Optional entOptional = schoolRepository.findById(minCode); if(entOptional.isPresent()) { School school = schoolTransformer.transformToDTO(entOptional.get()); @@ -152,8 +147,9 @@ public List getCommonSchools() { private School adaptSchool(CommonSchool commonSchool) { School school = new School(); school.setMinCode(commonSchool.getDistNo()+commonSchool.getSchlNo()); + school.setSchoolId(commonService.getSchoolIdStrFromRedisCache(school.getMinCode())); school.setSchoolName(commonSchool.getSchoolName()); - school.setSchoolCategory(commonSchool.getSchoolCategoryCode()); + school.setSchoolCategoryCode(commonSchool.getSchoolCategoryCode()); return school; } @@ -170,20 +166,9 @@ private void adaptSchool(School school, CommonSchool commonSchool) { return; } if(school != null) { - school.setSchoolCategory(commonSchool.getSchoolCategoryCode()); - - if(StringUtils.isNotBlank(school.getCountryCode())) { - GradCountry country = codeService.getSpecificCountryCode(school.getCountryCode()); - if(country != null) { - school.setCountryName(country.getCountryName()); - } - } - if(StringUtils.isNotBlank(school.getProvCode())) { - GradProvince province = codeService.getSpecificProvinceCode(school.getProvCode()); - if(province != null) { - school.setProvinceName(province.getProvName()); - } - } + school.setSchoolCategoryCode(commonSchool.getSchoolCategoryCode()); + school.setSchoolId(commonService.getSchoolIdStrFromRedisCache(school.getMinCode())); } } + } diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/service/TraxCommonService.java b/api/src/main/java/ca/bc/gov/educ/api/trax/service/TraxCommonService.java index bf39e8d3..48f96199 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/service/TraxCommonService.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/service/TraxCommonService.java @@ -8,6 +8,7 @@ import ca.bc.gov.educ.api.trax.repository.GradCourseRepository; import ca.bc.gov.educ.api.trax.repository.TraxStudentNoRepository; import ca.bc.gov.educ.api.trax.repository.TraxStudentRepository; +import ca.bc.gov.educ.api.trax.service.institute.CommonService; import ca.bc.gov.educ.api.trax.util.EducGradTraxApiConstants; import ca.bc.gov.educ.api.trax.util.EducGradTraxApiUtils; import lombok.extern.slf4j.Slf4j; @@ -25,6 +26,8 @@ @Service @Slf4j public class TraxCommonService { + + private final CommonService commonSerivce; private final TraxStudentRepository traxStudentRepository; private final TraxStudentNoRepository traxStudentNoRepository; private final GradCourseRepository gradCourseRepository; @@ -35,12 +38,14 @@ public class TraxCommonService { private final EducGradTraxApiConstants constants; @Autowired - public TraxCommonService(TraxStudentRepository traxStudentRepository, + public TraxCommonService(CommonService commonService, + TraxStudentRepository traxStudentRepository, TraxStudentNoRepository traxStudentNoRepository, GradCourseRepository gradCourseRepository, TraxStudentNoTransformer traxStudentNoTransformer, GradCourseTransformer gradCourseTransformer, EducGradTraxApiConstants constants) { + this.commonSerivce = commonService; this.traxStudentRepository = traxStudentRepository; this.traxStudentNoRepository = traxStudentNoRepository; this.gradCourseRepository = gradCourseRepository; diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/service/TraxUpdateService.java b/api/src/main/java/ca/bc/gov/educ/api/trax/service/TraxUpdateService.java index c8af73ac..20bbbc83 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/service/TraxUpdateService.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/service/TraxUpdateService.java @@ -7,6 +7,7 @@ import ca.bc.gov.educ.api.trax.model.entity.TraxUpdateInGradEntity; import ca.bc.gov.educ.api.trax.repository.TraxUpdatedPubEventRepository; import ca.bc.gov.educ.api.trax.repository.TraxUpdateInGradRepository; +import ca.bc.gov.educ.api.trax.service.institute.CommonService; import ca.bc.gov.educ.api.trax.util.JsonUtil; import ca.bc.gov.educ.api.trax.util.RestUtils; import com.fasterxml.jackson.core.JsonProcessingException; @@ -33,6 +34,9 @@ public class TraxUpdateService { @Autowired private TraxCommonService traxCommonService; + @Autowired + private CommonService commonService; + @Autowired private TraxUpdateInGradRepository traxUpdateInGradRepository; @@ -102,7 +106,6 @@ private TraxUpdatedPubEvent persistTraxUpdatedEvent(TraxUpdateInGradEntity traxS private ConvGradStudent populateNewStudent(String pen) { ConvGradStudent payload = null; - String accessToken = fetchAccessToken(); List results = traxCommonService.getStudentMasterDataFromTrax(pen); if (results != null && !results.isEmpty()) { payload = results.get(0); @@ -131,7 +134,6 @@ private TraxDemographicsUpdateDTO populateDemographicsInfo(String pen) { private TraxStudentUpdateDTO populateEventPayload(String updateType, String pen) { TraxStudentUpdateDTO result = null; ConvGradStudent traxStudent; - String accessToken = fetchAccessToken(); List results = traxCommonService.getStudentMasterDataFromTrax(pen); if (results != null && !results.isEmpty()) { traxStudent = results.get(0); @@ -145,6 +147,8 @@ private TraxStudentUpdateDTO populateEventPayload(String updateType, String pen) gradUpdate.setGraduationRequirementYear(traxStudent.getGraduationRequirementYear()); gradUpdate.setStudentGrade(traxStudent.getStudentGrade()); gradUpdate.setSchoolOfRecord(traxStudent.getSchoolOfRecord()); + // TODO(jsung): implement after GRAD2-2645 is merged +// gradUpdate.setSchoolOfRecordId(commonService.getSchoolIdFromRedisCache(gradUpdate.getSchoolOfRecord())); gradUpdate.setSlpDate(traxStudent.getSlpDate()); gradUpdate.setCitizenship(traxStudent.getStudentCitizenship()); gradUpdate.setStudentStatus(traxStudent.getStudentStatus()); @@ -184,11 +188,4 @@ private void publishToJetStream(final TraxUpdatedPubEvent traxUpdatedPubEvent) { } } - private String fetchAccessToken() { - ResponseObj res = restUtils.getTokenResponseObject(); - if (res != null) { - return res.getAccess_token(); - } - return null; - } } diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/CodeService.java b/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/CodeService.java index 8c376ab5..23afeac4 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/CodeService.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/CodeService.java @@ -62,6 +62,11 @@ public void loadSchoolCategoryCodesIntoRedisCache(List schoo log.info(String.format("%s School Category Codes Loaded into cache.", schoolCategoryCodes.size())); } + public SchoolCategoryCode getSchoolCategoryCodeFromRedisCache(String schoolCategoryCode) { + log.debug("**** Getting school category codes from Redis Cache."); + return schoolCategoryCodeTransformer.transformToDTO(schoolCategoryCodeRedisRepository.findById(schoolCategoryCode)); + } + public List getSchoolCategoryCodesFromRedisCache() { log.debug("**** Getting school category codes from Redis Cache."); return schoolCategoryCodeTransformer.transformToDTO(schoolCategoryCodeRedisRepository.findAll()); 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 new file mode 100644 index 00000000..03b81492 --- /dev/null +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/service/institute/CommonService.java @@ -0,0 +1,120 @@ +package ca.bc.gov.educ.api.trax.service.institute; + +import ca.bc.gov.educ.api.trax.model.dto.School; +import ca.bc.gov.educ.api.trax.model.dto.institute.District; +import ca.bc.gov.educ.api.trax.model.dto.institute.SchoolAddress; +import ca.bc.gov.educ.api.trax.model.dto.institute.SchoolCategoryCode; +import ca.bc.gov.educ.api.trax.model.dto.institute.SchoolDetail; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +@Service +@Slf4j +public class CommonService { + private CodeService codeService; + private DistrictService districtService; + private SchoolService schoolService; + + @Autowired + public CommonService(@Qualifier("InstituteCodeService") CodeService codeService, + @Qualifier("InstituteDistrictService") DistrictService districtService, + @Qualifier("InstituteSchoolService") SchoolService schoolService) { + this.codeService = codeService; + this.districtService = districtService; + this.schoolService = schoolService; + } + + // ================ Graduation Algorithm Data supports (School Clob) ==================== + // All of Schools Clob data for Algorithm Data from RedisCache + public List getSchoolsForClobDataFromRedisCache() { + log.debug("Get All Schools Clob data from Redis Cache."); + List schoolDetails = schoolService.getSchoolDetailsFromRedisCache(); + return schoolDetails.stream().map(this::convertSchoolDetailIntoSchoolClob).toList(); + } + + // School Clob data for Algorithm Data by minCode from RedisCache + 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; + } + + public UUID getSchoolIdFromRedisCache(String minCode) { + ca.bc.gov.educ.api.trax.model.dto.institute.School school = schoolService.getSchoolByMinCodeFromRedisCache(minCode); + return school != null && StringUtils.isNotBlank(school.getSchoolId())? UUID.fromString(school.getSchoolId()) : null; + } + + public String getSchoolIdStrFromRedisCache(String minCode) { + ca.bc.gov.educ.api.trax.model.dto.institute.School school = schoolService.getSchoolByMinCodeFromRedisCache(minCode); + return school != null && StringUtils.isNotBlank(school.getSchoolId())? school.getSchoolId() : null; + } + + public List getSchoolsByDistrictNumberFromRedisCache(String districtNumber) { + District district = districtService.getDistrictByDistNoFromRedisCache(districtNumber); + if (district != null) { + List schoolDetails = schoolService.getSchoolDetailsByDistrictFromRedisCache(district.getDistrictId()); + return schoolDetails.stream().map(this::convertSchoolDetailIntoSchoolClob).toList(); + } + return new ArrayList<>(); + } + + private School convertSchoolDetailIntoSchoolClob(ca.bc.gov.educ.api.trax.model.dto.institute.SchoolDetail schoolDetail) { + School school = new School(); + + school.setMinCode(schoolDetail.getMincode()); + school.setSchoolId(schoolDetail.getSchoolId()); + school.setSchoolName(schoolDetail.getDisplayName()); + school.setTranscriptEligibility(schoolDetail.isCanIssueTranscripts()? "Y" : "N"); + school.setCertificateEligibility(schoolDetail.isCanIssueCertificates()? "Y" : "N"); + school.setOpenFlag(schoolDetail.getClosedDate() == null? "Y" : "N"); + + // District + ca.bc.gov.educ.api.trax.model.dto.institute.District district = districtService.getDistrictByIdFromRedisCache(schoolDetail.getDistrictId()); + if (district != null) { + school.setDistrictName(district.getDisplayName()); + } + + // School Category Code + school.setSchoolCategoryCodeInstitute(schoolDetail.getSchoolCategoryCode()); + populateSchoolCategoryLegacyCode(school, schoolDetail.getSchoolCategoryCode()); + + // Address + populateSchoolAddress(school, schoolDetail.getAddresses()); + + return school; + } + + private void populateSchoolAddress(School school, List addresses) { + // Address + if (addresses != null && !addresses.isEmpty()) { + for (SchoolAddress address : addresses) { + if ("MAILING".equalsIgnoreCase(address.getAddressTypeCode())) { + school.setAddress1(address.getAddressLine1()); + school.setAddress2(address.getAddressLine2()); + + school.setCity(address.getCity()); + school.setPostal(address.getPostal()); + school.setProvCode(address.getProvinceCode()); + school.setCountryCode(address.getCountryCode()); + } + } + } + } + + private void populateSchoolCategoryLegacyCode(School school, String schoolCategoryCode) { + if (StringUtils.isNotBlank(schoolCategoryCode)) { + SchoolCategoryCode schoolCategoryCodeObj = codeService.getSchoolCategoryCodeFromRedisCache(schoolCategoryCode); + if (schoolCategoryCodeObj != null) { + school.setSchoolCategoryCode(schoolCategoryCodeObj.getLegacyCode()); + } + } + } + +} 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 21cdad6f..90993798 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 @@ -69,9 +69,9 @@ public List getSchoolsFromRedisCache() { return schoolTransformer.transformToDTO(schoolRedisRepository.findAll()); } - public School getSchoolByMincodeFromRedisCache(String mincode) { - log.debug("Get School by Mincode from Redis Cache"); - return schoolTransformer.transformToDTO(schoolRedisRepository.findByMincode(mincode)); + public School getSchoolByMinCodeFromRedisCache(String minCode) { + log.debug("Get School by Mincode from Redis Cache: {}", minCode); + return schoolTransformer.transformToDTO(schoolRedisRepository.findByMincode(minCode)); } public boolean checkIfSchoolExists(String minCode) { @@ -133,6 +133,11 @@ public List getSchoolDetailsBySchoolCategoryCode(String schoolCate schoolDetailRedisRepository.findBySchoolCategoryCode(schoolCategoryCode)); } + public List getSchoolDetailsByDistrictFromRedisCache(String districtId) { + return schoolDetailTransformer.transformToDTO( + schoolDetailRedisRepository.findByDistrictId(districtId)); + } + /** * Updates the school and school details in the cache * based on schoolId 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 abcc5051..a7c690a4 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 @@ -47,7 +47,8 @@ public class EducGradTraxApiConstants { public static final String GRAD_TSW_URL_MAPPING_V2 = GRAD_TRAX_API_ROOT_MAPPING_V2 + "/tsw"; public static final String GRAD_EDW_URL_MAPPING_V1 = GRAD_TRAX_API_ROOT_MAPPING_V1 + "/edw"; public static final String GRAD_EDW_URL_MAPPING_V2 = GRAD_TRAX_API_ROOT_MAPPING_V2 + "/edw"; - + public static final String GRAD_SCHOOL_CLOB_URL_MAPPING_V2 = GRAD_TRAX_API_ROOT_MAPPING_V2 + "/school-clob"; + public static final String GRAD_SCHOOLS_BY_DISTRICT_URL_MAPPING_V2 = GRAD_TRAX_API_ROOT_MAPPING_V2 + "/schools-by-district"; // Service Methods Mappings public static final String GET_ALL_COUNTRY_MAPPING = "/country"; 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 new file mode 100644 index 00000000..5abd62a8 --- /dev/null +++ b/api/src/test/java/ca/bc/gov/educ/api/trax/controller/CommonControllerTest.java @@ -0,0 +1,98 @@ +package ca.bc.gov.educ.api.trax.controller; + +import ca.bc.gov.educ.api.trax.controller.v2.CommonController; +import ca.bc.gov.educ.api.trax.model.dto.School; +import ca.bc.gov.educ.api.trax.service.institute.CommonService; +import ca.bc.gov.educ.api.trax.util.EducGradTraxApiConstants; +import ca.bc.gov.educ.api.trax.util.GradValidation; +import ca.bc.gov.educ.api.trax.util.MessageHelper; +import ca.bc.gov.educ.api.trax.util.ResponseHelper; +import org.junit.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; +import java.util.UUID; + +@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) +@SuppressWarnings("ALL") +public class CommonControllerTest { + + @InjectMocks + private CommonController commonController; + + @Mock + CommonService commonService; + + @Mock + GradValidation validation; + + @Mock + MessageHelper messagesHelper; + + @Mock + ResponseHelper response; + + @Autowired + private EducGradTraxApiConstants constants; + + @Test + public void testGetAllSchoolsClobData() { + School school = new School(); + school.setSchoolId(UUID.randomUUID().toString()); + school.setSchoolName("Test School"); + school.setMinCode("12345678"); + + Mockito.when(commonService.getSchoolsForClobDataFromRedisCache()).thenReturn(List.of(school)); + commonController.getAllSchoolsForClobData(); + Mockito.verify(commonService).getSchoolsForClobDataFromRedisCache(); + } + + @Test + public void testGetSchoolsForClobDataByDistrictNumber() { + String distNo = "003"; + School school = new School(); + school.setSchoolId(UUID.randomUUID().toString()); + school.setSchoolName("Test School"); + school.setMinCode("12345678"); + + Mockito.when(commonService.getSchoolsByDistrictNumberFromRedisCache(distNo)).thenReturn(List.of(school)); + commonController.getSchoolsForClobDataByDistrictNumber(distNo); + Mockito.verify(commonService).getSchoolsByDistrictNumberFromRedisCache(distNo); + } + + @Test + public void testGetSchoolClobData() { + School school = new School(); + school.setSchoolId(UUID.randomUUID().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()); + } + + @Test + public void testGetSchoolClobData_whenMinCode_isNot_Provided() { + Mockito.when(validation.hasErrors()).thenReturn(true); + commonController.getSchoolForClobDataByMinCode(""); + 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); + } + +} diff --git a/api/src/test/java/ca/bc/gov/educ/api/trax/controller/SchoolControllerTest.java b/api/src/test/java/ca/bc/gov/educ/api/trax/controller/SchoolControllerTest.java index f21c28aa..7a51b4cb 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/trax/controller/SchoolControllerTest.java +++ b/api/src/test/java/ca/bc/gov/educ/api/trax/controller/SchoolControllerTest.java @@ -81,9 +81,9 @@ public void testGetSchoolDetails() { final School school = new School(); school.setMinCode("1234567"); school.setSchoolName("Test School"); - Mockito.when(schoolService.getSchoolDetails("1234567", "accessToken")).thenReturn(school); - schoolController.getSchoolDetails("1234567", "accessToken"); - Mockito.verify(schoolService).getSchoolDetails("1234567", "accessToken"); + Mockito.when(schoolService.getSchoolDetails("1234567")).thenReturn(school); + schoolController.getSchoolDetails("1234567"); + Mockito.verify(schoolService).getSchoolDetails("1234567"); } @@ -189,9 +189,9 @@ public void whenGetSchoolByMincode_ReturnsSchool() { school.setDistrictId("9876543"); school.setMincode(mincode); - Mockito.when(schoolServiceV2.getSchoolByMincodeFromRedisCache(mincode)).thenReturn(school); + Mockito.when(schoolServiceV2.getSchoolByMinCodeFromRedisCache(mincode)).thenReturn(school); schoolControllerV2.getSchoolByMincode(mincode); - Mockito.verify(schoolServiceV2).getSchoolByMincodeFromRedisCache(mincode); + Mockito.verify(schoolServiceV2).getSchoolByMinCodeFromRedisCache(mincode); } @Test @@ -202,9 +202,9 @@ public void whenGetSchoolByMincode_Return_NOT_FOUND() { school.setDistrictId("9876543"); school.setMincode(mincode); - Mockito.when(schoolServiceV2.getSchoolByMincodeFromRedisCache(mincode)).thenReturn(null); + Mockito.when(schoolServiceV2.getSchoolByMinCodeFromRedisCache(mincode)).thenReturn(null); schoolControllerV2.getSchoolByMincode(mincode); - Mockito.verify(schoolServiceV2).getSchoolByMincodeFromRedisCache(mincode); + Mockito.verify(schoolServiceV2).getSchoolByMinCodeFromRedisCache(mincode); assertEquals(responseHelper.NOT_FOUND(), schoolControllerV2.getSchoolByMincode(mincode)); } @@ -270,4 +270,5 @@ public void whenGetSchoolDetailsByMincode_Return_NOT_FOUND() { Mockito.verify(schoolServiceV2).getSchoolDetailByMincodeFromRedisCache(mincode); assertEquals(responseHelper.NOT_FOUND(), schoolControllerV2.getSchoolDetailsByMincode(mincode)); } + } diff --git a/api/src/test/java/ca/bc/gov/educ/api/trax/service/SchoolServiceTest.java b/api/src/test/java/ca/bc/gov/educ/api/trax/service/SchoolServiceTest.java index 49408079..cb95d699 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/trax/service/SchoolServiceTest.java +++ b/api/src/test/java/ca/bc/gov/educ/api/trax/service/SchoolServiceTest.java @@ -11,6 +11,7 @@ import ca.bc.gov.educ.api.trax.repository.SchoolRepository; import ca.bc.gov.educ.api.trax.repository.TraxSchoolSearchCriteria; import ca.bc.gov.educ.api.trax.repository.TraxSchoolSearchSpecification; +import ca.bc.gov.educ.api.trax.service.institute.CommonService; import ca.bc.gov.educ.api.trax.util.CommonSchoolCache; import ca.bc.gov.educ.api.trax.util.EducGradTraxApiConstants; import org.apache.commons.lang3.StringUtils; @@ -37,6 +38,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.UUID; import java.util.function.Consumer; import static org.assertj.core.api.Assertions.assertThat; @@ -59,6 +61,9 @@ class SchoolServiceTest { @MockBean private CodeService codeService; + @MockBean + private CommonService commonService; + @Autowired private EducGradTraxApiConstants constants; @@ -261,7 +266,7 @@ void testGetSchoolDetails() { Mockito.when(codeService.getSpecificProvinceCode(province.getProvCode())).thenReturn(province); mockCommonSchool("02121000", "Test School"); - var result = schoolService.getSchoolDetails("02121000", "accessToken"); + var result = schoolService.getSchoolDetails("02121000"); assertThat(result).isNotNull(); assertThat(result.getMinCode()).isEqualTo("02121000"); @@ -302,7 +307,7 @@ void testGetSchoolDetails_empty() { Mockito.when(codeService.getSpecificProvinceCode(province.getProvCode())).thenReturn(province); mockCommonSchool("02121000", null); - var result = schoolService.getSchoolDetails("02121000", "accessToken"); + var result = schoolService.getSchoolDetails("02121000"); assertThat(result).isNull(); } @@ -342,7 +347,7 @@ void testGetSchoolDetails_not_empty_null() { mockCommonSchool("02121000", null); - var result = schoolService.getSchoolDetails("1234567", "accessToken"); + var result = schoolService.getSchoolDetails("1234567"); assertThat(result).isNull(); } @@ -492,10 +497,12 @@ void testCommonSchoolIsNull() { } void mockCommonSchool(String minCode, String schoolName) { + String schoolCategoryCode = "02"; + CommonSchool commonSchool = new CommonSchool(); commonSchool.setSchlNo(minCode); commonSchool.setSchoolName(schoolName); - commonSchool.setSchoolCategoryCode("02"); + commonSchool.setSchoolCategoryCode(schoolCategoryCode); Mockito.when(this.commonSchoolCache.getAllCommonSchools()).thenReturn(List.of(commonSchool)); Mockito.when(this.commonSchoolCache.getSchoolByMincode(minCode)).thenReturn(commonSchool); @@ -512,13 +519,26 @@ void mockCommonSchool(String minCode, String schoolName) { Mockito.when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock); Mockito.when(this.responseMock.bodyToMono(new ParameterizedTypeReference>() { })).thenReturn(Mono.just(List.of(commonSchool))); + + // Institute School from Redis Cache + School school = new School(); + school.setSchoolId(UUID.randomUUID().toString()); + school.setMinCode(minCode); + school.setSchoolName(schoolName); + school.setSchoolCategoryCode(schoolCategoryCode); + Mockito.when(commonService.getSchoolIdFromRedisCache(any())).thenReturn(UUID.fromString(school.getSchoolId())); + Mockito.when(commonService.getSchoolIdStrFromRedisCache(any())).thenReturn(school.getSchoolId()); + Mockito.when(commonService.getSchoolForClobDataByMinCodeFromRedisCache(any())).thenReturn(school); + Mockito.when(commonService.getSchoolsForClobDataFromRedisCache()).thenReturn(List.of(school)); } void mockCommonSchools() { + String mincode = "1234567"; + String schoolCategoryCode = "02"; CommonSchool commonSchool = new CommonSchool(); - commonSchool.setSchlNo("1234567"); + commonSchool.setSchlNo(mincode); commonSchool.setSchoolName("Test School"); - commonSchool.setSchoolCategoryCode("02"); + commonSchool.setSchoolCategoryCode(schoolCategoryCode); Mockito.when(this.commonSchoolCache.getAllCommonSchools()).thenReturn(List.of(commonSchool)); @@ -528,5 +548,15 @@ void mockCommonSchools() { Mockito.when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock); Mockito.when(this.responseMock.bodyToMono(new ParameterizedTypeReference>() { })).thenReturn(Mono.just(List.of(commonSchool))); + + // Institute School from Redis Cache + School school = new School(); + school.setSchoolId(UUID.randomUUID().toString()); + school.setMinCode(mincode); + school.setSchoolCategoryCode(schoolCategoryCode); + Mockito.when(commonService.getSchoolIdFromRedisCache(any())).thenReturn(UUID.fromString(school.getSchoolId())); + Mockito.when(commonService.getSchoolIdStrFromRedisCache(any())).thenReturn(school.getSchoolId()); + Mockito.when(commonService.getSchoolForClobDataByMinCodeFromRedisCache(any())).thenReturn(school); + Mockito.when(commonService.getSchoolsForClobDataFromRedisCache()).thenReturn(List.of(school)); } } diff --git a/api/src/test/java/ca/bc/gov/educ/api/trax/service/TraxCommonServiceTest.java b/api/src/test/java/ca/bc/gov/educ/api/trax/service/TraxCommonServiceTest.java index dc82677d..3c1bbef4 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/trax/service/TraxCommonServiceTest.java +++ b/api/src/test/java/ca/bc/gov/educ/api/trax/service/TraxCommonServiceTest.java @@ -10,6 +10,7 @@ import ca.bc.gov.educ.api.trax.model.transformer.GradCourseTransformer; import ca.bc.gov.educ.api.trax.model.transformer.TraxStudentNoTransformer; import ca.bc.gov.educ.api.trax.repository.*; +import ca.bc.gov.educ.api.trax.service.institute.CommonService; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -57,6 +58,9 @@ public class TraxCommonServiceTest { @MockBean TraxStudentNoRepository traxStudentNoRepository; + @MockBean + CommonService commonService; + @MockBean SchoolService schoolService; @@ -116,6 +120,7 @@ public void testGetStudentMasterDataFromTrax() { list.add(cols); when(this.traxStudentRepository.loadTraxStudent(pen)).thenReturn(results); + when(this.commonService.getSchoolIdFromRedisCache("12345678")).thenReturn(UUID.randomUUID()); var result = traxCommonService.getStudentMasterDataFromTrax(pen); diff --git a/api/src/test/java/ca/bc/gov/educ/api/trax/service/TraxUpdateServiceTest.java b/api/src/test/java/ca/bc/gov/educ/api/trax/service/TraxUpdateServiceTest.java index 513ea66a..02ab8744 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/trax/service/TraxUpdateServiceTest.java +++ b/api/src/test/java/ca/bc/gov/educ/api/trax/service/TraxUpdateServiceTest.java @@ -11,6 +11,7 @@ import ca.bc.gov.educ.api.trax.model.transformer.TraxUpdateInGradTransformer; import ca.bc.gov.educ.api.trax.repository.TraxUpdateInGradRepository; import ca.bc.gov.educ.api.trax.repository.TraxUpdatedPubEventRepository; +import ca.bc.gov.educ.api.trax.service.institute.CommonService; import ca.bc.gov.educ.api.trax.util.JsonUtil; import ca.bc.gov.educ.api.trax.util.RestUtils; import com.fasterxml.jackson.core.JsonProcessingException; @@ -60,6 +61,9 @@ public class TraxUpdateServiceTest { @MockBean TraxCommonService traxCommonService; + @MockBean + CommonService commonService; + @Autowired TraxUpdateInGradTransformer traxUpdateInGradTransformer; @@ -176,6 +180,7 @@ public void testScheduledRunForTraxUpdates() throws JsonProcessingException { when(traxUpdateInGradRepository.findOutstandingUpdates(any())).thenReturn(Arrays.asList(traxUpdateInGradEntity)); when(traxUpdatedPubEventRepository.save(traxUpdatedPubEvent)).thenReturn(traxUpdatedPubEvent); when(traxCommonService.getStudentMasterDataFromTrax(pen)).thenReturn(Arrays.asList(traxStudent)); +// when(commonService.getSchoolIdFromRedisCache(mincode)).thenReturn(schoolOfRecordId); traxUpdateService.publishTraxUpdatedEvent(traxUpdateInGradEntity); traxUpdateService.updateStatus(traxUpdateInGradEntity); @@ -335,6 +340,7 @@ public void testProcess_whenGraduation_isUpdated() throws JsonProcessingExceptio when(restUtils.getTokenResponseObject()).thenReturn(tokenObj); when(traxUpdatedPubEventRepository.save(traxUpdatedPubEvent)).thenReturn(traxUpdatedPubEvent); when(traxCommonService.getStudentMasterDataFromTrax(pen)).thenReturn(Arrays.asList(traxStudent)); +// when(commonService.getSchoolIdFromRedisCache(mincode)).thenReturn(schoolOfRecordId); traxUpdateService.publishTraxUpdatedEvent(traxUpdateInGradEntity); traxUpdateService.updateStatus(traxUpdateInGradEntity); @@ -385,6 +391,7 @@ public void testProcess_whenStudentStatus_isUpdated() throws JsonProcessingExcep when(restUtils.getTokenResponseObject()).thenReturn(tokenObj); when(traxUpdatedPubEventRepository.save(traxUpdatedPubEvent)).thenReturn(traxUpdatedPubEvent); when(traxCommonService.getStudentMasterDataFromTrax(pen)).thenReturn(Arrays.asList(traxStudent)); +// when(commonService.getSchoolIdFromRedisCache(mincode)).thenReturn(schoolOfRecordId); traxUpdateService.publishTraxUpdatedEvent(traxUpdateInGradEntity); traxUpdateService.updateStatus(traxUpdateInGradEntity); 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 new file mode 100644 index 00000000..21dfa551 --- /dev/null +++ b/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/CommonServiceTest.java @@ -0,0 +1,162 @@ +package ca.bc.gov.educ.api.trax.service.institute; + +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.institute.*; +import org.junit.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.runner.RunWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import redis.clients.jedis.JedisCluster; + +import java.util.List; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +@RunWith(SpringRunner.class) +@SpringBootTest +@ActiveProfiles("test") +@ExtendWith(MockitoExtension.class) +public class CommonServiceTest { + @Autowired + private CommonService commonService; + @MockBean + private CodeService codeService; + @MockBean + private DistrictService districtService; + @MockBean + private SchoolService schoolService; + + @MockBean + private JedisConnectionFactory jedisConnectionFactoryMock; + @MockBean + private JedisCluster jedisClusterMock; + // NATS + @MockBean + private NatsConnection natsConnection; + @MockBean + private Publisher publisher; + @MockBean + private Subscriber subscriber; + + @Test + public void testGetSchoolIdFromRedisCache() { + String minCode = "12345678"; + String distNo = "123"; + SchoolDetail schoolDetail = mockInstituteData(minCode, distNo, "PUBLIC", "01"); + + var result = commonService.getSchoolIdFromRedisCache(minCode); + assertThat(result).isEqualTo(UUID.fromString(schoolDetail.getSchoolId())); + } + + @Test + public void testGetSchoolIdStrFromRedisCache() { + String minCode = "12345678"; + String distNo = "123"; + SchoolDetail schoolDetail = mockInstituteData(minCode, distNo, "PUBLIC", "01"); + + var result = commonService.getSchoolIdStrFromRedisCache(minCode); + assertThat(result).isEqualTo(schoolDetail.getSchoolId()); + } + + @Test + public void testGetAllSchoolClobs() { + String minCode = "12345678"; + String distNo = "123"; + SchoolDetail schoolDetail = mockInstituteData(minCode, distNo, "PUBLIC", "01"); + + var result = commonService.getSchoolsForClobDataFromRedisCache(); + assertThat(result).hasSize(1); + assertThat(result.get(0).getSchoolId()).isEqualTo(schoolDetail.getSchoolId()); + } + + @Test + public void testGetSchoolsByDistrictNumber() { + String minCode = "12345678"; + String distNo = "123"; + SchoolDetail schoolDetail = mockInstituteData(minCode, distNo, "PUBLIC", "01"); + + when(schoolService.getSchoolDetailsByDistrictFromRedisCache(schoolDetail.getDistrictId())).thenReturn(List.of(schoolDetail)); + + var result = commonService.getSchoolsByDistrictNumberFromRedisCache(distNo); + assertThat(result).hasSize(1); + assertThat(result.get(0).getSchoolId()).isEqualTo(schoolDetail.getSchoolId()); + } + + @Test + public void testGetSchoolsByDistrictNumber_returnEmptyList() { + var result = commonService.getSchoolsByDistrictNumberFromRedisCache("123"); + assertThat(result).isEmpty(); + } + + @Test + public void testGetSchoolClob() { + String minCode = "12345678"; + String distNo = "123"; + String schoolCategoryLegacyCode = "01"; + SchoolDetail schoolDetail = mockInstituteData(minCode, distNo, "PUBLIC", schoolCategoryLegacyCode); + + var result = commonService.getSchoolForClobDataByMinCodeFromRedisCache(minCode); + assertThat(result).isNotNull(); + assertThat(result.getSchoolId()).isEqualTo(schoolDetail.getSchoolId()); + assertThat(result.getMinCode()).isEqualTo(schoolDetail.getMincode()); + assertThat(result.getSchoolCategoryCode()).isEqualTo(schoolCategoryLegacyCode); + assertThat(result.getDistrictName()).isNotNull(); + assertThat(result.getAddress1()).isNotNull(); + } + + public SchoolDetail mockInstituteData(String minCode, String distNo, String schoolCategoryCode, String schoolCategoryLegacyCode) { + District district = new District(); + district.setDistrictId(UUID.randomUUID().toString()); + district.setDistrictNumber(distNo); + district.setDisplayName("Test District"); + + SchoolCategoryCode schoolCategory = new SchoolCategoryCode(); + schoolCategory.setLegacyCode(schoolCategoryLegacyCode); + schoolCategory.setSchoolCategoryCode(schoolCategoryCode); + + SchoolDetail schoolDetail = new SchoolDetail(); + schoolDetail.setSchoolId(UUID.randomUUID().toString()); + schoolDetail.setMincode(minCode); + schoolDetail.setDisplayName("Test School"); + schoolDetail.setDistrictId(district.getDistrictId()); + schoolDetail.setSchoolCategoryCode(schoolCategoryCode); + + SchoolAddress address = new SchoolAddress(); + address.setSchoolId(schoolDetail.getSchoolId()); + address.setAddressLine1("123 Test"); + address.setCity("Vancouver"); + address.setProvinceCode("BC"); + address.setPostal("V4N3Y2"); + address.setAddressTypeCode("MAILING"); + address.setCountryCode("CA"); + + schoolDetail.setAddresses(List.of(address)); + + School school = new School(); + school.setMincode(minCode); + school.setDistrictId(district.getDistrictId()); + school.setSchoolId(schoolDetail.getSchoolId()); + school.setSchoolCategoryCode(schoolCategoryLegacyCode); + + 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.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); + + return schoolDetail; + } + +} 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 e1720176..80cd3f34 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 @@ -247,7 +247,40 @@ public void whenGetSchoolByMincodeFromRedisCache_ReturnSchool() { .thenReturn(schoolEntity); when(this.schoolTransformer.transformToDTO(schoolEntity)) .thenReturn(school); - assertEquals(school, schoolService.getSchoolByMincodeFromRedisCache(mincode)); + assertEquals(school, schoolService.getSchoolByMinCodeFromRedisCache(mincode)); + } + + @Test + public void whenGetSchoolDetailsByDistrictFromRedisCache_ReturnSchools() { + String districtId = "DistID"; + String mincode = "12345678"; + SchoolDetail schoolDetail = new SchoolDetail(); + schoolDetail.setSchoolId("ID"); + schoolDetail.setDistrictId(districtId); + schoolDetail.setSchoolNumber("12345"); + schoolDetail.setMincode(mincode); + schoolDetail.setSchoolCategoryCode("SCC"); + schoolDetail.setEmail("abc@xyz.ca"); + + List schoolDetails = new ArrayList<>(); + schoolDetails.add(schoolDetail); + + SchoolDetailEntity schoolDetailEntity = new SchoolDetailEntity(); + schoolDetailEntity.setSchoolId("ID"); + schoolDetailEntity.setDistrictId(districtId); + schoolDetailEntity.setSchoolNumber("12345"); + schoolDetailEntity.setMincode(mincode); + schoolDetailEntity.setSchoolCategoryCode("SCC"); + schoolDetailEntity.setEmail("abc@xyz.ca"); + + List schoolDetailEntities = new ArrayList<>(); + schoolDetailEntities.add(schoolDetailEntity); + + when(this.schoolDetailRedisRepository.findByDistrictId(districtId)) + .thenReturn(List.of(schoolDetailEntity)); + when(this.schoolDetailTransformer.transformToDTO(schoolDetailEntities)) + .thenReturn(schoolDetails); + assertEquals(schoolDetails, schoolService.getSchoolDetailsByDistrictFromRedisCache(districtId)); } @Test