From 060e31d1fd6e117056eb8e4153e0429e1807c001 Mon Sep 17 00:00:00 2001 From: agrgr Date: Mon, 26 Feb 2024 14:42:36 +0200 Subject: [PATCH] add test for an object with nested POJOs when typeKey == null --- .../convert/AerospikeTypeAliasAccessor.java | 16 ++++++------ .../MappingAerospikeConverterTypesTests.java | 26 +++++++++++++++++++ .../query/AerospikeQueryCreatorUnitTests.java | 3 +-- .../data/aerospike/sample/SampleClasses.java | 8 ++++++ 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/springframework/data/aerospike/convert/AerospikeTypeAliasAccessor.java b/src/main/java/org/springframework/data/aerospike/convert/AerospikeTypeAliasAccessor.java index ab8e9682a..3e7ad0a5f 100644 --- a/src/main/java/org/springframework/data/aerospike/convert/AerospikeTypeAliasAccessor.java +++ b/src/main/java/org/springframework/data/aerospike/convert/AerospikeTypeAliasAccessor.java @@ -24,28 +24,28 @@ public class AerospikeTypeAliasAccessor implements TypeAliasAccessor> { - private final String typeKey; + private final String classKey; - public AerospikeTypeAliasAccessor(String typeKey) { - this.typeKey = typeKey; + public AerospikeTypeAliasAccessor(String classKey) { + this.classKey = classKey; } public AerospikeTypeAliasAccessor() { - this.typeKey = CLASS_KEY; + this.classKey = CLASS_KEY; } @Override public Alias readAliasFrom(Map source) { - if (typeKey == null) { + if (classKey == null) { return Alias.NONE; } - return Alias.ofNullable(source.get(typeKey)); + return Alias.ofNullable(source.get(classKey)); } @Override public void writeTypeTo(Map sink, Object alias) { - if (typeKey != null) { - sink.put(typeKey, alias); + if (classKey != null) { + sink.put(classKey, alias); } } } diff --git a/src/test/java/org/springframework/data/aerospike/convert/MappingAerospikeConverterTypesTests.java b/src/test/java/org/springframework/data/aerospike/convert/MappingAerospikeConverterTypesTests.java index 619fab56f..a8b7fa467 100644 --- a/src/test/java/org/springframework/data/aerospike/convert/MappingAerospikeConverterTypesTests.java +++ b/src/test/java/org/springframework/data/aerospike/convert/MappingAerospikeConverterTypesTests.java @@ -753,6 +753,32 @@ void shouldWriteAsByteArrayAndReadAsArrayList() { (byte) 3))); } + @Test + void shouldWriteAndReadNestedPOJOs() { + MappingAerospikeConverter converter = + getMappingAerospikeConverter(settings, new AerospikeTypeAliasAccessor(null)); + + AerospikeWriteData forWrite = AerospikeWriteData.forWrite(NAMESPACE); + List
addressesList = List.of( + new Address(new Street("Street1", 1), 1), + new Address(new Street("Street2", 2), 2) + ); + SampleClasses.idAndAddressesList testObj = new idAndAddressesList("testId", addressesList); + converter.write(testObj, forWrite); + + assertThat(forWrite.getBins()).containsOnly( + new Bin("addresses", List.of( + Map.of("apartment", 1, "street", Map.of("name", "Street1", "number", 1)), + Map.of("apartment", 2, "street", Map.of("name", "Street2", "number", 2)) + )) + ); + + AerospikeReadData forRead = AerospikeReadData.forRead(forWrite.getKey(), aeroRecord(forWrite.getBins())); + SampleClasses.idAndAddressesList actual = converter.read(idAndAddressesList.class, forRead); + + assertThat(actual).isEqualTo(new idAndAddressesList("testId", addressesList)); + } + private void assertWriteAndRead(int converterOption, T object, String expectedSet, diff --git a/src/test/java/org/springframework/data/aerospike/repository/query/AerospikeQueryCreatorUnitTests.java b/src/test/java/org/springframework/data/aerospike/repository/query/AerospikeQueryCreatorUnitTests.java index d39d9d4c4..bcfa8be0a 100644 --- a/src/test/java/org/springframework/data/aerospike/repository/query/AerospikeQueryCreatorUnitTests.java +++ b/src/test/java/org/springframework/data/aerospike/repository/query/AerospikeQueryCreatorUnitTests.java @@ -46,7 +46,7 @@ public void createsQueryCorrectly() { AerospikeQueryCreator creator = new AerospikeQueryCreator( tree, new StubParameterAccessor("Oliver"), context, converter); - Query query = creator.createQuery(); + creator.createQuery(); } @Test @@ -66,7 +66,6 @@ tree2, new StubParameterAccessor( QueryParam.of(new Person("id", "firstName")) ), context, converter); creator2.createQuery(); - } private MappingAerospikeConverter getMappingAerospikeConverter(AerospikeCustomConversions conversions) { diff --git a/src/test/java/org/springframework/data/aerospike/sample/SampleClasses.java b/src/test/java/org/springframework/data/aerospike/sample/SampleClasses.java index 21f73fe34..f9f5c499c 100644 --- a/src/test/java/org/springframework/data/aerospike/sample/SampleClasses.java +++ b/src/test/java/org/springframework/data/aerospike/sample/SampleClasses.java @@ -551,6 +551,14 @@ private Person(Set
addresses) { } } + @Data + public static class idAndAddressesList { + + @Id + final String id; + final List
addresses; + } + @Data @Document(collection = "versioned-set") public static class VersionedClass {