Skip to content

Commit

Permalink
FMWK-305 Add test for an object with nested POJOs when typeKey is null (
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr authored Feb 26, 2024
1 parent bd1e162 commit 61a8692
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@

public class AerospikeTypeAliasAccessor implements TypeAliasAccessor<Map<String, Object>> {

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<String, Object> 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<String, Object> sink, Object alias) {
if (typeKey != null) {
sink.put(typeKey, alias);
if (classKey != null) {
sink.put(classKey, alias);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,32 @@ void shouldWriteAsByteArrayAndReadAsArrayList() {
(byte) 3)));
}

@Test
void shouldWriteAndReadNestedPOJOs() {
MappingAerospikeConverter converter =
getMappingAerospikeConverter(settings, new AerospikeTypeAliasAccessor(null));

AerospikeWriteData forWrite = AerospikeWriteData.forWrite(NAMESPACE);
List<Address> 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 <T> void assertWriteAndRead(int converterOption,
T object,
String expectedSet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void createsQueryCorrectly() {

AerospikeQueryCreator creator = new AerospikeQueryCreator(
tree, new StubParameterAccessor("Oliver"), context, converter);
Query query = creator.createQuery();
creator.createQuery();
}

@Test
Expand All @@ -66,7 +66,6 @@ tree2, new StubParameterAccessor(
QueryParam.of(new Person("id", "firstName"))
), context, converter);
creator2.createQuery();

}

private MappingAerospikeConverter getMappingAerospikeConverter(AerospikeCustomConversions conversions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,14 @@ private Person(Set<Address> addresses) {
}
}

@Data
public static class idAndAddressesList {

@Id
final String id;
final List<Address> addresses;
}

@Data
@Document(collection = "versioned-set")
public static class VersionedClass {
Expand Down

0 comments on commit 61a8692

Please sign in to comment.