Skip to content

Commit

Permalink
RWA-3300: Received messages help check repo tests added (#697)
Browse files Browse the repository at this point in the history
* RWA-3300: Received messages help check repo tests added

* RWA-3300: Received messages help check repo tests changes

* RWA-3300: Received messages help check repo tests added

* RWA-3300: Tests corrected.

* Updated test names

* Renamed the tests
  • Loading branch information
adityadwadasi authored Jul 9, 2024
1 parent 627bc65 commit 770788e
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
Expand All @@ -18,9 +20,11 @@
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.zone.ZoneRules;
import java.util.stream.Stream;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -95,9 +99,9 @@ void test_health_reports_up_if_no_messages_received_in_last_hour_during_weekend(
}

@Test
void test_health_reports_up_if_no_messages_received_in_last_hour_during_holiday() throws Exception {
void should_health_reports_up_when_no_messages_received_in_last_hour_during_holiday() throws Exception {
// GIVEN
LocalDateTime localDateTime = LocalDateTime.of(2022, 8, 29, 12,15);
LocalDateTime localDateTime = LocalDateTime.of(2022, 8, 29, 12, 15);
setClock(localDateTime);

// THEN
Expand Down Expand Up @@ -129,6 +133,85 @@ void test_health_reports_up_if_time_outside_of_working_hours_end_time() throws E
verify(caseEventMessageRepository, never()).getNumberOfMessagesReceivedInLastHour(any());
}

@Test
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD,
scripts = {"classpath:scripts/insert_case_event_messages_for_received_messages_check.sql"})
void should_health_check_reports_up_when_messages_received_in_last_hour() throws Exception {

// GIVEN
setClock(LocalDateTime.of(2024, 4, 2, 14,15));

// THEN
mockMvc.perform(get("/health"))
.andExpect(
jsonPath("$.components.ccdMessagesReceived.status")
.value(UP.toString()))
.andExpect(jsonPath("$.components.ccdMessagesReceived.details." + CASE_EVENT_HANDLER_MESSAGE_HEALTH)
.value(String.format(MESSAGES_RECEIVED, "test")));

}

@Test
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD,
scripts = {"classpath:scripts/insert_case_event_messages_for_received_messages_check.sql"})
void should_verify_health_check_reports_is_down_when_no_messages_received_in_last_hour() throws Exception {

// GIVEN
setClock(LocalDateTime.of(2024, 4, 2, 15,15));

// THEN
mockMvc.perform(get("/health"))
.andExpect(
jsonPath("$.components.ccdMessagesReceived.status")
.value(DOWN.toString()))
.andExpect(jsonPath("$.components.ccdMessagesReceived.details." + CASE_EVENT_HANDLER_MESSAGE_HEALTH)
.value(String.format(NO_MESSAGES_RECEIVED, "test")));

}

@ParameterizedTest
@MethodSource(value = "workingHoursWithTimeZoneScenarioProvider")
void should_invoke_case_event_repo_when_time_is_within_working_hours(
LocalDateTime withinWorkingHoursDate) throws Exception {

// GIVEN
setClock(withinWorkingHoursDate);

when(caseEventMessageRepository.getNumberOfMessagesReceivedInLastHour(any())).thenReturn(10);

// THEN
assertReceivedMessagesHealthStatus(UP, MESSAGES_RECEIVED);

}

@ParameterizedTest
@MethodSource(value = "nonWorkingHoursForDstTimeZoneStartTimeAndEndTime")
void should_not_invoke_case_event_repo_when_time_is_outside_working_hours(
LocalDateTime outsideWorkingHoursDate) throws Exception {

// GIVEN
setClock(outsideWorkingHoursDate);

// THEN
assertReceivedMessagesHealthStatus(UP, NO_MESSAGE_CHECK);

verify(caseEventMessageRepository, never()).getNumberOfMessagesReceivedInLastHour(any());
}

