Skip to content

Commit

Permalink
move tests to a separate class to check
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr committed Oct 1, 2024
1 parent 8394cf8 commit fcbf465
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 54 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,9 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
<!-- <include>**/*Test.java</include>-->
<!-- <include>**/*Tests.java</include>-->
<include>**/AerospikeTemplateSaveWithDuplicatesTests.java</include>
</includes>
<argLine>
--add-opens java.base/java.util=ALL-UNNAMED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.aerospike.client.Record;
import com.aerospike.client.policy.Policy;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.dao.ConcurrencyFailureException;
Expand Down Expand Up @@ -366,57 +365,6 @@ public void shouldSaveAllAndSetVersionWithSetName() {
template.delete(second, OVERRIDE_SET_NAME); // cleanup
}

@Disabled // TODO: fix and enable
@Test
public void shouldSaveAllVersionedDocumentsAndSetVersionAndThrowExceptionIfDuplicatesWithinOneBatch() {
// batch write operations are supported starting with Server version 6.0+
if (serverVersionSupport.isBatchWriteSupported()) {
VersionedClass first = new VersionedClass("newId1", "foo");
VersionedClass second = new VersionedClass("newId2", "foo");

// The documents’ versions are equal to zero, meaning the documents have not been saved to the database yet
assertThat(first.getVersion()).isSameAs(0);
assertThat(second.getVersion()).isSameAs(0);

// An attempt to save the same versioned documents in one batch results in getting an exception
assertThatThrownBy(() -> template.saveAll(List.of(first, first, second, second)))
.isInstanceOf(OptimisticLockingFailureException.class)
.hasMessageFindingMatch("Failed to save the record with ID .* due to versions mismatch");

// The documents' versions get updated after they are read from the corresponding database records
assertThat(first.getVersion()).isSameAs(1); // TODO: fix "0 instead of 1" assertion error
assertThat(second.getVersion()).isSameAs(1);

template.delete(first); // cleanup
template.delete(second); // cleanup
}
}

@Disabled // TODO: fix and enable
@Test
public void shouldSaveAllVersionedDocumentsIfDuplicatesNotWithinOneBatch() {
// batch write operations are supported starting with Server version 6.0+
if (serverVersionSupport.isBatchWriteSupported()) {
// The same versioned documents can be saved if they are not in the same batch.
// This way, the generation counts of the corresponding database records can be used
// to update the documents’ versions each time.
VersionedClass newFirst = new VersionedClass("newId1", "foo");
VersionedClass newSecond = new VersionedClass("newId2", "bar");

assertThat(newFirst.getVersion()).isSameAs(0);
assertThat(newSecond.getVersion()).isSameAs(0);

template.saveAll(List.of(newFirst, newSecond)); // TODO: OptimisticLockingFailure
// Failed to save the record with ID 'newId2' due to versions mismatch
assertThat(newFirst.getVersion()).isSameAs(1);
assertThat(newSecond.getVersion()).isSameAs(1);

template.saveAll(List.of(newFirst, newSecond));
assertThat(newFirst.getVersion()).isSameAs(2);
assertThat(newSecond.getVersion()).isSameAs(2);
}
}

@Test
public void shouldSaveAllNotVersionedDocumentsIfAlreadyExist() {
// batch write operations are supported starting with Server version 6.0+
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2019 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.aerospike.core.sync;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.aerospike.BaseBlockingIntegrationTests;
import org.springframework.data.aerospike.sample.Person;
import org.springframework.data.aerospike.sample.SampleClasses;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.springframework.data.aerospike.sample.SampleClasses.CustomCollectionClass;
import static org.springframework.data.aerospike.sample.SampleClasses.DocumentWithTouchOnRead;
import static org.springframework.data.aerospike.sample.SampleClasses.VersionedClass;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class AerospikeTemplateSaveWithDuplicatesTests extends BaseBlockingIntegrationTests {

@AfterAll
public void afterAll() {
template.deleteAll(VersionedClass.class);
template.deleteAll(SampleClasses.DocumentWithIntArray.class);
template.deleteAll(SampleClasses.DocumentWithBigIntegerAndNestedArray.class);
template.deleteAll(Person.class);
template.deleteAll(CustomCollectionClass.class);
template.deleteAll(DocumentWithTouchOnRead.class);
template.deleteAll(OVERRIDE_SET_NAME);
}

// @Disabled // TODO: fix and enable
@Test
public void shouldSaveAllVersionedDocumentsAndSetVersionAndThrowExceptionIfDuplicatesWithinOneBatch() {
// batch write operations are supported starting with Server version 6.0+
if (serverVersionSupport.isBatchWriteSupported()) {
VersionedClass first = new VersionedClass("newId1", "foo");
VersionedClass second = new VersionedClass("newId2", "foo");

// The documents’ versions are equal to zero, meaning the documents have not been saved to the database yet
assertThat(first.getVersion()).isSameAs(0);
assertThat(second.getVersion()).isSameAs(0);

// An attempt to save the same versioned documents in one batch results in getting an exception
assertThatThrownBy(() -> template.saveAll(List.of(first, first, second, second)))
.isInstanceOf(OptimisticLockingFailureException.class)
.hasMessageFindingMatch("Failed to save the record with ID .* due to versions mismatch");

// The documents' versions get updated after they are read from the corresponding database records
assertThat(first.getVersion()).isSameAs(1); // TODO: fix "0 instead of 1" assertion error
assertThat(second.getVersion()).isSameAs(1);

template.delete(first); // cleanup
template.delete(second); // cleanup
}
}

// @Disabled // TODO: fix and enable
@Test
public void shouldSaveAllVersionedDocumentsIfDuplicatesNotWithinOneBatch() {
// batch write operations are supported starting with Server version 6.0+
if (serverVersionSupport.isBatchWriteSupported()) {
// The same versioned documents can be saved if they are not in the same batch.
// This way, the generation counts of the corresponding database records can be used
// to update the documents’ versions each time.
VersionedClass newFirst = new VersionedClass("newId1", "foo");
VersionedClass newSecond = new VersionedClass("newId2", "bar");

assertThat(newFirst.getVersion()).isSameAs(0);
assertThat(newSecond.getVersion()).isSameAs(0);

template.saveAll(List.of(newFirst, newSecond)); // TODO: OptimisticLockingFailure
// Failed to save the record with ID 'newId2' due to versions mismatch
assertThat(newFirst.getVersion()).isSameAs(1);
assertThat(newSecond.getVersion()).isSameAs(1);

template.saveAll(List.of(newFirst, newSecond));
assertThat(newFirst.getVersion()).isSameAs(2);
assertThat(newSecond.getVersion()).isSameAs(2);
}
}
}

0 comments on commit fcbf465

Please sign in to comment.