Skip to content
Open
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
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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"));
}

}
}
Loading