From 01ae14522d84cc3c706488fbc2387efee953caa4 Mon Sep 17 00:00:00 2001 From: Andrei Isvoran Date: Fri, 12 Jan 2024 08:23:22 +0100 Subject: [PATCH] RED-8173 - Fix for removing and adding redaction in the same place --- .../document/ManualEntityCreationService.java | 8 ++- .../v1/server/RedactionIntegrationTest.java | 52 ++++++++++++++----- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/ManualEntityCreationService.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/ManualEntityCreationService.java index 85e3ee4d..cd070686 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/ManualEntityCreationService.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/ManualEntityCreationService.java @@ -51,12 +51,10 @@ public class ManualEntityCreationService { SemanticNode node, String dossierTemplateId) { - Set manualRedactionEntries = manualRedactions.getEntriesToAdd(); Set idRemovals = manualRedactions.getIdsToRemove(); - if (!manualRedactionEntries.isEmpty() && !idRemovals.isEmpty() && manualRedactionEntries.size() != idRemovals.size()) { - manualRedactionEntries.removeIf(manualRedactionEntry -> idRemovals.stream().map(BaseAnnotation::getAnnotationId).toList().contains(manualRedactionEntry.getAnnotationId())); - } - List manualEntities = manualRedactionEntries.stream() + List manualEntities = manualRedactions.getEntriesToAdd().stream() + .filter(manualRedactionEntry -> !(idRemovals.stream().map(BaseAnnotation::getAnnotationId).toList().contains(manualRedactionEntry.getAnnotationId()) && + manualRedactionEntry.getRequestDate().isBefore(idRemovals.stream().filter(idRemoval -> idRemoval.getAnnotationId().equals(manualRedactionEntry.getAnnotationId())).findFirst().get().getRequestDate()))) .filter(manualRedactionEntry -> !(manualRedactionEntry.isAddToDictionary() || manualRedactionEntry.isAddToDossierDictionary())) .map(manualRedactionEntry -> ManualEntity.fromManualRedactionEntry(manualRedactionEntry, dictionaryService.isHint(manualRedactionEntry.getType(), dossierTemplateId))).peek(manualEntity -> { diff --git a/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/RedactionIntegrationTest.java b/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/RedactionIntegrationTest.java index c0006e54..1ee51f15 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/RedactionIntegrationTest.java +++ b/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/RedactionIntegrationTest.java @@ -1190,7 +1190,6 @@ public class RedactionIntegrationTest extends AbstractRedactionIntegrationTest { public void testRemovingAndAddingRedactionOnTheSameValue() { String pdfFile = "files/new/test1S1T1.pdf"; - ManualRedactions manualRedactions = new ManualRedactions(); String manualAddId = UUID.randomUUID().toString(); String manualAddId2 = UUID.randomUUID().toString(); @@ -1198,9 +1197,11 @@ public class RedactionIntegrationTest extends AbstractRedactionIntegrationTest { ManualRedactionEntry manualRedactionEntry = getManualRedactionEntry(manualAddId, positions, "the manufacturing or production process, including the method and innovative aspects thereof, as well as other technical and industrial specifications inherent to that process or method, except for information which is relevant to the assessment of safety"); ManualRedactionEntry manualRedactionEntry2 = getManualRedactionEntry(manualAddId2, positions, "commercial information revealing sourcing, market shares or business strategy of the applicant"); - manualRedactions.getEntriesToAdd().add(manualRedactionEntry); + IdRemoval idRemoval = getIdRemoval(manualAddId); + IdRemoval idRemoval2 = getIdRemoval(manualAddId2); + AnalyzeRequest request = uploadFileToStorage(pdfFile); - request.setManualRedactions(manualRedactions); + request.setManualRedactions(ManualRedactions.builder().entriesToAdd(Set.of(manualRedactionEntry)).build()); analyzeDocumentStructure(LayoutParsingType.REDACT_MANAGER, request); analyzeService.analyze(request); @@ -1209,14 +1210,7 @@ public class RedactionIntegrationTest extends AbstractRedactionIntegrationTest { assertTrue(entityLog.getEntityLogEntry().stream().anyMatch(entityLogEntry -> entityLogEntry.getId().equals(manualAddId))); assertEquals(entityLog.getEntityLogEntry().stream().filter(entityLogEntry -> entityLogEntry.getId().equals(manualAddId)).findFirst().get().getState(), EntryState.APPLIED); - manualRedactions.getIdsToRemove().add(IdRemoval.builder() - .annotationId(manualAddId) - .removeFromAllDossiers(false) - .removeFromDictionary(false) - .status(AnnotationStatus.APPROVED) - .requestDate(OffsetDateTime.now()) - .build()); - request.setManualRedactions(manualRedactions); + request.setManualRedactions(ManualRedactions.builder().entriesToAdd(Set.of(manualRedactionEntry)).idsToRemove(Set.of(idRemoval)).build()); analyzeService.reanalyze(request); entityLog = redactionStorageService.getEntityLog(TEST_DOSSIER_ID, TEST_FILE_ID); @@ -1224,8 +1218,7 @@ public class RedactionIntegrationTest extends AbstractRedactionIntegrationTest { assertTrue(entityLog.getEntityLogEntry().stream().anyMatch(entityLogEntry -> entityLogEntry.getId().equals(manualAddId))); assertEquals(entityLog.getEntityLogEntry().stream().filter(entityLogEntry -> entityLogEntry.getId().equals(manualAddId)).findFirst().get().getState(), EntryState.REMOVED); - manualRedactions.getEntriesToAdd().add(manualRedactionEntry2); - request.setManualRedactions(manualRedactions); + request.setManualRedactions(ManualRedactions.builder().entriesToAdd(Set.of(manualRedactionEntry, manualRedactionEntry2)).idsToRemove(Set.of(idRemoval)).build()); analyzeService.reanalyze(request); entityLog = redactionStorageService.getEntityLog(TEST_DOSSIER_ID, TEST_FILE_ID); @@ -1234,6 +1227,39 @@ public class RedactionIntegrationTest extends AbstractRedactionIntegrationTest { assertEquals(entityLog.getEntityLogEntry().stream().filter(entityLogEntry -> entityLogEntry.getId().equals(manualAddId)).findFirst().get().getState(), EntryState.REMOVED); assertTrue(entityLog.getEntityLogEntry().stream().anyMatch(entityLogEntry -> entityLogEntry.getId().equals(manualAddId2))); assertEquals(entityLog.getEntityLogEntry().stream().filter(entityLogEntry -> entityLogEntry.getId().equals(manualAddId2)).findFirst().get().getState(), EntryState.APPLIED); + + request.setManualRedactions(ManualRedactions.builder().entriesToAdd(Set.of(manualRedactionEntry, manualRedactionEntry2)).idsToRemove(Set.of(idRemoval, idRemoval2)).build()); + analyzeService.reanalyze(request); + + entityLog = redactionStorageService.getEntityLog(TEST_DOSSIER_ID, TEST_FILE_ID); + + assertTrue(entityLog.getEntityLogEntry().stream().anyMatch(entityLogEntry -> entityLogEntry.getId().equals(manualAddId))); + assertEquals(entityLog.getEntityLogEntry().stream().filter(entityLogEntry -> entityLogEntry.getId().equals(manualAddId)).findFirst().get().getState(), EntryState.REMOVED); + assertTrue(entityLog.getEntityLogEntry().stream().anyMatch(entityLogEntry -> entityLogEntry.getId().equals(manualAddId2))); + assertEquals(entityLog.getEntityLogEntry().stream().filter(entityLogEntry -> entityLogEntry.getId().equals(manualAddId2)).findFirst().get().getState(), EntryState.REMOVED); + + manualRedactionEntry.setRequestDate(OffsetDateTime.now()); + request.setManualRedactions(ManualRedactions.builder().entriesToAdd(Set.of(manualRedactionEntry, manualRedactionEntry2)).idsToRemove(Set.of(idRemoval, idRemoval2)).build()); + analyzeService.reanalyze(request); + + entityLog = redactionStorageService.getEntityLog(TEST_DOSSIER_ID, TEST_FILE_ID); + + assertTrue(entityLog.getEntityLogEntry().stream().anyMatch(entityLogEntry -> entityLogEntry.getId().equals(manualAddId))); + assertEquals(entityLog.getEntityLogEntry().stream().filter(entityLogEntry -> entityLogEntry.getId().equals(manualAddId)).findFirst().get().getState(), EntryState.APPLIED); + assertTrue(entityLog.getEntityLogEntry().stream().anyMatch(entityLogEntry -> entityLogEntry.getId().equals(manualAddId2))); + assertEquals(entityLog.getEntityLogEntry().stream().filter(entityLogEntry -> entityLogEntry.getId().equals(manualAddId2)).findFirst().get().getState(), EntryState.REMOVED); + } + + + private IdRemoval getIdRemoval(String id) { + + return IdRemoval.builder() + .annotationId(id) + .removeFromAllDossiers(false) + .removeFromDictionary(false) + .status(AnnotationStatus.APPROVED) + .requestDate(OffsetDateTime.now()) + .build(); }