diff --git a/src/main/java/uk/gov/hmcts/reform/civil/service/documentremoval/DocumentRemovalService.java b/src/main/java/uk/gov/hmcts/reform/civil/service/documentremoval/DocumentRemovalService.java index 554c69efe2d2..2ce77b2c9a32 100644 --- a/src/main/java/uk/gov/hmcts/reform/civil/service/documentremoval/DocumentRemovalService.java +++ b/src/main/java/uk/gov/hmcts/reform/civil/service/documentremoval/DocumentRemovalService.java @@ -14,6 +14,8 @@ import uk.gov.hmcts.reform.civil.model.documentremoval.DocumentToKeep; import uk.gov.hmcts.reform.civil.model.documentremoval.DocumentToKeepCollection; +import feign.FeignException; + import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Comparator; @@ -240,11 +242,15 @@ private void deleteDocument(DocumentToKeep documentToKeep, String authorisationT documentUrl)); documentManagementService.deleteDocument(authorisationToken, documentUrl); } catch (Exception e) { - log.error(format( - "Failed to delete document url %s", - documentUrl), e); - - throw new DocumentDeleteException(e.getMessage(), e); + if (e.getCause() instanceof FeignException.NotFound) { + log.warn("Document not found in CDAM for url {} - may have already been deleted. " + + "Continuing with case data removal.", documentUrl); + } else { + log.error(format( + "Failed to delete document url %s", + documentUrl), e); + throw new DocumentDeleteException(e.getMessage(), e); + } } } diff --git a/src/test/java/uk/gov/hmcts/reform/civil/service/documentremoval/DocumentRemovalServiceTest.java b/src/test/java/uk/gov/hmcts/reform/civil/service/documentremoval/DocumentRemovalServiceTest.java index 7890e8ed1149..248736478153 100644 --- a/src/test/java/uk/gov/hmcts/reform/civil/service/documentremoval/DocumentRemovalServiceTest.java +++ b/src/test/java/uk/gov/hmcts/reform/civil/service/documentremoval/DocumentRemovalServiceTest.java @@ -7,6 +7,9 @@ import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; +import feign.FeignException; +import feign.Request; +import feign.Request.HttpMethod; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -28,15 +31,23 @@ import uk.gov.hmcts.reform.civil.model.documents.DocumentWithName; import uk.gov.hmcts.reform.civil.sampledata.CaseDataBuilder; +import uk.gov.hmcts.reform.civil.documentmanagement.DocumentDownloadException; + +import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doThrow; import static uk.gov.hmcts.reform.civil.utils.ElementUtils.element; import static uk.gov.hmcts.reform.civil.utils.ElementUtils.wrapElements; @@ -473,5 +484,45 @@ void shouldFlagBundlesAsSystemGeneratedRemoveDocuments_TopLevelDoc() { assertNull(result.getCaseData().getDocumentToKeepCollection()); } + @Test + void shouldContinueWhenCdamReturns404ForDocument() { + FeignException.NotFound notFoundException = new FeignException.NotFound( + "not found", + Request.create(HttpMethod.DELETE, "", Map.of(), new byte[]{}, StandardCharsets.UTF_8, null), + "not found".getBytes(StandardCharsets.UTF_8), + Collections.emptyMap()); + + doThrow(new DocumentDownloadException("https://example1.com/123", notFoundException)) + .when(documentManagementService).deleteDocument(anyString(), anyString()); + + CaseData caseData = CaseData.builder() + .ccdCaseReference(CASE_ID) + .decisionOnReconsiderationDocument(buildCaseDocument( + "https://example1.com/123", "Decision.pdf", "https://example1.com/binary", null, "user")) + .documentToKeepCollection(List.of()) + .build(); + + DocumentRemovalCaseDataDTO result = documentRemovalService.removeDocuments(caseData, CASE_ID, "Auth"); + + assertNull(result.getCaseData().getDecisionOnReconsiderationDocument().getDocumentLink()); + assertNull(result.getCaseData().getDocumentToKeepCollection()); + } + + @Test + void shouldThrowWhenCdamReturnsNon404Error() { + doThrow(new DocumentDownloadException("https://example1.com/123", new RuntimeException("server error"))) + .when(documentManagementService).deleteDocument(anyString(), anyString()); + + CaseData caseData = CaseData.builder() + .ccdCaseReference(CASE_ID) + .decisionOnReconsiderationDocument(buildCaseDocument( + "https://example1.com/123", "Decision.pdf", "https://example1.com/binary", null, "user")) + .documentToKeepCollection(List.of()) + .build(); + + assertThrows(DocumentDeleteException.class, + () -> documentRemovalService.removeDocuments(caseData, CASE_ID, "Auth")); + } + } }