private static Stream<LocalDateTime> workingHoursWithTimeZoneScenarioProvider() {
return Stream.of(
LocalDateTime.of(2024, Month.JANUARY, 02, 10, 00),
LocalDateTime.of(2024, Month.MAY, 01, 9, 00)
);
}

private static Stream<LocalDateTime> nonWorkingHoursForDstTimeZoneStartTimeAndEndTime() {
return Stream.of(
LocalDateTime.of(2024, Month.OCTOBER, 27, 07, 00),
LocalDateTime.of(2024, Month.MARCH, 31, 17, 00)
);
}

private void assertReceivedMessagesHealthStatus(Status status, String details) throws Exception {
mockMvc.perform(get("/health"))
.andExpect(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
TRUNCATE TABLE public.wa_case_event_messages;

INSERT INTO public.wa_case_event_messages (message_id,
sequence,
case_id,
event_timestamp,
from_dlq,
state,
message_properties,
message_content,
received,
delivery_count,
hold_until,
retry_count)
VALUES
('MessageId_bc8299fc-5d31-45c7-b847-c2622014a85b',
7,
'9140-9312-3701-4412',
'2024-04-02 14:00:00.887100',
false,
'NEW',
'null',
'{"EventInstanceId":"77838838-2369-443d-a076-9c80bcda17d6","EventTimeStamp":[2022,1,14,12,45,54,887100000],"CaseId":"9140-9312-3701-4413","JurisdictionId":"ia","CaseTypeId":"asylum","EventId":"dummyEvent","PreviousStateId":"","NewStateId":"","UserId":"some user Id","AdditionalData":{"Data":{"lastModifiedDirection":{"dateDue":"","uniqueId":"","directionType":""},"appealType":"protection"},"Definition":null}}',
'2024-04-02 14:00:00.350754',
0,
null,
0);
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@ void should_return_case_event_message_with_message_id() {
assertEquals("MessageId_bc8299fc-5d31-45c7-b847-c2622014a85a", caseEventMessageEntity.getMessageId());
}

@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD,
scripts = {"classpath:sql/insert_case_event_messages_for_received_messages_check.sql"})
@Test
void should_return_number_of_messages_received_as_1_after_specified_time() {
final int numberOfMessagesReceived =
caseEventMessageRepository.getNumberOfMessagesReceivedInLastHour(
LocalDateTime.of(2024, 4, 2, 13,55));
assertEquals(1, numberOfMessagesReceived);
}

@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD,
scripts = {"classpath:sql/insert_case_event_messages_for_received_messages_check.sql"})
@Test
void should_return_number_of_messages_received_as_0_after_specified_time() {
final int numberOfMessagesReceived =
caseEventMessageRepository.getNumberOfMessagesReceivedInLastHour(
LocalDateTime.of(2024, 4, 2, 14,5));
assertEquals(0, numberOfMessagesReceived);
}

@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD,
scripts = {"classpath:sql/insert_case_event_messages.sql"})
@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
INSERT INTO public.wa_case_event_messages (message_id,
sequence,
case_id,
event_timestamp,
from_dlq,
state,
message_properties,
message_content,
received,
delivery_count,
hold_until,
retry_count)
VALUES
('MessageId_bc8299fc-5d31-45c7-b847-c2622014a85b',
7,
'9140-9312-3701-4412',
'2024-04-02 14:00:00.887100',
false,
'NEW',
'null',
'{"EventInstanceId":"77838838-2369-443d-a076-9c80bcda17d6","EventTimeStamp":[2022,1,14,12,45,54,887100000],"CaseId":"9140-9312-3701-4413","JurisdictionId":"ia","CaseTypeId":"asylum","EventId":"dummyEvent","PreviousStateId":"","NewStateId":"","UserId":"some user Id","AdditionalData":{"Data":{"lastModifiedDirection":{"dateDue":"","uniqueId":"","directionType":""},"appealType":"protection"},"Definition":null}}',
'2024-04-02 14:00:00.350754',
0,
null,
0);

0 comments on commit 770788e

Please sign in to comment.