Skip to content

Commit

Permalink
use custom converters bean and class key property
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr committed Dec 16, 2024
1 parent 014627c commit 7f99c0b
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
@AutoConfiguration
@ConditionalOnClass(IAerospikeClient.class)
@ConditionalOnProperty("spring.aerospike.hosts")
@EnableConfigurationProperties({AerospikeProperties.class, AerospikeDataProperties.class}) // TODO: shoud data properties be here?
@EnableConfigurationProperties({AerospikeProperties.class, AerospikeDataProperties.class}) // TODO: should data properties be here?
public class AerospikeAutoConfiguration {
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public abstract static class PolicyDefault {
public Duration sleepBetweenRetries;

/**
* If the key is sent on a write, the key will be stored with the record on the server.
* If the key is sent in a write operation, it will be stored with the record on the server.
*/
public Boolean sendKey;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.data.aerospike.config.AerospikeDataSettings;

/**
* Configuration properties for Spring Data Aerospike.
Expand All @@ -38,12 +37,12 @@ public class AerospikeDataProperties {
private String namespace;

/**
* Bin name that will be used for storing entity's type.
* Bin name that will be used for storing entity's type. Default value is null
* <p>
*
* @see org.springframework.data.aerospike.convert.AerospikeTypeAliasAccessor
*/
private String typeKey = "@_class";
private String classKey = "@_class";

/**
* Gives ability to disable queries that will run scan on Aerospike server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected String nameSpace() {
}

@Override
protected EventLoops eventLoops() {
public EventLoops eventLoops() {
return new NioEventLoops();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public static ClientPolicy getClientPolicyConfig(ClientPolicy clientPolicy, Aero
clientPolicy.batchUDFPolicyDefault = setupBatchUDFPolicy(properties);
// aerospikeEventLoops.ifPresent(loops -> clientPolicy.eventLoops = loops); // TODO

// clientPolicy.user = "tester";
// clientPolicy.password = "psw";

return clientPolicy;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.data.aerospike;

import com.aerospike.client.AerospikeClient;
import com.aerospike.client.IAerospikeClient;
import com.aerospike.client.reactor.AerospikeReactorClient;
import com.aerospike.client.reactor.IAerospikeReactorClient;
import com.aerospike.client.reactor.retry.AerospikeReactorRetryClient;
Expand Down Expand Up @@ -84,16 +85,16 @@ public void classKeyDefault() {
AerospikeTypeAliasAccessor aliasAccessor = context.getBean(AerospikeTypeAliasAccessor.class);
String classKey = getField(aliasAccessor, "classKey");

assertThat(classKey).isEqualTo(AerospikeConverter.CLASS_KEY);
assertThat(classKey).isEqualTo(AerospikeConverter.CLASS_KEY_DEFAULT);
});
}

// @Test // TODO: requires ability to configure typeKey
@Test
public void typeKeyCanBeCustomized() {
contextRunner
.withPropertyValues("spring.aerospike.hosts=localhost:3000")
.withPropertyValues("spring.data.aerospike.namespace=TEST")
.withPropertyValues("spring.data.aerospike.type-key=++amazing++")
.withPropertyValues("spring.data.aerospike.class-key=++amazing++")
.run((context) -> {
AerospikeTypeAliasAccessor aliasAccessor = context.getBean(AerospikeTypeAliasAccessor.class);
String typeKey = getField(aliasAccessor, "classKey");
Expand All @@ -102,12 +103,12 @@ public void typeKeyCanBeCustomized() {
});
}

// @Test
@Test
public void typeKeyCanBeNull() {
contextRunner
.withPropertyValues("spring.aerospike.hosts=localhost:3000")
.withPropertyValues("spring.data.aerospike.namespace=TEST")
.withPropertyValues("spring.data.aerospike.type-key=")
.withPropertyValues("spring.data.aerospike.class-key=")
.run((context) -> {
AerospikeTypeAliasAccessor aliasAccessor = context.getBean(AerospikeTypeAliasAccessor.class);
String typeKey = getField(aliasAccessor, "classKey");
Expand All @@ -116,9 +117,7 @@ public void typeKeyCanBeNull() {
});
}

// @Test
// TODO: there already is CustomConversions bean, needs a change: override the method
// or change SpringData Aerospike
@Test
public void customConversions() {
contextRunner
.withPropertyValues("spring.aerospike.hosts=localhost:3000")
Expand All @@ -130,6 +129,27 @@ public void customConversions() {
});
}

@Test
public void sendKeyPropertyIsRead() {
contextRunner
.withPropertyValues("spring.aerospike.hosts=localhost:3000")
.withPropertyValues("spring.data.aerospike.namespace=TEST")
.withPropertyValues("spring.aerospike.write.sendKey=false")
.run(context -> {
IAerospikeClient client = context.getBean(IAerospikeClient.class);
assertThat(client.getWritePolicyDefault().sendKey).isFalse();
});

contextRunner
.withPropertyValues("spring.aerospike.hosts=localhost:3000")
.withPropertyValues("spring.data.aerospike.namespace=TEST")
.withPropertyValues("spring.aerospike.write.sendKey=true")
.run(context -> {
IAerospikeClient client = context.getBean(IAerospikeClient.class);
assertThat(client.getWritePolicyDefault().sendKey).isTrue();
});
}

@Test
public void configurationIsApplied() {
contextRunner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void entityScanShouldSetInitialEntitySet() {
});
}

// @Test // TODO: requires ability to configure typeKey
@Test
public void classKeyDefault() {
contextRunner
.withPropertyValues("spring.aerospike.hosts=localhost:3000")
Expand All @@ -80,16 +80,16 @@ public void classKeyDefault() {
AerospikeTypeAliasAccessor aliasAccessor = context.getBean(AerospikeTypeAliasAccessor.class);
String typeKey = getField(aliasAccessor, "classKey");

assertThat(typeKey).isEqualTo(AerospikeConverter.CLASS_KEY);
assertThat(typeKey).isEqualTo(AerospikeConverter.CLASS_KEY_DEFAULT);
});
}

// @Test
@Test
public void classKeyCanBeCustomized() {
contextRunner
.withPropertyValues("spring.aerospike.hosts=localhost:3000")
.withPropertyValues("spring.data.aerospike.namespace=TEST")
.withPropertyValues("spring.data.aerospike.type-key=++amazing++")
.withPropertyValues("spring.data.aerospike.class-key=++amazing++")
.run((context) -> {
AerospikeTypeAliasAccessor aliasAccessor = context.getBean(AerospikeTypeAliasAccessor.class);
String typeKey = getField(aliasAccessor, "classKey");
Expand All @@ -98,16 +98,12 @@ public void classKeyCanBeCustomized() {
});
}

// @Test
// TODO: there already is CustomConversions bean, needs a change: override the method
// or change SpringData Aerospike
@Test
public void customConversions() {
contextRunner
.withPropertyValues("spring.aerospike.hosts=localhost:3000")
.withPropertyValues("spring.data.aerospike.namespace=TEST")
.withUserConfiguration(AerospikeTestConfigurations.CustomConversionsConfig.class,
AerospikeClientMockConfiguration.class, MockReactiveIndexRefresher.class,
AerospikeServerVersionSupportMockConfiguration.class)
.withUserConfiguration(AerospikeTestConfigurations.CustomConversionsConfig.class)
.run(context -> {
MappingAerospikeConverter converter = context.getBean(MappingAerospikeConverter.class);
assertThat(converter.getConversionService().canConvert(City.class, String.class)).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.aerospike.convert.AerospikeCustomConversions;
import org.springframework.data.aerospike.convert.AerospikeCustomConverters;
import org.springframework.data.aerospike.query.cache.ReactorIndexRefresher;
import org.springframework.data.aerospike.server.version.ServerVersionSupport;

Expand Down Expand Up @@ -60,9 +60,9 @@ public static class EntityScanConfiguration {
@AutoConfiguration
public static class CustomConversionsConfig {

@Bean(name = "aerospikeCustomConversions")
AerospikeCustomConversions myCustomConversions() {
return new AerospikeCustomConversions(List.of(new CityToStringConverter()));
@Bean
AerospikeCustomConverters myCustomConversions() {
return new AerospikeCustomConverters(List.of(new CityToStringConverter()));
}
}

Expand Down

0 comments on commit 7f99c0b

Please sign in to comment.