Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issues with RecordExistsAction #157

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/com/aerospike/mapper/tools/AeroMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ private <T> void save(WritePolicy writePolicy, @NotNull T object, RecordExistsAc
ClassCacheEntry<T> entry = MapperUtils.getEntryAndValidateNamespace(clazz, this);
if (writePolicy == null) {
writePolicy = new WritePolicy(entry.getWritePolicy());
if (recordExistsAction != null) {
if (recordExistsAction != null && (writePolicy.recordExistsAction == null || writePolicy.recordExistsAction == RecordExistsAction.UPDATE)) {
// Override the default with the passed policy. Only do this if the policy is already at the default.
// Otherwise, "save" with an INSERT_ONLY policy would fail for example.
writePolicy.recordExistsAction = recordExistsAction;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ private <T> Mono<T> save(WritePolicy writePolicy, @NotNull T object, RecordExist
ClassCacheEntry<T> entry = MapperUtils.getEntryAndValidateNamespace(clazz, this);
if (writePolicy == null) {
writePolicy = new WritePolicy(entry.getWritePolicy());
if (recordExistsAction != null) {
if (recordExistsAction != null && (writePolicy.recordExistsAction == null || writePolicy.recordExistsAction == RecordExistsAction.UPDATE)) {
// Override the default with the passed policy. Only do this if the policy is already at the default.
// Otherwise, "save" with an INSERT_ONLY policy would fail for example.
writePolicy.recordExistsAction = recordExistsAction;
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/aerospike/mapper/AeroMapperBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void setupClass() {
ClientPolicy policy = new ClientPolicy();
// Set event loops to use in asynchronous commands.
policy.eventLoops = new NioEventLoops(1);
client = new AerospikeClient(policy, "localhost", 3000);
client = new AerospikeClient(policy, "localhost", 3100);
}

@AfterAll
Expand Down
71 changes: 71 additions & 0 deletions src/test/java/com/aerospike/mapper/InsertOnlyModeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.aerospike.mapper;

import static org.junit.jupiter.api.Assertions.fail;

import org.junit.jupiter.api.Test;

import com.aerospike.client.AerospikeException;
import com.aerospike.client.policy.ClientPolicy;
import com.aerospike.client.policy.RecordExistsAction;
import com.aerospike.client.policy.WritePolicy;
import com.aerospike.mapper.annotations.AerospikeKey;
import com.aerospike.mapper.annotations.AerospikeRecord;
import com.aerospike.mapper.tools.AeroMapper;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

public class InsertOnlyModeTest extends AeroMapperBaseTest {
@AerospikeRecord(namespace = "test", set = "custs")
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Customer {
@AerospikeKey
private String name;
private int age;
}

@Test
public void tetDefaultPolicies() {
WritePolicy writePolicy = new WritePolicy();
writePolicy.sendKey = true;
writePolicy.recordExistsAction=RecordExistsAction.CREATE_ONLY;

ClientPolicy policy = new ClientPolicy();
policy.writePolicyDefault = writePolicy;

AeroMapper mapper = new AeroMapper.Builder(client).withWritePolicy(writePolicy).forAll().build();

Customer customer = new Customer("Tim", 312);
mapper.delete(customer);
// First one should succeed.
mapper.save(customer);
try {
mapper.save(customer);
fail("Expected an exception to be thrown");
}
catch (AerospikeException e) {
}
}
@Test
public void testExplicitPolicies() {
WritePolicy writePolicy = new WritePolicy();
writePolicy.sendKey = true;
writePolicy.recordExistsAction=RecordExistsAction.CREATE_ONLY;

AeroMapper mapper = new AeroMapper.Builder(client).withWritePolicy(writePolicy).forAll().build();

Customer customer = new Customer("Tim", 312);
mapper.delete(customer);
// First one should succeed.
mapper.save(customer);
try {
mapper.save(customer);
fail("Expected an exception to be thrown");
}
catch (AerospikeException e) {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.aerospike.mapper.reactive;

import static org.junit.jupiter.api.Assertions.fail;

import org.junit.jupiter.api.Test;

import com.aerospike.client.AerospikeException;
import com.aerospike.client.policy.ClientPolicy;
import com.aerospike.client.policy.RecordExistsAction;
import com.aerospike.client.policy.WritePolicy;
import com.aerospike.mapper.annotations.AerospikeKey;
import com.aerospike.mapper.annotations.AerospikeRecord;
import com.aerospike.mapper.tools.ReactiveAeroMapper;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

public class ReactiveInsertOnlyModeTest extends ReactiveAeroMapperBaseTest {
@AerospikeRecord(namespace = "test", set = "custs")
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Customer {
@AerospikeKey
private String name;
private int age;
}

@Test
public void tetDefaultPolicies() {
WritePolicy writePolicy = new WritePolicy();
writePolicy.sendKey = true;
writePolicy.recordExistsAction=RecordExistsAction.CREATE_ONLY;

ClientPolicy policy = new ClientPolicy();
policy.writePolicyDefault = writePolicy;

ReactiveAeroMapper mapper = new ReactiveAeroMapper.Builder(reactorClient).withWritePolicy(writePolicy).forAll().build();

Customer customer = new Customer("Tim", 312);
mapper.delete(customer);
// First one should succeed.
mapper.save(customer).doOnError(c -> fail("Expected an succcess"));
mapper.save(customer).doOnSuccess(c -> {
fail("Expected an exception to be thrown");
});
}
@Test
public void testExplicitPolicies() {
WritePolicy writePolicy = new WritePolicy();
writePolicy.sendKey = true;
writePolicy.recordExistsAction=RecordExistsAction.CREATE_ONLY;

ReactiveAeroMapper mapper = new ReactiveAeroMapper.Builder(reactorClient).withWritePolicy(writePolicy).forAll().build();

Customer customer = new Customer("Tim", 312);
mapper.delete(customer);
// First one should succeed.
mapper.save(customer).doOnError(c -> fail("Expected an succcess"));
mapper.save(customer).doOnSuccess(c -> {
fail("Expected an exception to be thrown");
});
}
}
Loading