Skip to content

Commit

Permalink
FACT-2035 remove launchdarkly from send letter service [DO NOT MERGE] (
Browse files Browse the repository at this point in the history
…#2649)

* FACT-2035 remove launchdarkly

* removed ld stuff

* sonarqube fixes

* remove ld references
  • Loading branch information
justiceia authored Nov 1, 2024
1 parent 7e07075 commit 523d1f4
Show file tree
Hide file tree
Showing 16 changed files with 28 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class SendLetterProviderConfiguration {
@Bean
@Primary
public LetterChecksumService letterChecksumService() {
return new LetterChecksumService(launchDarklyClient());
return new LetterChecksumService();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void should_return_200_when_valid_json_is_sent_with_additionaldata_but_no_recipi
.header("ServiceAuthorization", "auth-header-value")
.contentType(MediaTypes.LETTER_V2)
.content(json)
).andExpect(status().isOk());
).andExpect(status().isBadRequest());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import uk.gov.hmcts.reform.sendletter.entity.Letter;
import uk.gov.hmcts.reform.sendletter.entity.LetterEventRepository;
import uk.gov.hmcts.reform.sendletter.entity.LetterRepository;
import uk.gov.hmcts.reform.sendletter.launchdarkly.LaunchDarklyClient;
import uk.gov.hmcts.reform.sendletter.model.in.LetterRequest;
import uk.gov.hmcts.reform.sendletter.services.ftp.ServiceFolderMapping;
import uk.gov.hmcts.reform.sendletter.services.pdf.DuplexPreparator;
Expand All @@ -37,7 +36,6 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
Expand All @@ -57,8 +55,7 @@ class LetterServiceTest {
private ExecusionService execusionService;
private DuplicateLetterService duplicateLetterService;
private ExceptionLetterService exceptionLetterService;
private final LaunchDarklyClient launchDarklyClient = mock(LaunchDarklyClient.class);
private final LetterChecksumService letterChecksumService = new LetterChecksumService(launchDarklyClient);
private final LetterChecksumService letterChecksumService = new LetterChecksumService();

@Autowired
private LetterRepository letterRepository;
Expand All @@ -77,7 +74,6 @@ void setUp() {
exceptionLetterService = mock(ExceptionLetterService.class);
BDDMockito.given(serviceFolderMapping.getFolderFor(any())).willReturn(Optional.of("some_folder_name"));
objectMapper = new ObjectMapper();
given(launchDarklyClient.isFeatureEnabled("FACT-1388")).willReturn(true);
service = new LetterService(
new PdfCreator(new DuplexPreparator(), new HTMLToPDFConverter()::convert),
letterRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
Expand All @@ -15,7 +14,6 @@
import uk.gov.hmcts.reform.sendletter.entity.Letter;
import uk.gov.hmcts.reform.sendletter.entity.LetterEventRepository;
import uk.gov.hmcts.reform.sendletter.entity.LetterRepository;
import uk.gov.hmcts.reform.sendletter.launchdarkly.LaunchDarklyClient;
import uk.gov.hmcts.reform.sendletter.model.in.LetterRequest;
import uk.gov.hmcts.reform.sendletter.services.encryption.PgpDecryptionHelper;
import uk.gov.hmcts.reform.sendletter.services.ftp.ServiceFolderMapping;
Expand All @@ -28,7 +26,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -54,13 +51,7 @@ class LetterServiceWithEncryptionEnabledTest {
@Mock
private DocumentService documentService;

private final LaunchDarklyClient launchDarklyClient = mock(LaunchDarklyClient.class);
private final LetterChecksumService letterChecksumService = new LetterChecksumService(launchDarklyClient);

@BeforeEach
void setUp() {
given(launchDarklyClient.isFeatureEnabled("FACT-1388")).willReturn(true);
}
private final LetterChecksumService letterChecksumService = new LetterChecksumService();

@AfterEach
void tearDown() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -72,7 +71,7 @@ class UploadLettersTaskTest {
private DocumentService documentService;

private final LaunchDarklyClient launchDarklyClient = mock(LaunchDarklyClient.class);
private final LetterChecksumService letterChecksumService = new LetterChecksumService(launchDarklyClient);
private final LetterChecksumService letterChecksumService = new LetterChecksumService();

@BeforeEach
void setUp() {
Expand All @@ -82,8 +81,6 @@ void setUp() {
DuplicateLetterService duplicateLetterService = mock(DuplicateLetterService.class);
ExceptionLetterService exceptionLetterService = mock(ExceptionLetterService.class);

given(launchDarklyClient.isFeatureEnabled("FACT-1388")).willReturn(true);

letterRepository.deleteAll();
this.letterService = new LetterService(
new PdfCreator(new DuplexPreparator(), new HTMLToPDFConverter()::convert),
Expand Down Expand Up @@ -111,7 +108,6 @@ void uploads_file_to_sftp_and_sets_letter_status_to_uploaded(String async) throw
availabilityChecker,
letterEventService,
serviceFolderMapping,
launchDarklyClient,
0
);

Expand Down Expand Up @@ -149,7 +145,6 @@ void should_fail_to_upload_to_sftp_and_stop_from_uploading_any_other_letters(Str
availabilityChecker,
letterEventService,
serviceFolderMapping,
launchDarklyClient,
0
);

Expand Down Expand Up @@ -186,7 +181,6 @@ void should_process_only_one_batch_of_files_in_single_run(String async) throws E
availabilityChecker,
letterEventService,
serviceFolderMapping,
launchDarklyClient,
0
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ private Flags() {
}

public static final String SEND_LETTER_SERVICE_TEST = "send-letter-service-test";
public static final String FACT_1593_INTERNATIONAL_POST_FLAG = "FACT-1593-international-post-flag";
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import jakarta.validation.ConstraintValidatorContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import uk.gov.hmcts.reform.sendletter.launchdarkly.LaunchDarklyClient;

import java.util.ArrayList;
import java.util.LinkedHashMap;
Expand All @@ -17,22 +15,6 @@
public class RecipientsValidator implements ConstraintValidator<ValidRecipients, Object> {

private static final Logger logger = LoggerFactory.getLogger(RecipientsValidator.class);
private final LaunchDarklyClient launchDarklyClient;

public RecipientsValidator() {
// for pact testing only - default is @Autowired constructor
launchDarklyClient = null;
}

/**
* Constructor for the RecipientsValidator.
*
* @param launchDarklyClient The LaunchDarklyClient
*/
@Autowired
public RecipientsValidator(LaunchDarklyClient launchDarklyClient) {
this.launchDarklyClient = launchDarklyClient;
}

/**
* Initializes the validator.
Expand All @@ -53,29 +35,23 @@ public void initialize(ValidRecipients constraintAnnotation) {
*/
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
// Toggle: FACT-1388 for making recipients field mandatory
if (launchDarklyClient != null && launchDarklyClient.isFeatureEnabled("FACT-1388")) {
if (value == null) {
logger.error("Additional_data field is null");
return false; // Skip validation if the value is null
}
if (value == null) {
logger.error("Additional_data field is null");
return false; // Skip validation if the value is null
}

// Check if the "recipients" field exists using reflection
try {
ArrayList recipients = (ArrayList) Optional.ofNullable(((LinkedHashMap) value).get("recipients"))
.orElseThrow(() -> new NoSuchFieldException("Recipients field is not present"));
if (recipients.isEmpty()) { // Check it is not an empty string as well
throw new NoSuchFieldException("Recipients field is empty");
}
logger.debug("Additional_data field is populated and recipients are: {}", recipients);
return true;
} catch (NoSuchFieldException e) {
logger.error(e.toString());
return false; // Field does not exist or is empty
// Check if the "recipients" field exists using reflection
try {
ArrayList recipients = (ArrayList) Optional.ofNullable(((LinkedHashMap) value).get("recipients"))
.orElseThrow(() -> new NoSuchFieldException("Recipients field is not present"));
if (recipients.isEmpty()) { // Check it is not an empty string as well
throw new NoSuchFieldException("Recipients field is empty");
}
} else {
logger.debug("toggle is turned off for FACT-1388, or LD Client is null");
logger.debug("Additional_data field is populated and recipients are: {}", recipients);
return true;
} catch (NoSuchFieldException e) {
logger.error(e.toString());
return false; // Field does not exist or is empty
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import uk.gov.hmcts.reform.sendletter.exception.ChecksumGenerationException;
import uk.gov.hmcts.reform.sendletter.launchdarkly.LaunchDarklyClient;
import uk.gov.hmcts.reform.sendletter.model.in.Doc;

import java.io.IOException;
Expand All @@ -22,16 +21,6 @@
@Service
public class LetterChecksumService {

private final LaunchDarklyClient launchDarklyClient;

/**
* Constructor for the LetterChecksumService.
* @param launchDarklyClient The client for launch darkly
*/
public LetterChecksumService(LaunchDarklyClient launchDarklyClient) {
this.launchDarklyClient = launchDarklyClient;
}

/**
* Creates a document for a PDF based upon the contents of each page.
* This is done because the entire PDF will have metadata against it. This means
Expand Down Expand Up @@ -70,13 +59,8 @@ public String calculateContentChecksum(byte[] pdfBytes) {
* @return the checksum
*/
public String generateChecksumForPdfPages(Object obj) {
return launchDarklyClient.isFeatureEnabled("FACT-1388")
? obj instanceof Doc
? calculateContentChecksum(((Doc) obj).content)
// If we have an old request for the old Document type: at present this is unavailable.
// This is unlikely to be hit as the client being used targets the latest version of the endpoint
: generateChecksum(obj)
// if toggle disabled, go to default check
return (obj instanceof Doc doc)
? calculateContentChecksum(doc.content)
: generateChecksum(obj);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import uk.gov.hmcts.reform.sendletter.entity.Letter;
import uk.gov.hmcts.reform.sendletter.entity.LetterRepository;
import uk.gov.hmcts.reform.sendletter.entity.LetterStatus;
import uk.gov.hmcts.reform.sendletter.launchdarkly.LaunchDarklyClient;
import uk.gov.hmcts.reform.sendletter.services.LetterEventService;
import uk.gov.hmcts.reform.sendletter.services.ftp.FileToSend;
import uk.gov.hmcts.reform.sendletter.services.ftp.FtpClient;
Expand All @@ -26,7 +25,6 @@
import java.util.Optional;

import static java.time.LocalDateTime.now;
import static uk.gov.hmcts.reform.sendletter.launchdarkly.Flags.FACT_1593_INTERNATIONAL_POST_FLAG;
import static uk.gov.hmcts.reform.sendletter.util.TimeZones.EUROPE_LONDON;

/**
Expand All @@ -47,7 +45,6 @@ public class UploadLettersTask {
private final IFtpAvailabilityChecker availabilityChecker;
private final LetterEventService letterEventService;
private final ServiceFolderMapping serviceFolderMapping;
private final LaunchDarklyClient launchDarklyClient;
private final int dbPollDelay;

/**
Expand All @@ -57,7 +54,6 @@ public class UploadLettersTask {
* @param availabilityChecker The FTP availability checker
* @param letterEventService The service for letter event
* @param serviceFolderMapping The service folder mapping
* @param launchDarklyClient The LaunchDarkly client
* @param dbPollDelay The database poll delay
*/
public UploadLettersTask(
Expand All @@ -66,15 +62,13 @@ public UploadLettersTask(
IFtpAvailabilityChecker availabilityChecker,
LetterEventService letterEventService,
ServiceFolderMapping serviceFolderMapping,
LaunchDarklyClient launchDarklyClient,
@Value("${tasks.upload-letters.db-poll-delay}") int dbPollDelay
) {
this.repo = repo;
this.ftp = ftp;
this.availabilityChecker = availabilityChecker;
this.letterEventService = letterEventService;
this.serviceFolderMapping = serviceFolderMapping;
this.launchDarklyClient = launchDarklyClient;
this.dbPollDelay = dbPollDelay;
}

Expand Down Expand Up @@ -150,12 +144,10 @@ private boolean processLetter(Letter letter, SFTPClient sftpClient) {

if (serviceFolder.isPresent()) {
String grabbedServiceFolder = serviceFolder.get();
if (launchDarklyClient.isFeatureEnabled(FACT_1593_INTERNATIONAL_POST_FLAG)) {
if (letter.getAdditionalData() != null
&& letter.getAdditionalData().has("isInternational")
&& letter.getAdditionalData().get("isInternational").asBoolean()) {
grabbedServiceFolder = serviceFolder.get() + INTERNATIONAL_FOLDER;
}
if (letter.getAdditionalData() != null
&& letter.getAdditionalData().has("isInternational")
&& letter.getAdditionalData().get("isInternational").asBoolean()) {
grabbedServiceFolder = serviceFolder.get() + INTERNATIONAL_FOLDER;
}
uploadLetter(letter, grabbedServiceFolder, sftpClient);
letter.setStatus(LetterStatus.Uploaded);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.gov.hmcts.reform.sendletter.controllers.sendlettercontroller;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -15,10 +14,8 @@
import uk.gov.hmcts.reform.sendletter.controllers.SendLetterController;
import uk.gov.hmcts.reform.sendletter.exception.ServiceNotConfiguredException;
import uk.gov.hmcts.reform.sendletter.exception.UnauthenticatedException;
import uk.gov.hmcts.reform.sendletter.launchdarkly.LaunchDarklyClient;
import uk.gov.hmcts.reform.sendletter.model.in.LetterRequest;
import uk.gov.hmcts.reform.sendletter.services.AuthService;
import uk.gov.hmcts.reform.sendletter.services.LetterChecksumService;
import uk.gov.hmcts.reform.sendletter.services.LetterService;

import java.util.List;
Expand Down Expand Up @@ -50,15 +47,6 @@ class SendLetterControllerTest {
private LetterService letterService;
@MockBean
private AuthService authService;
@MockBean
private LaunchDarklyClient launchDarklyClient;
@MockBean
private LetterChecksumService letterChecksumService;

@BeforeEach
public void beforeAll() {
given(launchDarklyClient.isFeatureEnabled("FACT-1388")).willReturn(true);
}

@ParameterizedTest
@ValueSource(strings = {"false", "true"})
Expand Down Expand Up @@ -117,17 +105,6 @@ void should_return_400_client_error_when_letter_is_sent_with_additional_data_no_
verify(letterService, never()).save(any(LetterRequest.class), anyString(), eq(async));
}

@ParameterizedTest
@ValueSource(strings = {"false", "true"})
void should_return_200_when_letter_is_sent_with_additional_data_no_recipients_but_toggle_off(String async)
throws Exception {
given(launchDarklyClient.isFeatureEnabled("FACT-1388")).willReturn(false);
sendLetter(readResource("controller/letter/v1/letter-additional-data-no-recipients.json"), async)
.andExpect(status().isOk());

verify(letterService, never()).save(any(LetterRequest.class), anyString(), eq(async));
}

@ParameterizedTest
@ValueSource(strings = {"false", "true"})
void should_return_400_client_error_when_letter_is_sent_with_additional_data_empty_recipients(String async)
Expand Down
Loading

0 comments on commit 523d1f4

Please sign in to comment.