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

FMWK-305 Add test for an object with nested POJOs when typeKey is null #707

Merged
merged 1 commit into from
Feb 26, 2024
Merged
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
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
Loading