From 2658de38855e0fb3c2e6a138fdc1070d5728c3d1 Mon Sep 17 00:00:00 2001 From: Andrey G Date: Sun, 19 May 2024 12:02:03 +0300 Subject: [PATCH] FMWK-439 Find by "enum in" fails with AerospikeException Parameter error (#741) * fix for 'findByIn' with a single element list * rename Person's property 'sex' to 'gender' --- .../data/aerospike/query/FilterOperation.java | 4 ++-- .../BaseBlockingIntegrationTests.java | 17 +++++++++++++++++ .../AerospikeTemplateFindByQueryTests.java | 13 ++++++++++--- .../core/AerospikeTemplateInsertTests.java | 19 +++++++++++++------ .../ReactiveAerospikeTemplateInsertTests.java | 4 ++-- .../noindex/PersonRepositoryQueryTests.java | 4 ++-- .../noindex/findBy/CustomQueriesTests.java | 8 ++++---- .../blocking/noindex/findBy/EqualsTests.java | 2 +- .../blocking/noindex/findBy/InTests.java | 13 +++++++++++++ .../data/aerospike/sample/IndexedPerson.java | 2 +- .../data/aerospike/sample/Person.java | 4 ++-- .../aerospike/sample/PersonRepository.java | 4 +++- 12 files changed, 70 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/springframework/data/aerospike/query/FilterOperation.java b/src/main/java/org/springframework/data/aerospike/query/FilterOperation.java index 30efd73a8..e5376084d 100644 --- a/src/main/java/org/springframework/data/aerospike/query/FilterOperation.java +++ b/src/main/java/org/springframework/data/aerospike/query/FilterOperation.java @@ -126,7 +126,7 @@ public Exp filterExp(Map qualifierMap) { .getFilterExp() ).toArray(Exp[]::new); - return Exp.or(arrElementsExp); + return arrElementsExp.length > 1 ? Exp.or(arrElementsExp) : arrElementsExp[0]; }); } @@ -150,7 +150,7 @@ public Exp filterExp(Map qualifierMap) { .getFilterExp() ).toArray(Exp[]::new); - return Exp.and(arrElementsExp); + return arrElementsExp.length > 1 ? Exp.and(arrElementsExp) : arrElementsExp[0]; }); } diff --git a/src/test/java/org/springframework/data/aerospike/BaseBlockingIntegrationTests.java b/src/test/java/org/springframework/data/aerospike/BaseBlockingIntegrationTests.java index aa9d98960..a631875aa 100644 --- a/src/test/java/org/springframework/data/aerospike/BaseBlockingIntegrationTests.java +++ b/src/test/java/org/springframework/data/aerospike/BaseBlockingIntegrationTests.java @@ -168,4 +168,21 @@ protected boolean queryHasSecIndexFilter(String namespace, String setName, Query throw new IllegalArgumentException("The result of conversion is not a Map, expecting only a POJO argument"); } + + /** + * Delete all entities of a class or a set. + * + * @param objectsToDelete Each of the objects must be either a Class or a String. + */ + protected void deleteAll(Object... objectsToDelete) { + for (Object toDelete : objectsToDelete) { + if (toDelete instanceof Class) { + template.deleteAll((Class) toDelete); + } else if (toDelete instanceof String) { + template.deleteAll((String) toDelete); + } else { + throw new IllegalArgumentException("Expecting either a Class or a String"); + } + } + } } diff --git a/src/test/java/org/springframework/data/aerospike/core/AerospikeTemplateFindByQueryTests.java b/src/test/java/org/springframework/data/aerospike/core/AerospikeTemplateFindByQueryTests.java index 0f673ffc6..633d73830 100644 --- a/src/test/java/org/springframework/data/aerospike/core/AerospikeTemplateFindByQueryTests.java +++ b/src/test/java/org/springframework/data/aerospike/core/AerospikeTemplateFindByQueryTests.java @@ -83,7 +83,8 @@ public class AerospikeTemplateFindByQueryTests extends BaseBlockingIntegrationTe @BeforeAll public void beforeAllSetUp() { - deleteOneByOne(allPersons); + template.deleteAll(Person.class); + template.deleteAll(OVERRIDE_SET_NAME); // batch write operations are supported starting with Server version 6.0+ if (serverVersionSupport.isBatchWriteSupported()) { @@ -118,8 +119,14 @@ public void setUp() { @AfterAll public void afterAll() { - deleteOneByOne(allPersons); - deleteOneByOne(allPersons, OVERRIDE_SET_NAME); + // batch write operations are supported starting with Server version 6.0+ + if (serverVersionSupport.isBatchWriteSupported()) { + template.deleteAll(allPersons); + template.deleteAll(allPersons, OVERRIDE_SET_NAME); + } else { + deleteOneByOne(allPersons); + deleteOneByOne(allPersons, OVERRIDE_SET_NAME); + } additionalAerospikeTestOperations.dropIndex(Person.class, "person_age_index"); additionalAerospikeTestOperations.dropIndex(Person.class, "person_first_name_index"); additionalAerospikeTestOperations.dropIndex(Person.class, "person_last_name_index"); diff --git a/src/test/java/org/springframework/data/aerospike/core/AerospikeTemplateInsertTests.java b/src/test/java/org/springframework/data/aerospike/core/AerospikeTemplateInsertTests.java index feac7988b..c2279c906 100644 --- a/src/test/java/org/springframework/data/aerospike/core/AerospikeTemplateInsertTests.java +++ b/src/test/java/org/springframework/data/aerospike/core/AerospikeTemplateInsertTests.java @@ -18,9 +18,11 @@ import com.aerospike.client.Key; import com.aerospike.client.Record; import com.aerospike.client.policy.Policy; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.OptimisticLockingFailureException; import org.springframework.data.aerospike.BaseBlockingIntegrationTests; @@ -42,14 +44,19 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.springframework.data.aerospike.sample.SampleClasses.VersionedClass; +@TestInstance(TestInstance.Lifecycle.PER_CLASS) public class AerospikeTemplateInsertTests extends BaseBlockingIntegrationTests { @BeforeEach public void beforeEach() { - template.deleteAll(Person.class); - template.deleteAll(CustomCollectionClass.class); - template.deleteAll(DocumentWithByteArray.class); - template.deleteAll(VersionedClass.class); + deleteAll(Person.class, CustomCollectionClass.class, DocumentWithByteArray.class, VersionedClass.class, + OVERRIDE_SET_NAME); + } + + @AfterAll + public void afterAll() { + deleteAll(Person.class, CustomCollectionClass.class, DocumentWithByteArray.class, VersionedClass.class, + OVERRIDE_SET_NAME); } @Test @@ -77,7 +84,7 @@ public void insertsDocumentWithListMapDateStringLongValues() { .strings(Arrays.asList("a", "b", "c")) .friend(new Person(null, "Anna", 43)) .isActive(true) - .sex(Person.Sex.MALE) + .gender(Person.Gender.MALE) .dateOfBirth(new Date()) .build(); template.insert(customer); @@ -99,7 +106,7 @@ public void insertsDocumentWithListMapDateStringLongValuesAndSetName() { .strings(Arrays.asList("a", "b", "c")) .friend(new Person(null, "Anna", 43)) .isActive(true) - .sex(Person.Sex.MALE) + .gender(Person.Gender.MALE) .dateOfBirth(new Date()) .build(); template.insert(customer, OVERRIDE_SET_NAME); diff --git a/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateInsertTests.java b/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateInsertTests.java index 18d81f2e1..3e5e3e67e 100644 --- a/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateInsertTests.java +++ b/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateInsertTests.java @@ -75,7 +75,7 @@ public void insertsDocumentWithListMapDateStringLongValues() { .strings(Arrays.asList("a", "b", "c")) .friend(new Person(null, "Anna", 43)) .isActive(true) - .sex(Person.Sex.MALE) + .gender(Person.Gender.MALE) .dateOfBirth(new Date()) .build(); @@ -100,7 +100,7 @@ public void insertsDocumentWithListMapDateStringLongValuesAndSetName() { .strings(Arrays.asList("a", "b", "c")) .friend(new Person(null, "Anna", 43)) .isActive(true) - .sex(Person.Sex.MALE) + .gender(Person.Gender.MALE) .dateOfBirth(new Date()) .build(); diff --git a/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/PersonRepositoryQueryTests.java b/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/PersonRepositoryQueryTests.java index e17de349b..c8c8be689 100644 --- a/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/PersonRepositoryQueryTests.java +++ b/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/PersonRepositoryQueryTests.java @@ -13,7 +13,7 @@ import java.util.List; -import static org.springframework.data.aerospike.sample.Person.Sex.FEMALE; +import static org.springframework.data.aerospike.sample.Person.Gender.FEMALE; @TestInstance(TestInstance.Lifecycle.PER_CLASS) public class PersonRepositoryQueryTests extends BaseBlockingIntegrationTests { @@ -45,7 +45,7 @@ public class PersonRepositoryQueryTests extends BaseBlockingIntegrationTests { .id(nextId()) .firstName("Alicia") .lastName("Keys") - .sex(FEMALE) + .gender(FEMALE) .age(30) .ints(List.of(550, 600, 990)) .build(); diff --git a/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/findBy/CustomQueriesTests.java b/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/findBy/CustomQueriesTests.java index a7435c58c..a1a40cb80 100644 --- a/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/findBy/CustomQueriesTests.java +++ b/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/findBy/CustomQueriesTests.java @@ -286,12 +286,12 @@ private MappingAerospikeConverter getMappingAerospikeConverter(AerospikeCustomCo @Test void findBySimplePropertyEquals_Enum() { - Qualifier sexEqFemale = Qualifier.builder() - .setBinName("sex") + Qualifier genderEqFemale = Qualifier.builder() + .setBinName("gender") .setFilterOperation(FilterOperation.EQ) - .setValue(Value.get(Person.Sex.FEMALE)) + .setValue(Value.get(Person.Gender.FEMALE)) .build(); - assertThat(repository.findUsingQuery(new Query(sexEqFemale))).containsOnly(alicia); + assertThat(repository.findUsingQuery(new Query(genderEqFemale))).containsOnly(alicia); } } diff --git a/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/findBy/EqualsTests.java b/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/findBy/EqualsTests.java index 866c8b80b..213a131c7 100644 --- a/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/findBy/EqualsTests.java +++ b/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/findBy/EqualsTests.java @@ -55,7 +55,7 @@ void findBySimplePropertyEquals_String_NegativeTest() { @Test void findBySimplePropertyEquals_Enum() { - List result = repository.findBySex(Person.Sex.FEMALE); + List result = repository.findByGender(Person.Gender.FEMALE); assertThat(result).containsOnly(alicia); } diff --git a/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/findBy/InTests.java b/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/findBy/InTests.java index 620a58078..8e5e426eb 100644 --- a/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/findBy/InTests.java +++ b/src/test/java/org/springframework/data/aerospike/repository/query/blocking/noindex/findBy/InTests.java @@ -35,6 +35,19 @@ void findByNestedSimplePropertyIn_String() { .containsExactlyInAnyOrder(dave, carter); } + @Test + void findByEnumIn() { + List result; + result = repository.findByGenderIn(List.of(Person.Gender.FEMALE, Person.Gender.MALE)); + assertThat(result).contains(alicia); + + result = repository.findByGenderIn(List.of(Person.Gender.FEMALE)); + assertThat(result).contains(alicia); + + result = repository.findByGenderIn(List.of(Person.Gender.MALE)); + assertThat(result).isEmpty(); + } + @Test void findByCollectionIn() { if (serverVersionSupport.isFindByCDTSupported()) { diff --git a/src/test/java/org/springframework/data/aerospike/sample/IndexedPerson.java b/src/test/java/org/springframework/data/aerospike/sample/IndexedPerson.java index 0cebb6aaa..cbd4b4e30 100644 --- a/src/test/java/org/springframework/data/aerospike/sample/IndexedPerson.java +++ b/src/test/java/org/springframework/data/aerospike/sample/IndexedPerson.java @@ -34,7 +34,7 @@ public static IndexedPerson from(Person person) { .lastName(person.getLastName()) .age(person.getAge()) .waist(person.getWaist()) - .sex(person.getSex()) + .gender(person.getGender()) .intMap(person.getIntMap()) .stringMap(person.getStringMap()) .friend(person.getFriend()) diff --git a/src/test/java/org/springframework/data/aerospike/sample/Person.java b/src/test/java/org/springframework/data/aerospike/sample/Person.java index 32bee7e7a..506fec27f 100644 --- a/src/test/java/org/springframework/data/aerospike/sample/Person.java +++ b/src/test/java/org/springframework/data/aerospike/sample/Person.java @@ -49,7 +49,7 @@ public class Person { private BigInteger ageBigInteger; private BigDecimal ageBigDecimal; private int waist; - private Sex sex; + private Gender gender; private Map stringMap; private Map intMap; private Map longIntMap; @@ -100,7 +100,7 @@ public PersonSomeFields toPersonSomeFields() { .build(); } - public enum Sex { + public enum Gender { MALE, FEMALE } } diff --git a/src/test/java/org/springframework/data/aerospike/sample/PersonRepository.java b/src/test/java/org/springframework/data/aerospike/sample/PersonRepository.java index 1ede5bb2d..67033b8fc 100644 --- a/src/test/java/org/springframework/data/aerospike/sample/PersonRepository.java +++ b/src/test/java/org/springframework/data/aerospike/sample/PersonRepository.java @@ -1326,7 +1326,9 @@ List

findByFriendStringMapNotContaining(AerospikeQueryCriterion criterion, List

findByFirstName(String name); - List

findBySex(Person.Sex sex); + List

findByGender(Person.Gender gender); + + List

findByGenderIn(List list); List

findByFirstNameIs(String name);