diff --git a/src/main/java/org/springframework/data/aerospike/core/AerospikeTemplate.java b/src/main/java/org/springframework/data/aerospike/core/AerospikeTemplate.java index d77d76d7d..93f75ffb6 100644 --- a/src/main/java/org/springframework/data/aerospike/core/AerospikeTemplate.java +++ b/src/main/java/org/springframework/data/aerospike/core/AerospikeTemplate.java @@ -152,7 +152,7 @@ public void save(T document, String setName) { @Override public void saveAll(Iterable documents) { - validateForBatchWrite(documents, "IDsDocuments for saving"); + validateForBatchWrite(documents, "Documents for saving"); saveAll(documents, getSetName(documents.iterator().next())); } @@ -460,11 +460,8 @@ private void deleteByIds(Collection ids, String setName) { @Override public void deleteByIds(GroupedKeys groupedKeys) { - Assert.notNull(groupedKeys, "Grouped keys must not be null!"); - Assert.notNull(groupedKeys.getEntitiesKeys(), "Entities keys must not be null!"); + validateGroupedKeys(groupedKeys); Assert.notEmpty(groupedKeys.getEntitiesKeys(), "Entities keys must not be empty!"); - Assert.isTrue(batchWriteSupported(), "Batch write operations are supported starting with the major " + - "server version " + SERVER_VERSION_6 + ", see serverMajorVersion configuration parameter"); if (groupedKeys.getEntitiesKeys() == null || groupedKeys.getEntitiesKeys().isEmpty()) { return; @@ -839,9 +836,7 @@ public List findByIds(Iterable ids, Class entityClass, Class @Override public GroupedEntities findByIds(GroupedKeys groupedKeys) { - Assert.notNull(groupedKeys, "Grouped keys must not be null!"); - Assert.isTrue(batchWriteSupported(), "Batch write operations are supported starting with the major " + - "server version " + SERVER_VERSION_6 + ", see serverMajorVersion configuration parameter"); + validateGroupedKeys(groupedKeys); if (groupedKeys.getEntitiesKeys() == null || groupedKeys.getEntitiesKeys().isEmpty()) { return GroupedEntities.builder().build(); diff --git a/src/main/java/org/springframework/data/aerospike/core/BaseAerospikeTemplate.java b/src/main/java/org/springframework/data/aerospike/core/BaseAerospikeTemplate.java index b663af7ff..f09522928 100644 --- a/src/main/java/org/springframework/data/aerospike/core/BaseAerospikeTemplate.java +++ b/src/main/java/org/springframework/data/aerospike/core/BaseAerospikeTemplate.java @@ -463,6 +463,13 @@ public BatchWriteData getBatchWriteForUpdate(T document, String setName) entity.hasVersionProperty()); } + protected void validateGroupedKeys(GroupedKeys groupedKeys) { + Assert.notNull(groupedKeys, "Grouped keys must not be null!"); + Assert.notNull(groupedKeys.getEntitiesKeys(), "Entities keys must not be null!"); + Assert.isTrue(batchWriteSupported(), "Batch write operations are supported starting with the major " + + "server version " + SERVER_VERSION_6 + ", see serverMajorVersion configuration parameter"); + } + protected boolean batchWriteSizeMatch(int batchSize, int currentSize) { return batchSize > 0 && currentSize == batchSize; } diff --git a/src/main/java/org/springframework/data/aerospike/core/ReactiveAerospikeTemplate.java b/src/main/java/org/springframework/data/aerospike/core/ReactiveAerospikeTemplate.java index 626a1ad66..d5d816143 100644 --- a/src/main/java/org/springframework/data/aerospike/core/ReactiveAerospikeTemplate.java +++ b/src/main/java/org/springframework/data/aerospike/core/ReactiveAerospikeTemplate.java @@ -483,11 +483,8 @@ private Mono batchDeleteAndCheckForErrors(IAerospikeReactorClient reactorC @Override public Mono deleteByIds(GroupedKeys groupedKeys) { - Assert.notNull(groupedKeys, "Grouped keys must not be null!"); - Assert.notNull(groupedKeys.getEntitiesKeys(), "Entities keys must not be null!"); + validateGroupedKeys(groupedKeys); Assert.notEmpty(groupedKeys.getEntitiesKeys(), "Entities keys must not be empty!"); - Assert.isTrue(batchWriteSupported(), "Batch write operations are supported starting with the major " + - "server version " + SERVER_VERSION_6 + ", see serverMajorVersion configuration parameter"); if (groupedKeys.getEntitiesKeys() == null || groupedKeys.getEntitiesKeys().isEmpty()) { return Mono.empty(); @@ -766,9 +763,7 @@ private Flux findByIds(Collection ids, Class targetClass, String se @Override public Mono findByIds(GroupedKeys groupedKeys) { - Assert.notNull(groupedKeys, "Grouped keys must not be null!"); - Assert.isTrue(batchWriteSupported(), "Batch write operations are supported starting with the major " + - "server version " + SERVER_VERSION_6 + ", see serverMajorVersion configuration parameter"); + validateGroupedKeys(groupedKeys); if (groupedKeys.getEntitiesKeys() == null || groupedKeys.getEntitiesKeys().isEmpty()) { return Mono.just(GroupedEntities.builder().build()); diff --git a/src/test/java/org/springframework/data/aerospike/core/AbstractFindByEntitiesTest.java b/src/test/java/org/springframework/data/aerospike/core/AbstractFindByEntitiesTest.java index 640311e8e..74d6f357f 100644 --- a/src/test/java/org/springframework/data/aerospike/core/AbstractFindByEntitiesTest.java +++ b/src/test/java/org/springframework/data/aerospike/core/AbstractFindByEntitiesTest.java @@ -25,8 +25,8 @@ public interface AbstractFindByEntitiesTest { @Test default void shouldFindAllRequestedEntities() { - List persons = generatePersons(5); - List customers = generateCustomers(3); + List persons = saveGeneratedPersons(5); + List customers = saveGeneratedCustomers(3); GroupedKeys groupedKeys = getGroupedKeys(persons, customers); GroupedEntities byIds = findByIds(groupedKeys); @@ -38,8 +38,8 @@ default void shouldFindAllRequestedEntities() { @Test default void shouldReturnAnEmptyResultIfKeysWhereSetToWrongEntities() { - List persons = generatePersons(5); - List customers = generateCustomers(3); + List persons = saveGeneratedPersons(5); + List customers = saveGeneratedCustomers(3); Set requestedPersonsIds = persons.stream() .map(Person::getId) @@ -59,8 +59,8 @@ default void shouldReturnAnEmptyResultIfKeysWhereSetToWrongEntities() { @Test default void shouldFindSomeOfIdsOfRequestedEntities() { - List persons = generatePersons(2); - List customers = generateCustomers(3); + List persons = saveGeneratedPersons(2); + List customers = saveGeneratedCustomers(3); GroupedKeys requestMapWithRandomExtraIds = getGroupedEntitiesKeysWithRandomExtraIds(persons, customers); GroupedEntities results = findByIds(requestMapWithRandomExtraIds); @@ -72,7 +72,7 @@ default void shouldFindSomeOfIdsOfRequestedEntities() { @Test default void shouldFindResultsOfOneOfRequestedEntity() { - List persons = generatePersons(3); + List persons = saveGeneratedPersons(3); GroupedKeys groupedKeysWithRandomExtraIds = getGroupedEntitiesKeysWithRandomExtraIds(persons, emptyList()); GroupedEntities results = findByIds(groupedKeysWithRandomExtraIds); @@ -84,7 +84,7 @@ default void shouldFindResultsOfOneOfRequestedEntity() { @Test default void shouldFindForOneEntityIfAnotherContainsEmptyRequestList() { - List persons = generatePersons(3); + List persons = saveGeneratedPersons(3); GroupedKeys groupedKeys = getGroupedKeys(persons, emptyList()); GroupedEntities batchGroupedEntities = findByIds(groupedKeys); @@ -126,7 +126,7 @@ default void shouldReturnMapWithEmptyResultsIfNoEntitiesWhereFound() { @Test default void shouldThrowMappingExceptionOnNonAerospikeEntityClass() { - List persons = generatePersons(2); + List persons = saveGeneratedPersons(2); Set personIds = persons.stream() .map(Person::getId) .collect(Collectors.toSet()); @@ -188,7 +188,7 @@ default GroupedKeys getGroupedEntitiesKeysWithRandomExtraIds(Collection .build(); } - default List generateCustomers(int count) { + default List saveGeneratedCustomers(int count) { return IntStream.range(0, count) .mapToObj(i -> Customer.builder().id(nextId()) .firstName("firstName" + i) @@ -198,7 +198,7 @@ default List generateCustomers(int count) { .collect(Collectors.toList()); } - default List generatePersons(int count) { + default List saveGeneratedPersons(int count) { return IntStream.range(0, count) .mapToObj(i -> Person.builder().id(nextId()) .firstName("firstName" + i) 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 fcf0f3ca2..5c6b51754 100644 --- a/src/test/java/org/springframework/data/aerospike/core/AerospikeTemplateInsertTests.java +++ b/src/test/java/org/springframework/data/aerospike/core/AerospikeTemplateInsertTests.java @@ -19,8 +19,8 @@ import com.aerospike.client.Key; import com.aerospike.client.Record; import com.aerospike.client.policy.Policy; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.dao.DuplicateKeyException; import org.springframework.data.aerospike.BaseBlockingIntegrationTests; @@ -48,9 +48,12 @@ // this test class does not require secondary indexes created on startup public class AerospikeTemplateInsertTests extends BaseBlockingIntegrationTests { - @AfterEach - public void afterEach() { + @BeforeEach + public void beforeEach() { template.deleteAll(Person.class); + template.deleteAll(CustomCollectionClass.class); + template.deleteAll(DocumentWithByteArray.class); + template.deleteAll(VersionedClass.class); } @Test @@ -63,7 +66,6 @@ public void insertsAndFindsWithCustomCollectionSet() { assertThat(record.getString("data")).isEqualTo("data0"); CustomCollectionClass result = template.findById(id, CustomCollectionClass.class); assertThat(result).isEqualTo(initial); - template.delete(result); // cleanup } @Test @@ -86,7 +88,6 @@ public void insertsDocumentWithListMapDateStringLongValues() { Person actual = template.findById(id, Person.class); assertThat(actual).isEqualTo(customer); - template.delete(actual); // cleanup } @Test @@ -109,7 +110,6 @@ public void insertsDocumentWithListMapDateStringLongValuesAndSetName() { Person actual = template.findById(id, Person.class, OVERRIDE_SET_NAME); assertThat(actual).isEqualTo(customer); - template.delete(actual, OVERRIDE_SET_NAME); // cleanup } @Test @@ -119,7 +119,6 @@ public void insertsAndFindsDocumentWithByteArrayField() { DocumentWithByteArray result = template.findById(id, DocumentWithByteArray.class); assertThat(result).isEqualTo(document); - template.delete(result); // cleanup } @Test @@ -128,7 +127,6 @@ public void insertsDocumentWithNullFields() { template.insert(document); assertThat(document.getField()).isNull(); - template.delete(template.findById(id, VersionedClass.class)); // cleanup } @Test @@ -137,7 +135,6 @@ public void insertsDocumentWithZeroVersionIfThereIsNoDocumentWithSameKey() { template.insert(document); assertThat(document.getVersion()).isEqualTo(1); - template.delete(template.findById(id, VersionedClass.class)); // cleanup } @Test @@ -148,7 +145,6 @@ public void insertsDocumentWithVersionGreaterThanZeroIfThereIsNoDocumentWithSame template.insert(document); assertThat(document.getVersion()).isEqualTo(1); - template.delete(template.findById(id, VersionedClass.class)); // cleanup } @Test @@ -158,7 +154,6 @@ public void throwsExceptionForDuplicateId() { assertThatThrownBy(() -> template.insert(person)) .isInstanceOf(DuplicateKeyException.class); - template.delete(template.findById(id, Person.class)); // cleanup } @Test @@ -168,7 +163,6 @@ public void throwsExceptionForDuplicateIdForVersionedDocument() { template.insert(document); assertThatThrownBy(() -> template.insert(document)) .isInstanceOf(DuplicateKeyException.class); - template.delete(template.findById(id, VersionedClass.class)); // cleanup } @Test @@ -188,7 +182,6 @@ public void insertsOnlyFirstDocumentAndNextAttemptsShouldFailWithDuplicateKeyExc }); assertThat(duplicateKeyCounter.intValue()).isEqualTo(numberOfConcurrentSaves - 1); - template.delete(template.findById(id, VersionedClass.class)); // cleanup } @Test @@ -208,7 +201,6 @@ public void insertsOnlyFirstDocumentAndNextAttemptsShouldFailWithDuplicateKeyExc }); assertThat(duplicateKeyCounter.intValue()).isEqualTo(numberOfConcurrentSaves - 1); - template.delete(template.findById(id, Person.class)); // cleanup } @Test @@ -239,7 +231,6 @@ public void insertAll_insertsAllDocuments() { .collect(Collectors.toList()); result = template.findByIds(ids, Person.class); assertThat(result).hasSameElementsAs(personsToInsert); - template.deleteAll(Person.class); // cleanup } } @@ -262,7 +253,6 @@ public void insertAllWithSetName_insertsAllDocuments() { .collect(Collectors.toList()), Person.class, OVERRIDE_SET_NAME); assertThat(result).hasSameElementsAs(persons); - template.deleteAll(Person.class); // cleanup } @Test @@ -275,7 +265,6 @@ public void insertAll_rejectsDuplicateIds() { .isInstanceOf(AerospikeException.BatchRecordArray.class) .hasMessageContaining("Errors during batch insert"); Assertions.assertEquals(1, (long) first.getVersion()); - template.delete(first); // cleanup } } diff --git a/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateDeleteRelatedTests.java b/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateDeleteRelatedTests.java index 119e8866e..ef3476b9a 100644 --- a/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateDeleteRelatedTests.java +++ b/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateDeleteRelatedTests.java @@ -214,14 +214,6 @@ public void deleteAllFromDifferentSets_ShouldDeleteAllDocuments() { .entityKeys(SampleClasses.VersionedClass.class, List.of(entity2_1.getId(), entity2_2.getId())) .entityKeys(Person.class, List.of(entity3_1.getId(), entity3_2.getId())) .build(); -// reactiveTemplate.deleteByIds(GroupedKeys.builder() -// .entityKeys(SampleClasses.DocumentWithExpiration.class, List.of(entity1_1.getId())).build()).block(); -// .entityKeys(SampleClasses.DocumentWithExpiration.class, List.of(entity1_1.getId(), entity1_2.getId -// ())).build()).block(); -// reactiveTemplate.deleteByIds(GroupedKeys.builder() -// .entityKeys(SampleClasses.VersionedClass.class, List.of(entity2.getId())).build()).block(); -// .entityKeys(SampleClasses.VersionedClass.class, List.of(entity2_1.getId(), entity2_2.getId())) -// .build()).block(); reactiveTemplate.deleteByIds(groupedKeys).block(); List list1 = reactiveTemplate.findByIds( diff --git a/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateFindByQueryProjectionTest.java b/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateFindByQueryProjectionTest.java index 53cea122a..659591fef 100644 --- a/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateFindByQueryProjectionTest.java +++ b/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateFindByQueryProjectionTest.java @@ -32,6 +32,7 @@ public class ReactiveAerospikeTemplateFindByQueryProjectionTest extends BaseReac @BeforeAll public void beforeAllSetUp() { reactiveTemplate.deleteAll(Person.class); + reactiveTemplate.deleteAll(OVERRIDE_SET_NAME); additionalAerospikeTestOperations.createIndex( Person.class, "person_age_index", "age", IndexType.NUMERIC); additionalAerospikeTestOperations.createIndex( @@ -50,6 +51,7 @@ public void beforeAllSetUp() { @BeforeEach public void setUp() { super.setUp(); + reactiveTemplate.deleteAll(Person.class).block(); } @AfterAll @@ -59,7 +61,7 @@ public void afterAll() { additionalAerospikeTestOperations.dropIndex(Person.class, "person_first_name_index"); additionalAerospikeTestOperations.dropIndex(OVERRIDE_SET_NAME, "person_age_index" + OVERRIDE_SET_NAME); additionalAerospikeTestOperations.dropIndex(OVERRIDE_SET_NAME, "person_last_name_index" + OVERRIDE_SET_NAME); - additionalAerospikeTestOperations.dropIndex(OVERRIDE_SET_NAME, "person_first_name_index"+ OVERRIDE_SET_NAME); + additionalAerospikeTestOperations.dropIndex(OVERRIDE_SET_NAME, "person_first_name_index" + OVERRIDE_SET_NAME); } @Test diff --git a/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateUpdateTests.java b/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateUpdateTests.java index 882cb5df4..cc63d75dd 100644 --- a/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateUpdateTests.java +++ b/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateUpdateTests.java @@ -3,7 +3,7 @@ import com.aerospike.client.AerospikeException; import com.aerospike.client.Key; import com.aerospike.client.policy.Policy; -import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.dao.DataRetrievalFailureException; import org.springframework.dao.OptimisticLockingFailureException; @@ -31,8 +31,8 @@ // this test class does not require secondary indexes created on startup public class ReactiveAerospikeTemplateUpdateTests extends BaseReactiveIntegrationTests { - @AfterEach - public void afterEach() { + @BeforeEach + public void beforeEach() { reactiveTemplate.deleteAll(Person.class).block(); reactiveTemplate.deleteAll(VersionedClass.class).block(); } @@ -390,8 +390,6 @@ public void updateAllIfDocumentsChanged() { assertThat(result1.getFirstName()).isEqualTo("Wolfgang M"); assertThat(result2.getAge()).isEqualTo(age2); assertThat(result2.getFirstName()).isEqualTo("Johann B"); - reactiveTemplate.delete(result1).block(); // cleanup - reactiveTemplate.delete(result2).block(); // cleanup List persons = additionalAerospikeTestOperations.saveGeneratedPersons(101); Iterable personsWithUpdate = persons.stream()