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 781589c19..8dcc0cfdb 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 @@ -53,6 +53,16 @@ import lombok.extern.slf4j.Slf4j; @RequiredArgsConstructor public class DictionaryService { + /** + * Comparator to sort entries list. + */ + public static Comparator entryComparator = (e1, e2) -> { + if (e1.compareToIgnoreCase(e2) == 0) { + return e1.compareTo(e2); + } + return e1.compareToIgnoreCase(e2); + }; + private final DictionaryManagementService dictionaryManagementService; private final ColorsService colorsService; @@ -272,19 +282,19 @@ public class DictionaryService { .stream() .filter(e -> !e.isDeleted()) .map(DictionaryEntry::getValue) - .sorted(Comparator.comparing(String::toLowerCase)) + .sorted(entryComparator) .collect(Collectors.toList())) .falsePositiveEntries(dictionaryForType.getFalsePositiveEntries() .stream() .filter(e -> !e.isDeleted()) .map(DictionaryEntry::getValue) - .sorted(Comparator.comparing(String::toLowerCase)) + .sorted(entryComparator) .collect(Collectors.toList())) .falseRecommendationEntries(dictionaryForType.getFalseRecommendationEntries() .stream() .filter(e -> !e.isDeleted()) .map(DictionaryEntry::getValue) - .sorted(Comparator.comparing(String::toLowerCase)) + .sorted(entryComparator) .collect(Collectors.toList())) .hexColor(dictionaryForType.getHexColor()) .recommendationHexColor(dictionaryForType.getRecommendationHexColor()) @@ -340,19 +350,19 @@ public class DictionaryService { .stream() .filter(e -> !e.isDeleted()) .map(DictionaryEntry::getValue) - .sorted(Comparator.comparing(String::toLowerCase)) + .sorted(entryComparator) .collect(Collectors.toList())) .falsePositiveEntries(cdm.getFalsePositives() .stream() .filter(e -> !e.isDeleted()) .map(DictionaryEntry::getValue) - .sorted(Comparator.comparing(String::toLowerCase)) + .sorted(entryComparator) .collect(Collectors.toList())) .falseRecommendationEntries(cdm.getFalseRecommendations() .stream() .filter(e -> !e.isDeleted()) .map(DictionaryEntry::getValue) - .sorted(Comparator.comparing(String::toLowerCase)) + .sorted(entryComparator) .collect(Collectors.toList())) .hexColor(entity.getHexColor()) .recommendationHexColor(entity.getRecommendationHexColor()) 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 6db1a4ee5..8325fe108 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 @@ -19,6 +19,7 @@ import com.iqser.red.service.peristence.v1.server.integration.service.DossierTem import com.iqser.red.service.peristence.v1.server.integration.service.DossierTesterAndProvider; import com.iqser.red.service.peristence.v1.server.integration.service.TypeProvider; import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest; +import com.iqser.red.service.persistence.management.v1.processor.service.DictionaryService; import com.iqser.red.service.persistence.service.v1.api.shared.model.CreateTypeValue; import com.iqser.red.service.persistence.service.v1.api.shared.model.Dictionary; import com.iqser.red.service.persistence.service.v1.api.shared.model.TypeValue; @@ -45,6 +46,10 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { @Autowired private DossierTemplateClient dossierTemplateClient; + private List testList1 = List.of("William c. Spare", "Charalampos", "Carina Wilson", "PATRICIA A. SHEEHY", "William C. Spare", "carlsen", "Patricia A. Sheehy"); + private List testList2 = List.of("William C. Spare", "Charalampos", "Carina Wilson", "Patricia A. Sheehy", "William c. Spare", "carlsen", "PATRICIA A. SHEEHY"); + + @BeforeEach public void createDossierRedactionDictionary() { @@ -404,9 +409,9 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { var dossier = dossierTesterAndProvider.provideTestDossier(dossierTemplate, "Dossier"); var type = typeProvider.testAndProvideType(dossierTemplate); - Assertions.assertThrows(FeignException.BadRequest.class, () -> dictionaryClient.getAllTypes(dossierTemplate1.getDossierTemplateId(), dossier.getDossierId(), false)); + Assertions.assertThrows(FeignException.BadRequest.class, () -> dictionaryClient.getAllTypes(dossierTemplate1.getId(), dossier.getId(), false)); - Assertions.assertThrows(FeignException.BadRequest.class, () -> dictionaryClient.getDictionaryForType(type.getType(), dossierTemplate1.getDossierTemplateId(), dossier.getDossierId())); + Assertions.assertThrows(FeignException.BadRequest.class, () -> dictionaryClient.getDictionaryForType(type.getType(), dossierTemplate1.getId(), dossier.getId())); } @@ -435,18 +440,18 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { assertThat(loadedType1.getFalsePositiveEntries()).hasSize(2); assertThat(loadedType1.getFalseRecommendationEntries()).hasSize(2); - dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("word1", "word4"), false, dossier.getDossierId(), DictionaryEntryType.ENTRY); - dictionaryClient.deleteEntries(type.getType(), type.getDossierTemplateId(), List.of("word2"), dossier.getDossierId(), DictionaryEntryType.ENTRY); - dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("false_positive1", "false_positive3"), false, dossier.getDossierId(), DictionaryEntryType.FALSE_POSITIVE); + dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("word1", "word4"), false, dossier.getId(), DictionaryEntryType.ENTRY); + dictionaryClient.deleteEntries(type.getType(), type.getDossierTemplateId(), List.of("word2"), dossier.getId(), DictionaryEntryType.ENTRY); + dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("false_positive1", "false_positive3"), false, dossier.getId(), DictionaryEntryType.FALSE_POSITIVE); dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("false_recommendation3", "false_recommendation4"), false, - dossier.getDossierId(), + dossier.getId(), DictionaryEntryType.FALSE_RECOMMENDATION); - Assertions.assertThrows(FeignException.Unauthorized.class, () -> dictionaryClient.getMergedDictionaries(type.getType() + ";", dossierTemplate.getDossierTemplateId(), dossier.getDossierId())); + Assertions.assertThrows(FeignException.Unauthorized.class, () -> dictionaryClient.getMergedDictionaries(type.getType() + ";", dossierTemplate.getId(), dossier.getId())); - Dictionary mergedDict = dictionaryClient.getMergedDictionaries(type.getType(), dossierTemplate.getDossierTemplateId(), dossier.getDossierId()); + Dictionary mergedDict = dictionaryClient.getMergedDictionaries(type.getType(), dossierTemplate.getId(), dossier.getId()); assertThat(mergedDict).isNotNull(); assertThat(mergedDict.getEntries().size()).isEqualTo(3); assertThat(mergedDict.getFalsePositiveEntries().size()).isEqualTo(3); @@ -475,17 +480,17 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { assertThat(loadedType1.getFalsePositiveEntries()).hasSize(2); assertThat(loadedType1.getFalseRecommendationEntries()).hasSize(2); - dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("word1", "word4"), false, dossier.getDossierId(), DictionaryEntryType.ENTRY); - dictionaryClient.deleteEntries(type.getType(), type.getDossierTemplateId(), List.of("word2"), dossier.getDossierId(), DictionaryEntryType.ENTRY); - dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("false_positive1", "false_positive3"), false, dossier.getDossierId(), DictionaryEntryType.FALSE_POSITIVE); + dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("word1", "word4"), false, dossier.getId(), DictionaryEntryType.ENTRY); + dictionaryClient.deleteEntries(type.getType(), type.getDossierTemplateId(), List.of("word2"), dossier.getId(), DictionaryEntryType.ENTRY); + dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("false_positive1", "false_positive3"), false, dossier.getId(), DictionaryEntryType.FALSE_POSITIVE); dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("false_recommendation3", "false_recommendation4"), false, - dossier.getDossierId(), + dossier.getId(), DictionaryEntryType.FALSE_RECOMMENDATION); - var loadedType2 = dictionaryClient.getDictionaryForType(type.getType(), type.getDossierTemplateId(), dossier.getDossierId()); + var loadedType2 = dictionaryClient.getDictionaryForType(type.getType(), type.getDossierTemplateId(), dossier.getId()); assertThat(loadedType2.getEntries()).hasSize(2); assertThat(loadedType2.getFalsePositiveEntries()).hasSize(2); @@ -505,8 +510,8 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { assertThat(type.getSkippedHexColor()).isEqualTo("#aaaaaa"); 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); + dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), testList1, false, null, DictionaryEntryType.ENTRY); + dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), testList2, false, null, DictionaryEntryType.FALSE_POSITIVE); dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("false_recommendation1", "afalse_recommendation2"), @@ -517,19 +522,60 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { var loadedType1 = dictionaryClient.getDictionaryForType(type.getType(), type.getDossierTemplateId(), null); var entries = loadedType1.getEntries(); - assertThat(entries).hasSize(3); + assertThat(entries).hasSize(7); assertThat(entries.get(0)).isEqualTo("Carina Wilson"); assertThat(entries.get(1)).isEqualTo("carlsen"); assertThat(entries.get(2)).isEqualTo("Charalampos"); - assertThat(loadedType1.getFalsePositiveEntries()).hasSize(2); - assertThat(loadedType1.getFalsePositiveEntries().get(0)).isEqualTo("false_positive"); - assertThat(loadedType1.getFalsePositiveEntries().get(1)).isEqualTo("false_positive1"); + assertThat(entries.get(3)).isEqualTo("PATRICIA A. SHEEHY"); + assertThat(entries.get(4)).isEqualTo("Patricia A. Sheehy"); + assertThat(entries.get(5)).isEqualTo("William C. Spare"); + assertThat(entries.get(6)).isEqualTo("William c. Spare"); +// assertThat(entries).isEqualTo(loadedType1.getFalsePositiveEntries()); + + var falsePositives = loadedType1.getEntries(); + assertThat(falsePositives).hasSize(7); + assertThat(falsePositives.get(0)).isEqualTo("Carina Wilson"); + assertThat(falsePositives.get(1)).isEqualTo("carlsen"); + assertThat(falsePositives.get(2)).isEqualTo("Charalampos"); + assertThat(falsePositives.get(3)).isEqualTo("PATRICIA A. SHEEHY"); + assertThat(falsePositives.get(4)).isEqualTo("Patricia A. Sheehy"); + assertThat(falsePositives.get(5)).isEqualTo("William C. Spare"); + assertThat(falsePositives.get(6)).isEqualTo("William c. Spare"); assertThat(loadedType1.getFalseRecommendationEntries()).hasSize(2); assertThat(loadedType1.getFalseRecommendationEntries().get(0)).isEqualTo("afalse_recommendation2"); assertThat(loadedType1.getFalseRecommendationEntries().get(1)).isEqualTo("false_recommendation1"); } + @Test + public void testSortedEntriesTest() { + var testList1 = List.of("William c. Spare", "Charalampos", "Carina Wilson", "PATRICIA A. SHEEHY", "William C. Spare", "carlsen", "Patricia A. Sheehy"); + var testList2 = List.of("William C. Spare", "Charalampos", "Carina Wilson", "Patricia A. Sheehy", "William c. Spare", "carlsen", "PATRICIA A. SHEEHY"); + System.out.println("Test list 1: " + testList1); + var sortedList1 = testList1.stream().sorted().sorted(Comparator.comparing(String::toLowerCase)).toList(); + var sortedList12 = testList1.stream().sorted().toList(); + var sortedList11 = testList1.stream().sorted(String:: compareToIgnoreCase).toList(); + var sortedList13 = testList1.stream().sorted(DictionaryService.entryComparator).toList(); + + var sortedList2 = testList2.stream().sorted().sorted(Comparator.comparing(String::toLowerCase)).toList(); + var sortedList22 = testList2.stream().sorted().toList(); + var sortedList21 = testList2.stream().sorted(String::compareToIgnoreCase).toList(); + var sortedList23 = testList2.stream().sorted(DictionaryService.entryComparator).toList(); + System.out.println("Test list 2: " + testList2); + System.out.println("With sorted()sorted(Comparator.comparing(String::toLowerCase)): " + sortedList1); + System.out.println("With sorted()sorted(Comparator.comparing(String::toLowerCase)): " + sortedList2); + System.out.println("Just sorted(): " + sortedList12); + System.out.println("Just sorted(): " + sortedList22); + System.out.println("sorted(String:: compareToIgnoreCase): " + sortedList11); + System.out.println("sorted(String:: compareToIgnoreCase): " + sortedList21); + System.out.println("sorted(custom): " + sortedList13); + System.out.println("sorted(custom): " + sortedList23); + + assertThat(sortedList1).isEqualTo(sortedList2); + assertThat(sortedList13).isEqualTo(sortedList23); + assertThat(sortedList12).isEqualTo(sortedList22); // the result are equal but not in the desired order + assertThat(sortedList11).isNotEqualTo(sortedList21); + } @Test public void testCreateAndDeleteLargeDictionary() { @@ -559,7 +605,7 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest { assertThat(actualEntries.size()).isEqualTo(entries.size()); assertThat(actualEntries).usingComparator(new ListContentWithoutOrderAndWithoutDuplicatesComparator<>()).isEqualTo(entries); - dictionaryClient.deleteEntries(type.getType(), dossierTemplate.getDossierTemplateId(), entries, null, null); + dictionaryClient.deleteEntries(type.getType(), dossierTemplate.getId(), entries, null, null); dictionary = dictionaryClient.getDictionaryForType(type.getType(), type.getDossierTemplateId(), null);