Skip to content

FMWK-467 Issue using refs with yaml file #159

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

Closed
wants to merge 2 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,12 @@ public Builder mappingToBin(String name) {
}

public Builder beingReferencedBy(AerospikeReference.ReferenceType type) {
this.binConfig.setReference(new ReferenceConfig(type, false));
this.binConfig.setReference(new ReferenceConfig(type, false, true));
return this.end();
}

public Builder beingLazilyReferencedBy(AerospikeReference.ReferenceType type) {
this.binConfig.setReference(new ReferenceConfig(type, true));
this.binConfig.setReference(new ReferenceConfig(type, true, true));
return this.end();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ public class ReferenceConfig {
private Boolean batchLoad;

public ReferenceConfig() {}
public ReferenceConfig(ReferenceType type, boolean lazy) {
public ReferenceConfig(ReferenceType type, boolean lazy, boolean batchLoad) {
this.type = type;
this.lazy = lazy;
this.batchLoad = batchLoad;
}

public ReferenceType getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ private static TypeMapper getMapper(Class<?> clazz, AnnotatedType type, IBaseAer
} else {
// Reference
ReferenceConfig ref = binConfig.getReference();
typeMapper = new ObjectReferenceMapper(ClassCache.getInstance().loadClass(clazz, mapper), ref.getLazy(), ref.getBatchLoad(), ref.getType(), mapper);
typeMapper = new ObjectReferenceMapper(
ClassCache.getInstance().loadClass(clazz, mapper),
ref.getLazy() == null? false : ref.getLazy(),
ref.getBatchLoad() == null ? true : ref.getBatchLoad(),
ref.getType(), mapper);
addToMap = false;
}
} else {
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");
});
}
}