Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set up extract tasks on back office for k8s cron #2959

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
52 changes: 52 additions & 0 deletions src/main/java/uk/gov/hmcts/probate/schedule/ExelaExtractTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package uk.gov.hmcts.probate.schedule;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import uk.gov.hmcts.probate.service.dataextract.DataExtractDateValidator;
import uk.gov.hmcts.probate.service.dataextract.ExelaDataExtractService;
import uk.gov.hmcts.reform.probate.model.client.ApiClientException;

import java.time.LocalDate;

import static uk.gov.hmcts.probate.model.Constants.DATE_FORMAT;

@Component
@Slf4j
@RequiredArgsConstructor
public class ExelaExtractTask implements Runnable {

private final DataExtractDateValidator dataExtractJobDateValidator;
private final ExelaDataExtractService exelaDataExtractService;

@Value("${adhocSchedulerJobDate}")
public String adHocJobStartDate;
@Value("${adhocSchedulerJobToDate}")
public String adHocJobEndDate;

@Override
public void run() {
log.info("Scheduled task ExelaExtractTask started to extract data for Hmrc");
String fromDate = DATE_FORMAT.format(LocalDate.now().minusDays(1L));
String toDate = fromDate;
if (StringUtils.isNotEmpty(adHocJobStartDate)) {
fromDate = adHocJobStartDate;
toDate = StringUtils.isNotEmpty(adHocJobEndDate) ? adHocJobEndDate : adHocJobStartDate;
log.info("Running ExelaDataExtractTask with Adhoc dates {} {}", fromDate, toDate);
}
log.info("Calling perform Exela data extract from date, to date {} {}", fromDate, toDate);
try {
dataExtractJobDateValidator.dateValidator(fromDate, toDate);
log.info("Perform Exela data extract from date started");
exelaDataExtractService.performExelaExtractForDateRange(fromDate, toDate);
log.info("Perform Exela data extract from date finished");
} catch (ApiClientException e) {
log.error(e.getMessage());
} catch (Exception e) {
log.error("Error on ExelaExtractTask Scheduler {}", e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package uk.gov.hmcts.probate.schedule;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import uk.gov.hmcts.probate.model.GrantScheduleResponse;
import uk.gov.hmcts.probate.service.GrantNotificationService;
import uk.gov.hmcts.probate.service.dataextract.DataExtractDateValidator;
import uk.gov.hmcts.reform.probate.model.client.ApiClientException;

import java.time.LocalDate;

import static uk.gov.hmcts.probate.model.Constants.DATE_FORMAT;

@Component
@Slf4j
@RequiredArgsConstructor
public class GrantAwaitingDocumentationExtractTask {

private final DataExtractDateValidator dataExtractDateValidator;
private final GrantNotificationService grantNotificationService;

@Value("${adhocSchedulerJobDate}")
public String adHocJobFromDate;

public void run() {
log.info("Scheduled task GrantAwaitingDocumentation started to"
+ " extract data for Grant Awaiting Documentation Job");
String fromDate = DATE_FORMAT.format(LocalDate.now().minusDays(1L));
if (StringUtils.isNotEmpty(adHocJobFromDate)) {
fromDate = adHocJobFromDate;
log.info("Running GrantAwaitingDocumentationExtractTask with Adhoc dates {}", fromDate);
}
log.info("Calling perform grant awaiting documentation data extract from date {}", fromDate);
try {
dataExtractDateValidator.dateValidator(fromDate);
log.info("Perform grant awaiting documentation data extract from date started");
GrantScheduleResponse grantScheduleResponse =
grantNotificationService.handleAwaitingDocumentationNotification(fromDate);
log.info("Grants awaiting documents attempted for: {} grants, {}",
grantScheduleResponse.getScheduleResponseData().size(),
StringUtils.joinWith(",", grantScheduleResponse.getScheduleResponseData()));
log.info("Perform grant awaiting documentation data extract from date finished");
} catch (ApiClientException e) {
log.error(e.getMessage());
} catch (Exception e) {
log.error("Error on GrantAwaitingDocumentationExtractTask Scheduler {}", e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package uk.gov.hmcts.probate.schedule;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import uk.gov.hmcts.probate.model.GrantScheduleResponse;
import uk.gov.hmcts.probate.service.GrantNotificationService;
import uk.gov.hmcts.probate.service.dataextract.DataExtractDateValidator;
import uk.gov.hmcts.reform.probate.model.client.ApiClientException;

import java.time.LocalDate;

import static uk.gov.hmcts.probate.model.Constants.DATE_FORMAT;

@Component
@Slf4j
@RequiredArgsConstructor
public class GrantDelayedExtractTask {

private final DataExtractDateValidator dataExtractDateValidator;
private final GrantNotificationService grantNotificationService;

@Value("${adhocSchedulerJobDate}")
public String adHocJobFromDate;

public void run() {
log.info("Scheduled task GrantDelayedExtractTask started to extract data for Grant Delayed Job");
String fromDate = DATE_FORMAT.format(LocalDate.now().minusDays(1L));
if (StringUtils.isNotEmpty(adHocJobFromDate)) {
fromDate = adHocJobFromDate;
log.info("Running GrantDelayedExtractTask with Adhoc dates {}", fromDate);
}
log.info("Calling perform grant delayed data extract from date {}", fromDate);
try {
dataExtractDateValidator.dateValidator(fromDate);
log.info("Perform grant delayed data extract from date started");
GrantScheduleResponse grantScheduleResponse = grantNotificationService
.handleGrantDelayedNotification(fromDate);
log.info("Grants delayed attempted for: {} grants, {}", grantScheduleResponse
.getScheduleResponseData().size(),
StringUtils.joinWith(",", grantScheduleResponse.getScheduleResponseData()));
log.info("Perform grant delayed data extract from date finished");
} catch (ApiClientException e) {
log.error(e.getMessage());
} catch (Exception e) {
log.error("Error on GrantDelayedExtractTask Scheduler {}", e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package uk.gov.hmcts.probate.schedule;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import uk.gov.hmcts.probate.service.dataextract.DataExtractDateValidator;
import uk.gov.hmcts.probate.service.dataextract.IronMountainDataExtractService;
import uk.gov.hmcts.reform.probate.model.client.ApiClientException;

import java.time.LocalDate;

import static uk.gov.hmcts.probate.model.Constants.DATE_FORMAT;

@Component
@Slf4j
@RequiredArgsConstructor
public class IronMountainExtractTask implements Runnable {

private final DataExtractDateValidator dataExtractionDateValidator;
private final IronMountainDataExtractService ironMountainDataExtractService;

@Value("${adhocSchedulerJobDate}")
public String adHocJobFromDate;

@Override
public void run() {
log.info("Scheduled task IronMountainExtractTask started to extract data for IronMountain");
String fromDate = DATE_FORMAT.format(LocalDate.now().minusDays(1L));
if (StringUtils.isNotEmpty(adHocJobFromDate)) {
fromDate = adHocJobFromDate;
log.info("Running IronMountainDataExtractTask with Adhoc dates {}", fromDate);
}
log.info("Calling perform iron mountain data extract from date {}", fromDate);
try {
dataExtractionDateValidator.dateValidator(fromDate);
log.info("Perform iron mountain data extract from date started");
ironMountainDataExtractService.performIronMountainExtractForDate(fromDate);
log.info("Perform iron mountain data extract from date finished");
} catch (ApiClientException e) {
log.error(e.getMessage());
} catch (Exception e) {
log.error("Error on IronMountainExtractTask Scheduler {}", e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package uk.gov.hmcts.probate.schedule;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import uk.gov.hmcts.probate.service.dataextract.DataExtractDateValidator;
import uk.gov.hmcts.probate.service.dataextract.ExelaDataExtractService;
import uk.gov.hmcts.reform.probate.model.client.ApiClientException;

import java.time.LocalDate;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.doThrow;
import static uk.gov.hmcts.probate.model.Constants.DATE_FORMAT;

@ExtendWith(SpringExtension.class)
class ExelaExtractTaskTest {

@Mock
private DataExtractDateValidator dataExtractDateValidator;

@Mock
private ExelaDataExtractService exelaDataExtractService;

@InjectMocks
private ExelaExtractTask exelaExtractTask;
private static final String DATE = DATE_FORMAT.format(LocalDate.now().minusDays(1L));
private String adhocDate = "2022-09-05";
private String adhocToDate = "2022-09-10";

@Test
void shouldPerformExelaExtractDateRange() {
ResponseEntity<String> responseEntity = ResponseEntity.accepted()
.body("Perform exela data extract from date finished");
exelaExtractTask.run();
assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
assertEquals("Perform exela data extract from date finished", responseEntity.getBody());
verify(dataExtractDateValidator).dateValidator(DATE, DATE);
verify(exelaDataExtractService).performExelaExtractForDateRange(DATE, DATE);
}

@Test
void shouldPerformHmrcExtractForAdhocDate() {
exelaExtractTask.adHocJobStartDate = "2022-09-05";
ResponseEntity<String> responseEntity = ResponseEntity.accepted()
.body("Perform exela data extract from date finished");
exelaExtractTask.run();
assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
assertEquals("Perform exela data extract from date finished", responseEntity.getBody());
verify(dataExtractDateValidator).dateValidator(adhocDate, adhocDate);
verify(exelaDataExtractService).performExelaExtractForDateRange(adhocDate, adhocDate);
}

@Test
void shouldPerformHmrcExtractForAdhocDateRange() {
exelaExtractTask.adHocJobStartDate = "2022-09-05";
exelaExtractTask.adHocJobEndDate = "2022-09-10";
ResponseEntity<String> responseEntity = ResponseEntity.accepted()
.body("Perform exela data extract from date finished");
exelaExtractTask.run();
assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
assertEquals("Perform exela data extract from date finished", responseEntity.getBody());
verify(dataExtractDateValidator).dateValidator(adhocDate, adhocToDate);
verify(exelaDataExtractService).performExelaExtractForDateRange(adhocDate, adhocToDate);
}

@Test
void shouldThrowClientExceptionWithBadRequestForHmrcExtractWithIncorrectDateFormat() {
doThrow(new ApiClientException(HttpStatus.BAD_REQUEST.value(), null)).when(dataExtractDateValidator)
.dateValidator(DATE, DATE);
exelaExtractTask.run();
verify(dataExtractDateValidator).dateValidator(DATE, DATE);
verifyNoInteractions(exelaDataExtractService);
}

@Test
void shouldThrowExceptionForHmrcExtract() {
doThrow(new NullPointerException()).when(dataExtractDateValidator)
.dateValidator(DATE, DATE);
exelaExtractTask.run();
verify(dataExtractDateValidator).dateValidator(DATE, DATE);
verifyNoInteractions(exelaDataExtractService);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package uk.gov.hmcts.probate.schedule;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import uk.gov.hmcts.probate.service.GrantNotificationService;
import uk.gov.hmcts.probate.service.dataextract.DataExtractDateValidator;
import uk.gov.hmcts.reform.probate.model.client.ApiClientException;

import java.time.LocalDate;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static uk.gov.hmcts.probate.model.Constants.DATE_FORMAT;

@ExtendWith(SpringExtension.class)
class GrantAwaitingDocumentationExtractTaskTest {

@Mock
private DataExtractDateValidator dataExtractDateValidator;

@Mock
private GrantNotificationService grantNotificationService;

@InjectMocks
private GrantAwaitingDocumentationExtractTask grantAwaitingDocumentationExtractTask;
private static final String DATE = DATE_FORMAT.format(LocalDate.now().minusDays(1L));
private String adhocDate = "2022-09-05";

@Test
void shouldPerformGrantAwaitingDocumentationExtractYesterdayDate() {
ResponseEntity<String> responseEntity = ResponseEntity.accepted()
.body("Perform grant awaiting documentation data extract from date finished");
grantAwaitingDocumentationExtractTask.run();
assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
assertEquals("Perform grant awaiting documentation data extract from date finished",
responseEntity.getBody());
verify(dataExtractDateValidator).dateValidator(DATE);
verify(grantNotificationService).handleAwaitingDocumentationNotification(DATE);
}

@Test
void shouldPerformGrantAwaitingDocumentationExtractForAdhocDate() {
grantAwaitingDocumentationExtractTask.adHocJobFromDate = "2022-09-05";
ResponseEntity<String> responseEntity = ResponseEntity.accepted()
.body("Perform grant awaiting documentation data extract from date finished");
grantAwaitingDocumentationExtractTask.run();
assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
assertEquals("Perform grant awaiting documentation data extract from date finished",
responseEntity.getBody());
verify(dataExtractDateValidator).dateValidator(adhocDate);
verify(grantNotificationService).handleAwaitingDocumentationNotification(adhocDate);
}

@Test
void shouldThrowClientExceptionWithBadRequestForGrantAwaitingDocumentationExtractWithIncorrectDateFormat() {
doThrow(new ApiClientException(HttpStatus.BAD_REQUEST.value(), null)).when(dataExtractDateValidator)
.dateValidator(DATE);
grantAwaitingDocumentationExtractTask.run();
verify(dataExtractDateValidator).dateValidator(DATE);
verifyNoInteractions(grantNotificationService);
}

@Test
void shouldThrowExceptionForGrantAwaitingDocumentationExtract() {
doThrow(new NullPointerException()).when(dataExtractDateValidator)
.dateValidator(DATE);
grantAwaitingDocumentationExtractTask.run();
verify(dataExtractDateValidator).dateValidator(DATE);
verifyNoInteractions(grantNotificationService);
}

}
Loading
Loading