Skip to content

Commit

Permalink
RWA-3295: Added staging env check for receivedMessages api (#695)
Browse files Browse the repository at this point in the history
* RWA-3295: Added staging env check for receivedMessages api

* RWA-3295: Added tests

* RWA-3295: Added integration test, logs

* RWA-3295: modified test names.
  • Loading branch information
adityadwadasi authored Apr 22, 2024
1 parent 8371569 commit 5c214fd
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package uk.gov.hmcts.reform.wacaseeventhandler.controllers;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.web.servlet.MockMvc;
import uk.gov.hmcts.reform.wacaseeventhandler.repository.CaseEventMessageRepository;
import uk.gov.hmcts.reform.wacaseeventhandler.services.holidaydates.HolidayService;

import java.time.Clock;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.zone.ZoneRules;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.boot.actuate.health.Status.UP;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static uk.gov.hmcts.reform.wacaseeventhandler.controllers.ReceivedMessagesHealthController.CASE_EVENT_HANDLER_MESSAGE_HEALTH;
import static uk.gov.hmcts.reform.wacaseeventhandler.controllers.ReceivedMessagesHealthController.CHECK_DISABLED_MESSAGE;

@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
@ActiveProfiles(profiles = {"db", "integration"})
@TestPropertySource(properties = {"azure.servicebus.enableASB-DLQ=false",
"environment=aat",
"management.endpoint.health.receivedMessageCheckEnvEnabled=aat"})
@Sql({"classpath:sql/delete_from_case_event_messages.sql", "classpath:scripts/insert_case_event_messages.sql"})
public class ReceivedMessagesHealthControllerStagingEnvTest {

@Autowired
private MockMvc mockMvc;

@SpyBean
private CaseEventMessageRepository caseEventMessageRepository;

@SpyBean
private HolidayService holidayService;

@MockBean
private Clock clock;

@BeforeEach
void setup() {
reset(caseEventMessageRepository);
}

@Test
void test_health_reports_up_api_invoked_from_staging() throws Exception {

// GIVEN
setClock(LocalDateTime.of(2022, 8, 26, 11,29));

// THEN
mockMvc.perform(get("/health").header("Host", "case-event-handler.aat.staging"))
.andExpect(
jsonPath("$.components.ccdMessagesReceived.status")
.value(UP.toString()))
.andExpect(jsonPath("$.components.ccdMessagesReceived.details." + CASE_EVENT_HANDLER_MESSAGE_HEALTH)
.value(String.format(CHECK_DISABLED_MESSAGE, "aat")));

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

private void setClock(LocalDateTime localDateTime) {
when(clock.instant()).thenReturn(localDateTime.toInstant(ZoneOffset.UTC));
ZoneId zoneId = mock(ZoneId.class);
ZoneRules zoneRules = mock(ZoneRules.class);

when(zoneRules.getOffset(any(Instant.class))).thenReturn(ZoneOffset.UTC);
when(zoneId.getRules()).thenReturn(zoneRules);
when(clock.getZone()).thenReturn(zoneId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import uk.gov.hmcts.reform.wacaseeventhandler.repository.CaseEventMessageRepository;
import uk.gov.hmcts.reform.wacaseeventhandler.services.holidaydates.HolidayService;

import java.net.URI;
import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
Expand All @@ -29,6 +31,10 @@ public class ReceivedMessagesHealthController implements HealthIndicator {
protected static final String NO_MESSAGE_CHECK = "Out Of Hours, no check for messages";
protected static final String CHECK_DISABLED_MESSAGE = "check disabled in %s";

protected static final String ENV_AAT = "aat";

protected static final String STAGING_TEXT = "staging";

@Value("${management.endpoint.health.receivedMessageCheckEnvEnabled}")
private String receivedMessageCheckEnvEnabled;

Expand Down Expand Up @@ -113,7 +119,15 @@ private boolean isDateWithinWorkingHours(LocalDateTime localDateTime) {
&& (localDateTime.equals(workingHoursEndTime) || localDateTime.isBefore(workingHoursEndTime));
}

private boolean isNotEnabledForEnvironment(String env) {
public boolean isNotEnabledForEnvironment(String env) {
if (ENV_AAT.equals(env)) {

URI currentUri = ServletUriComponentsBuilder.fromCurrentRequestUri().build().toUri();
log.info("Invoked API URI: {}", currentUri);
if (currentUri.toString().contains(STAGING_TEXT)) {
return true;
}
}
Set<String> envsToEnable = Arrays.stream(receivedMessageCheckEnvEnabled.split(","))
.map(String::trim).collect(Collectors.toSet());
return !envsToEnable.contains(env);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.actuate.health.Health;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import uk.gov.hmcts.reform.wacaseeventhandler.repository.CaseEventMessageRepository;
import uk.gov.hmcts.reform.wacaseeventhandler.services.holidaydates.HolidayService;

Expand All @@ -21,6 +24,9 @@
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -173,7 +179,8 @@ void test_health_calls_repository_if_working_day_time_is_outside_working_hours_w
assertEquals(NO_MESSAGE_CHECK, health.getDetails().get(CASE_EVENT_HANDLER_MESSAGE_HEALTH));
}

@Test
@ParameterizedTest
@MethodSource(value = "nonWorkingHoursForDstTimeZoneStartTimeAndEndTime")
void test_health_reports_success_if_messages_check_disabled_in_current_environment() {
// GIVEN
setField(receivedMessagesHealthController, "environment", "invalidEnvironment");
Expand All @@ -191,6 +198,57 @@ void test_health_reports_success_if_messages_check_disabled_in_current_environme
verifyNoInteractions(caseEventMessageRepository);
}

@ParameterizedTest
@MethodSource(value = "enabledEnvProvider")
void test_not_enabled_for_environment_return_false_prod_aat(String env) {

setField(receivedMessagesHealthController, "receivedMessageCheckEnvEnabled", "prod,aat");

MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
mockHttpServletRequest.setServerName("case-event-handler.any.environment");
mockHttpServletRequest.setRequestURI("/health");

RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockHttpServletRequest));

assertFalse(receivedMessagesHealthController.isNotEnabledForEnvironment(env));
}

@ParameterizedTest
@MethodSource(value = "disabledEnvProvider")
void test_not_enabled_for_environment_return_true_demo_ithc(String env) {

setField(receivedMessagesHealthController, "receivedMessageCheckEnvEnabled", "prod,aat");

MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
mockHttpServletRequest.setServerName("case-event-handler.any.environment");
mockHttpServletRequest.setRequestURI("/health");

RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockHttpServletRequest));

assertTrue(receivedMessagesHealthController.isNotEnabledForEnvironment(env));
}

@Test
void test_not_enabled_for_environment_return_true_staging_aat() {

MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
mockHttpServletRequest.setServerName("case-event-handler.staging.aat");
mockHttpServletRequest.setRequestURI("/health");

RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockHttpServletRequest));

assertTrue(receivedMessagesHealthController.isNotEnabledForEnvironment("aat"));
}

@Test
void test_enabled_for_environment_throw_exception_outside_http_context() {

assertThrows(IllegalStateException.class, () -> {
receivedMessagesHealthController.isNotEnabledForEnvironment("aat");
});

}

private void setupDefaultMockClock() {
setupMockClock(LocalDateTime.of(2022, Month.SEPTEMBER, 01, 11, 30));
}
Expand Down Expand Up @@ -232,4 +290,16 @@ private static Stream<LocalDateTime> nonWorkingHoursForDstTimeZoneStartTimeAndEn
LocalDateTime.of(2024, Month.MARCH, 31, 17, 00)
);
}

private static Stream<String> enabledEnvProvider() {
return Stream.of(
"prod","aat"
);
}

private static Stream<String> disabledEnvProvider() {
return Stream.of(
"demo","ithc"
);
}
}

0 comments on commit 5c214fd

Please sign in to comment.