RED-3003 Bugfix: Prevent duplicates of dictionary entries and extended dictionary tests

This commit is contained in:
Philipp Schramm 2021-12-13 16:08:47 +01:00
parent 8c4be5780e
commit 516b6ad11d
3 changed files with 171 additions and 10 deletions

View File

@ -10,7 +10,7 @@ import java.util.List;
public interface EntryRepository extends JpaRepository<DictionaryEntryEntity, Long> {
@Modifying
@Query("update DictionaryEntryEntity e set e.deleted = true , e.version = :version where e.type.id =:typeId and e.value in :values")
@Query("update DictionaryEntryEntity e set e.deleted = true , e.version = :version where e.type.id =:typeId and e.value in :values")
void deleteAllByTypeIdAndVersionAndValueIn(String typeId, long version, List<String> values);
@Modifying

View File

@ -28,14 +28,18 @@ import org.springframework.web.bind.annotation.RestController;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.iqser.red.service.persistence.management.v1.processor.utils.MagicConverter.convert;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
@RestController
@RequiredArgsConstructor
@ -52,7 +56,7 @@ public class DictionaryController implements DictionaryResource {
public void addEntries(@PathVariable(TYPE_PARAMETER_NAME) String typeId, @RequestBody List<String> entries,
@RequestParam(value = "removeCurrent", required = false, defaultValue = "false") boolean removeCurrent) {
List<String> cleanEntries = entries.stream().map(this::cleanDictionaryEntry).collect(toList());
Set<String> cleanEntries = entries.stream().map(this::cleanDictionaryEntry).collect(toSet());
validateEntries(cleanEntries);
@ -237,6 +241,10 @@ public class DictionaryController implements DictionaryResource {
private void validateEntries(List<String> entries) {
validateEntries(new HashSet<>(entries));
}
private void validateEntries(Set<String> entries) {
if (CollectionUtils.isEmpty(entries)) {
throw new BadRequestException("Entry list is empty.");

View File

@ -1,27 +1,31 @@
package com.iqser.red.service.peristence.v1.server.integration.tests;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.ArrayList;
import java.util.stream.Collectors;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.common.collect.Sets;
import com.iqser.red.service.peristence.v1.server.integration.client.DictionaryClient;
import com.iqser.red.service.peristence.v1.server.integration.client.DossierClient;
import com.iqser.red.service.peristence.v1.server.integration.client.DossierTemplateClient;
import com.iqser.red.service.peristence.v1.server.integration.service.DossierTemplateTesterAndProvider;
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.service.v1.api.model.dossiertemplate.dossier.CreateOrUpdateDossierRequest;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.type.Type;
import feign.FeignException;
import java.util.ArrayList;
public class DictionaryTest extends AbstractPersistenceServerServiceTest {
@Autowired
private TypeProvider typeProvider;
@Autowired
private DossierTemplateTesterAndProvider dossierTemplateTesterAndProvider;
@Autowired
private DossierTesterAndProvider dossierTesterAndProvider;
@ -31,9 +35,11 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest {
@Autowired
private DossierTemplateClient dossierTemplateClient;
@Test
public void testAddEntriesWithStopWord() {
var dossier = dossierTesterAndProvider.provideTestDossier();
dossierTesterAndProvider.provideTestDossier();
var typeId = "dossier_redaction";
var entries = new ArrayList<String>();
entries.add("age");
@ -45,6 +51,153 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest {
}
}
@Test
public void testAddWordMultipleTimes() {
// Arrange
var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate();
var type = typeProvider.testAndProvideType(dossierTemplate);
var word = "Luke Skywalker";
// Act & Assert: Add same word multiple times; Only one should exist
var entries = new ArrayList<String>();
entries.add(word);
entries.add(word);
dictionaryClient.addEntries(type.getTypeId(), entries, false);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId())).hasSize(1);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId()).get(0).getValue()).isEqualTo(word);
// Act & Assert: Add same word again; Only one should exist
entries = new ArrayList<>();
entries.add(word);
dictionaryClient.addEntries(type.getTypeId(), entries, false);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId())).hasSize(1);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId()).get(0).getValue()).isEqualTo(word);
// Act & Assert: Add same word multiple times again; Only one should exist
entries = new ArrayList<>();
entries.add(word);
entries.add(word);
entries.add(word);
entries.add(word);
dictionaryClient.addEntries(type.getTypeId(), entries, false);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId())).hasSize(1);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId()).get(0).getValue()).isEqualTo(word);
// Act & Assert: Delete word; Should have 'deleted' flag
entries = new ArrayList<>();
entries.add(word);
dictionaryClient.deleteEntries(type.getTypeId(), entries);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId())).hasSize(1);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId()).get(0).getValue()).isEqualTo(word);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId()).get(0).isDeleted()).isTrue();
}
@Test
public void testAddWordMultipleTimesAndRemoveCurrent() {
// Arrange
var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate();
var type = typeProvider.testAndProvideType(dossierTemplate);
var word = "Anakin Skywalker";
// Act & Assert: Add same word multiple times; Only one should exist
var entries = new ArrayList<String>();
entries.add(word);
entries.add(word);
dictionaryClient.addEntries(type.getTypeId(), entries, true);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId())).hasSize(1);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId()).get(0).getValue()).isEqualTo(word);
// Act & Assert: Add same word again; Only one should exist
entries = new ArrayList<>();
entries.add(word);
dictionaryClient.addEntries(type.getTypeId(), entries, true);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId())).hasSize(1);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId()).get(0).getValue()).isEqualTo(word);
// Act & Assert: Add same word multiple times again; Only one should exist
entries = new ArrayList<>();
entries.add(word);
entries.add(word);
entries.add(word);
entries.add(word);
dictionaryClient.addEntries(type.getTypeId(), entries, true);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId())).hasSize(1);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId()).get(0).getValue()).isEqualTo(word);
// Act & Assert: Delete word; Should have 'deleted' flag
entries = new ArrayList<>();
entries.add(word);
dictionaryClient.deleteEntries(type.getTypeId(), entries);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId())).hasSize(1);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId()).get(0).getValue()).isEqualTo(word);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId()).get(0).isDeleted()).isTrue();
}
@Test
public void testAddDifferentWordMultipleTimes() {
// Arrange
var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate();
var type = typeProvider.testAndProvideType(dossierTemplate);
var word1 = "Luke Skywalker";
var word2 = "Anakin Skywalker";
var word3 = "Yoda";
// Act & Assert: Add different words; All three should exist
var entries = new ArrayList<String>();
entries.add(word1);
entries.add(word2);
entries.add(word3);
dictionaryClient.addEntries(type.getTypeId(), entries, false);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId())).hasSize(3);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId())
.stream()
.map(e -> e.getValue())
.collect(Collectors.toList())).contains(word1, word2, word3);
// Act & Assert: Add same word again; No duplicate should exist
entries = new ArrayList<>();
entries.add(word1);
dictionaryClient.addEntries(type.getTypeId(), entries, false);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId())).hasSize(3);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId())
.stream()
.map(e -> e.getValue())
.collect(Collectors.toList())).contains(word1, word2, word3);
// Act & Assert: Add words multiple times again; No duplicate should exist
entries = new ArrayList<>();
entries.add(word1);
entries.add(word2);
entries.add(word1);
entries.add(word1);
entries.add(word2);
entries.add(word3);
dictionaryClient.addEntries(type.getTypeId(), entries, false);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId())).hasSize(3);
assertThat(dictionaryClient.getEntriesForType(type.getTypeId())
.stream()
.map(e -> e.getValue())
.collect(Collectors.toList())).contains(word1, word2, word3);
}
@Test
public void testCreateDossierDictionaryForTwoDossiers() {