Skip to content

Commit

Permalink
update validations to support "find by enum" queries
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr committed May 8, 2024
1 parent 16978d0 commit ecf02a9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ protected static boolean isAssignableValueOrConverted(Class<?> propertyType, Obj
MappingAerospikeConverter converter) {
return isAssignableValue(propertyType, obj)
|| converter.getCustomConversions().hasCustomReadTarget(obj.getClass(), propertyType)
|| isPojoMap(obj, propertyType);
// POJOs and enums got converted to Strings when query parameters were set
|| isPojoMap(obj, propertyType)
|| (propertyType.isEnum() && obj instanceof String);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import java.util.List;

import static org.springframework.data.aerospike.sample.Person.Sex.FEMALE;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class PersonRepositoryQueryTests extends BaseBlockingIntegrationTests {

Expand Down Expand Up @@ -43,6 +45,7 @@ public class PersonRepositoryQueryTests extends BaseBlockingIntegrationTests {
.id(nextId())
.firstName("Alicia")
.lastName("Keys")
.sex(FEMALE)
.age(30)
.ints(List.of(550, 600, 990))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,5 +283,15 @@ private MappingAerospikeConverter getMappingAerospikeConverter(AerospikeCustomCo
converter.afterPropertiesSet();
return converter;
}

@Test
void findBySimplePropertyEquals_Enum() {
Qualifier sexEqFemale = Qualifier.builder()
.setBinName("sex")
.setFilterOperation(FilterOperation.EQ)
.setValue(Value.get(Person.Sex.FEMALE))
.build();
assertThat(repository.findUsingQuery(new Query(sexEqFemale))).containsOnly(alicia);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,39 @@
public class EqualsTests extends PersonRepositoryQueryTests {

@Test
void findByBooleanIntSimplePropertyEquals() {
void findBySimplePropertyEquals_String() {
List<Person> result = repository.findByFirstName("Leroi");
assertThat(result).containsOnly(leroi, leroi2);

List<Person> result1 = repository.findByFirstNameIgnoreCase("lEroi");
assertThat(result1).containsOnly(leroi, leroi2);

List<Person> result2 = repository.findByFirstNameIs("lEroi"); // another way to call the query method
assertThat(result2).hasSize(0);

List<Person> result3 = repository.findByFirstNameEquals("leroi "); // another way to call the query method
assertThat(result3).hasSize(0);
}

@Test
void findBySimplePropertyEquals_String_NegativeTest() {
assertThatThrownBy(() -> negativeTestsRepository.findByFirstName(100))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.firstName EQ: Type mismatch, expecting String");

assertThatThrownBy(() -> negativeTestsRepository.findByLastName("Beauford", "Boford"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.lastName EQ: invalid number of arguments, expecting one");
}

@Test
void findBySimplePropertyEquals_Enum() {
List<Person> result = repository.findBySex(Person.Sex.FEMALE);
assertThat(result).containsOnly(alicia);
}

@Test
void findBySimplePropertyEquals_BooleanInt() {
boolean initialValue = Value.UseBoolBin;
Value.UseBoolBin = false; // save boolean as int
Person intBoolBinPerson = Person.builder()
Expand All @@ -46,7 +78,7 @@ void findByBooleanIntSimplePropertyEquals() {
}

@Test
void findByBooleanIntSimplePropertyEquals_NegativeTest() {
void findBySimplePropertyEquals_BooleanInt_NegativeTest() {
boolean initialValue = Value.UseBoolBin;
Value.UseBoolBin = false; // save boolean as int

Expand All @@ -62,7 +94,7 @@ void findByBooleanIntSimplePropertyEquals_NegativeTest() {
}

@Test
void findByBooleanSimplePropertyEquals() {
void findBySimplePropertyEquals_Boolean() {
boolean initialValue = Value.UseBoolBin;
Value.UseBoolBin = true; // save boolean as bool, available in Server 5.6+
Person intBoolBinPerson = Person.builder().id(BaseIntegrationTests.nextId()).isActive(true).firstName("Test")
Expand All @@ -77,7 +109,7 @@ void findByBooleanSimplePropertyEquals() {
}

@Test
void findByBooleanSimplePropertyEquals_NegativeTest() {
void findBySimplePropertyEquals_Boolean_NegativeTest() {
boolean initialValue = Value.UseBoolBin;
Value.UseBoolBin = true; // save boolean as bool, available in Server 5.6+

Expand All @@ -92,32 +124,6 @@ void findByBooleanSimplePropertyEquals_NegativeTest() {
Value.UseBoolBin = initialValue; // set back to the default value
}

@Test
void findBySimplePropertyEquals_String() {
List<Person> result = repository.findByFirstName("Leroi");
assertThat(result).containsOnly(leroi, leroi2);

List<Person> result1 = repository.findByFirstNameIgnoreCase("lEroi");
assertThat(result1).containsOnly(leroi, leroi2);

List<Person> result2 = repository.findByFirstNameIs("lEroi"); // another way to call the query method
assertThat(result2).hasSize(0);

List<Person> result3 = repository.findByFirstNameEquals("leroi "); // another way to call the query method
assertThat(result3).hasSize(0);
}

@Test
void findBySimplePropertyEquals_String_NegativeTest() {
assertThatThrownBy(() -> negativeTestsRepository.findByFirstName(100))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.firstName EQ: Type mismatch, expecting String");

assertThatThrownBy(() -> negativeTestsRepository.findByLastName("Beauford", "Boford"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.lastName EQ: invalid number of arguments, expecting one");
}

@Test
void findPersonById_AND_simpleProperty() {
QueryParam ids = of(List.of(dave.getId(), boyd.getId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,8 @@ List<P> findByFriendStringMapNotContaining(AerospikeQueryCriterion criterion,

List<P> findByFirstName(String name);

List<P> findBySex(Person.Sex sex);

List<P> findByFirstNameIs(String name);

List<P> findByFirstNameEquals(String name);
Expand Down

0 comments on commit ecf02a9

Please sign in to comment.