From 69566a2614412f5a74c15a44c56a6b0c2e145d39 Mon Sep 17 00:00:00 2001 From: Corina Olariu Date: Fri, 7 Jul 2023 09:54:09 +0300 Subject: [PATCH 1/3] RED-7034 - Check for dossierDictionaryOnly flag when add to template dict is requested - before adding / deleting entries to a dossier template dictionary check if the flag dossierDictionaryOnly is set true - the same check is needed in case of manual redactions ; adding, removing - add junit tests --- .../service/DictionaryManagementService.java | 15 ++++ .../processor/service/DictionaryService.java | 11 ++- .../service/ManualRedactionService.java | 47 +++++++---- .../integration/service/TypeProvider.java | 9 ++- .../integration/tests/DictionaryTest.java | 43 ++++++++-- .../tests/ManualRedactionTest.java | 78 ++++++++++++++++--- 6 files changed, 162 insertions(+), 41 deletions(-) diff --git a/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/DictionaryManagementService.java b/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/DictionaryManagementService.java index e8480c70e..db4282d8f 100644 --- a/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/DictionaryManagementService.java +++ b/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/DictionaryManagementService.java @@ -25,6 +25,7 @@ import com.iqser.red.service.persistence.management.v1.processor.entity.configur import com.iqser.red.service.persistence.management.v1.processor.entity.configuration.TypeEntity; import com.iqser.red.service.persistence.management.v1.processor.exception.BadRequestException; import com.iqser.red.service.persistence.management.v1.processor.exception.ConflictException; +import com.iqser.red.service.persistence.management.v1.processor.exception.NotAllowedException; import com.iqser.red.service.persistence.management.v1.processor.exception.NotFoundException; import com.iqser.red.service.persistence.management.v1.processor.service.persistence.DictionaryPersistenceService; import com.iqser.red.service.persistence.management.v1.processor.service.persistence.EntryPersistenceService; @@ -379,4 +380,18 @@ public class DictionaryManagementService { return currentVersion; } + public void validateAddRemoveToDossierTemplateDictionary(String dossierTemplateTypeId) { + this.validateAddRemoveToDossierTemplateDictionary(dossierTemplateTypeId, true, true); + } + + public void validateAddRemoveToDossierTemplateDictionary(String dossierTemplateTypeId, boolean isAddRemoveToDictionary, boolean isAddRemoveToAllDossiers) { + + var type = dictionaryPersistenceService.getType(dossierTemplateTypeId); + + if (isAddRemoveToDictionary && isAddRemoveToAllDossiers && type.isDossierDictionaryOnly()) { + throw new NotAllowedException("Adding / removing entry to dossier template dictionary is not allowed for this type: " + dossierTemplateTypeId); + } + + } + } diff --git a/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/DictionaryService.java b/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/DictionaryService.java index 5ee30a693..2233248d4 100644 --- a/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/DictionaryService.java +++ b/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/DictionaryService.java @@ -67,13 +67,15 @@ public class DictionaryService { @PreAuthorize("hasAuthority('" + ADD_DICTIONARY_ENTRY + "')") public void addGlobalEntries(String type, String dossierTemplateId, List entries, boolean removeCurrent, DictionaryEntryType dictionaryEntryType) { - addEntries(toTypeId(type, dossierTemplateId), entries, removeCurrent, dictionaryEntryType); + var typeId = toTypeId(type, dossierTemplateId); + dictionaryManagementService.validateAddRemoveToDossierTemplateDictionary(typeId); + addEntries(typeId, entries, removeCurrent, dictionaryEntryType); } - public void addEntries(String type, List entries, boolean removeCurrent, DictionaryEntryType dictionaryEntryType) { + public void addEntries(String typeId, List entries, boolean removeCurrent, DictionaryEntryType dictionaryEntryType) { - dictionaryManagementService.addEntries(type, entries, removeCurrent, false, dictionaryEntryType); + dictionaryManagementService.addEntries(typeId, entries, removeCurrent, false, dictionaryEntryType); } @@ -87,6 +89,8 @@ public class DictionaryService { @PreAuthorize("hasAuthority('" + DELETE_DICTIONARY_ENTRY + "')") public void deleteGlobalEntries(String type, String dossierTemplateId, List entries, DictionaryEntryType dictionaryEntryType) { + var typeId = toTypeId(type, dossierTemplateId); + dictionaryManagementService.validateAddRemoveToDossierTemplateDictionary(typeId); deleteEntries(toTypeId(type, dossierTemplateId), entries, dictionaryEntryType); } @@ -419,4 +423,5 @@ public class DictionaryService { return MagicConverter.convert(colorsService.getColors(dossierTemplateId), Colors.class); } + } diff --git a/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/ManualRedactionService.java b/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/ManualRedactionService.java index b50d21979..982e966f4 100644 --- a/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/ManualRedactionService.java +++ b/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/ManualRedactionService.java @@ -15,7 +15,6 @@ import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.hash.HashFunction; import com.google.common.hash.Hashing; @@ -100,6 +99,8 @@ public class ManualRedactionService { dossierPersistenceService.getAndValidateDossier(dossierId); var actionPerformed = false; + // validate add to dossier template dictionaries + addRedactionRequests.forEach(request -> dictionaryService.validateAddRemoveToDossierTemplateDictionary(request.getDossierTemplateTypeId(), request.isAddToDictionary(), request.isAddToAllDossiers())); for (var addRedactionRequest : addRedactionRequests) { if (addRedactionRequest.isAddToDictionary()) { @@ -279,13 +280,24 @@ public class ManualRedactionService { public List addRemoveRedaction(String dossierId, String fileId, List removeRedactionRequests) { - RedactionLog redactionLog = null; + var response = new ArrayList(); var dossier = dossierPersistenceService.getAndValidateDossier(dossierId); + RedactionLog redactionLog = fileManagementStorageService.getRedactionLog(dossier.getId(), fileId); + var actionPerformed = false; var manualRedactions = manualRedactionProviderService.getManualRedactions(fileId); + //validate removing from dossier template dictionary + removeRedactionRequests.forEach(request -> { + if (request.isRemoveFromDictionary()) { + RedactionLogEntry redactionLogEntry = getRedactionLogEntry(redactionLog, request.getAnnotationId()); + var dossierTemplateTypeId = toTypeId(redactionLogEntry.getType(), dossier.getDossierTemplateId()); + dictionaryService.validateAddRemoveToDossierTemplateDictionary(dossierTemplateTypeId, request.isRemoveFromDictionary(), request.isRemoveFromAllDossiers()); + } + }); + for (var removeRedactionRequest : removeRedactionRequests) { if (manualAddRedactionsContains(manualRedactions, removeRedactionRequest.getAnnotationId()) && AnnotationStatus.APPROVED.equals(removeRedactionRequest.getStatus())) { @@ -296,10 +308,6 @@ public class ManualRedactionService { log.info("add removeRedaction for file {} and annotation {}", fileId, removeRedactionRequest.getAnnotationId()); var idRemoval = MagicConverter.convert(removeRedactionPersistenceService.insert(fileId, removeRedactionRequest), IdRemoval.class); - if (redactionLog == null) { - redactionLog = fileManagementStorageService.getRedactionLog(dossier.getId(), fileId); - } - Long commentId = null; if (removeRedactionRequest.getComment() != null) { commentId = addComment(fileId, removeRedactionRequest.getAnnotationId(), removeRedactionRequest.getComment(), removeRedactionRequest.getUser()).getId(); @@ -367,16 +375,7 @@ public class ManualRedactionService { if (removeFromDictionary) { - Optional redactionLogEntryOptional = redactionLog.getRedactionLogEntry() - .stream() - .filter(entry -> entry.getId().equals(annotationId)) - .findFirst(); - - if (redactionLogEntryOptional.isEmpty()) { - throw new NotFoundException("Annotation does not exist in redaction log."); - } - - var redactionLogEntry = redactionLogEntryOptional.get(); + RedactionLogEntry redactionLogEntry = getRedactionLogEntry(redactionLog, annotationId); if (revert) { var idRemovalEntity = removeRedactionPersistenceService.findRemoveRedaction(fileId, annotationId); @@ -413,6 +412,22 @@ public class ManualRedactionService { } + private RedactionLogEntry getRedactionLogEntry(RedactionLog redactionLog, String annotationId) { + + Optional redactionLogEntryOptional = redactionLog.getRedactionLogEntry() + .stream() + .filter(entry -> entry.getId().equals(annotationId)) + .findFirst(); + + if (redactionLogEntryOptional.isEmpty()) { + throw new NotFoundException("Annotation does not exist in redaction log."); + } + + var redactionLogEntry = redactionLogEntryOptional.get(); + return redactionLogEntry; + } + + private String buildTypeId(RedactionLogEntry redactionLogEntry, DossierEntity dossier) { if (redactionLogEntry.isDossierDictionaryEntry()) { diff --git a/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/service/TypeProvider.java b/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/service/TypeProvider.java index b234ebdb4..eea4fe85d 100644 --- a/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/service/TypeProvider.java +++ b/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/service/TypeProvider.java @@ -23,12 +23,17 @@ public class TypeProvider { public TypeValue testAndProvideType(DossierTemplateModel dossierTemplate) { - return testAndProvideType(dossierTemplate, null, "test"); + return testAndProvideType(dossierTemplate, null, "test", false); } public TypeValue testAndProvideType(DossierTemplateModel dossierTemplate, Dossier dossier, String typeName) { + return testAndProvideType(dossierTemplate, dossier, typeName, false); + } + + public TypeValue testAndProvideType(DossierTemplateModel dossierTemplate, Dossier dossier, String typeName, boolean dossierDictionaryOnly) { + var type = new CreateTypeValue(); type.setType(typeName); type.setDescription("test"); @@ -42,7 +47,7 @@ public class TypeProvider { type.setCaseInsensitive(true); type.setDossierTemplateId(dossierTemplate.getId()); type.setHasDictionary(true); - type.setDossierDictionaryOnly(true); + type.setDossierDictionaryOnly(dossierDictionaryOnly); dictionaryClient.addType(type); var allTypes = dictionaryClient.getAllTypes(dossierTemplate.getDossierTemplateId(),dossier != null ? dossier.getId() : null,false); diff --git a/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/tests/DictionaryTest.java b/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/tests/DictionaryTest.java index 3d6268d61..12e6c7d74 100644 --- a/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/tests/DictionaryTest.java +++ b/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/tests/DictionaryTest.java @@ -2,8 +2,6 @@ package com.iqser.red.service.peristence.v1.server.integration.tests; import static org.assertj.core.api.Assertions.assertThat; -import java.io.ByteArrayInputStream; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -14,7 +12,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; import com.iqser.red.service.peristence.v1.server.integration.client.DictionaryClient; import com.iqser.red.service.peristence.v1.server.integration.client.DossierTemplateClient; @@ -55,6 +52,33 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { TenantContext.setTenantId("redaction"); } + @Test + public void testAddEntriesToDossierTemplateDictionaryWithDossierDictioanryOnlyFlag() { + + var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate(); + var type = typeProvider.testAndProvideType(dossierTemplate, null, "type", true); + assertThat(type.isDossierDictionaryOnly()).isTrue(); + + var entries = List.of("word1", "word2"); + var falsePositives = List.of("false_positive1", "false_positive"); + var falseRecommendations = List.of("false_recommendation1", "false_recommendation2"); + Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), entries, false, null, DictionaryEntryType.ENTRY)); + Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), falsePositives, false, null, DictionaryEntryType.FALSE_POSITIVE)); + Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), falseRecommendations, false, null, DictionaryEntryType.FALSE_RECOMMENDATION)); + + var loadedType1 = dictionaryClient.getDictionaryForType(type.getType(), type.getDossierTemplateId(), null); + assertThat(loadedType1).isNotNull(); + assertThat(loadedType1.getEntries()).isNull(); + assertThat(loadedType1.getFalsePositiveEntries()).isNull(); + assertThat(loadedType1.getFalseRecommendationEntries()).isNull(); + + Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.deleteEntries(type.getType(), type.getDossierTemplateId(), entries, null, DictionaryEntryType.ENTRY)); + Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.deleteEntries(type.getType(), type.getDossierTemplateId(), falsePositives, null, DictionaryEntryType.FALSE_POSITIVE)); + Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.deleteEntries(type.getType(), type.getDossierTemplateId(), falseRecommendations, null, DictionaryEntryType.FALSE_RECOMMENDATION)); +// Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.deleteEntry(type.getType(), type.getDossierTemplateId(), entries.get(0), null, DictionaryEntryType.ENTRY)); +// Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.deleteEntry(type.getType(), type.getDossierTemplateId(), falsePositives.get(0), null, DictionaryEntryType.FALSE_POSITIVE)); +// Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.deleteEntry(type.getType(), type.getDossierTemplateId(), falseRecommendations.get(0), null, DictionaryEntryType.FALSE_RECOMMENDATION)); + } @Test public void testAddFalsePositiveAndFalseRecommendation() { @@ -63,7 +87,7 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { var type = typeProvider.testAndProvideType(dossierTemplate); assertThat(type.getRecommendationHexColor()).isEqualTo("#aaaaaa"); assertThat(type.getSkippedHexColor()).isEqualTo("#aaaaaa"); - assertThat(type.isDossierDictionaryOnly()).isTrue(); + assertThat(type.isDossierDictionaryOnly()).isFalse(); dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("word1", "word2"), false, null, DictionaryEntryType.ENTRY); dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("false_positive1", "false_positive"), false, null, DictionaryEntryType.FALSE_POSITIVE); @@ -200,7 +224,7 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { dictionary = dictionaryClient.getDictionaryForType(type.getType(), type.getDossierTemplateId(), null); assertThat(dictionary.getEntries()).hasSize(1); assertThat(dictionary.getEntries().iterator().next()).isEqualTo(word); - assertThat((dictionary.isDossierDictionaryOnly())).isTrue(); + assertThat((dictionary.isDossierDictionaryOnly())).isFalse(); // Act & Assert: Delete word; Should have 'deleted' flag entries = new ArrayList<>(); @@ -394,7 +418,7 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { var type = typeProvider.testAndProvideType(dossierTemplate); assertThat(type.getRecommendationHexColor()).isEqualTo("#aaaaaa"); assertThat(type.getSkippedHexColor()).isEqualTo("#aaaaaa"); - assertThat(type.isDossierDictionaryOnly()).isTrue(); + assertThat(type.isDossierDictionaryOnly()).isFalse(); dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("word1", "word2", "word3"), false, null, DictionaryEntryType.ENTRY); dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("false_positive1", "false_positive"), false, null, DictionaryEntryType.FALSE_POSITIVE); @@ -420,7 +444,7 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { false, dossier.getDossierId(), DictionaryEntryType.FALSE_RECOMMENDATION); - Exception exception = Assertions.assertThrows(FeignException.Unauthorized.class, () -> dictionaryClient.getMergedDictionaries(type.getType() + ";", dossierTemplate.getDossierTemplateId(), dossier.getDossierId())); + Assertions.assertThrows(FeignException.Unauthorized.class, () -> dictionaryClient.getMergedDictionaries(type.getType() + ";", dossierTemplate.getDossierTemplateId(), dossier.getDossierId())); Dictionary mergedDict = dictionaryClient.getMergedDictionaries(type.getType(), dossierTemplate.getDossierTemplateId(), dossier.getDossierId()); assertThat(mergedDict).isNotNull(); @@ -479,7 +503,7 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { var type = typeProvider.testAndProvideType(dossierTemplate); assertThat(type.getRecommendationHexColor()).isEqualTo("#aaaaaa"); assertThat(type.getSkippedHexColor()).isEqualTo("#aaaaaa"); - assertThat(type.isDossierDictionaryOnly()).isTrue(); + assertThat(type.isDossierDictionaryOnly()).isFalse(); dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("Charalampos", "Carina Wilson", "carlsen"), false, null, DictionaryEntryType.ENTRY); dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("false_positive1", "false_positive"), false, null, DictionaryEntryType.FALSE_POSITIVE); @@ -504,6 +528,9 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { assertThat(loadedType1.getFalseRecommendationEntries().get(0)).isEqualTo("afalse_recommendation2"); assertThat(loadedType1.getFalseRecommendationEntries().get(1)).isEqualTo("false_recommendation1"); } + + + @Test public void testCreateAndDeleteLargeDictionary() { diff --git a/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/tests/ManualRedactionTest.java b/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/tests/ManualRedactionTest.java index ac79e76cf..e334eb7ef 100644 --- a/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/tests/ManualRedactionTest.java +++ b/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/tests/ManualRedactionTest.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; @@ -92,6 +93,71 @@ public class ManualRedactionTest extends AbstractPersistenceServerServiceTest { private InternalDictionaryClient internalDictionaryClient; + @Test + public void testRemoveToDossierTemplateWithDossierDictionaryOnlyTrue(){ + var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate(); + var dossier = dossierTesterAndProvider.provideTestDossier(dossierTemplate); + var file = fileTesterAndProvider.testAndProvideFile(dossier); + + var type = typeProvider.testAndProvideType(dossierTemplate, null, "type", true); + assertThat(type.isDossierDictionaryOnly()).isTrue(); + + dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("Luke Skywalker"), false, dossier.getDossierId(), DictionaryEntryType.ENTRY); + + var redactionLog = new RedactionLog(1, + 1, + List.of(RedactionLogEntry.builder().id("AnnotationId").type(type.getType()).value("Luke Skywalker").isDictionaryEntry(true).build()), + null, + 0, + 0, + 0, + 0); + fileManagementStorageService.storeJSONObject(dossier.getId(), file.getId(), FileType.REDACTION_LOG, redactionLog); + + when(redactionLogMergeService.provideRedactionLog(Mockito.any())).thenReturn(redactionLog); + + Assertions.assertThrows(FeignException.Forbidden.class, () -> + manualRedactionClient.removeRedactionBulk(dossier.getId(), + file.getId(), + Set.of(RemoveRedactionRequest.builder().annotationId("AnnotationId").removeFromDictionary(true).removeFromAllDossiers(true).build())));//.get(0); + + + var dossierTemplateDictionary = internalDictionaryClient.getDictionaryForType(toTypeId(type.getType(), dossierTemplate.getDossierTemplateId()), null); + assertThat(dossierTemplateDictionary.getEntries().size()).isZero(); + + + } + + @Test + public void testAddToDossierTemplateWithDossierDictionaryOnlyTrue(){ + var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate(); + var dossier = dossierTesterAndProvider.provideTestDossier(dossierTemplate); + var file = fileTesterAndProvider.testAndProvideFile(dossier); + + var type = typeProvider.testAndProvideType(dossierTemplate, null, "type", true); + assertThat(type.isDossierDictionaryOnly()).isTrue(); + Assertions.assertThrows(FeignException.Forbidden.class, () -> + + manualRedactionClient.addRedactionBulk(dossier.getId(), + file.getId(), + Set.of(AddRedactionRequest.builder() + .positions(List.of(Rectangle.builder().topLeftY(1).topLeftX(1).height(1).width(1).build())) + .section("section test") + .addToDictionary(true) + .addToAllDossiers(true) + .type(type.getType()) + .reason("1") + .value("Luke Skywalker") + .legalBasis("1") + .sourceId("SourceId") + .build()))); + + var dossierTemplateDictionary = internalDictionaryClient.getDictionaryForType(toTypeId(type.getType(), dossierTemplate.getDossierTemplateId()), null); + assertThat(dossierTemplateDictionary.getEntries().size()).isZero(); + + + } + @Test public void testAddToAllDossiersIfDeletedInOneDossier(){ var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate(); @@ -100,10 +166,6 @@ public class ManualRedactionTest extends AbstractPersistenceServerServiceTest { var type = typeProvider.testAndProvideType(dossierTemplate); - //FIXME should be created on the fly. -// CreateTypeValue dossierDictionaryType = MagicConverter.convert(type, CreateTypeValue.class); -// dictionaryClient.addType(dossierDictionaryType, dossier.getDossierId()); - dictionaryClient.deleteEntries(type.getType(), type.getDossierTemplateId(), List.of("Luke Skywalker"), dossier.getDossierId(), DictionaryEntryType.ENTRY); var dossierDictionary = internalDictionaryClient.getDictionaryForType(toTypeId(type.getType(), dossierTemplate.getDossierTemplateId(), dossier.getDossierId()), null); @@ -143,10 +205,6 @@ public class ManualRedactionTest extends AbstractPersistenceServerServiceTest { var type = typeProvider.testAndProvideType(dossierTemplate); - //FIXME should be created on the fly. -// CreateTypeValue dossierDictionaryType = MagicConverter.convert(type, CreateTypeValue.class); -// dictionaryClient.addType(dossierDictionaryType, dossier.getDossierId()); - manualRedactionClient.addRedactionBulk(dossier.getId(), file.getId(), Set.of(AddRedactionRequest.builder() @@ -182,10 +240,6 @@ public class ManualRedactionTest extends AbstractPersistenceServerServiceTest { entries.add("Darth Vader"); dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), entries, false, null, DictionaryEntryType.ENTRY); - //FIXME should be created on the fly. -// CreateTypeValue dossierDictionaryType = MagicConverter.convert(type, CreateTypeValue.class); -// dictionaryClient.addType(dossierDictionaryType, dossier.getDossierId()); - dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("Luke Skywalker"), false, dossier.getDossierId(), DictionaryEntryType.ENTRY); var dossierTemplateDictionary = dictionaryClient.getDictionaryForType(type.getType(), type.getDossierTemplateId(), null); -- 2.47.2 From 8860e9d040711e37fe534cef95245479e8b1b268 Mon Sep 17 00:00:00 2001 From: Corina Olariu Date: Fri, 7 Jul 2023 12:42:29 +0300 Subject: [PATCH 2/3] RED-7034 - Check for dossierDictionaryOnly flag when add to template dict is requested - update junit tests --- .../server/integration/tests/DictionaryTest.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/tests/DictionaryTest.java b/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/tests/DictionaryTest.java index 12e6c7d74..6db1a4ee5 100644 --- a/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/tests/DictionaryTest.java +++ b/persistence-service-v1/persistence-service-server-v1/src/test/java/com/iqser/red/service/peristence/v1/server/integration/tests/DictionaryTest.java @@ -53,10 +53,10 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { } @Test - public void testAddEntriesToDossierTemplateDictionaryWithDossierDictioanryOnlyFlag() { + public void testAddEntriesToDossierTemplateDictionaryWithDossierDictionaryOnlyFlag() { var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate(); - var type = typeProvider.testAndProvideType(dossierTemplate, null, "type", true); + var type = typeProvider.testAndProvideType(dossierTemplate, null, "type1", true); assertThat(type.isDossierDictionaryOnly()).isTrue(); var entries = List.of("word1", "word2"); @@ -68,16 +68,16 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { var loadedType1 = dictionaryClient.getDictionaryForType(type.getType(), type.getDossierTemplateId(), null); assertThat(loadedType1).isNotNull(); - assertThat(loadedType1.getEntries()).isNull(); - assertThat(loadedType1.getFalsePositiveEntries()).isNull(); - assertThat(loadedType1.getFalseRecommendationEntries()).isNull(); + assertThat(loadedType1.getEntries()).isEmpty(); + assertThat(loadedType1.getFalsePositiveEntries()).isEmpty(); + assertThat(loadedType1.getFalseRecommendationEntries()).isEmpty(); Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.deleteEntries(type.getType(), type.getDossierTemplateId(), entries, null, DictionaryEntryType.ENTRY)); Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.deleteEntries(type.getType(), type.getDossierTemplateId(), falsePositives, null, DictionaryEntryType.FALSE_POSITIVE)); Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.deleteEntries(type.getType(), type.getDossierTemplateId(), falseRecommendations, null, DictionaryEntryType.FALSE_RECOMMENDATION)); -// Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.deleteEntry(type.getType(), type.getDossierTemplateId(), entries.get(0), null, DictionaryEntryType.ENTRY)); -// Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.deleteEntry(type.getType(), type.getDossierTemplateId(), falsePositives.get(0), null, DictionaryEntryType.FALSE_POSITIVE)); -// Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.deleteEntry(type.getType(), type.getDossierTemplateId(), falseRecommendations.get(0), null, DictionaryEntryType.FALSE_RECOMMENDATION)); + Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.deleteEntry(type.getType(), type.getDossierTemplateId(), entries.get(0), null, DictionaryEntryType.ENTRY)); + Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.deleteEntry(type.getType(), type.getDossierTemplateId(), falsePositives.get(0), null, DictionaryEntryType.FALSE_POSITIVE)); + Assertions.assertThrows(FeignException.Forbidden.class, () -> dictionaryClient.deleteEntry(type.getType(), type.getDossierTemplateId(), falseRecommendations.get(0), null, DictionaryEntryType.FALSE_RECOMMENDATION)); } @Test -- 2.47.2 From 9b2ea9dfc4421f4532ddf86298d8dbb4e903aef9 Mon Sep 17 00:00:00 2001 From: Corina Olariu Date: Fri, 7 Jul 2023 18:58:30 +0300 Subject: [PATCH 3/3] RED-7034 - Check for dossierDictionaryOnly flag when add to template dict is requested - update after review --- .../service/ManualRedactionService.java | 48 ++++++++----------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/ManualRedactionService.java b/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/ManualRedactionService.java index 982e966f4..aa65cf3d8 100644 --- a/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/ManualRedactionService.java +++ b/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/ManualRedactionService.java @@ -88,7 +88,7 @@ public class ManualRedactionService { private final RabbitTemplate rabbitTemplate; private final ObjectMapper objectMapper; private final RedactionLogService redactionLogService; - private final DictionaryManagementService dictionaryService; + private final DictionaryManagementService dictionaryManagementService; private final HashFunction hashFunction = Hashing.murmur3_128(); @@ -100,7 +100,7 @@ public class ManualRedactionService { dossierPersistenceService.getAndValidateDossier(dossierId); var actionPerformed = false; // validate add to dossier template dictionaries - addRedactionRequests.forEach(request -> dictionaryService.validateAddRemoveToDossierTemplateDictionary(request.getDossierTemplateTypeId(), request.isAddToDictionary(), request.isAddToAllDossiers())); + addRedactionRequests.forEach(request -> dictionaryManagementService.validateAddRemoveToDossierTemplateDictionary(request.getDossierTemplateTypeId(), request.isAddToDictionary(), request.isAddToAllDossiers())); for (var addRedactionRequest : addRedactionRequests) { if (addRedactionRequest.isAddToDictionary()) { @@ -209,7 +209,7 @@ public class ManualRedactionService { }); } else { if (addToAllDossiers) { - var dictionaryEntriesToUnDelete = dictionaryService.getAllEntriesInDossierTemplate(dossierTemplateTypeId, value); + var dictionaryEntriesToUnDelete = dictionaryManagementService.getAllEntriesInDossierTemplate(dossierTemplateTypeId, value); dictionaryEntriesToUnDelete.forEach(entry -> { typeIdsOfModifiedDictionaries.add(entry.getTypeId()); addToDictionary(entry.getTypeId(), value, dossierId, fileId, dictionaryEntryType); @@ -260,7 +260,7 @@ public class ManualRedactionService { try { log.debug("Deleting entries to {} for {} / {}", typeId, dossierId, fileId); - dictionaryService.deleteEntries(typeId, List.of(value), dictionaryEntryType != null ? dictionaryEntryType : DictionaryEntryType.ENTRY); + dictionaryManagementService.deleteEntries(typeId, List.of(value), dictionaryEntryType != null ? dictionaryEntryType : DictionaryEntryType.ENTRY); } catch (FeignException e) { throw new BadRequestException(e.getMessage()); } @@ -271,7 +271,7 @@ public class ManualRedactionService { try { log.debug("Adding entry: {} to {} for {} / {}", value, typeId, dossierId, fileId); - dictionaryService.addEntries(typeId, List.of(value), false, false, dictionaryEntryType != null ? dictionaryEntryType : DictionaryEntryType.ENTRY); + dictionaryManagementService.addEntries(typeId, List.of(value), false, false, dictionaryEntryType != null ? dictionaryEntryType : DictionaryEntryType.ENTRY); } catch (Exception e) { throw new BadRequestException(e.getMessage()); } @@ -294,7 +294,7 @@ public class ManualRedactionService { if (request.isRemoveFromDictionary()) { RedactionLogEntry redactionLogEntry = getRedactionLogEntry(redactionLog, request.getAnnotationId()); var dossierTemplateTypeId = toTypeId(redactionLogEntry.getType(), dossier.getDossierTemplateId()); - dictionaryService.validateAddRemoveToDossierTemplateDictionary(dossierTemplateTypeId, request.isRemoveFromDictionary(), request.isRemoveFromAllDossiers()); + dictionaryManagementService.validateAddRemoveToDossierTemplateDictionary(dossierTemplateTypeId, request.isRemoveFromDictionary(), request.isRemoveFromAllDossiers()); } }); @@ -318,7 +318,14 @@ public class ManualRedactionService { .stream() .filter(entry -> entry.getId().equals(removeRedactionRequest.getAnnotationId())) .findFirst(); - var requiresAnalysis = redactionLogEntryOptional.isPresent() && redactionLogEntryOptional.get().isHint(); + RedactionLogEntry redactionLogEntry = null; + boolean requiresAnalysis = false; + try { + redactionLogEntry = getRedactionLogEntry(redactionLog, removeRedactionRequest.getAnnotationId()); + requiresAnalysis = redactionLogEntry.isHint(); + } catch (NotFoundException e) { + + } actionPerformed = actionPerformed || requiresAnalysis; if (!requiresAnalysis && idRemoval.isApproved()) { removeRedactionPersistenceService.markAsProcessed(idRemoval); @@ -384,7 +391,7 @@ public class ManualRedactionService { }); } else { if (removeFromAllDossiers) { - var dictionaryEntriesToRemove = dictionaryService.getAllEntriesInDossierTemplate(toTypeId(redactionLogEntry.getType(),dossier.getDossierTemplateId()), + var dictionaryEntriesToRemove = dictionaryManagementService.getAllEntriesInDossierTemplate(toTypeId(redactionLogEntry.getType(),dossier.getDossierTemplateId()), redactionLogEntry.getValue()); dictionaryEntriesToRemove.forEach(entry -> { typeIdsOfModifiedDictionaries.add(entry.getTypeId()); @@ -715,17 +722,13 @@ public class ManualRedactionService { private void updateDictionaryForResizeRedactions(String dossierId, String fileId, ManualResizeRedactionEntity resizeRedaction, RedactionLog redactionLog) { - Optional redactionLogEntryOptional = redactionLog.getRedactionLogEntry() - .stream() - .filter(r -> r.getId().equals(resizeRedaction.getId().getAnnotationId())) - .findFirst(); - - if (redactionLogEntryOptional.isEmpty()) { + RedactionLogEntry redactionLogEntry = null; + try { + redactionLogEntry = getRedactionLogEntry(redactionLog, resizeRedaction.getId().getAnnotationId()); + } catch (NotFoundException e) { return; } - var redactionLogEntry = redactionLogEntryOptional.get(); - if (resizeRedaction.getUpdateDictionary() != null && resizeRedaction.getUpdateDictionary() && resizeRedaction.getStatus() .equals(AnnotationStatus.APPROVED) && (redactionLogEntry.isDictionaryEntry() || redactionLogEntry.isDossierDictionaryEntry())) { var dossier = dossierPersistenceService.findByDossierId(dossierId); @@ -768,19 +771,10 @@ public class ManualRedactionService { for (var annotationId : annotationIds) { var idRemoval = MagicConverter.convert(removeRedactionPersistenceService.findRemoveRedaction(fileId, annotationId), IdRemoval.class); - Optional redactionLogEntryOptional = redactionLog.getRedactionLogEntry() - .stream() - .filter(entry -> entry.getId().equals(idRemoval.getAnnotationId())) - .findFirst(); - - if (redactionLogEntryOptional.isEmpty()) { - throw new NotFoundException("Annotation does not exist in redaction log."); - } + RedactionLogEntry redactionLogEntry = getRedactionLogEntry(redactionLog, idRemoval.getAnnotationId()); if (idRemoval.isRemoveFromDictionary()) { - var redactionLogEntry = redactionLogEntryOptional.get(); - if (annotationStatus == AnnotationStatus.APPROVED) { removeFromDictionary(buildTypeId(redactionLogEntry, dossier), redactionLogEntry.getValue(), dossierId, fileId, DictionaryEntryType.ENTRY); approveStatusForRedactionsWithSameValue(dossier, false, true, redactionLogEntry.getValue()); @@ -794,7 +788,7 @@ public class ManualRedactionService { } } - } else if (redactionLogEntryOptional.get().isHint()) { + } else if (redactionLogEntry.isHint()) { reprocess(dossierId, fileId); } -- 2.47.2