From 7d3895d510fe310e0e6e0fb664868a5b17a2dcc8 Mon Sep 17 00:00:00 2001 From: Hanna Prinz Date: Wed, 27 Sep 2023 11:49:06 +0200 Subject: [PATCH] Delete linked active citations on docunit deletion RISDEV-2502 --- .../caselaw/domain/DocumentUnitService.java | 24 ++++- .../domain/DocumentUnitServiceTest.java | 102 ++++++++++++++++++ 2 files changed, 124 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/de/bund/digitalservice/ris/caselaw/domain/DocumentUnitService.java b/backend/src/main/java/de/bund/digitalservice/ris/caselaw/domain/DocumentUnitService.java index dc89f34914..a7792230c5 100644 --- a/backend/src/main/java/de/bund/digitalservice/ris/caselaw/domain/DocumentUnitService.java +++ b/backend/src/main/java/de/bund/digitalservice/ris/caselaw/domain/DocumentUnitService.java @@ -253,6 +253,12 @@ public Mono deleteByUuid(UUID documentUnitUuid) { ? Flux.empty() : Flux.fromIterable(documentUnit.proceedingDecisions()); + Flux activeCitations = + documentUnit.contentRelatedIndexing() == null + || documentUnit.contentRelatedIndexing().activeCitations() == null + ? Flux.empty() + : Flux.fromIterable(documentUnit.contentRelatedIndexing().activeCitations()); + String logMsg = "Dokumentationseinheit gelöscht: " + documentUnitUuid @@ -261,7 +267,14 @@ public Mono deleteByUuid(UUID documentUnitUuid) { ? "" : ", zudem die Verknüpfungen mit " + documentUnit.proceedingDecisions().size() - + " vorgehenden Entscheidungen"); + + " vorgehenden Entscheidungen") + + (documentUnit.contentRelatedIndexing() == null + || documentUnit.contentRelatedIndexing().activeCitations() == null + || documentUnit.contentRelatedIndexing().activeCitations().isEmpty() + ? "" + : ", zudem die Verknüpfungen mit " + + documentUnit.contentRelatedIndexing().activeCitations().size() + + " Aktivzitierungen"); return deleteAttachedFile .thenMany( @@ -271,6 +284,13 @@ public Mono deleteByUuid(UUID documentUnitUuid) { documentUnitUuid, proceedingDecision.getUuid(), DocumentationUnitLinkType.PREVIOUS_DECISION))) + .thenMany( + activeCitations.flatMap( + activeCitation -> + removeLinkedDocumentationUnit( + documentUnitUuid, + activeCitation.getUuid(), + DocumentationUnitLinkType.ACTIVE_CITATION))) .then(repository.delete(documentUnit)) .thenReturn(logMsg); }) @@ -281,7 +301,7 @@ public Mono deleteByUuid(UUID documentUnitUuid) { return Mono.error( new DocumentUnitDeletionException( "die Dokumentationseinheit konnte nicht gelöscht werden, " - + "da sie eine vorgehende Entscheidung für " + + "da sie eine vorgehende Entscheidung oder Aktivzitierung für " + documentUnitsThisOneIsAChildOf.get() + " andere Dokumentationseinheiten darstellt")); } diff --git a/backend/src/test/java/de/bund/digitalservice/ris/caselaw/domain/DocumentUnitServiceTest.java b/backend/src/test/java/de/bund/digitalservice/ris/caselaw/domain/DocumentUnitServiceTest.java index ed525d179e..96a1164ec4 100644 --- a/backend/src/test/java/de/bund/digitalservice/ris/caselaw/domain/DocumentUnitServiceTest.java +++ b/backend/src/test/java/de/bund/digitalservice/ris/caselaw/domain/DocumentUnitServiceTest.java @@ -226,6 +226,108 @@ void testDeleteByUuid_withoutFileAttached() { verify(s3AsyncClient, times(0)).deleteObject(any(DeleteObjectRequest.class)); } + @Test + void testDeleteByUuid_withProceedingDecisions() { + var pdUuid = UUID.randomUUID(); + DocumentUnit documentUnit = + DocumentUnit.builder() + .uuid(TEST_UUID) + .proceedingDecisions( + List.of( + ProceedingDecision.builder() + .uuid(pdUuid) + .dataSource(DataSource.PROCEEDING_DECISION) + .build())) + .build(); + when(repository.countLinksByChildDocumentUnitUuid(TEST_UUID)).thenReturn(Mono.just(1L)); + when(repository.findByUuid(TEST_UUID)).thenReturn(Mono.just(documentUnit)); + when(repository.deleteIfOrphanedLinkedDocumentationUnit(pdUuid)).thenReturn(Mono.empty()); + when(repository.unlinkDocumentUnit( + TEST_UUID, pdUuid, DocumentationUnitLinkType.PREVIOUS_DECISION)) + .thenReturn(Mono.empty()); + + when(repository.findAllLinkedDocumentUnitsByParentDocumentUnitUuidAndType( + TEST_UUID, DocumentationUnitLinkType.ACTIVE_CITATION)) + .thenReturn(Flux.empty()); + when(repository.findAllLinkedDocumentUnitsByParentDocumentUnitUuidAndType( + TEST_UUID, DocumentationUnitLinkType.PREVIOUS_DECISION)) + .thenReturn( + Flux.just( + LinkedDocumentationUnit.builder() + .uuid(pdUuid) + .dataSource(DataSource.PROCEEDING_DECISION) + .build())); + when(repository.delete(any(DocumentUnit.class))).thenReturn(Mono.just(mock(Void.class))); + + StepVerifier.create(service.deleteByUuid(TEST_UUID)) + .consumeNextWith( + string -> { + assertNotNull(string); + assertEquals( + "Dokumentationseinheit gelöscht: " + + TEST_UUID + + ", zudem die Verknüpfungen mit 1 vorgehenden Entscheidungen", + string); + }) + .verifyComplete(); + + verify(repository) + .unlinkDocumentUnit(TEST_UUID, pdUuid, DocumentationUnitLinkType.PREVIOUS_DECISION); + verify(repository).deleteIfOrphanedLinkedDocumentationUnit(pdUuid); + } + + @Test + void testDeleteByUuid_withActiveCitations() { + var acUuid = UUID.randomUUID(); + DocumentUnit documentUnit = + DocumentUnit.builder() + .uuid(TEST_UUID) + .contentRelatedIndexing( + ContentRelatedIndexing.builder() + .activeCitations( + List.of( + ActiveCitation.builder() + .uuid(acUuid) + .dataSource(DataSource.ACTIVE_CITATION) + .build())) + .build()) + .build(); + when(repository.countLinksByChildDocumentUnitUuid(TEST_UUID)).thenReturn(Mono.just(1L)); + when(repository.findByUuid(TEST_UUID)).thenReturn(Mono.just(documentUnit)); + when(repository.deleteIfOrphanedLinkedDocumentationUnit(acUuid)).thenReturn(Mono.empty()); + when(repository.unlinkDocumentUnit( + TEST_UUID, acUuid, DocumentationUnitLinkType.ACTIVE_CITATION)) + .thenReturn(Mono.empty()); + when(repository.findAllLinkedDocumentUnitsByParentDocumentUnitUuidAndType( + TEST_UUID, DocumentationUnitLinkType.PREVIOUS_DECISION)) + .thenReturn(Flux.empty()); + when(repository.findAllLinkedDocumentUnitsByParentDocumentUnitUuidAndType( + TEST_UUID, DocumentationUnitLinkType.ACTIVE_CITATION)) + .thenReturn( + Flux.just( + LinkedDocumentationUnit.builder() + .uuid(acUuid) + .dataSource(DataSource.ACTIVE_CITATION) + .build())); + when(repository.delete(any(DocumentUnit.class))).thenReturn(Mono.just(mock(Void.class))); + + StepVerifier.create(service.deleteByUuid(TEST_UUID)) + .consumeNextWith( + string -> { + assertNotNull(string); + assertEquals( + "Dokumentationseinheit gelöscht: " + + TEST_UUID + + ", zudem die Verknüpfungen mit 1 Aktivzitierungen", + string); + }) + .verifyComplete(); + + verify(repository) + .unlinkDocumentUnit(TEST_UUID, acUuid, DocumentationUnitLinkType.ACTIVE_CITATION); + verify(repository).deleteIfOrphanedLinkedDocumentationUnit(acUuid); + } + @Test void testDeleteByUuid_withFileAttached() { DocumentUnit documentUnit =