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

DFPL 2512 Cafcass API Feature toggling by region (court) #5558

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package uk.gov.hmcts.reform.fpl.model.cafcass.api;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.util.List;

@Getter
@Builder
@EqualsAndHashCode
public class CafcassApiFeatureFlag {
private boolean enableApi;
private List<String> whitelist;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import uk.gov.hmcts.reform.fpl.model.Court;
import uk.gov.hmcts.reform.fpl.model.cafcass.api.CafcassApiFeatureFlag;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.apache.commons.lang3.ObjectUtils.isEmpty;

@Service
public class FeatureToggleService {
Expand Down Expand Up @@ -100,6 +105,33 @@ public boolean isCourtNotificationEnabledForWa(Court court) {
createLDUser(Map.of(COURT_CODE_KEY, LDValue.of(court.getCode()))), true);
}

public boolean isCafcassAPIEnabledForCourt(Court court) {
CafcassApiFeatureFlag flag = getCafcassAPIFlag();

if (flag.isEnableApi()) {
if (isEmpty(flag.getWhitelist())) {
return true;
} else {
return flag.getWhitelist().stream()
.anyMatch(whiteListCode -> court.getCode().equalsIgnoreCase(whiteListCode));
}
}
return false;
}

public CafcassApiFeatureFlag getCafcassAPIFlag() {
LDValue flag = ldClient.jsonValueVariation("cafcass-api-court", createLDUser(), LDValue.ofNull());

LDValue whiteList = flag.get("whitelist");
return CafcassApiFeatureFlag.builder()
.enableApi(flag.get("enableApi").booleanValue())
.whitelist((!whiteList.isNull())
? StreamSupport.stream(whiteList.valuesAs(LDValue.Convert.String).spliterator(), false)
.collect(Collectors.toList())
: null)
.build();
}

private LDUser createLDUser() {
return createLDUser(Map.of());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ public void sendEmail(CaseData caseData,
Set<DocumentReference> documentReferences,
CafcassRequestEmailContentProvider provider,
CafcassData cafcassData) {
if (featureToggleService.isCafcassAPIEnabledForCourt(caseData.getCourt())) {
log.info("For case id: {} Cafcass API is enabled, skip notifying Cafcass for: {}",
caseData.getId(),
provider.name());
return;
}

log.info("For case id: {} notifying Cafcass for: {}",
caseData.getId(),
provider.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@
import uk.gov.hmcts.reform.fpl.model.CaseData;
import uk.gov.hmcts.reform.fpl.model.cafcass.api.CafcassApiCase;
import uk.gov.hmcts.reform.fpl.model.cafcass.api.CafcassApiCaseData;
import uk.gov.hmcts.reform.fpl.model.cafcass.api.CafcassApiFeatureFlag;
import uk.gov.hmcts.reform.fpl.service.CaseConverter;
import uk.gov.hmcts.reform.fpl.service.FeatureToggleService;
import uk.gov.hmcts.reform.fpl.service.search.SearchService;
import uk.gov.hmcts.reform.fpl.utils.elasticsearch.BooleanQuery;
import uk.gov.hmcts.reform.fpl.utils.elasticsearch.Filter;
import uk.gov.hmcts.reform.fpl.utils.elasticsearch.MatchQuery;
import uk.gov.hmcts.reform.fpl.utils.elasticsearch.Must;
import uk.gov.hmcts.reform.fpl.utils.elasticsearch.MustNot;
import uk.gov.hmcts.reform.fpl.utils.elasticsearch.RangeQuery;
import uk.gov.hmcts.reform.fpl.utils.elasticsearch.TermsQuery;

import java.time.LocalDateTime;
import java.util.List;

import static org.apache.commons.lang3.ObjectUtils.isNotEmpty;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
Expand All @@ -33,25 +39,37 @@ public class CafcassApiSearchCaseService {
private final CaseConverter caseConverter;
private final SearchService searchService;
private final List<CafcassApiCaseDataConverter> cafcassApiCaseDataConverters;
private final FeatureToggleService featureToggleService;

public List<CafcassApiCase> searchCaseByDateRange(LocalDateTime startDate, LocalDateTime endDate) {
final RangeQuery searchRange = RangeQuery.builder()
.field("last_modified")
.greaterThanOrEqual(startDate)
.lessThanOrEqual(endDate).build();
CafcassApiFeatureFlag flag = featureToggleService.getCafcassAPIFlag();

final BooleanQuery searchCaseQuery = BooleanQuery.builder()
.mustNot(CASE_STATES)
.filter(Filter.builder()
.clauses(List.of(searchRange))
.build())
.build();
if (flag.isEnableApi()) {
final RangeQuery searchRange = RangeQuery.builder()
.field("last_modified")
.greaterThanOrEqual(startDate)
.lessThanOrEqual(endDate).build();

final BooleanQuery.BooleanQueryBuilder searchCaseQuery = BooleanQuery.builder()
.mustNot(CASE_STATES)
.filter(Filter.builder()
.clauses(List.of(searchRange))
.build());

List<CaseDetails> caseDetails = searchService.search(searchCaseQuery, 10000, 0);
if (isNotEmpty(flag.getWhitelist())) {
searchCaseQuery.must(Must.builder()
.clauses(List.of(TermsQuery.of("data.court.code", flag.getWhitelist())))
.build());
}

return caseDetails.stream()
.map(this::convertToCafcassApiCase)
.toList();
List<CaseDetails> caseDetails = searchService.search(searchCaseQuery.build(), 10000, 0);

return caseDetails.stream()
.map(this::convertToCafcassApiCase)
.toList();
} else {
return List.of();
}
}

private CafcassApiCase convertToCafcassApiCase(CaseDetails caseDetails) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package uk.gov.hmcts.reform.fpl.service;

import com.launchdarkly.sdk.LDUser;
import com.launchdarkly.sdk.LDValue;
import com.launchdarkly.sdk.UserAttribute;
import com.launchdarkly.sdk.server.LDClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import uk.gov.hmcts.reform.fpl.model.Court;
import uk.gov.hmcts.reform.fpl.model.cafcass.api.CafcassApiFeatureFlag;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -172,6 +176,95 @@ void shouldMakeCorrectCallForCourtNotificationEnabledForWa(Boolean toggleState)
eq(true));
}

@Nested
class CafcassAPIFeatureFlag {
private static final LDValue CAFCASS_API_ENABLED =
LDValue.parse("{\"enableApi\":true}");
private static final LDValue CAFCASS_API_DISABLED =
LDValue.parse("{\"enableApi\":false}");
private static final LDValue CAFCASS_API_WHITELISTED =
LDValue.parse("{\"enableApi\": true, \"whitelist\": [\"151\", \"111\"]}");

@Test
void shouldReturnCafcassApiFeatureFlagIfEnabled() {
when(ldClient.jsonValueVariation(any(), any(), any()))
.thenReturn(CAFCASS_API_ENABLED);

assertThat(service.getCafcassAPIFlag())
.isEqualTo(CafcassApiFeatureFlag.builder().enableApi(true).build());

verify(ldClient).jsonValueVariation(
eq("cafcass-api-court"),
argThat(ldUser(ENVIRONMENT).build()),
eq(LDValue.ofNull()));
}

@Test
void shouldReturnCafcassApiFeatureFlagIfDisabled() {
when(ldClient.jsonValueVariation(eq("cafcass-api-court"), any(), any()))
.thenReturn(CAFCASS_API_DISABLED);

assertThat(service.getCafcassAPIFlag())
.isEqualTo(CafcassApiFeatureFlag.builder().enableApi(false).build());

verify(ldClient).jsonValueVariation(
eq("cafcass-api-court"),
argThat(ldUser(ENVIRONMENT).build()),
eq(LDValue.ofNull()));
}

@Test
void shouldReturnCafcassApiFeatureFlagIfWhiteListed() {
when(ldClient.jsonValueVariation(eq("cafcass-api-court"), any(), any()))
.thenReturn(CAFCASS_API_WHITELISTED);

assertThat(service.getCafcassAPIFlag())
.isEqualTo(CafcassApiFeatureFlag.builder().enableApi(true)
.whitelist(List.of("151", "111")).build());

verify(ldClient).jsonValueVariation(
eq("cafcass-api-court"),
argThat(ldUser(ENVIRONMENT).build()),
eq(LDValue.ofNull()));
}

@Test
void shouldReturnTrueIfCourtInTheWhitelist() {
when(ldClient.jsonValueVariation(eq("cafcass-api-court"), any(), any()))
.thenReturn(CAFCASS_API_WHITELISTED);

assertThat(service.isCafcassAPIEnabledForCourt(Court.builder().code("151").build()))
.isEqualTo(true);
}

@Test
void shouldReturnTrueIfEnabledToAllCourt() {
when(ldClient.jsonValueVariation(eq("cafcass-api-court"), any(), any()))
.thenReturn(CAFCASS_API_ENABLED);

assertThat(service.isCafcassAPIEnabledForCourt(Court.builder().code("151").build()))
.isEqualTo(true);
}

@Test
void shouldReturnFalseIfCourtNotInTheWhitelist() {
when(ldClient.jsonValueVariation(eq("cafcass-api-court"), any(), any()))
.thenReturn(CAFCASS_API_WHITELISTED);

assertThat(service.isCafcassAPIEnabledForCourt(Court.builder().code("987").build()))
.isEqualTo(false);
}

@Test
void shouldReturnFalseIfAPIDisabled() {
when(ldClient.jsonValueVariation(eq("cafcass-api-court"), any(), any()))
.thenReturn(CAFCASS_API_DISABLED);

assertThat(service.isCafcassAPIEnabledForCourt(Court.builder().code("151").build()))
.isEqualTo(false);
}
}

private static List<UserAttribute> buildAttributes(String... additionalAttributes) {
List<UserAttribute> attributes = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.quality.Strictness.LENIENT;
import static uk.gov.hmcts.reform.fpl.enums.HearingType.CASE_MANAGEMENT;
Expand Down Expand Up @@ -156,6 +157,7 @@ void setUp() {
entry("newApplication", "APPLICATION")

));
when(featureToggleService.isCafcassAPIEnabledForCourt(any())).thenReturn(false);
}

@ParameterizedTest
Expand Down Expand Up @@ -762,6 +764,26 @@ void shouldNotifyWithAttachmentAndLinkWhenThereIsSmallAndLargeDocs(boolean flag,
largeDocMessage));
}

@Test
void shouldNotNotifyIfAPIEnabled() {
when(featureToggleService.isCafcassAPIEnabledForCourt(any())).thenReturn(true);

underTest.sendEmail(caseData,
of(getDocumentReference()),
COURT_BUNDLE,
CourtBundleData.builder()
.hearingDetails(TITLE)
.build()
);

underTest.sendEmail(caseData,
CHANGE_OF_ADDRESS,
ChangeOfAddressData.builder().children(true).build()
);

verifyNoInteractions(emailService);
}

private DocumentReference getDocumentReference() {
return DocumentReference.builder().binaryUrl(DOCUMENT_BINARY_URL)
.url(DOCUMENT_URL)
Expand Down
Loading
Loading