Skip to content

Commit

Permalink
Merge pull request #541 from bcgov/main
Browse files Browse the repository at this point in the history
Applying hotfix to grad release
  • Loading branch information
githubmamatha authored Jul 30, 2024
2 parents d126f3a + 5383af7 commit 3e39f9d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 50 deletions.
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ca.bc.gov.educ</groupId>
<artifactId>educ-grad-graduation-api</artifactId>
<version>1.8.60</version>
<version>1.8.61</version>
<name>educ-grad-graduation-api</name>
<description>Ministry of Education GRAD GRADUATION API</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

Expand All @@ -21,7 +22,7 @@ public class RESTService {
private final WebClient webClient;
private final WebClient graduationServiceWebClient;

private static final String ERROR_5xx = "5xx error.";
private static final String SERVER_ERROR = "5xx error.";
private static final String SERVICE_FAILED_ERROR = "Service failed to process after max retries.";

@Autowired
Expand Down Expand Up @@ -53,7 +54,7 @@ public <T> T get(String url, Class<T> clazz, String accessToken) {
.retrieve()
// if 5xx errors, throw Service error
.onStatus(HttpStatusCode::is5xxServerError,
clientResponse -> Mono.error(new ServiceException(getErrorMessage(url, ERROR_5xx), clientResponse.statusCode().value())))
clientResponse -> Mono.error(new ServiceException(getErrorMessage(url, SERVER_ERROR), clientResponse.statusCode().value())))
.bodyToMono(clazz)
// only does retry if initial error was 5xx as service may be temporarily down
// 4xx errors will always happen if 404, 401, 403 etc, so does not retry
Expand All @@ -76,11 +77,11 @@ public <T> T get(String url, Class<T> clazz) {
obj = graduationServiceWebClient
.get()
.uri(url)
.headers(h -> { h.set(EducGraduationApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID()); })
.headers(h -> h.set(EducGraduationApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID()))
.retrieve()
// if 5xx errors, throw Service error
.onStatus(HttpStatusCode::is5xxServerError,
clientResponse -> Mono.error(new ServiceException(getErrorMessage(url, ERROR_5xx), clientResponse.statusCode().value())))
clientResponse -> Mono.error(new ServiceException(getErrorMessage(url, SERVER_ERROR), clientResponse.statusCode().value())))
.bodyToMono(clazz)
// only does retry if initial error was 5xx as service may be temporarily down
// 4xx errors will always happen if 404, 401, 403 etc, so does not retry
Expand All @@ -92,7 +93,10 @@ public <T> T get(String url, Class<T> clazz) {
.block();
} catch (Exception e) {
// catches IOExceptions and the like
throw new ServiceException(getErrorMessage(url, e.getLocalizedMessage()), HttpStatus.SERVICE_UNAVAILABLE.value(), e);
throw new ServiceException(
getErrorMessage(url, e.getLocalizedMessage()),
(e instanceof WebClientResponseException exception) ? exception.getStatusCode().value() : HttpStatus.SERVICE_UNAVAILABLE.value(),
e);
}
return obj;
}
Expand All @@ -115,7 +119,7 @@ public <T> T post(String url, Object body, Class<T> clazz, String accessToken) {
.body(BodyInserters.fromValue(body))
.retrieve()
.onStatus(HttpStatusCode::is5xxServerError,
clientResponse -> Mono.error(new ServiceException(getErrorMessage(url, ERROR_5xx), clientResponse.statusCode().value())))
clientResponse -> Mono.error(new ServiceException(getErrorMessage(url, SERVER_ERROR), clientResponse.statusCode().value())))
.bodyToMono(clazz)
.retryWhen(Retry.backoff(3, Duration.ofSeconds(2))
.filter(ServiceException.class::isInstance)
Expand All @@ -124,7 +128,10 @@ public <T> T post(String url, Object body, Class<T> clazz, String accessToken) {
}))
.block();
} catch (Exception e) {
throw new ServiceException(getErrorMessage(url, e.getLocalizedMessage()), HttpStatus.SERVICE_UNAVAILABLE.value(), e);
throw new ServiceException(
getErrorMessage(url, e.getLocalizedMessage()),
(e instanceof WebClientResponseException exception) ? exception.getStatusCode().value() : HttpStatus.SERVICE_UNAVAILABLE.value(),
e);
}
return obj;
}
Expand All @@ -134,11 +141,11 @@ public <T> T post(String url, Object body, Class<T> clazz) {
try {
obj = graduationServiceWebClient.post()
.uri(url)
.headers(h -> { h.set(EducGraduationApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID()); })
.headers(h -> h.set(EducGraduationApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID()))
.body(BodyInserters.fromValue(body))
.retrieve()
.onStatus(HttpStatusCode::is5xxServerError,
clientResponse -> Mono.error(new ServiceException(getErrorMessage(url, ERROR_5xx), clientResponse.statusCode().value())))
clientResponse -> Mono.error(new ServiceException(getErrorMessage(url, SERVER_ERROR), clientResponse.statusCode().value())))
.bodyToMono(clazz)
.retryWhen(Retry.backoff(3, Duration.ofSeconds(2))
.filter(ServiceException.class::isInstance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,28 @@ public class ReportService {
private static final String NO_CONTENT = "NO_CONTENT";
private static final String INTERNAL_SERVER_ERROR = "INTERNAL_SERVER_ERROR";

@Autowired
WebClient webClient;

@Autowired
WebClient webClient;
JsonTransformer jsonTransformer;

@Autowired
EducGraduationApiConstants educGraduationApiConstants;

@Autowired
SchoolService schoolService;
OptionalProgramService optionalProgramService;
RESTService restService;

@Autowired
OptionalProgramService optionalProgramService;
public ReportService(WebClient webClient, JsonTransformer jsonTransformer, EducGraduationApiConstants educGraduationApiConstants, SchoolService schoolService, OptionalProgramService optionalProgramService, RESTService restService) {
this.webClient = webClient;
this.jsonTransformer = jsonTransformer;
this.educGraduationApiConstants = educGraduationApiConstants;
this.schoolService = schoolService;
this.optionalProgramService = optionalProgramService;
this.restService = restService;
}

public ProgramCertificateTranscript getTranscript(GraduationStudentRecord gradResponse, ca.bc.gov.educ.api.graduation.model.dto.GraduationData graduationDataStatus, String accessToken, ExceptionMessage exception) {
ProgramCertificateReq req = new ProgramCertificateReq();
req.setProgramCode(gradResponse.getProgram());
req.setSchoolCategoryCode(getSchoolCategoryCode(accessToken, graduationDataStatus.getGradStatus().getSchoolOfRecord()));
req.setSchoolCategoryCode(getSchoolCategoryCode(graduationDataStatus.getGradStatus().getSchoolOfRecord()));
try {
return webClient.post().uri(educGraduationApiConstants.getTranscript())
.headers(h -> {
Expand All @@ -80,7 +83,7 @@ public List<ProgramCertificateTranscript> getCertificateList(GraduationStudentRe
req.setOptionalProgram(optionalPrograms.getOptionalProgramCode());
}
}
req.setSchoolCategoryCode(getSchoolCategoryCode(accessToken, graduationDataStatus.getGradStatus().getSchoolOfRecord()));
req.setSchoolCategoryCode(getSchoolCategoryCode(graduationDataStatus.getGradStatus().getSchoolOfRecord()));
try {
return webClient.post().uri(educGraduationApiConstants.getCertList())
.headers(h -> {
Expand All @@ -95,16 +98,10 @@ public List<ProgramCertificateTranscript> getCertificateList(GraduationStudentRe
}
}

public String getSchoolCategoryCode(String accessToken, String mincode) {
CommonSchool commonSchoolObj = webClient.get().uri(String.format(educGraduationApiConstants.getSchoolCategoryCode(), mincode))
.headers(h -> {
h.setBearerAuth(accessToken);
h.set(EducGraduationApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID());
}).retrieve().bodyToMono(CommonSchool.class).block();
if (commonSchoolObj != null) {
return commonSchoolObj.getSchoolCategoryCode();
}
return null;
public String getSchoolCategoryCode(String mincode) {
// Send to restclient instead
CommonSchool commonSchool = this.restService.get(String.format(educGraduationApiConstants.getSchoolCategoryCode(), mincode), CommonSchool.class);
return (commonSchool == null) ? null : commonSchool.getSchoolCategoryCode();
}

public List<ReportGradStudentData> getStudentsForSchoolYearEndReport(String accessToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ private Integer createAndStoreDistrictReports(String reportType, List<ReportGrad
for (ReportGradStudentData reportGradStudentData : reportGradStudentDataList) {
String mincode = StringUtils.isBlank(reportGradStudentData.getMincodeAtGrad()) ? reportGradStudentData.getMincode() : reportGradStudentData.getMincodeAtGrad();
String districtCode = StringUtils.substring(mincode, 0, 3);
String schoolCategoryCode = reportService.getSchoolCategoryCode(accessToken, mincode);
String schoolCategoryCode = reportService.getSchoolCategoryCode(mincode);
if(!StringUtils.equalsAnyIgnoreCase(schoolCategoryCode, "02")) {
School district = populateDistrictObjectByReportGradStudentData(districtSchoolsMap, reportGradStudentData);
processDistrictSchoolMap(districtSchoolsMap.get(district), reportGradStudentData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1999,9 +1999,6 @@ public void testReportDataByPen_withErrors() throws Exception {
gradSearchStudent.setPen(pen);
gradSearchStudent.setStudentID(gradStatus.getGradStudent().getStudentID());

final ParameterizedTypeReference<List<GradSearchStudent>> gradSearchStudentResponseType = new ParameterizedTypeReference<>() {
};

when(this.webClient.get()).thenReturn(this.requestHeadersUriMock);
when(this.requestHeadersUriMock.uri(String.format(constants.getPenStudentApiByPenUrl(),pen))).thenReturn(this.requestHeadersMock);
when(this.requestHeadersMock.headers(any(Consumer.class))).thenReturn(this.requestHeadersMock);
Expand Down Expand Up @@ -2332,8 +2329,6 @@ public void testReportDataByGraduationData() throws Exception {
public void testReportDataByGraduationData_GSRNULL() throws Exception {
GraduationData gradStatus = createGraduationData("json/gradstatus.json");
assertNotNull(gradStatus);
String pen = gradStatus.getGradStudent().getPen();


String studentGradData = readFile("json/gradstatus.json");
assertNotNull(studentGradData);
Expand Down Expand Up @@ -2461,7 +2456,6 @@ public void testReportDataByGraduationData_StudentNull() throws Exception {
GraduationData gradStatus = createGraduationData("json/gradstatus.json");
assertNotNull(gradStatus);
gradStatus.getGradStudent().setStudentID(null);
String pen = gradStatus.getGradStudent().getPen();

ReportData data = reportService.prepareTranscriptData(gradStatus, true, "accessToken", exception);
assertNotNull(data);
Expand Down Expand Up @@ -2624,8 +2618,6 @@ public void testPrepareCertificateData() throws Exception {
public void testPrepareCertificateData_GSRNULL() throws Exception {
GraduationData gradStatus = createGraduationData("json/gradstatus.json");
assertNotNull(gradStatus);
String pen = gradStatus.getGradStudent().getPen();


String studentGradData = readFile("json/gradstatus.json");
assertNotNull(studentGradData);
Expand Down Expand Up @@ -2682,7 +2674,6 @@ public void testPrepareCertificateData_StudentNull() throws Exception {
GraduationData gradStatus = createGraduationData("json/gradstatus.json");
assertNotNull(gradStatus);
gradStatus.getGradStudent().setStudentID(null);
String pen = gradStatus.getGradStudent().getPen();

ReportData data = reportService.prepareCertificateData(gradStatus, "accessToken", exception);
assertNotNull(data);
Expand Down Expand Up @@ -2843,16 +2834,9 @@ public void testGetSchoolCategoryCode() {
CommonSchool commSch = new CommonSchool();
commSch.setSchlNo("09323027");
commSch.setSchoolCategoryCode("02");

when(this.webClient.get()).thenReturn(this.requestHeadersUriMock);
when(this.requestHeadersUriMock.uri(String.format(constants.getSchoolCategoryCode(),"09323027"))).thenReturn(this.requestHeadersMock);
when(this.requestHeadersMock.headers(any(Consumer.class))).thenReturn(this.requestHeadersMock);
when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock);
when(this.responseMock.bodyToMono(CommonSchool.class)).thenReturn(Mono.just(commSch));

var result = reportService.getSchoolCategoryCode("accessToken", commSch.getSchlNo());
when(this.restService.get(String.format(constants.getSchoolCategoryCode(),"09323027"), CommonSchool.class)).thenReturn(commSch);
var result = reportService.getSchoolCategoryCode(commSch.getSchlNo());
assertThat(result).isNotNull();

}

@Test
Expand All @@ -2864,7 +2848,7 @@ public void testGetSchoolCategoryCodeNull() {
when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock);
when(this.responseMock.bodyToMono(CommonSchool.class)).thenReturn(Mono.empty());

var result = reportService.getSchoolCategoryCode("accessToken", "09323027");
var result = reportService.getSchoolCategoryCode("09323027");
assertThat(result).isNull();

}
Expand Down

0 comments on commit 3e39f9d

Please sign in to comment.