Skip to content

Commit

Permalink
Merge pull request #370 from bcgov/feature/GRAD2-2973
Browse files Browse the repository at this point in the history
GRAD2-2973 - District Cache on Trax API needs to also import District Details
  • Loading branch information
infstar authored Nov 13, 2024
2 parents 494a072 + 6cb13fb commit 77aee6d
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ResponseEntity<District> getDistrictDetails(@PathVariable String distNo)

@GetMapping(EducGradTraxApiConstants.GRAD_DISTRICT_URL_MAPPING_V1 + EducGradTraxApiConstants.GET_DISTRICTS_BY_SCHOOL_CATEGORY_MAPPING)
@PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA)
@Operation(summary = "Get Districts by SchoolCategory", description = "Get Districts by SchoolCategory", tags = { "School" })
@Operation(summary = "Get Districts by SchoolCategory", description = "Get Districts by SchoolCategory", tags = { "District" })
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "204", description = "NO CONTENT")})
public ResponseEntity<List<District>> getDistrictBySchoolCategory(@RequestParam(required = false) String schoolCategory) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ca.bc.gov.educ.api.trax.controller.v2;

import ca.bc.gov.educ.api.trax.service.institute.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;
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.*;

@Slf4j
@RestController("codeControllerV2")
@CrossOrigin
@OpenAPIDefinition(info = @Info(title = "API for TRAX Code Tables Data.",
description = "This API is for Reading TRAX Code Tables data.", version = "1"),
security = {@SecurityRequirement(name = "OAUTH2",
scopes = {"READ_GRAD_COUNTRY_CODE_DATA",
"READ_GRAD_PROVINCE_CODE_DATA",
})})
public class CodeController {

CodeService codeService;
GradValidation validation;
ResponseHelper response;

@Autowired
public CodeController(CodeService codeService, GradValidation validation, ResponseHelper response) {
this.codeService = codeService;
this.validation = validation;
this.response = response;
}

@PutMapping(EducGradTraxApiConstants.GRAD_TRAX_CODE_URL_MAPPING_V2 + EducGradTraxApiConstants.PUT_SCHOOL_CATEGORY_CODES_MAPPING)
@PreAuthorize(PermissionsConstants.UPDATE_GRAD_TRAX_CACHE)
@Operation(summary = "Reload School Category Codes in the cache", description = "Reload School Category Codes in the cache", tags = {"Cache"})
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "422", description = "UNPROCESSABLE CONTENT")})
public ResponseEntity<String> reloadSchoolCategoryCodesIntoCache() {
log.debug("reloadSchoolCategoryCodesIntoCache : ");
try {
codeService.initializeSchoolCategoryCodeCache(true);
} catch (Exception e) {
return ResponseEntity.unprocessableEntity().body("Error loading School Category Codes into cache");
}
return ResponseEntity.ok("School Category Codes loaded into cache!");
}

@PutMapping(EducGradTraxApiConstants.GRAD_TRAX_CODE_URL_MAPPING_V2 + EducGradTraxApiConstants.PUT_SCHOOL_FUNDING_GROUP_CODES_MAPPING)
@PreAuthorize(PermissionsConstants.UPDATE_GRAD_TRAX_CACHE)
@Operation(summary = "Reload School Funding Group Codes in the cache", description = "Reload School Funding Group Codes in the cache", tags = {"Cache"})
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "422", description = "UNPROCESSABLE CONTENT")})
public ResponseEntity<String> reloadSchoolFundingGroupCodesIntoCache() {
log.debug("reloadSchoolFundingGroupCodesIntoCache : ");
try {
codeService.initializeSchoolFundingGroupCodeCache(true);
} catch (Exception e) {
return ResponseEntity.unprocessableEntity().body("Error loading School Funding Group Codes into cache");
}
return ResponseEntity.ok("School Funding Group Codes loaded into cache!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ public DistrictController(DistrictService districtService, GradValidation valida
this.response = response;
}

@PutMapping(EducGradTraxApiConstants.GRAD_DISTRICT_URL_MAPPING_V2 + EducGradTraxApiConstants.PUT_DISTRICTS_MAPPING)
@PreAuthorize(PermissionsConstants.UPDATE_GRAD_TRAX_CACHE)
@Operation(summary = "Reload Districts in the cache", description = "Reload Districts in the cache", tags = {"Cache"})
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "422", description = "UNPROCESSABLE CONTENT")})
public ResponseEntity<String> reloadDistrictsIntoCache() {
log.debug("reloadDistrictsIntoCache : ");
try {
districtService.initializeDistrictCache(true);
} catch (Exception e) {
return ResponseEntity.unprocessableEntity().body("Error loading Districts into cache");
}
return ResponseEntity.ok("Districts loaded into cache!");
}

