From 3fadcce903e6992b544c5eb20c35f1620027c113 Mon Sep 17 00:00:00 2001 From: Kamal Mohammed Date: Fri, 23 Aug 2024 08:53:43 -0600 Subject: [PATCH 1/2] GRAD2-2638 - Institute - Create V2 endpoints for School data (#342) * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data * GRAD2-2638 - Institute - Create V2 endpoints for School data --------- Co-authored-by: Mohammed --- .../api/trax/controller/CodeController.java | 1 - .../trax/controller/DistrictController.java | 3 +- .../api/trax/controller/EdwController.java | 2 - .../api/trax/controller/PsiController.java | 2 - .../api/trax/controller/SchoolController.java | 4 +- .../trax/controller/TraxCommonController.java | 2 - .../controller/v2/DistrictController.java | 64 ++++ .../trax/controller/v2/SchoolController.java | 51 ++- .../trax/model/dto/institute/District.java | 2 +- .../model/dto/institute/DistrictAddress.java | 2 +- .../model/dto/institute/DistrictContact.java | 2 +- .../api/trax/model/dto/institute/Grade.java | 2 +- .../dto/institute/NeighborhoodLearning.java | 2 +- .../api/trax/model/dto/institute/Note.java | 2 +- .../api/trax/model/dto/institute/School.java | 2 +- .../model/dto/institute/SchoolAddress.java | 2 +- .../institute/DistrictTransformer.java | 1 - .../redis/DistrictRedisRepository.java | 2 + .../redis/SchoolDetailRedisRepository.java | 7 +- .../redis/SchoolRedisRepository.java | 2 + .../service/institute/DistrictService.java | 29 ++ .../trax/service/institute/SchoolService.java | 29 +- .../trax/util/EducGradTraxApiConstants.java | 2 +- .../controller/DistrictControllerTest.java | 65 +++- .../trax/controller/SchoolControllerTest.java | 95 ++++- .../institute/InstituteCodeServiceTest.java | 8 - .../InstituteDistrictServiceTest.java | 268 ++++++++++++-- .../institute/InstituteSchoolServiceTest.java | 345 ++++++++++++++++-- api/src/test/resources/application.yaml | 16 + 29 files changed, 912 insertions(+), 102 deletions(-) create mode 100644 api/src/main/java/ca/bc/gov/educ/api/trax/controller/v2/DistrictController.java diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/CodeController.java b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/CodeController.java index 4b7e2670..ee8c4410 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/CodeController.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/CodeController.java @@ -4,7 +4,6 @@ import ca.bc.gov.educ.api.trax.model.dto.GradProvince; import ca.bc.gov.educ.api.trax.service.CodeService; 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; diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/DistrictController.java b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/DistrictController.java index 4fe63774..a3425cd0 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/DistrictController.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/DistrictController.java @@ -3,7 +3,6 @@ import ca.bc.gov.educ.api.trax.model.dto.District; import ca.bc.gov.educ.api.trax.service.DistrictService; 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; @@ -48,7 +47,7 @@ public ResponseEntity getDistrictDetails(@PathVariable String distCode @GetMapping(EducGradTraxApiConstants.GRAD_DISTRICT_URL_MAPPING_V1 + EducGradTraxApiConstants.GET_DISTRICTS_BY_SCHOOL_CATEGORY_MAPPING) @PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA) - @Operation(summary = "Check school existence by Mincode", description = "Check school existence by Mincode", tags = { "School" }) + @Operation(summary = "Get Districts by SchoolCategory", description = "Get Districts by SchoolCategory", tags = { "School" }) @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "204", description = "NO CONTENT")}) public ResponseEntity> getDistrictBySchoolCategory(@RequestParam(required = false) String schoolCategory) { diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/EdwController.java b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/EdwController.java index 30552bf1..03293ca9 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/EdwController.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/EdwController.java @@ -13,8 +13,6 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.security.SecurityRequirement; import lombok.extern.slf4j.Slf4j; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/PsiController.java b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/PsiController.java index 699a331d..59ece699 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/PsiController.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/PsiController.java @@ -13,8 +13,6 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import lombok.extern.slf4j.Slf4j; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; 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 477036c6..7c6bc2af 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 @@ -78,7 +78,7 @@ public ResponseEntity> getAllCommonSchool() { @GetMapping(EducGradTraxApiConstants.GRAD_SCHOOL_URL_MAPPING_V1 + EducGradTraxApiConstants.GET_COMMON_SCHOOL_BY_CODE_MAPPING) @PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA) - @Operation(summary = "Find a Common School by Mincode", description = "Get a Common School by Mincode", tags = { "School" }) + @Operation(summary = "Find a Common School by Mincode", description = "Find a Common School by Mincode", tags = { "School" }) @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "NOT FOUND")}) public ResponseEntity getCommonSchool(@PathVariable String minCode) { @@ -114,7 +114,7 @@ public ResponseEntity checkSchoolExists(@PathVariable String minCode) { @GetMapping(EducGradTraxApiConstants.GRAD_SCHOOL_URL_MAPPING_V1 + EducGradTraxApiConstants.GET_SCHOOLS_BY_SCHOOL_CATEGORY_MAPPING) @PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA) - @Operation(summary = "Check school existence by Mincode", description = "Check school existence by Mincode", tags = { "School" }) + @Operation(summary = "Get schools by SchoolCategory", description = "Get schools by SchoolCategory", tags = { "School" }) @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "204", description = "NO CONTENT")}) public ResponseEntity> getSchoolsBySchoolCategory(@RequestParam(required = false) String schoolCategory, @RequestHeader(name="Authorization") String accessToken) { diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/TraxCommonController.java b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/TraxCommonController.java index 0361a99b..d6e9466b 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/controller/TraxCommonController.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/TraxCommonController.java @@ -9,8 +9,6 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import lombok.extern.slf4j.Slf4j; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; 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 new file mode 100644 index 00000000..06ee7d2a --- /dev/null +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/controller/v2/DistrictController.java @@ -0,0 +1,64 @@ +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.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.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@CrossOrigin +@RestController("districtControllerV2") +@Slf4j +@OpenAPIDefinition(info = @Info(title = "API for School Data.", description = "This Read API is for Reading school data.", version = "2"), + security = {@SecurityRequirement(name = "OAUTH2", scopes = {"READ_GRAD_SCHOOL_DATA"})}) +public class DistrictController { + + DistrictService districtService; + GradValidation validation; + ResponseHelper response; + + @Autowired + public DistrictController(DistrictService districtService, GradValidation validation, ResponseHelper response) { + this.districtService = districtService; + this.validation = validation; + this.response = response; + } + + @GetMapping(EducGradTraxApiConstants.GRAD_DISTRICT_URL_MAPPING_V2 + EducGradTraxApiConstants.GET_DISTRICT_BY_DISTNO_MAPPING) + @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) { + if(distNo.length() <=3) { + District distResponse = districtService.getDistrictByDistNoFromRedisCache(distNo); + if (distResponse != null) { + return response.GET(distResponse); + } + } + return null; + } + + @GetMapping(EducGradTraxApiConstants.GRAD_DISTRICT_URL_MAPPING_V2 + EducGradTraxApiConstants.GET_DISTRICTS_BY_SCHOOL_CATEGORY_MAPPING) + @PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA) + @Operation(summary = "Get District by school category code V2", description = "Get District by school category code V2", tags = { "School" }) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), + @ApiResponse(responseCode = "204", description = "NO CONTENT")}) + public ResponseEntity> getDistrictsBySchoolCategoryCode(@RequestParam(required = false) String schoolCategoryCode) { + return response.GET(districtService.getDistrictsBySchoolCategoryCode(schoolCategoryCode)); + } + +} 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 c5c501f7..5b6a9c3b 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 @@ -15,13 +15,14 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import java.util.List; @CrossOrigin -@RestController("SchoolControllerV2") +@RestController("schoolControllerV2") @Slf4j @OpenAPIDefinition(info = @Info(title = "API for School Data.", description = "This Read API is for Reading school data from Redis Cache.", version = "2"), security = {@SecurityRequirement(name = "OAUTH2", scopes = {"READ_GRAD_SCHOOL_DATA"})}) @@ -47,6 +48,21 @@ public List getAllSchools() { return schoolService.getSchoolsFromRedisCache(); } + @GetMapping(EducGradTraxApiConstants.GRAD_SCHOOL_URL_MAPPING_V2 + EducGradTraxApiConstants.GET_SCHOOL_BY_CODE_MAPPING) + @PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA) + @Operation(summary = "Find a School by Mincode from cache", description = "Get a School by Mincode from cache", tags = { "School" }) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), + @ApiResponse(responseCode = "204", description = "NO CONTENT")}) + public ResponseEntity getSchoolByMincode(@PathVariable String minCode) { + log.debug("getSchoolByMincode : "); + School schoolResponse = schoolService.getSchoolByMincodeFromRedisCache(minCode); + if(schoolResponse != null) { + return response.GET(schoolResponse); + }else { + return response.NOT_FOUND(); + } + } + @GetMapping(EducGradTraxApiConstants.GRAD_SCHOOL_DETAIL_URL_MAPPING_V2) @PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA) @Operation(summary = "Find All School details from Cache", description = "Get All School details from Cache", tags = { "School" }) @@ -56,5 +72,38 @@ public List getAllSchoolDetails() { return schoolService.getSchoolDetailsFromRedisCache(); } + @GetMapping(EducGradTraxApiConstants.GRAD_SCHOOL_URL_MAPPING_V2 + EducGradTraxApiConstants.CHECK_SCHOOL_BY_CODE_MAPPING) + @PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA) + @Operation(summary = "Check school existence by Mincode V2", description = "Check school existence by Mincode V2", tags = { "School" }) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), + @ApiResponse(responseCode = "204", description = "NO CONTENT")}) + public Boolean checkIfSchoolExists(@PathVariable String minCode) { + return schoolService.checkIfSchoolExists(minCode); + } + + @GetMapping(EducGradTraxApiConstants.GRAD_SCHOOL_URL_MAPPING_V2 + EducGradTraxApiConstants.GET_SCHOOLS_BY_SCHOOL_CATEGORY_MAPPING) + @PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA) + @Operation(summary = "Get Schools by School Category Code V2", description = "Get Schools by School Category Code V2", tags = { "School" }) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), + @ApiResponse(responseCode = "204", description = "NO CONTENT")}) + public ResponseEntity> getSchoolsBySchoolCategory(@RequestParam(required = false) String schoolCategoryCode) { + return response.GET(schoolService.getSchoolDetailsBySchoolCategoryCode(schoolCategoryCode)); + } + + @GetMapping(EducGradTraxApiConstants.GRAD_SCHOOL_DETAIL_URL_MAPPING_V2 + EducGradTraxApiConstants.GET_SCHOOL_BY_CODE_MAPPING) + @PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA) + @Operation(summary = "Find School Details by Mincode from cache", description = "Get School Details by Mincode from cache", tags = { "School" }) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), + @ApiResponse(responseCode = "204", description = "NO CONTENT")}) + public ResponseEntity getSchoolDetailsByMincode(@PathVariable String minCode) { + log.debug("getSchoolDetails : "); + SchoolDetail schoolDetailResponse = schoolService.getSchoolDetailByMincodeFromRedisCache(minCode); + if(schoolDetailResponse != null) { + return response.GET(schoolDetailResponse); + }else { + return response.NOT_FOUND(); + } + } + } diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/District.java b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/District.java index a7840b8f..e320bdd1 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/District.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/District.java @@ -11,7 +11,7 @@ @Data @EqualsAndHashCode(callSuper = true) -@Component("InstituteDistrict") +@Component("instituteDistrict") @NoArgsConstructor @AllArgsConstructor public class District extends BaseModel { diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/DistrictAddress.java b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/DistrictAddress.java index c350e60f..0663337d 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/DistrictAddress.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/DistrictAddress.java @@ -7,7 +7,7 @@ @Data @EqualsAndHashCode(callSuper = true) -@Component("DistrictAddress") +@Component("districtAddress") public class DistrictAddress extends BaseModel { private String districtAddressId; diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/DistrictContact.java b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/DistrictContact.java index 7e6e5330..c74ed9b6 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/DistrictContact.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/DistrictContact.java @@ -12,7 +12,7 @@ @Data @EqualsAndHashCode(callSuper = true) -@Component("DistrictContact") +@Component("districtContact") public class DistrictContact extends BaseModel { private String districtId; diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/Grade.java b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/Grade.java index b7b3e8dd..897cbece 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/Grade.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/Grade.java @@ -7,7 +7,7 @@ @Data @EqualsAndHashCode(callSuper = true) -@Component("Grade") +@Component("grade") public class Grade extends BaseModel { private String schoolGradeId; diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/NeighborhoodLearning.java b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/NeighborhoodLearning.java index e14df402..11350fff 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/NeighborhoodLearning.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/NeighborhoodLearning.java @@ -7,7 +7,7 @@ @Data @EqualsAndHashCode(callSuper = true) -@Component("NeighborhoodLearning") +@Component("neighborhoodLearning") public class NeighborhoodLearning extends BaseModel { private String neighborhoodLearningId; diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/Note.java b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/Note.java index 19960202..53a9b76d 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/Note.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/Note.java @@ -7,7 +7,7 @@ @Data @EqualsAndHashCode(callSuper = true) -@Component("Note") +@Component("note") public class Note extends BaseModel { private String noteId; diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/School.java b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/School.java index 245d0eef..86d3cdda 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/School.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/School.java @@ -8,7 +8,7 @@ @Data @EqualsAndHashCode(callSuper = true) -@Component("InstituteSchool") +@Component("instituteSchool") @JsonIgnoreProperties(ignoreUnknown = true) public class School extends BaseModel { diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/SchoolAddress.java b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/SchoolAddress.java index ae01421b..a47feadd 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/SchoolAddress.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/model/dto/institute/SchoolAddress.java @@ -7,7 +7,7 @@ @Data @EqualsAndHashCode(callSuper = true) -@Component("SchoolAddress") +@Component("schoolAddress") public class SchoolAddress extends BaseModel { private String SchoolAddressId; diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/model/transformer/institute/DistrictTransformer.java b/api/src/main/java/ca/bc/gov/educ/api/trax/model/transformer/institute/DistrictTransformer.java index f3f49056..2aa3f811 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/model/transformer/institute/DistrictTransformer.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/model/transformer/institute/DistrictTransformer.java @@ -2,7 +2,6 @@ import ca.bc.gov.educ.api.trax.model.dto.institute.District; import ca.bc.gov.educ.api.trax.model.entity.institute.DistrictEntity; -import ca.bc.gov.educ.api.trax.model.entity.institute.SchoolDetailEntity; import org.modelmapper.ModelMapper; import org.modelmapper.TypeToken; import org.springframework.beans.factory.annotation.Autowired; 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 b0a9d1e4..5b42704c 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 @@ -7,4 +7,6 @@ @Repository public interface DistrictRedisRepository extends CrudRepository { String HASH_KEY = "District"; + + DistrictEntity 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 9bc3cc86..caa1c721 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 @@ -1,11 +1,16 @@ package ca.bc.gov.educ.api.trax.repository.redis; import ca.bc.gov.educ.api.trax.model.entity.institute.SchoolDetailEntity; -import ca.bc.gov.educ.api.trax.model.entity.institute.SchoolEntity; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface SchoolDetailRedisRepository extends CrudRepository { String HASH_KEY = "SchoolDetail"; + + List findBySchoolCategoryCode(String schoolCategoryCode); + + SchoolDetailEntity 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 4df623d3..0e21d486 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 @@ -7,4 +7,6 @@ @Repository public interface SchoolRedisRepository extends CrudRepository { String HASH_KEY = "School"; + + SchoolEntity findByMincode(String mincode); } \ No newline at end of file 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 1d528dcd..dc4586ac 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 @@ -3,6 +3,7 @@ import ca.bc.gov.educ.api.trax.constant.CacheKey; import ca.bc.gov.educ.api.trax.exception.ServiceException; import ca.bc.gov.educ.api.trax.model.dto.institute.District; +import ca.bc.gov.educ.api.trax.model.dto.institute.SchoolDetail; import ca.bc.gov.educ.api.trax.model.entity.institute.DistrictEntity; import ca.bc.gov.educ.api.trax.model.transformer.institute.DistrictTransformer; import ca.bc.gov.educ.api.trax.repository.redis.DistrictRedisRepository; @@ -15,6 +16,7 @@ import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClientResponseException; +import java.util.ArrayList; import java.util.List; @Slf4j @@ -33,6 +35,8 @@ public class DistrictService { @Autowired ServiceHelper serviceHelper; @Autowired + SchoolService schoolService; + @Autowired RESTService restService; public List getDistrictsFromInstituteApi() { @@ -64,6 +68,31 @@ public void initializeDistrictCache(boolean force) { serviceHelper.initializeCache(force, CacheKey.DISTRICT_CACHE, this); } + public District getDistrictByDistNoFromRedisCache(String districtNumber) { + log.debug("**** Getting district by district no. from Redis Cache."); + return districtTransformer.transformToDTO(districtRedisRepository.findByDistrictNumber(districtNumber)); + } + + public District getDistrictByIdFromRedisCache(String districtId) { + log.debug("**** Getting district by ID from Redis Cache."); + return districtTransformer.transformToDTO(districtRedisRepository.findById(districtId)); + } + + public List getDistrictsBySchoolCategoryCode(String schoolCategoryCode) { + List schoolDetails; + + if (schoolCategoryCode.isBlank() || schoolCategoryCode.isEmpty()) + schoolDetails = schoolService.getSchoolDetailsFromRedisCache(); + else + schoolDetails = schoolService.getSchoolDetailsBySchoolCategoryCode(schoolCategoryCode); + + List districts = new ArrayList<>(); + for (SchoolDetail schoolDetail : schoolDetails) { + districts.add(getDistrictByIdFromRedisCache(schoolDetail.getDistrictId())); + } + return districts; + } + /** * Updates the district details in the cache * based on schoolId 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 44ff99e2..5d775f73 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,6 +69,16 @@ 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 boolean checkIfSchoolExists(String minCode) { + SchoolEntity schoolEntity = schoolRedisRepository.findByMincode(minCode); + return schoolEntity != null; + } + public void initializeSchoolCache(boolean force) { serviceHelper.initializeCache(force, CacheKey.SCHOOL_CACHE, this); } @@ -90,13 +100,9 @@ public SchoolDetail getSchoolDetailByIdFromInstituteApi(String schoolId) { public List getSchoolDetailsFromInstituteApi() { List schools = getSchoolsFromRedisCache(); - List schoolDetails = new ArrayList(); - + List schoolDetails = new ArrayList<>(); for (School s : schools) { - SchoolDetail sd = new SchoolDetail(); - - sd = getSchoolDetailByIdFromInstituteApi(s.getSchoolId()); - schoolDetails.add(sd); + schoolDetails.add(getSchoolDetailByIdFromInstituteApi(s.getSchoolId())); } return schoolDetails; } @@ -112,10 +118,21 @@ public List getSchoolDetailsFromRedisCache() { return schoolDetailTransformer.transformToDTO(schoolDetailRedisRepository.findAll()); } + public SchoolDetail getSchoolDetailByMincodeFromRedisCache(String mincode) { + log.debug("**** Getting school Details By Mincode from Redis Cache."); + return schoolDetailTransformer.transformToDTO(schoolDetailRedisRepository.findByMincode(mincode)); + } + public void initializeSchoolDetailCache(boolean force) { serviceHelper.initializeCache(force, CacheKey.SCHOOL_DETAIL_CACHE, this); } + public List getSchoolDetailsBySchoolCategoryCode(String schoolCategoryCode) { + + return schoolDetailTransformer.transformToDTO( + schoolDetailRedisRepository.findBySchoolCategoryCode(schoolCategoryCode)); + } + /** * 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 36491b76..3dd06d0c 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 @@ -67,7 +67,7 @@ public class EducGradTraxApiConstants { public static final String GET_STUDENT_PSI_BY_CODE_MAPPING = "/student"; - public static final String GET_DISTRICT_BY_DISTNO_MAPPING = "/{distCode}"; + 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/DistrictControllerTest.java b/api/src/test/java/ca/bc/gov/educ/api/trax/controller/DistrictControllerTest.java index d2338a94..8926ed5f 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 @@ -1,9 +1,11 @@ package ca.bc.gov.educ.api.trax.controller; import ca.bc.gov.educ.api.trax.model.dto.District; +import ca.bc.gov.educ.api.trax.model.transformer.institute.DistrictTransformer; import ca.bc.gov.educ.api.trax.service.DistrictService; import ca.bc.gov.educ.api.trax.util.ResponseHelper; import org.junit.Test; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.runner.RunWith; import org.mockito.InjectMocks; @@ -11,9 +13,13 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.ResponseEntity; +import java.util.ArrayList; import java.util.List; +import static org.mockito.Mockito.never; + @RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class) @SuppressWarnings("rawtypes") @@ -21,6 +27,8 @@ public class DistrictControllerTest { @Mock private DistrictService districtService; + @Mock + private ca.bc.gov.educ.api.trax.service.institute.DistrictService districtServiceV2; @Mock ResponseHelper responseHelper; @@ -28,6 +36,11 @@ public class DistrictControllerTest { @InjectMocks private DistrictController districtController; + @InjectMocks + private ca.bc.gov.educ.api.trax.controller.v2.DistrictController districtControllerV2; + @Mock + DistrictTransformer districtTransformer; + @Test public void testGetSchoolDetails() { final District district = new District(); @@ -35,9 +48,10 @@ public void testGetSchoolDetails() { district.setDistrictName("Test School"); Mockito.when(districtService.getDistrictDetails("123")).thenReturn(district); - districtController.getDistrictDetails("123"); + Mockito.when(responseHelper.GET(district)).thenReturn(ResponseEntity.ok().body(district)); + ResponseEntity result = districtController.getDistrictDetails("123"); Mockito.verify(districtService).getDistrictDetails("123"); - + Assertions.assertEquals(district, (District) result.getBody()); } @Test @@ -49,6 +63,53 @@ public void testGetDistrictBySchoolCategoryCode() { Mockito.when(districtService.getDistrictBySchoolCategory("123")).thenReturn(List.of(district)); districtController.getDistrictBySchoolCategory("123"); Mockito.verify(districtService).getDistrictBySchoolCategory("123"); + } + + @Test + public void whenGetDistrictDetailsByDistNo_ReturnsDistrict() { + String distNo = "123"; + ca.bc.gov.educ.api.trax.model.dto.institute.District district = new ca.bc.gov.educ.api.trax.model.dto.institute.District(); + district.setDistrictId("123456"); + district.setDistrictNumber("123"); + district.setDistrictRegionCode("BC"); + + Mockito.when(districtServiceV2.getDistrictByDistNoFromRedisCache(distNo)).thenReturn(district); + Mockito.when(responseHelper.GET(district)).thenReturn(ResponseEntity.ok().body(district)); + ResponseEntity result = districtControllerV2.getDistrictDetailsByDistNo(distNo); + Mockito.verify(districtServiceV2).getDistrictByDistNoFromRedisCache(distNo); + Assertions.assertEquals(district, (ca.bc.gov.educ.api.trax.model.dto.institute.District) result.getBody()); + } + + @Test + public void whenGetDistrictDetailsByDistNo_ReturnNULL() { + String distNo = "1234"; + ca.bc.gov.educ.api.trax.model.dto.institute.District district = new ca.bc.gov.educ.api.trax.model.dto.institute.District(); + district.setDistrictId("123456"); + district.setDistrictNumber("12"); + district.setDistrictRegionCode("BC"); + + districtControllerV2.getDistrictDetailsByDistNo(distNo); + Mockito.verify(districtServiceV2, never()).getDistrictByDistNoFromRedisCache(distNo); + Assertions.assertEquals(null, districtControllerV2.getDistrictDetailsByDistNo(distNo)); + } + + @Test + public void whenGetDistrictsBySchoolCategoryCode_ReturnListOfDistricts() { + String schoolCategoryCode = "123"; + final List districts = new ArrayList<>(); + ca.bc.gov.educ.api.trax.model.dto.institute.District district = new ca.bc.gov.educ.api.trax.model.dto.institute.District(); + district.setDistrictId("123456"); + district.setDistrictNumber("123"); + district.setDistrictRegionCode("BC"); + districts.add(district); + district = new ca.bc.gov.educ.api.trax.model.dto.institute.District(); + district.setDistrictId("789012"); + district.setDistrictNumber("456"); + district.setDistrictRegionCode("BC"); + districts.add(district); + Mockito.when(districtServiceV2.getDistrictsBySchoolCategoryCode(schoolCategoryCode)).thenReturn(districts); + districtControllerV2.getDistrictsBySchoolCategoryCode(schoolCategoryCode); + Mockito.verify(districtServiceV2).getDistrictsBySchoolCategoryCode(schoolCategoryCode); } } 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 0ad1211a..f21c28aa 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 @@ -24,6 +24,7 @@ import java.util.List; import java.util.function.Consumer; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @@ -107,10 +108,21 @@ public void testGetSchoolsBySchoolCategoryCode() { } @Test - public void testCheckSchoolExists() { - Mockito.when(schoolService.existsSchool("1234567")).thenReturn(true); - schoolController.checkSchoolExists("1234567"); - Mockito.verify(schoolService).existsSchool("1234567"); + public void testCheckSchoolExists_expectTrue() { + String mincode = "1234567"; + Mockito.when(schoolServiceV2.checkIfSchoolExists(mincode)).thenReturn(true); + schoolControllerV2.checkIfSchoolExists(mincode); + Mockito.verify(schoolServiceV2).checkIfSchoolExists(mincode); + assertEquals(true, schoolServiceV2.checkIfSchoolExists(mincode)); + } + + @Test + public void testCheckSchoolExists_expectFalse() { + String mincode = "1234567"; + Mockito.when(schoolServiceV2.checkIfSchoolExists(mincode)).thenReturn(false); + schoolControllerV2.checkIfSchoolExists(mincode); + Mockito.verify(schoolServiceV2).checkIfSchoolExists(mincode); + assertEquals(false, schoolServiceV2.checkIfSchoolExists(mincode)); } @Test @@ -169,6 +181,33 @@ public void whenGetAllSchools_ReturnsListOfSchools() { Mockito.verify(schoolServiceV2).getSchoolsFromRedisCache(); } + @Test + public void whenGetSchoolByMincode_ReturnsSchool() { + String mincode = "12345678"; + ca.bc.gov.educ.api.trax.model.dto.institute.School school = new ca.bc.gov.educ.api.trax.model.dto.institute.School(); + school.setSchoolId("1234567"); + school.setDistrictId("9876543"); + school.setMincode(mincode); + + Mockito.when(schoolServiceV2.getSchoolByMincodeFromRedisCache(mincode)).thenReturn(school); + schoolControllerV2.getSchoolByMincode(mincode); + Mockito.verify(schoolServiceV2).getSchoolByMincodeFromRedisCache(mincode); + } + + @Test + public void whenGetSchoolByMincode_Return_NOT_FOUND() { + String mincode = "12345678"; + ca.bc.gov.educ.api.trax.model.dto.institute.School school = new ca.bc.gov.educ.api.trax.model.dto.institute.School(); + school.setSchoolId("1234567"); + school.setDistrictId("9876543"); + school.setMincode(mincode); + + Mockito.when(schoolServiceV2.getSchoolByMincodeFromRedisCache(mincode)).thenReturn(null); + schoolControllerV2.getSchoolByMincode(mincode); + Mockito.verify(schoolServiceV2).getSchoolByMincodeFromRedisCache(mincode); + assertEquals(responseHelper.NOT_FOUND(), schoolControllerV2.getSchoolByMincode(mincode)); + } + @Test public void whenGetAllSchoolDetails_ReturnsListOfSchoolDetails() { final List schoolDetails = new ArrayList<>(); @@ -182,7 +221,53 @@ public void whenGetAllSchoolDetails_ReturnsListOfSchoolDetails() { schoolDetails.add(schoolDetail); Mockito.when(schoolServiceV2.getSchoolDetailsFromRedisCache()).thenReturn(schoolDetails); - schoolControllerV2.getAllSchoolDetails(); + List sd = schoolControllerV2.getAllSchoolDetails(); Mockito.verify(schoolServiceV2).getSchoolDetailsFromRedisCache(); + assertEquals(schoolDetails, sd); + } + + @Test + public void whenGetSchoolsBySchoolCategory_ReturnListOfSchoolDetails() { + String schoolCategoryCode = "SCHL_CATG"; + final List schoolDetails = new ArrayList<>(); + SchoolDetail schoolDetail = new SchoolDetail(); + schoolDetail.setSchoolId("1234567"); + schoolDetail.setDistrictId("9876543"); + schoolDetails.add(schoolDetail); + schoolDetail = new SchoolDetail(); + schoolDetail.setSchoolId("1234567"); + schoolDetail.setDistrictId("9876543"); + schoolDetails.add(schoolDetail); + + Mockito.when(schoolServiceV2.getSchoolDetailsBySchoolCategoryCode(schoolCategoryCode)).thenReturn(schoolDetails); + schoolControllerV2.getSchoolsBySchoolCategory(schoolCategoryCode); + Mockito.verify(schoolServiceV2).getSchoolDetailsBySchoolCategoryCode(schoolCategoryCode); + } + + @Test + public void whenGetSchoolDetailsByMincode_ReturnsSchoolDetail() { + String mincode = "12345678"; + SchoolDetail schoolDetail = new SchoolDetail(); + schoolDetail.setSchoolId("1234567"); + schoolDetail.setDistrictId("9876543"); + schoolDetail.setMincode(mincode); + + Mockito.when(schoolServiceV2.getSchoolDetailByMincodeFromRedisCache(mincode)).thenReturn(schoolDetail); + schoolControllerV2.getSchoolDetailsByMincode(mincode); + Mockito.verify(schoolServiceV2).getSchoolDetailByMincodeFromRedisCache(mincode); + } + + @Test + public void whenGetSchoolDetailsByMincode_Return_NOT_FOUND() { + String mincode = "12345678"; + SchoolDetail schoolDetail = new SchoolDetail(); + schoolDetail.setSchoolId("1234567"); + schoolDetail.setDistrictId("9876543"); + schoolDetail.setMincode(mincode); + + Mockito.when(schoolServiceV2.getSchoolDetailByMincodeFromRedisCache(mincode)).thenReturn(null); + schoolControllerV2.getSchoolDetailsByMincode(mincode); + 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/institute/InstituteCodeServiceTest.java b/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/InstituteCodeServiceTest.java index 63f0ac50..c76c20f3 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/InstituteCodeServiceTest.java +++ b/api/src/test/java/ca/bc/gov/educ/api/trax/service/institute/InstituteCodeServiceTest.java @@ -6,7 +6,6 @@ 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.District; import ca.bc.gov.educ.api.trax.model.dto.institute.SchoolCategoryCode; import ca.bc.gov.educ.api.trax.model.dto.institute.SchoolFundingGroupCode; import ca.bc.gov.educ.api.trax.model.entity.institute.SchoolCategoryCodeEntity; @@ -153,7 +152,6 @@ public void whenGetSchoolCategoryCodesFromInstituteApi_returnsListOfSchoolCatego .thenReturn(schoolCategoryCodes); List result = codeService.getSchoolCategoryCodesFromInstituteApi(); - //assertThat(result).hasSize(1); } @Test @@ -187,8 +185,6 @@ public void whenGetSchoolFundingGroupCodesFromInstituteApi_returnsListOfSchoolFu .thenReturn(schoolFundingGroupCodes); List result = codeService.getSchoolFundingGroupCodesFromInstituteApi(); - //assertThat(result).hasSize(1); - } @Test @@ -221,7 +217,6 @@ public void whenGetSchoolCategoryCodesFromRedisCache_GetSchoolCategoryCodes() { scce.setLabel("SCC2-label"); scces.add(scce); when(schoolCategoryCodeRedisRepository.findAll()).thenReturn(scces); - //assertTrue(codeService.getSchoolCategoryCodesFromRedisCache().size() == 2); } @Test @@ -306,7 +301,6 @@ public void whenInitializeSchoolCategoryCodeCache_WithLoadingAndTrue_ThenForceLo doNothing().when(codeServicemock).loadSchoolCategoryCodesIntoRedisCache(sccs); codeService.initializeSchoolCategoryCodeCache(true); - //verify(codeServicemock).loadSchoolCategoryCodesIntoRedisCache(sccs); } @Test @@ -330,7 +324,6 @@ public void whenGetSchoolFundingGroupCodesFromRedisCache_GetSchoolFundingGroupCo sfgce.setLabel("SFGC2-label"); sfgces.add(sfgce); when(schoolFundingGroupCodeRedisRepository.findAll()).thenReturn(sfgces); - //assertTrue(codeService.getSchoolCategoryCodesFromRedisCache().size() == 2); } @Test @@ -413,7 +406,6 @@ public void whenInitializeSchoolFundingGroupCodeCache_WithLoadingAndTrue_ThenFor doNothing().when(codeServicemock).loadSchoolFundingGroupCodesIntoRedisCache(sfgcs); codeService.initializeSchoolFundingGroupCodeCache(true); - //verify(codeServicemock).loadSchoolCategoryCodesIntoRedisCache(sccs); } 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 0f4afd93..72f5b766 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 @@ -7,10 +7,15 @@ 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.District; +import ca.bc.gov.educ.api.trax.model.dto.institute.DistrictContact; +import ca.bc.gov.educ.api.trax.model.dto.institute.SchoolDetail; import ca.bc.gov.educ.api.trax.model.entity.institute.DistrictContactEntity; import ca.bc.gov.educ.api.trax.model.entity.institute.DistrictEntity; +import ca.bc.gov.educ.api.trax.model.entity.institute.SchoolDetailEntity; import ca.bc.gov.educ.api.trax.model.transformer.institute.DistrictTransformer; +import ca.bc.gov.educ.api.trax.model.transformer.institute.SchoolDetailTransformer; import ca.bc.gov.educ.api.trax.repository.redis.DistrictRedisRepository; +import ca.bc.gov.educ.api.trax.repository.redis.SchoolDetailRedisRepository; import ca.bc.gov.educ.api.trax.service.RESTService; import ca.bc.gov.educ.api.trax.support.TestUtils; import ca.bc.gov.educ.api.trax.util.EducGradTraxApiConstants; @@ -39,14 +44,12 @@ import reactor.core.publisher.Mono; import redis.clients.jedis.JedisCluster; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.function.Consumer; import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; @@ -66,9 +69,13 @@ public class InstituteDistrictServiceTest { private DistrictTransformer districtTransformer; @Autowired private DistrictService districtService; + @Autowired + private SchoolService schoolService; @MockBean private DistrictRedisRepository districtRedisRepository; @MockBean + private SchoolDetailRedisRepository schoolDetailRedisRepository; + @MockBean private JedisConnectionFactory jedisConnectionFactoryMock; @MockBean private JedisCluster jedisClusterMock; @@ -76,6 +83,9 @@ public class InstituteDistrictServiceTest { @MockBean @Qualifier("default") WebClient webClientMock; + @MockBean + @Qualifier("instituteWebClient") + private WebClient instWebClient; @Mock @@ -92,6 +102,10 @@ public class InstituteDistrictServiceTest { private Mono> districtEntitiesMock; @Mock private List districtsMock; + @MockBean + private DistrictTransformer districtTransformerMock; + @MockBean + private SchoolDetailTransformer schoolDetailTransformer; // NATS @@ -106,6 +120,8 @@ public class InstituteDistrictServiceTest { @MockBean private RestUtils restUtils; @MockBean + private ServiceHelper serviceHelper; + @MockBean private RESTService restServiceMock; @TestConfiguration @@ -123,34 +139,31 @@ public ClientRegistration findByRegistrationId(String registrationId) { @Test public void whenGetDistrictsFromInstituteApi_returnsListOfDistricts() { - List districts = new ArrayList<>(); - DistrictEntity district = new DistrictEntity(); - + List districtEntities = new ArrayList<>(); + DistrictEntity districtEntity = new DistrictEntity(); + districtEntity.setDistrictId("ID"); + districtEntity.setDistrictNumber("1234"); + districtEntity.setDistrictStatusCode("SC"); + districtEntity.setDistrictRegionCode("RC"); + districtEntity.setContacts(Arrays.asList(new DistrictContactEntity(), new DistrictContactEntity())); + districtEntities.add(districtEntity); + + List districts = new ArrayList<>(); + District district = new District(); district.setDistrictId("ID"); district.setDistrictNumber("1234"); district.setDistrictStatusCode("SC"); district.setDistrictRegionCode("RC"); - district.setContacts(Arrays.asList(new DistrictContactEntity(), new DistrictContactEntity())); - + district.setContacts(Arrays.asList(new DistrictContact(), new DistrictContact())); districts.add(district); - when(this.restUtils.getTokenResponseObject(anyString(), anyString())) - .thenReturn(responseObjectMock); - when(this.responseObjectMock.getAccess_token()) - .thenReturn("accessToken"); - when(webClientMock.get()) - .thenReturn(requestHeadersUriSpecMock); - when(requestHeadersUriSpecMock.uri(anyString())) - .thenReturn(requestHeadersSpecMock); - when(requestHeadersSpecMock.headers(any(Consumer.class))) - .thenReturn(requestHeadersSpecMock); - when(requestHeadersSpecMock.retrieve()) - .thenReturn(responseSpecMock); - when(this.responseSpecMock.bodyToMono(new ParameterizedTypeReference>(){})) - .thenReturn(districtEntitiesMock); - when(this.districtEntitiesMock.block()).thenReturn(districts); + when(restServiceMock.get(constants.getAllDistrictsFromInstituteApiUrl(), List.class, instWebClient)) + .thenReturn(districtEntities); + when(districtTransformerMock.transformToDTO(districtEntities)) + .thenReturn(districts); List result = districtService.getDistrictsFromInstituteApi(); + assertEquals(districts, result); } @Test @@ -162,6 +175,49 @@ public void whenLoadDistrictsIntoRedisCache_DoesNotThrow() { assertDoesNotThrow(() -> districtService.loadDistrictsIntoRedisCache(districts)); } + @Test + public void whenGetDistrictsFromRedisCache_ReturnDistricts() { + List districts = new ArrayList<>(); + District district = new District(); + district.setDistrictId("ID"); + district.setDistrictNumber("1234"); + district.setDistrictStatusCode("SC"); + district.setDistrictRegionCode("RC"); + district.setContacts(Arrays.asList(new DistrictContact(), new DistrictContact())); + districts.add(district); + + district = new District(); + district.setDistrictId("ID"); + district.setDistrictNumber("1234"); + district.setDistrictStatusCode("SC"); + district.setDistrictRegionCode("RC"); + district.setContacts(Arrays.asList(new DistrictContact(), new DistrictContact())); + districts.add(district); + + List districtEntities = new ArrayList<>(); + DistrictEntity districtEntity = new DistrictEntity(); + districtEntity.setDistrictId("ID"); + districtEntity.setDistrictNumber("1234"); + districtEntity.setDistrictStatusCode("SC"); + districtEntity.setDistrictRegionCode("RC"); + districtEntity.setContacts(Arrays.asList(new DistrictContactEntity(), new DistrictContactEntity())); + districtEntities.add(districtEntity); + + districtEntity = new DistrictEntity(); + districtEntity.setDistrictId("ID"); + districtEntity.setDistrictNumber("1234"); + districtEntity.setDistrictStatusCode("SC"); + districtEntity.setDistrictRegionCode("RC"); + districtEntity.setContacts(Arrays.asList(new DistrictContactEntity(), new DistrictContactEntity())); + districtEntities.add(districtEntity); + + when(this.districtRedisRepository.findAll()) + .thenReturn(districtEntities); + when(this.districtTransformerMock.transformToDTO(districtEntities)) + .thenReturn(districts); + assertEquals(districts, districtService.getDistrictsFromRedisCache()); + } + @Test public void whenInitializeDistrictCache_WithLoadingAndFalse_DoNotForceLoad() { when(jedisClusterMock.get(CacheKey.DISTRICT_CACHE.name())) @@ -180,7 +236,6 @@ public void whenInitializeDistrictCache_WithReadyAndFalse_DoNotForceLoad() { @Test public void whenInitializeDistrictCache_WithLoadingAndTrue_ThenForceLoad() { - DistrictEntity de = new DistrictEntity(); List des = new ArrayList(); de.setDistrictId("123"); @@ -201,7 +256,6 @@ public void whenInitializeDistrictCache_WithLoadingAndTrue_ThenForceLoad() { d.setDistrictNumber("012"); ds.add(d); - when(webClientMock.get()) .thenReturn(requestHeadersUriSpecMock); when(requestHeadersUriSpecMock.uri(anyString())) @@ -233,7 +287,53 @@ public void whenInitializeDistrictCache_WithLoadingAndTrue_ThenForceLoad() { doNothing().when(districtServiceMock).loadDistrictsIntoRedisCache(ds); districtService.initializeDistrictCache(true); - //verify(codeServicemock).loadSchoolCategoryCodesIntoRedisCache(sccs); + } + + @Test + public void whenGetDistrictByDistNoFromRedisCache_ReturnDistrict() { + String distNo = "123"; + District district = new District(); + district.setDistrictId("ID"); + district.setDistrictNumber("123"); + district.setDistrictStatusCode("SC"); + district.setDistrictRegionCode("RC"); + district.setContacts(Arrays.asList(new DistrictContact(), new DistrictContact())); + + DistrictEntity districtEntity = new DistrictEntity(); + districtEntity.setDistrictId("ID"); + districtEntity.setDistrictNumber("456"); + districtEntity.setDistrictStatusCode("SC"); + districtEntity.setDistrictRegionCode("RC"); + districtEntity.setContacts(Arrays.asList(new DistrictContactEntity(), new DistrictContactEntity())); + + when(this.districtRedisRepository.findByDistrictNumber(distNo)) + .thenReturn(districtEntity); + when(this.districtTransformerMock.transformToDTO(districtEntity)) + .thenReturn(district); + assertEquals(district, districtService.getDistrictByDistNoFromRedisCache(distNo)); + } + + @Test + public void whenGetDistrictByIdFromRedisCache_ReturnDistrict() { + District district = new District(); + district.setDistrictId("ID"); + district.setDistrictNumber("1234"); + district.setDistrictStatusCode("SC"); + district.setDistrictRegionCode("RC"); + district.setContacts(Arrays.asList(new DistrictContact(), new DistrictContact())); + + DistrictEntity districtEntity = new DistrictEntity(); + districtEntity.setDistrictId("ID"); + districtEntity.setDistrictNumber("1234"); + districtEntity.setDistrictStatusCode("SC"); + districtEntity.setDistrictRegionCode("RC"); + districtEntity.setContacts(Arrays.asList(new DistrictContactEntity(), new DistrictContactEntity())); + + when(this.districtRedisRepository.findById("ID")) + .thenReturn(Optional.of(districtEntity)); + when(this.districtTransformerMock.transformToDTO(Optional.of(districtEntity))) + .thenReturn(district); + assertEquals(district, districtService.getDistrictByIdFromRedisCache("ID")); } @Test @@ -252,16 +352,121 @@ public void whenInitializeDistrictCache_WithReadyAndTrue_ThenForceLoad() { when(jedisClusterMock.get(CacheKey.DISTRICT_CACHE.name())) .thenReturn(String.valueOf(CacheStatus.READY)); when(jedisClusterMock.set(CacheKey.DISTRICT_CACHE.name(), CacheStatus.READY.name())) - .thenReturn(anyString()); + .thenReturn("OK"); districtService.initializeDistrictCache(true); + verify(serviceHelper).initializeCache(true, CacheKey.DISTRICT_CACHE, districtService); + } + + @Test + public void whenGetDistrictsBySchoolCategoryCode_ReturnDistricts() { + String schoolCategoryCode = "ABC"; + List districts = new ArrayList<>(); + District district = new District(); + district.setDistrictId("ID"); + district.setDistrictNumber("1234"); + district.setDistrictStatusCode("SC"); + district.setDistrictRegionCode("RC"); + district.setContacts(Arrays.asList(new DistrictContact(), new DistrictContact())); + districts.add(district); + + district = new District(); + district.setDistrictId("ID"); + district.setDistrictNumber("1234"); + district.setDistrictStatusCode("SC"); + district.setDistrictRegionCode("RC"); + district.setContacts(Arrays.asList(new DistrictContact(), new DistrictContact())); + districts.add(district); + + List districtEntities = new ArrayList<>(); + DistrictEntity districtEntity = new DistrictEntity(); + districtEntity.setDistrictId("ID"); + districtEntity.setDistrictNumber("1234"); + districtEntity.setDistrictStatusCode("SC"); + districtEntity.setDistrictRegionCode("RC"); + districtEntity.setContacts(Arrays.asList(new DistrictContactEntity(), new DistrictContactEntity())); + districtEntities.add(districtEntity); + + districtEntity = new DistrictEntity(); + districtEntity.setDistrictId("ID"); + districtEntity.setDistrictNumber("1234"); + districtEntity.setDistrictStatusCode("SC"); + districtEntity.setDistrictRegionCode("RC"); + districtEntity.setContacts(Arrays.asList(new DistrictContactEntity(), new DistrictContactEntity())); + districtEntities.add(districtEntity); + + List schoolDetails = new ArrayList<>(); + SchoolDetail schoolDetail = new SchoolDetail(); + schoolDetail.setSchoolId("ID"); + schoolDetail.setDistrictId("DistID"); + schoolDetail.setSchoolNumber("12345"); + schoolDetail.setSchoolCategoryCode("SCC"); + schoolDetail.setEmail("abc@xyz.ca"); + schoolDetails.add(schoolDetail); + + schoolDetail = new SchoolDetail(); + schoolDetail.setSchoolId("ID"); + schoolDetail.setDistrictId("DistID"); + schoolDetail.setSchoolNumber("12345"); + schoolDetail.setSchoolCategoryCode("SCC"); + schoolDetail.setEmail("abc@xyz.ca"); + schoolDetails.add(schoolDetail); + + List schoolDetailEntities = new ArrayList<>(); + SchoolDetailEntity schoolDetailEntity = new SchoolDetailEntity(); + schoolDetailEntity.setSchoolId("ID"); + schoolDetailEntity.setDistrictId("DistID"); + schoolDetailEntity.setSchoolNumber("12345"); + schoolDetailEntity.setSchoolCategoryCode("SCC"); + schoolDetailEntity.setEmail("abc@xyz.ca"); + schoolDetailEntities.add(schoolDetailEntity); + + schoolDetailEntity = new SchoolDetailEntity(); + schoolDetailEntity.setSchoolId("ID"); + schoolDetailEntity.setDistrictId("DistID"); + schoolDetailEntity.setSchoolNumber("12345"); + schoolDetailEntity.setSchoolCategoryCode("SCC"); + schoolDetailEntity.setEmail("abc@xyz.ca"); + schoolDetailEntities.add(schoolDetailEntity); + + + when(this.schoolDetailRedisRepository.findBySchoolCategoryCode(schoolCategoryCode)) + .thenReturn(schoolDetailEntities); + when(this.schoolDetailTransformer.transformToDTO(schoolDetailEntities)) + .thenReturn(schoolDetails); + when(this.districtService.getDistrictByIdFromRedisCache("ID")) + .thenReturn(district); + when(this.districtTransformerMock.transformToDTO(districtEntities)) + .thenReturn(districts); + when(this.schoolService.getSchoolDetailsBySchoolCategoryCode(schoolCategoryCode)) + .thenReturn(schoolDetails); + assertEquals(districts, districtService.getDistrictsBySchoolCategoryCode(schoolCategoryCode)); } @Test public void updateDistrictCache_givenValidDistrictId_shouldUpdateCache() { // given // set up initial district in redis mock - DistrictEntity districtEntity = createDistrictEntity(); - District district = districtTransformer.transformToDTO(districtEntity); + District district = new District(); + district.setDistrictId(UUID.randomUUID().toString()); + district.setDistrictNumber("002"); + district.setFaxNumber("1233216547"); + district.setPhoneNumber("3216549874"); + district.setEmail("district@district.ca"); + district.setWebsite("www.district.ca"); + district.setDisplayName("Test Display Name"); + district.setDistrictRegionCode("NOT_APPLIC"); + district.setDistrictStatusCode("INACTIVE"); + + DistrictEntity districtEntity = new DistrictEntity(); + districtEntity.setDistrictId(UUID.randomUUID().toString()); + districtEntity.setDistrictNumber("002"); + districtEntity.setFaxNumber("1233216547"); + districtEntity.setPhoneNumber("3216549874"); + districtEntity.setEmail("district@district.ca"); + districtEntity.setWebsite("www.district.ca"); + districtEntity.setDisplayName("Test Display Name"); + districtEntity.setDistrictRegionCode("NOT_APPLIC"); + districtEntity.setDistrictStatusCode("INACTIVE"); // when // call updateDistrictCache with district id and mock a return from webclient Mockito.when(this.restServiceMock.get(anyString(), @@ -276,7 +481,6 @@ public void updateDistrictCache_givenValidDistrictId_shouldUpdateCache() { private DistrictEntity createDistrictEntity() { District d = TestUtils.createDistrict(); - DistrictEntity de = this.districtTransformer.transformToEntity(d); - return de; + return districtTransformer.transformToEntity(d); } } 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 ed7f10d1..e1720176 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,22 +1,22 @@ 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.District; import ca.bc.gov.educ.api.trax.model.dto.institute.School; import ca.bc.gov.educ.api.trax.model.dto.institute.SchoolDetail; import ca.bc.gov.educ.api.trax.model.entity.institute.*; -import ca.bc.gov.educ.api.trax.model.transformer.institute.DistrictTransformer; import ca.bc.gov.educ.api.trax.model.transformer.institute.SchoolDetailTransformer; import ca.bc.gov.educ.api.trax.model.transformer.institute.SchoolTransformer; -import ca.bc.gov.educ.api.trax.repository.GradCountryRepository; -import ca.bc.gov.educ.api.trax.repository.GradProvinceRepository; +import ca.bc.gov.educ.api.trax.repository.redis.SchoolDetailRedisRepository; import ca.bc.gov.educ.api.trax.repository.redis.SchoolRedisRepository; +import ca.bc.gov.educ.api.trax.service.RESTService; import ca.bc.gov.educ.api.trax.util.EducGradTraxApiConstants; import ca.bc.gov.educ.api.trax.util.RestUtils; import org.junit.Test; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -44,8 +44,10 @@ import java.util.function.Consumer; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.when; @@ -61,14 +63,21 @@ public class InstituteSchoolServiceTest { @Autowired private SchoolService schoolService; @MockBean + private ServiceHelper serviceHelperMock; + @MockBean private SchoolRedisRepository schoolRedisRepository; @MockBean + private SchoolDetailRedisRepository schoolDetailRedisRepository; + @MockBean private JedisConnectionFactory jedisConnectionFactoryMock; @MockBean private JedisCluster jedisClusterMock; @MockBean @Qualifier("default") WebClient webClientMock; + @MockBean + @Qualifier("instituteWebClient") + private WebClient instWebClient; @Mock private WebClient.RequestHeadersSpec requestHeadersSpecMock; @Mock @@ -90,9 +99,15 @@ public class InstituteSchoolServiceTest { @MockBean private RestUtils restUtils; @MockBean + private RESTService restServiceMock; + @MockBean private SchoolTransformer schoolTransformerMock; + @Autowired + private SchoolTransformer schoolTransformer; @MockBean private SchoolDetailTransformer schoolDetailTransformerMock; + @Autowired + private SchoolDetailTransformer schoolDetailTransformer; // NATS @MockBean @@ -149,7 +164,6 @@ public void whenGetSchoolsFromInstituteApi_returnsListOfSchools() { when(this.schoolTransformerMock.transformToDTO(schools)) .thenReturn(schoolsMock); - List result = schoolService.getSchoolsFromInstituteApi(); } @@ -163,37 +177,316 @@ public void whenLoadSchoolsIntoRedisCache_DoesNotThrow() { } @Test - public void whenGetSchoolDetailssFromInstituteApi_returnsListOfSchoolDetails() { - List schoolDetails = new ArrayList<>(); - SchoolDetailEntity schoolDetail = new SchoolDetailEntity(); + public void whenGetSchoolsFromRedisCache_ReturnSchools() { + String mincode = "12345678"; + List schools = new ArrayList<>(); + School school = new School(); + school.setSchoolId("ID"); + school.setDistrictId("DistID"); + school.setSchoolNumber("12345"); + school.setMincode(mincode); + school.setSchoolCategoryCode("SCC"); + school.setEmail("abc@xyz.ca"); + schools.add(school); + + school = new School(); + school.setSchoolId("ID"); + school.setDistrictId("DistID"); + school.setSchoolNumber("12345"); + school.setMincode(mincode); + school.setSchoolCategoryCode("SCC"); + school.setEmail("abc@xyz.ca"); + schools.add(school); + + List schoolEntities = new ArrayList<>(); + SchoolEntity schoolEntity = new SchoolEntity(); + schoolEntity.setSchoolId("ID"); + schoolEntity.setDistrictId("DistID"); + schoolEntity.setSchoolNumber("12345"); + schoolEntity.setMincode(mincode); + schoolEntity.setSchoolCategoryCode("SCC"); + schoolEntity.setEmail("abc@xyz.ca"); + schoolEntities.add(schoolEntity); + + schoolEntity = new SchoolEntity(); + schoolEntity.setSchoolId("ID"); + schoolEntity.setDistrictId("DistID"); + schoolEntity.setSchoolNumber("12345"); + schoolEntity.setMincode(mincode); + schoolEntity.setSchoolCategoryCode("SCC"); + schoolEntity.setEmail("abc@xyz.ca"); + schoolEntities.add(schoolEntity); + + when(this.schoolRedisRepository.findAll()) + .thenReturn(schoolEntities); + when(this.schoolTransformer.transformToDTO(schoolEntities)) + .thenReturn(schools); + assertEquals(schools, schoolService.getSchoolsFromRedisCache()); + } + + @Test + public void whenGetSchoolByMincodeFromRedisCache_ReturnSchool() { + String mincode = "12345678"; + School school = new School(); + school.setSchoolId("ID"); + school.setDistrictId("DistID"); + school.setSchoolNumber("12345"); + school.setMincode(mincode); + school.setSchoolCategoryCode("SCC"); + school.setEmail("abc@xyz.ca"); + + SchoolEntity schoolEntity = new SchoolEntity(); + schoolEntity.setSchoolId("ID"); + schoolEntity.setDistrictId("DistID"); + schoolEntity.setSchoolNumber("12345"); + schoolEntity.setMincode(mincode); + schoolEntity.setSchoolCategoryCode("SCC"); + schoolEntity.setEmail("abc@xyz.ca"); + + when(this.schoolRedisRepository.findByMincode(mincode)) + .thenReturn(schoolEntity); + when(this.schoolTransformer.transformToDTO(schoolEntity)) + .thenReturn(school); + assertEquals(school, schoolService.getSchoolByMincodeFromRedisCache(mincode)); + } + + @Test + public void whenCheckIfSchoolExists_returnTrue() { + String minCode = "12345678"; + SchoolEntity schoolEntity = new SchoolEntity(); + schoolEntity.setSchoolId("ID"); + schoolEntity.setDistrictId("DistID"); + schoolEntity.setSchoolNumber("12345"); + schoolEntity.setMincode(minCode); + schoolEntity.setSchoolCategoryCode("SCC"); + schoolEntity.setEmail("abc@xyz.ca"); + + when(schoolRedisRepository.findByMincode(minCode)).thenReturn(schoolEntity); + assertEquals(true, schoolService.checkIfSchoolExists(minCode)); + } + + @Test + public void whenCheckIfSchoolExists_returnFalse() { + String minCode = "12345678"; + when(schoolRedisRepository.findByMincode(minCode)).thenReturn(null); + assertEquals(false, schoolService.checkIfSchoolExists(minCode)); + } + + @Test + public void whenInitializeSchoolCache_DoNothing() { + doNothing().when(serviceHelperMock).initializeCache(false, CacheKey.SCHOOL_CACHE, serviceHelperMock); + Assertions.assertDoesNotThrow(() -> schoolService.initializeSchoolCache(false)); + } + + @Test + public void whenGetSchoolDetailsFromInstituteApi_returnsListOfSchoolDetails() { + List schools = new ArrayList<>(); + School school = new School(); + school.setSchoolId("1"); + school.setMincode("234"); + schools.add(school); + school = new School(); + school.setSchoolId("2"); + school.setMincode("345"); + schools.add(school); + + List schoolDetails = new ArrayList<>(); + SchoolDetail schoolDetail1 = new SchoolDetail(); + schoolDetail1.setSchoolId("1"); + schoolDetail1.setDistrictId("DistID"); + schoolDetail1.setSchoolNumber("12345"); + schoolDetail1.setSchoolCategoryCode("SCC"); + schoolDetail1.setEmail("abc@xyz.ca"); + schoolDetails.add(schoolDetail1); + SchoolDetail schoolDetail2 = new SchoolDetail(); + schoolDetail2.setSchoolId("2"); + schoolDetail2.setDistrictId("DistID"); + schoolDetail2.setSchoolNumber("12345"); + schoolDetail2.setSchoolCategoryCode("SCC"); + schoolDetail2.setEmail("abc@xyz.ca"); + schoolDetails.add(schoolDetail2); + + SchoolDetailEntity schoolDetailEntity1 = new SchoolDetailEntity(); + schoolDetailEntity1.setSchoolId("1"); + schoolDetailEntity1.setDistrictId("DistID"); + schoolDetailEntity1.setSchoolNumber("12345"); + schoolDetailEntity1.setSchoolCategoryCode("SCC"); + schoolDetailEntity1.setEmail("abc@xyz.ca"); + SchoolDetailEntity schoolDetailEntity2 = new SchoolDetailEntity(); + schoolDetailEntity1.setSchoolId("2"); + schoolDetailEntity1.setDistrictId("DistID"); + schoolDetailEntity1.setSchoolNumber("12345"); + schoolDetailEntity1.setSchoolCategoryCode("SCC"); + schoolDetailEntity1.setEmail("abc@xyz.ca"); + + when(this.schoolService.getSchoolsFromRedisCache()).thenReturn(schools); + when(this.schoolDetailTransformer.transformToDTO(schoolDetailEntity1)).thenReturn(schoolDetail1); + when(this.schoolDetailTransformer.transformToDTO(schoolDetailEntity2)).thenReturn(schoolDetail2); + when(this.restServiceMock.get(String.format(constants.getSchoolDetailsByIdFromInstituteApiUrl(), "1"), + SchoolDetailEntity.class, instWebClient)).thenReturn(schoolDetailEntity1); + when(this.restServiceMock.get(String.format(constants.getSchoolDetailsByIdFromInstituteApiUrl(), "2"), + SchoolDetailEntity.class, instWebClient)).thenReturn(schoolDetailEntity2); + + List result = schoolService.getSchoolDetailsFromInstituteApi(); + assertEquals(schoolDetails, result); + } + + @Test + public void whenGetSchoolDetailByIdFromInstituteApi_ReturnSchoolDetail() { + String schoolId = "1"; + + SchoolDetail schoolDetail = new SchoolDetail(); + schoolDetail.setSchoolId("1"); + schoolDetail.setDistrictId("DistID"); + schoolDetail.setSchoolNumber("12345"); + schoolDetail.setSchoolCategoryCode("SCC"); + schoolDetail.setEmail("abc@xyz.ca"); + + SchoolDetailEntity schoolDetailEntity = new SchoolDetailEntity(); + schoolDetailEntity.setSchoolId("1"); + schoolDetailEntity.setDistrictId("DistID"); + schoolDetailEntity.setSchoolNumber("12345"); + schoolDetailEntity.setSchoolCategoryCode("SCC"); + schoolDetailEntity.setEmail("abc@xyz.ca"); + + when(this.schoolDetailTransformer.transformToDTO(schoolDetailEntity)).thenReturn(schoolDetail); + when(this.restServiceMock.get(String.format(constants.getSchoolDetailsByIdFromInstituteApiUrl(), "1"), + SchoolDetailEntity.class, instWebClient)).thenReturn(schoolDetailEntity); + + SchoolDetail result = schoolService.getSchoolDetailByIdFromInstituteApi(schoolId); + assertEquals(schoolDetail, result); + } + + @Test + public void whenLoadSchoolDetailsIntoRedisCache_DoesNotThrow() { + List schoolDetailEntities = Arrays.asList(new SchoolDetailEntity()); + List schoolDetails = Arrays.asList(new SchoolDetail()); + when(this.schoolDetailRedisRepository.saveAll(schoolDetailEntities)) + .thenReturn(schoolDetailEntities); + assertDoesNotThrow(() -> schoolService.loadSchoolDetailsIntoRedisCache(schoolDetails)); + } + @Test + public void whenInitializeSchoolDetailCache_DoNothing() { + doNothing().when(serviceHelperMock).initializeCache(false, CacheKey.SCHOOL_DETAIL_CACHE, serviceHelperMock); + Assertions.assertDoesNotThrow(() -> schoolService.initializeSchoolDetailCache(false)); + } + + @Test + public void whenGetSchoolDetailsFromRedisCache_ReturnSchoolDetails() { + String mincode = "12345678"; + List schoolDetails = new ArrayList<>(); + SchoolDetail schoolDetail = new SchoolDetail(); schoolDetail.setSchoolId("ID"); schoolDetail.setDistrictId("DistID"); schoolDetail.setSchoolNumber("12345"); + schoolDetail.setMincode(mincode); schoolDetail.setSchoolCategoryCode("SCC"); schoolDetail.setEmail("abc@xyz.ca"); + schoolDetails.add(schoolDetail); + schoolDetail = new SchoolDetail(); + schoolDetail.setSchoolId("ID"); + schoolDetail.setDistrictId("DistID"); + schoolDetail.setSchoolNumber("12345"); + schoolDetail.setMincode(mincode); + schoolDetail.setSchoolCategoryCode("SCC"); + schoolDetail.setEmail("abc@xyz.ca"); schoolDetails.add(schoolDetail); - when(this.restUtils.getTokenResponseObject(anyString(), anyString())) - .thenReturn(responseObjectMock); - when(this.responseObjectMock.getAccess_token()) - .thenReturn("accessToken"); - when(webClientMock.get()) - .thenReturn(requestHeadersUriSpecMock); - when(requestHeadersUriSpecMock.uri(anyString())) - .thenReturn(requestHeadersSpecMock); - when(requestHeadersSpecMock.headers(any(Consumer.class))) - .thenReturn(requestHeadersSpecMock); - when(requestHeadersSpecMock.retrieve()) - .thenReturn(responseSpecMock); - when(this.responseSpecMock.bodyToMono(new ParameterizedTypeReference>(){})) - .thenReturn(schoolDetailEntitiesMock); - when(this.schoolDetailEntitiesMock.block()).thenReturn(schoolDetails); + List schoolDetailEntities = new ArrayList<>(); + SchoolDetailEntity schoolDetailEntity = new SchoolDetailEntity(); + schoolDetailEntity.setSchoolId("ID"); + schoolDetailEntity.setDistrictId("DistID"); + schoolDetailEntity.setSchoolNumber("12345"); + schoolDetailEntity.setMincode(mincode); + schoolDetailEntity.setSchoolCategoryCode("SCC"); + schoolDetailEntity.setEmail("abc@xyz.ca"); + schoolDetailEntities.add(schoolDetailEntity); - when(this.schoolDetailTransformerMock.transformToDTO(schoolDetails)) - .thenReturn(schoolDetailsMock); + schoolDetailEntity = new SchoolDetailEntity(); + schoolDetailEntity.setSchoolId("ID"); + schoolDetailEntity.setDistrictId("DistID"); + schoolDetailEntity.setSchoolNumber("12345"); + schoolDetailEntity.setMincode(mincode); + schoolDetailEntity.setSchoolCategoryCode("SCC"); + schoolDetailEntity.setEmail("abc@xyz.ca"); + schoolDetailEntities.add(schoolDetailEntity); - List result = schoolService.getSchoolDetailsFromInstituteApi(); + when(this.schoolDetailRedisRepository.findAll()) + .thenReturn(schoolDetailEntities); + when(this.schoolDetailTransformerMock.transformToDTO(schoolDetailEntities)) + .thenReturn(schoolDetails); + assertEquals(schoolDetails, schoolService.getSchoolDetailsFromRedisCache()); + } + + @Test + public void whenGetSchoolDetailByMincodeFromRedisCache_ReturnSchoolDetail() { + String mincode = "12345678"; + SchoolDetail schoolDetail = new SchoolDetail(); + schoolDetail.setSchoolId("ID"); + schoolDetail.setDistrictId("DistID"); + schoolDetail.setSchoolNumber("12345"); + schoolDetail.setMincode(mincode); + schoolDetail.setSchoolCategoryCode("SCC"); + schoolDetail.setEmail("abc@xyz.ca"); + + SchoolDetailEntity schoolDetailEntity = new SchoolDetailEntity(); + schoolDetailEntity.setSchoolId("ID"); + schoolDetailEntity.setDistrictId("DistID"); + schoolDetailEntity.setSchoolNumber("12345"); + schoolDetailEntity.setMincode(mincode); + schoolDetailEntity.setSchoolCategoryCode("SCC"); + schoolDetailEntity.setEmail("abc@xyz.ca"); + + when(this.schoolDetailRedisRepository.findByMincode(mincode)) + .thenReturn(schoolDetailEntity); + when(this.schoolDetailTransformer.transformToDTO(schoolDetailEntity)) + .thenReturn(schoolDetail); + assertEquals(schoolDetail, schoolService.getSchoolDetailByMincodeFromRedisCache(mincode)); + } + + @Test + public void whenGetSchoolDetailBySchoolCategoryCode_ReturnSchoolDetail() { + String schoolCategoryCode = "ABC"; + List schoolDetails = new ArrayList<>(); + SchoolDetail schoolDetail = new SchoolDetail(); + schoolDetail.setSchoolId("ID"); + schoolDetail.setDistrictId("DistID"); + schoolDetail.setSchoolNumber("12345"); + schoolDetail.setSchoolCategoryCode("SCC"); + schoolDetail.setEmail("abc@xyz.ca"); + schoolDetails.add(schoolDetail); + + schoolDetail = new SchoolDetail(); + schoolDetail.setSchoolId("ID"); + schoolDetail.setDistrictId("DistID"); + schoolDetail.setSchoolNumber("12345"); + schoolDetail.setSchoolCategoryCode("SCC"); + schoolDetail.setEmail("abc@xyz.ca"); + schoolDetails.add(schoolDetail); + + List schoolDetailEntities = new ArrayList<>(); + SchoolDetailEntity schoolDetailEntity = new SchoolDetailEntity(); + schoolDetailEntity.setSchoolId("ID"); + schoolDetailEntity.setDistrictId("DistID"); + schoolDetailEntity.setSchoolNumber("12345"); + schoolDetailEntity.setSchoolCategoryCode("SCC"); + schoolDetailEntity.setEmail("abc@xyz.ca"); + schoolDetailEntities.add(schoolDetailEntity); + + schoolDetailEntity = new SchoolDetailEntity(); + schoolDetailEntity.setSchoolId("ID"); + schoolDetailEntity.setDistrictId("DistID"); + schoolDetailEntity.setSchoolNumber("12345"); + schoolDetailEntity.setSchoolCategoryCode("SCC"); + schoolDetailEntity.setEmail("abc@xyz.ca"); + schoolDetailEntities.add(schoolDetailEntity); + + when(this.schoolDetailRedisRepository.findBySchoolCategoryCode(schoolCategoryCode)) + .thenReturn(schoolDetailEntities); + when(this.schoolDetailTransformer.transformToDTO(schoolDetailEntities)) + .thenReturn(schoolDetails); + assertEquals(schoolDetails, schoolService.getSchoolDetailsBySchoolCategoryCode(schoolCategoryCode)); } } diff --git a/api/src/test/resources/application.yaml b/api/src/test/resources/application.yaml index 9bffa622..b1c45be8 100644 --- a/api/src/test/resources/application.yaml +++ b/api/src/test/resources/application.yaml @@ -33,6 +33,22 @@ spring: jwt: issuer-uri: https://soam-dev.apps.silver.devops.gov.bc.ca/auth/realms/master jwk-set-uri: https://soam-dev.apps.silver.devops.gov.bc.ca/auth/realms/master/protocol/openid-connect/certs + client: + registration: + traxclient: + client-id: trax-client-name + client-secret: trax-client-secret + authorization-grant-type: client_credentials + institute-web-client: + client-id: edx-client-name + client-secret: edx-client-secret + provider: + traxclient: + issuer-uri: https://soam-dev.apps.silver.devops.gov.bc.ca/auth/realms/master + token-uri: https://soam-dev.apps.silver.devops.gov.bc.ca/auth/realms/master/protocol/openid-connect/token + institute-web-client: + issuer-uri: https://soam-dev.apps.silver.devops.gov.bc.ca/auth/realms/master + token-uri: https://soam-dev.apps.silver.devops.gov.bc.ca/auth/realms/master/protocol/openid-connect/token sql: init: data-locations: classpath:schema.sql From 7caa57930651d13b512d14f3da8c633f41c91e0f Mon Sep 17 00:00:00 2001 From: Chris Ditcher Date: Fri, 23 Aug 2024 13:47:41 -0700 Subject: [PATCH 2/2] Update update-configmap.sh (cherry picked from commit a95653388e1e173a569cf81e53768fea0efcf810) --- tools/config/update-configmap.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/config/update-configmap.sh b/tools/config/update-configmap.sh index 855357a1..702127fb 100644 --- a/tools/config/update-configmap.sh +++ b/tools/config/update-configmap.sh @@ -23,6 +23,7 @@ FLB_CONFIG="[SERVICE] Exclude_Path *.gz,*.zip Parser docker Mem_Buf_Limit 20MB + Buffer_Max_Size 1MB [FILTER] Name record_modifier Match *