-
Notifications
You must be signed in to change notification settings - Fork 4
SSCSCI-2275 - CTSC Task - FTA reply overdue #1874
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
Open
gemmatalbot
wants to merge
26
commits into
master
Choose a base branch
from
SSCSCI-2275-addLatestFtaId
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
d93ed86
add latest fta id field
gemmatalbot a48f448
add overdue Response
gemmatalbot 129544f
add taskCreatedForRequest field
gemmatalbot 385d6c2
Merge branch 'master' into SSCSCI-2275-addLatestFtaId
gemmatalbot 2852df2
add wa token
gemmatalbot 4365b86
add wa token
gemmatalbot 4e53391
logs
gemmatalbot 59f5f88
Merge branch 'master' into SSCSCI-2275-addLatestFtaId
gemmatalbot d96c5f2
update fta communication id
gemmatalbot d57770c
update overdueResponse
gemmatalbot 894a9d3
upgrade elastic search
gemmatalbot da9eb64
update idamServiceTest
gemmatalbot dbd4cb7
business days calculator
gemmatalbot cf42fe1
add public method
gemmatalbot 24cdd65
add test
gemmatalbot f70e81a
move CachedHolidayClient and BusinessDaysCalculatorService to uk.gov.…
hashimalisolirius 6d871a9
move CachedHolidayClient and BusinessDaysCalculatorService to uk.gov.…
hashimalisolirius e775ffb
update getHolidays
gemmatalbot af20835
update taskCreatedForRequest type
gemmatalbot 5af10b8
Merge branch 'master' into SSCSCI-2275-addLatestFtaId
gemmatalbot 1b295ce
Merge branch 'master' into SSCSCI-2275-addLatestFtaId
gemmatalbot 1ad8387
idam token config
gemmatalbot 94f6400
idam token retry
gemmatalbot 7f2fea3
Merge branch 'master' into SSCSCI-2275-addLatestFtaId
gemmatalbot 209366e
Merge branch 'master' into SSCSCI-2275-addLatestFtaId
gemmatalbot d5a2524
Merge branch 'master' into SSCSCI-2275-addLatestFtaId
gemmatalbot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/uk/gov/hmcts/reform/sscs/client/CachedHolidayClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package uk.gov.hmcts.reform.sscs.client; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import java.io.IOException; | ||
| import java.time.LocalDate; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import okhttp3.OkHttpClient; | ||
| import okhttp3.Request; | ||
| import okhttp3.Response; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| public class CachedHolidayClient { | ||
|
|
||
| private final OkHttpClient httpClient; | ||
| private Set<LocalDate> cachedHolidays; | ||
| private static final String HOLIDAY_API_URL = "https://www.gov.uk/bank-holidays.json"; | ||
|
|
||
| public CachedHolidayClient(OkHttpClient httpClient) { | ||
| this.httpClient = httpClient; | ||
| } | ||
|
|
||
| public synchronized Set<LocalDate> getHolidays() throws IOException { | ||
| if (cachedHolidays == null) { | ||
| cachedHolidays = fetchHolidaysFromApi(); | ||
| } | ||
| return cachedHolidays; | ||
| } | ||
|
|
||
| private Set<LocalDate> fetchHolidaysFromApi() throws IOException { | ||
| Request request = new Request.Builder().url(HOLIDAY_API_URL).build(); | ||
|
|
||
| try (Response response = httpClient.newCall(request).execute()) { | ||
| if (!response.isSuccessful()) { | ||
| throw new IOException("Response unsuccessful: " + response); | ||
| } | ||
|
|
||
| Set<LocalDate> holidays = new HashSet<>(); | ||
| ObjectMapper mapper = new ObjectMapper(); | ||
| JsonNode root = mapper.readTree(response.body().string()); | ||
|
|
||
| JsonNode events = root.path("england-and-wales").path("events"); | ||
| for (JsonNode event : events) { | ||
| String dateStr = event.path("date").asText(); | ||
| holidays.add(LocalDate.parse(dateStr)); | ||
| } | ||
|
|
||
| return holidays; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/uk/gov/hmcts/reform/sscs/service/BusinessDaysCalculatorService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package uk.gov.hmcts.reform.sscs.service; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.LocalDate; | ||
| import java.time.ZonedDateTime; | ||
| import java.util.Set; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import net.objectlab.kit.datecalc.common.DefaultHolidayCalendar; | ||
| import net.objectlab.kit.datecalc.jdk8.LocalDateKitCalculatorsFactory; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Service; | ||
| import uk.gov.hmcts.reform.sscs.client.CachedHolidayClient; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| public class BusinessDaysCalculatorService { | ||
|
|
||
| private final CachedHolidayClient cachedHolidayClient; | ||
|
|
||
| @Autowired | ||
| public BusinessDaysCalculatorService(CachedHolidayClient cachedHolidayClient) { | ||
| this.cachedHolidayClient = cachedHolidayClient; | ||
| } | ||
|
|
||
| public ZonedDateTime getBusinessDay(ZonedDateTime startDateTime, int numberOfBusinessDays) throws IOException { | ||
| initialiseHolidays(); | ||
| LocalDate startDate = startDateTime.toLocalDate(); | ||
| return calculateBusinessDay(startDate, numberOfBusinessDays, startDateTime); | ||
| } | ||
|
|
||
| public LocalDate getBusinessDay(LocalDate date, int numberOfBusinessDays) throws IOException { | ||
| initialiseHolidays(); | ||
| return calculateBusinessDay(date, numberOfBusinessDays); | ||
| } | ||
|
|
||
| public LocalDate getBusinessDayInPast(LocalDate date, int numberOfBusinessDays) throws IOException { | ||
| initialiseHolidays(); | ||
| return calculateBusinessDayInPast(date, numberOfBusinessDays); | ||
| } | ||
|
|
||
| private void initialiseHolidays() throws IOException { | ||
| Set<LocalDate> holidays = cachedHolidayClient.getHolidays(); | ||
| DefaultHolidayCalendar<LocalDate> ukCalendar = new DefaultHolidayCalendar<>(); | ||
| ukCalendar.setHolidays(holidays); | ||
| LocalDateKitCalculatorsFactory.getDefaultInstance().registerHolidays("UK", ukCalendar); | ||
| } | ||
|
|
||
| private ZonedDateTime calculateBusinessDay(LocalDate startDate, int numberOfBusinessDays, ZonedDateTime startDateTime) { | ||
| return ZonedDateTime.of( | ||
| calculateBusinessDay(startDate, numberOfBusinessDays), | ||
| startDateTime.toLocalTime(), | ||
| startDateTime.getZone() | ||
| ); | ||
| } | ||
|
|
||
| private LocalDate calculateBusinessDay(LocalDate startDate, int numberOfBusinessDays) { | ||
| return LocalDateKitCalculatorsFactory.forwardCalculator("UK") | ||
| .setStartDate(startDate) | ||
| .moveByBusinessDays(numberOfBusinessDays) | ||
| .getCurrentBusinessDate(); | ||
| } | ||
|
|
||
| private LocalDate calculateBusinessDayInPast(LocalDate startDate, int numberOfBusinessDays) { | ||
| return LocalDateKitCalculatorsFactory.backwardCalculator("UK") | ||
| .setStartDate(startDate) | ||
| .moveByBusinessDays(-numberOfBusinessDays) | ||
| .getCurrentBusinessDate(); | ||
| } | ||
| } |
86 changes: 86 additions & 0 deletions
86
src/test/java/uk/gov/hmcts/reform/sscs/client/CachedHolidayClientTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package uk.gov.hmcts.reform.sscs.client; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.LocalDate; | ||
| import java.util.Set; | ||
| import okhttp3.Call; | ||
| import okhttp3.OkHttpClient; | ||
| import okhttp3.Request; | ||
| import okhttp3.Response; | ||
| import okhttp3.ResponseBody; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockitoAnnotations; | ||
|
|
||
| class CachedHolidayClientTest { | ||
|
|
||
| @Mock | ||
| private OkHttpClient httpClient; | ||
|
|
||
| @Mock | ||
| private Call mockCall; | ||
|
|
||
| @Mock | ||
| private Response response; | ||
|
|
||
| @Mock | ||
| private ResponseBody responseBody; | ||
|
|
||
| private CachedHolidayClient cachedHolidayClient; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws IOException { | ||
| MockitoAnnotations.openMocks(this); | ||
| when(httpClient.newCall(any(Request.class))).thenReturn(mockCall); | ||
| when(mockCall.execute()).thenReturn(response); | ||
| when(response.isSuccessful()).thenReturn(true); | ||
| when(response.body()).thenReturn(responseBody); | ||
| when(responseBody.string()).thenReturn("{\"england-and-wales\":{\"events\":[{\"date\":\"2024-12-25\"},{\"date\":\"2024-12-26\"}]}}"); | ||
|
|
||
| cachedHolidayClient = new CachedHolidayClient(httpClient); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldFetchHolidaysFromApi() throws IOException { | ||
| Set<LocalDate> holidays = cachedHolidayClient.getHolidays(); | ||
|
|
||
| assertNotNull(holidays); | ||
| assertEquals(2, holidays.size()); | ||
| assertTrue(holidays.contains(LocalDate.of(2024, 12, 25))); | ||
| assertTrue(holidays.contains(LocalDate.of(2024, 12, 26))); | ||
|
|
||
| // Verify API call was made | ||
| verify(mockCall, times(1)).execute(); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldReturnCachedHolidaysOnSubsequentCalls() throws IOException { | ||
| Set<LocalDate> holidaysFirstCall = cachedHolidayClient.getHolidays(); | ||
| Set<LocalDate> holidaysSecondCall = cachedHolidayClient.getHolidays(); | ||
|
|
||
| assertSame(holidaysFirstCall, holidaysSecondCall); | ||
|
|
||
| // Verify API call was made only once | ||
| verify(mockCall, times(1)).execute(); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldThrowIoExceptionForUnsuccessfulResponse() throws IOException { | ||
| when(response.isSuccessful()).thenReturn(false); | ||
|
|
||
| IOException exception = assertThrows(IOException.class, () -> cachedHolidayClient.getHolidays()); | ||
| assertEquals("Response unsuccessful: " + response, exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldThrowIoExceptionForFailedApiCall() throws IOException { | ||
| when(mockCall.execute()).thenThrow(new IOException("Network error")); | ||
|
|
||
| IOException exception = assertThrows(IOException.class, () -> cachedHolidayClient.getHolidays()); | ||
| assertEquals("Network error", exception.getMessage()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.