@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" })
Expand All @@ -54,7 +69,7 @@ public ResponseEntity<District> getDistrictDetailsByDistNo(@PathVariable String

@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" })
@Operation(summary = "Get District by school category code V2", description = "Get District by school category code V2", tags = { "District" })
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "204", description = "NO CONTENT")})
public ResponseEntity<List<District>> getDistrictsBySchoolCategoryCode(@RequestParam(required = false) String schoolCategoryCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ public SchoolController(SchoolService schoolService, GradValidation validation,
this.response = response;
}

@PutMapping(EducGradTraxApiConstants.GRAD_SCHOOL_URL_MAPPING_V2 + EducGradTraxApiConstants.PUT_SCHOOLS_MAPPING)
@PreAuthorize(PermissionsConstants.UPDATE_GRAD_TRAX_CACHE)
@Operation(summary = "Reload Schools in the cache", description = "Reload Schools in the cache", tags = {"Cache"})
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "422", description = "UNPROCESSABLE CONTENT")})
public ResponseEntity<String> reloadSchoolsIntoCache() {
log.debug("reloadSchoolsIntoCache : ");
try {
schoolService.initializeSchoolCache(true);
} catch (Exception e) {
return ResponseEntity.unprocessableEntity().body("Error loading Schools into cache");
}
return ResponseEntity.ok("Schools loaded into cache!");
}

@GetMapping(EducGradTraxApiConstants.GRAD_SCHOOL_URL_MAPPING_V2)
@PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA)
@Operation(summary = "Find All Schools from Cache", description = "Get All Schools from Cache", tags = { "School" })
Expand All @@ -65,6 +80,21 @@ public ResponseEntity<School> getSchoolBySchoolId(@PathVariable UUID schoolId) {
}
}

@PutMapping(EducGradTraxApiConstants.GRAD_SCHOOL_URL_MAPPING_V2 + EducGradTraxApiConstants.PUT_SCHOOL_DETAILS_MAPPING)
@PreAuthorize(PermissionsConstants.UPDATE_GRAD_TRAX_CACHE)
@Operation(summary = "Reload School Details in the cache", description = "Reload School Details in the cache", tags = {"Cache"})
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "422", description = "UNPROCESSABLE CONTENT")})
public ResponseEntity<String> reloadSchoolDetailsIntoCache() {
log.debug("reloadSchoolDetailsIntoCache : ");
try {
schoolService.initializeSchoolDetailCache(true);
} catch (Exception e) {
return ResponseEntity.unprocessableEntity().body("Error loading School Details into cache");
}
return ResponseEntity.ok("School Details loaded into cache!");
}

