Skip to content

Commit 6b8313c

Browse files
RWA-4836 Adjust delay‑until calculation to count working days only. Update unit/integration tests and holiday stubs to match new behavior (#963)
* RWA-4836 Adjust delay‑until calculation to count working days only. Update unit/integration tests and holiday stubs to match new behavior * RWA-4836 Increased test coverage. * RWA-4836 Reworked code to make it SonaCloud compliant. --------- Co-authored-by: Martin Spasov <martin.y.spasov@gmail.com>
1 parent 224ce53 commit 6b8313c

File tree

4 files changed

+64
-17
lines changed

4 files changed

+64
-17
lines changed

src/integrationTest/java/uk/gov/hmcts/reform/wacaseeventhandler/handlers/InitiationCaseEventHandlerTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,10 @@ void should_handle_and_process_delay_until_set_to_holiday_as_next_working_day()
112112
when(isoDateFormatter.formatToZone(LocalDateTime.parse(EVENT_DATE)))
113113
.thenReturn(eventDateTime);
114114

115+
when(holidayService.isHoliday(any(ZonedDateTime.class))).thenReturn(false);
115116
when(holidayService.isHoliday(eventDateTime.plusDays(delayDuration)))
116117
.thenReturn(true);
117118

118-
when(holidayService.isHoliday(eventDateTime.plusDays(delayDuration + 1)))
119-
.thenReturn(false);
120-
121119
when(serviceAuthGenerator.generate())
122120
.thenReturn(SERVICE_AUTH_TOKEN);
123121

src/integrationTest/java/uk/gov/hmcts/reform/wacaseeventhandler/services/DueDateServiceTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void should_return_next_working_day_4_pm_when_calculated_due_date_matches_holida
4242

4343
int workingDaysAllowed = 2;
4444

45+
when(holidayService.isHoliday(any(ZonedDateTime.class))).thenReturn(false);
4546
when(holidayService.isHoliday(eventDateTime.plusDays(1)))
4647
.thenReturn(true);
4748

@@ -66,6 +67,7 @@ void should_return_next_working_day_4_pm_when_delay_date_matches_holiday() {
6667

6768
int delayDuration = 2;
6869

70+
when(holidayService.isHoliday(any(ZonedDateTime.class))).thenReturn(false);
6971
when(holidayService.isHoliday(eventDateTime.plusDays(delayDuration)))
7072
.thenReturn(true);
7173

@@ -74,9 +76,8 @@ void should_return_next_working_day_4_pm_when_delay_date_matches_holiday() {
7476
ZonedDateTime actualDateTime = dueDateService.calculateDelayUntil(eventDateTime, delayDuration);
7577

7678
assertThat(actualDateTime, is(expectedDelayDateTime));
77-
verify(holidayService, times(2)).isHoliday(any(ZonedDateTime.class));
79+
verify(holidayService, times(3)).isHoliday(any(ZonedDateTime.class));
7880
}
7981

8082

8183
}
82-

src/main/java/uk/gov/hmcts/reform/wacaseeventhandler/services/DueDateService.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ private ZonedDateTime addWorkingDays(ZonedDateTime dueDate, int numberOfDays) {
4545
}
4646

4747
private ZonedDateTime addWorkingDaysForDelayDuration(ZonedDateTime eventDate, int delayDuration) {
48-
49-
ZonedDateTime newDate = eventDate.plusDays(delayDuration);
50-
51-
if (holidayService.isWeekend(newDate) || holidayService.isHoliday(newDate)) {
52-
return addWorkingDaysForDelayDuration(eventDate, delayDuration + 1);
48+
ZonedDateTime newDate = eventDate;
49+
int remaining = delayDuration;
50+
while (remaining > 0) {
51+
newDate = newDate.plusDays(1);
52+
if (!(holidayService.isWeekend(newDate) || holidayService.isHoliday(newDate))) {
53+
remaining--;
54+
}
5355
}
5456

5557
return newDate;

src/test/java/uk/gov/hmcts/reform/wacaseeventhandler/services/DueDateServiceTest.java

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.mockito.ArgumentMatchers.any;
2121
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.verifyNoInteractions;
2223
import static org.mockito.Mockito.when;
2324

2425
@SuppressWarnings("PMD.JUnitAssertionsShouldIncludeMessage")
@@ -145,17 +146,46 @@ void should_return_event_date_when_delay_duration_is_0() {
145146
assertEquals(eventDate, actualDelayUntil);
146147
}
147148

149+
@Test
150+
void should_not_call_holiday_service_when_delay_duration_is_0() {
151+
ZonedDateTime eventDate = ZonedDateTime.of(2022, 7, 1, 9, 0, 0, 0, ZoneId.systemDefault());
152+
153+
ZonedDateTime actualDelayUntil = underTest.calculateDelayUntil(eventDate, 0);
154+
155+
assertEquals(eventDate, actualDelayUntil);
156+
verifyNoInteractions(holidayService);
157+
}
158+
159+
@Test
160+
void should_return_event_date_when_add_working_days_for_delay_duration_is_0() throws Exception {
161+
ZonedDateTime eventDate = ZonedDateTime.of(2022, 7, 1, 9, 0, 0, 0, ZoneId.systemDefault());
162+
163+
var method = DueDateService.class.getDeclaredMethod(
164+
"addWorkingDaysForDelayDuration",
165+
ZonedDateTime.class,
166+
int.class
167+
);
168+
method.setAccessible(true);
169+
170+
ZonedDateTime actual = (ZonedDateTime) method.invoke(underTest, eventDate, 0);
171+
172+
assertEquals(eventDate, actual);
173+
verifyNoInteractions(holidayService);
174+
}
175+
148176
@ParameterizedTest
149177
@ValueSource(ints = {1, 2, 3})
150178
void should_return_next_working_day_when_event_date_is_friday_and_has_delay_duration(int delayDuration) {
151179

152180
ZonedDateTime eventDate = ZonedDateTime.of(2022, 7, 1, 9, 0, 0, 0, ZoneId.systemDefault());
153181

154-
ZonedDateTime expectedDueDateMonday = ZonedDateTime.of(2022, 7, 4, 16, 0, 0, 0, ZoneId.systemDefault());
182+
ZonedDateTime expectedDelayUntilDate = eventDate
183+
.plusDays(3 + (delayDuration - 1))
184+
.withHour(16).withMinute(0).withSecond(0).withNano(0);
155185

156186
ZonedDateTime actualDelayUntilDate = underTest.calculateDelayUntil(eventDate, delayDuration);
157187

158-
assertEquals(expectedDueDateMonday, actualDelayUntilDate);
188+
assertEquals(expectedDelayUntilDate, actualDelayUntilDate);
159189
}
160190

161191
@ParameterizedTest
@@ -166,11 +196,27 @@ void should_return_next_working_day_when_event_date_plus_delay_duration_matches_
166196
when(holidayService.isHoliday(eventDate.plusDays(3)))
167197
.thenReturn(true);
168198

169-
ZonedDateTime expectedDueDateMonday = ZonedDateTime.of(2022, 8, 30, 16, 0, 0, 0, ZoneId.systemDefault());
199+
ZonedDateTime expectedDelayUntilDate = ZonedDateTime.of(2022, 8, 30, 16, 0, 0, 0, ZoneId.systemDefault())
200+
.plusDays(delayDuration - 1L);
170201

171202
ZonedDateTime actualDelayUntilDate = underTest.calculateDelayUntil(eventDate, delayDuration);
172203

173-
assertEquals(expectedDueDateMonday, actualDelayUntilDate);
204+
assertEquals(expectedDelayUntilDate, actualDelayUntilDate);
205+
}
206+
207+
@Test
208+
void should_skip_weekend_and_holiday_when_calculating_delay_duration() {
209+
ZonedDateTime eventDate = ZonedDateTime.of(2022, 8, 25, 9, 0, 0, 0, ZoneId.systemDefault());
210+
211+
when(holidayService.isHoliday(any(ZonedDateTime.class))).thenReturn(false);
212+
when(holidayService.isHoliday(ZonedDateTime.of(2022, 8, 29, 9, 0, 0, 0, ZoneId.systemDefault())))
213+
.thenReturn(true);
214+
215+
ZonedDateTime expectedDelayUntilDate = ZonedDateTime.of(2022, 8, 31, 16, 0, 0, 0, ZoneId.systemDefault());
216+
217+
ZonedDateTime actualDelayUntilDate = underTest.calculateDelayUntil(eventDate, 3);
218+
219+
assertEquals(expectedDelayUntilDate, actualDelayUntilDate);
174220
}
175221

176222
private void checkWorkingDays(ZonedDateTime startDay, int leadTimeDays, ZonedDateTime expectedDueDate) {
@@ -202,7 +248,7 @@ private static Stream<ZonedDateTimeScenario> scenarioProvider() {
202248
ZonedDateTime.of(2021, 3, 1, 16, 0, 0, 0, ZoneId.systemDefault()))
203249
.days(18)
204250
.expectedDateTime(
205-
ZonedDateTime.of(2021, 3, 19, 16, 0, 0, 0, ZoneId.systemDefault()))
251+
ZonedDateTime.of(2021, 3, 25, 16, 0, 0, 0, ZoneId.systemDefault()))
206252
.build(),
207253

208254
ZonedDateTimeScenario.builder()
@@ -226,15 +272,15 @@ private static Stream<ZonedDateTimeScenario> scenarioProvider() {
226272
ZonedDateTime.of(2021, 3, 1, 16, 0, 0, 0, ZoneId.systemDefault()))
227273
.days(30)
228274
.expectedDateTime(
229-
ZonedDateTime.of(2021, 3, 31, 16, 0, 0, 0, ZoneId.systemDefault()))
275+
ZonedDateTime.of(2021, 4, 12, 16, 0, 0, 0, ZoneId.systemDefault()))
230276
.build(),
231277

232278
ZonedDateTimeScenario.builder()
233279
.actualDateTime(
234280
ZonedDateTime.of(2021, 3, 1, 16, 0, 0, 0, ZoneId.systemDefault()))
235281
.days(31)
236282
.expectedDateTime(
237-
ZonedDateTime.of(2021, 4, 1, 16, 0, 0, 0, ZoneId.systemDefault()))
283+
ZonedDateTime.of(2021, 4, 13, 16, 0, 0, 0, ZoneId.systemDefault()))
238284
.build()
239285
);
240286
}

0 commit comments

Comments
 (0)