@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" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.web.reactive.function.client.WebClientResponseException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
Expand Down Expand Up @@ -44,11 +45,30 @@ public List<District> getDistrictsFromInstituteApi() {
log.debug("****Before Calling Institute API");
List<DistrictEntity> response = this.restService.get(constants.getAllDistrictsFromInstituteApiUrl(),
List.class, webClient);
return districtTransformer.transformToDTO(response);
List<District> dList = districtTransformer.transformToDTO(response);
List<District> districts = new ArrayList<>();
District dist;
for (District d : dList) {
dist = getDistrictByIdFromInstituteApi(d.getDistrictId());
districts.add(dist);
}
return districts;
} catch (WebClientResponseException e) {
log.warn(String.format("Error getting Common School List: %s", e.getMessage()));
log.warn(String.format("Error getting Districts from Institute API: %s", e.getMessage()));
} catch (Exception e) {
log.error(String.format("Error while calling school-api: %s", e.getMessage()));
log.error(String.format("Error while calling institute-api: %s", e.getMessage()));
}
return Collections.emptyList();
}

public District getDistrictByIdFromInstituteApi(String districtId) {
try {
return this.restService.get(String.format(constants.getGetDistrictFromInstituteApiUrl(), districtId),
District.class, webClient);
} catch (WebClientResponseException e) {
log.warn(String.format("Error getting District from Institute API: %s", e.getMessage()));
} catch (Exception e) {
log.error(String.format("Error while calling institute-api: %s", e.getMessage()));
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ public class EducGradTraxApiConstants {
public static final String POST_SAVE_TRAX_STUDENT_NO_MAPPING = "/trax-student-no";
public static final String DELETE_TRAX_STUDENT_NO_MAPPING = "/trax-student-no/{pen}";

//cache endpoints
public static final String PUT_SCHOOL_CATEGORY_CODES_MAPPING = "/cache/school-category-codes";
public static final String PUT_SCHOOL_FUNDING_GROUP_CODES_MAPPING = "/cache/school-funding-group-codes";
public static final String PUT_DISTRICTS_MAPPING = "/cache/districts";
public static final String PUT_SCHOOLS_MAPPING = "/cache/schools";
public static final String PUT_SCHOOL_DETAILS_MAPPING = "/cache/school-details";

// Event urls
public static final String EVENT_HISTORY_MAPPING_V1 = GRAD_TRAX_API_ROOT_MAPPING_V1 + "/event/history";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface PermissionsConstants {
String READ_GRAD_TRAX_STUDENT_DATA = _PREFIX + "SCOPE_READ_GRAD_TRAX_STUDENT_DATA" + _SUFFIX;
String UPDATE_GRAD_TRAX_STUDENT_DATA = _PREFIX + "SCOPE_UPDATE_GRAD_TRAX_STUDENT_DATA" + _SUFFIX;
String READ_GRAD_TRAX_COURSE_DATA = _PREFIX + "SCOPE_READ_GRAD_TRAX_COURSE_DATA" + _SUFFIX;
String UPDATE_GRAD_TRAX_CACHE = _PREFIX + "SCOPE_UPDATE_GRAD_TRAX_CACHE" + _SUFFIX;

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@
import java.util.ArrayList;
import java.util.List;


@ExtendWith(MockitoExtension.class)
public class CodeControllerTest {
class CodeControllerTest {

@Mock
private CodeService codeService;

@Mock
ResponseHelper response;

@InjectMocks
private CodeController codeController;

Expand All @@ -36,7 +35,7 @@ public class CodeControllerTest {
MessageHelper messagesHelper;

@Test
public void testGetAllCountryList() {
void testGetAllCountryList() {
List<GradCountry> gradCountryList = new ArrayList<>();
GradCountry obj = new GradCountry();
obj.setCountryCode("CA");
Expand All @@ -52,7 +51,7 @@ public void testGetAllCountryList() {
}

@Test
public void testGetSpecificCountryCode() {
void testGetSpecificCountryCode() {
String countryCode = "CA";
GradCountry obj = new GradCountry();
obj.setCountryCode("CA");
Expand All @@ -63,15 +62,15 @@ public void testGetSpecificCountryCode() {
}

@Test
public void testGetSpecificCountryCode_noContent() {
void testGetSpecificCountryCode_noContent() {
String countryCode = "AB";
Mockito.when(codeService.getSpecificCountryCode(countryCode)).thenReturn(null);
codeController.getSpecificCountryCode(countryCode);
Mockito.verify(codeService).getSpecificCountryCode(countryCode);
}

@Test
public void testGetAllProvinceList() {
void testGetAllProvinceList() {
List<GradProvince> gradProvinceList = new ArrayList<>();
GradProvince obj = new GradProvince();
obj.setProvCode("CA");
Expand All @@ -87,7 +86,7 @@ public void testGetAllProvinceList() {
}

@Test
public void testGetSpecificProvinceCode() {
void testGetSpecificProvinceCode() {
String countryCode = "CA";
GradProvince obj = new GradProvince();
obj.setProvCode("CA");
Expand All @@ -98,7 +97,7 @@ public void testGetSpecificProvinceCode() {
}

@Test
public void testGetSpecificProvinceCode_noContent() {
void testGetSpecificProvinceCode_noContent() {
String countryCode = "AB";
Mockito.when(codeService.getSpecificProvinceCode(countryCode)).thenReturn(null);
codeController.getSpecificProvinceCode(countryCode);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ca.bc.gov.educ.api.trax.controller.v2;

import ca.bc.gov.educ.api.trax.service.institute.CodeService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;

@ExtendWith(MockitoExtension.class)
class CodeControllerV2Test {

@Mock
private CodeService codeService;

@InjectMocks
private CodeController codeController;

@Test
void testReloadSchoolCategoryCodesIntoCache_shouldReturnOK() {
doNothing().when(codeService).initializeSchoolCategoryCodeCache(true);
codeController.reloadSchoolCategoryCodesIntoCache();
Mockito.verify(codeService).initializeSchoolCategoryCodeCache(true);
}

@Test
void testReloadSchoolCategoryCodesIntoCache_shouldThrowException() {
doThrow(new RuntimeException()).when(codeService).initializeSchoolCategoryCodeCache(true);
codeController.reloadSchoolCategoryCodesIntoCache();
assertThrows(RuntimeException.class, () -> codeService.initializeSchoolCategoryCodeCache(true));
}

@Test
void testReloadSchoolFundingGroupCodesIntoCache_shouldReturnOK() {
doNothing().when(codeService).initializeSchoolFundingGroupCodeCache(true);
codeController.reloadSchoolFundingGroupCodesIntoCache();
Mockito.verify(codeService).initializeSchoolFundingGroupCodeCache(true);
}

@Test
void testReloadSchoolFundingGroupCodesIntoCache_shouldThrowException() {
doThrow(new RuntimeException()).when(codeService).initializeSchoolFundingGroupCodeCache(true);
codeController.reloadSchoolFundingGroupCodesIntoCache();
assertThrows(RuntimeException.class, () -> codeService.initializeSchoolFundingGroupCodeCache(true));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ca.bc.gov.educ.api.trax.controller.v2;

import ca.bc.gov.educ.api.trax.service.institute.DistrictService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;

@ExtendWith(MockitoExtension.class)
class DistrictControllerV2Test {

@Mock
private DistrictService districtService;

@InjectMocks
private DistrictController districtController;

@Test
void testReloadSchoolCategoryCodesIntoCache_shouldReturnOK() {
doNothing().when(districtService).initializeDistrictCache(true);
districtController.reloadDistrictsIntoCache();
Mockito.verify(districtService).initializeDistrictCache(true);
}

@Test
void testReloadSchoolCategoryCodesIntoCache_shouldThrowException() {
doThrow(new RuntimeException()).when(districtService).initializeDistrictCache(true);
districtController.reloadDistrictsIntoCache();
assertThrows(RuntimeException.class, () -> districtService.initializeDistrictCache(true));
}

}
Loading

0 comments on commit 77aee6d

Please sign in to comment.