Skip to content

Commit

Permalink
add CombinedQueryParam, add validation for query methods
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr committed Feb 13, 2024
1 parent 5f127b5 commit bde8b05
Show file tree
Hide file tree
Showing 12 changed files with 457 additions and 236 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.springframework.data.aerospike.query;

import lombok.Getter;

/**
* This class stores arguments passed to each part of a combined repository query,
* e.g., repository.findByNameAndEmail(CombinedQueryParam.of("John"), CombinedQueryParam.of("[email protected]"))
*/
public record CombinedQueryParam(@Getter Object[] arguments) {

/**
* This method is required to pass arguments to each part of a combined query
* @param arguments necessary objects
* @return instance of {@link CombinedQueryParam}
*/
public static CombinedQueryParam of(Object... arguments) {
return new CombinedQueryParam(arguments);
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.aerospike.AsCollections;
import org.springframework.data.aerospike.BaseReactiveIntegrationTests;
import org.springframework.data.aerospike.ReactiveBlockingAerospikeTestOperations;
import org.springframework.data.aerospike.query.CombinedQueryParam;
import org.springframework.data.aerospike.query.FilterOperation;
import org.springframework.data.aerospike.query.Qualifier;
import org.springframework.data.aerospike.repository.query.CriteriaDefinition;
Expand All @@ -30,7 +32,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.springframework.data.aerospike.AsCollections.of;
import static org.springframework.data.aerospike.repository.query.CriteriaDefinition.AerospikeMapQueryCriteria.VALUE;
import static org.springframework.data.aerospike.repository.query.CriteriaDefinition.AerospikeMetadata.SINCE_UPDATE_TIME;

Expand All @@ -41,19 +42,19 @@ public class ReactiveIndexedPersonRepositoryQueryTests extends BaseReactiveInteg
.age(42).strings(Arrays.asList("str1", "str2"))
.address(new Address("Foo Street 1", 1, "C0123", "Bar")).build();
static final IndexedPerson luc = IndexedPerson.builder().id(nextId()).firstName("Luc").lastName("Besson").age(39)
.stringMap(of("key1", "val1", "key2", "val2")).address(new Address(null, null, null, null))
.stringMap(AsCollections.of("key1", "val1", "key2", "val2")).address(new Address(null, null, null, null))
.build();
static final IndexedPerson lilly = IndexedPerson.builder().id(nextId()).firstName("Lilly").lastName("Bertineau")
.age(28).intMap(of("key1", 1, "key2", 2))
.age(28).intMap(AsCollections.of("key1", 1, "key2", 2))
.address(new Address("Foo Street 2", 2, "C0124", "C0123")).build();
static final IndexedPerson daniel = IndexedPerson.builder().id(nextId()).firstName("Daniel").lastName("Morales")
.age(29).ints(Arrays.asList(500, 550, 990)).build();
static final IndexedPerson petra = IndexedPerson.builder().id(nextId()).firstName("Petra")
.lastName("Coutant-Kerbalec")
.age(34).stringMap(of("key1", "val1", "key2", "val2", "key3", "val3")).build();
.age(34).stringMap(AsCollections.of("key1", "val1", "key2", "val2", "key3", "val3")).build();
static final IndexedPerson emilien = IndexedPerson.builder().id(nextId()).firstName("Emilien")
.lastName("Coutant-Kerbalec").age(30)
.intMap(of("key1", 0, "key2", 1)).ints(Arrays.asList(450, 550, 990)).build();
.intMap(AsCollections.of("key1", 0, "key2", 1)).ints(Arrays.asList(450, 550, 990)).build();
public static final List<IndexedPerson> allIndexedPersons = Arrays.asList(alain, luc, lilly, daniel, petra,
emilien);
@Autowired
Expand Down Expand Up @@ -230,7 +231,9 @@ void findDistinctByFriendLastNameStartingWith() {

@Test
public void findPersonsByFirstnameAndByAge() {
List<IndexedPerson> results = reactiveRepository.findByFirstNameAndAge("Lilly", 28)
CombinedQueryParam firstName = CombinedQueryParam.of("Lilly");
CombinedQueryParam age = CombinedQueryParam.of(28);
List<IndexedPerson> results = reactiveRepository.findByFirstNameAndAge(firstName, age)
.subscribeOn(Schedulers.parallel()).collectList().block();

assertThat(results).containsOnly(lilly);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.aerospike.BaseBlockingIntegrationTests;
import org.springframework.data.aerospike.query.CombinedQueryParam;
import org.springframework.data.aerospike.query.model.Index;
import org.springframework.data.aerospike.repository.query.CriteriaDefinition;
import org.springframework.data.aerospike.sample.Address;
Expand Down Expand Up @@ -233,7 +234,9 @@ public void findsPersonsByFirstName() {
@Test
public void findsPersonsByActiveAndFirstName() {
assertThat(tricia.isActive()).isFalse();
List<IndexedPerson> result = repository.findByIsActiveAndFirstName(false, "Tricia");
CombinedQueryParam isActive = CombinedQueryParam.of(false);
CombinedQueryParam firstNames = CombinedQueryParam.of("Tricia");
List<IndexedPerson> result = repository.findByIsActiveAndFirstName(isActive, firstNames);

assertThat(result)
.hasSize(1)
Expand Down Expand Up @@ -310,10 +313,14 @@ public void findByAgeGreaterThan_forEmptyResult() {

@Test
public void findsPersonsByFirstNameAndByAge() {
List<IndexedPerson> result = repository.findByFirstNameAndAge("Billy", 25);
CombinedQueryParam firstNames = CombinedQueryParam.of("Billy");
CombinedQueryParam ages = CombinedQueryParam.of(25);
List<IndexedPerson> result = repository.findByFirstNameAndAge(firstNames, ages);
assertThat(result).containsOnly(billy);

result = repository.findByFirstNameAndAge("Peter", 41);
firstNames = CombinedQueryParam.of("Peter");
ages = CombinedQueryParam.of(41);
result = repository.findByFirstNameAndAge(firstNames, ages);
assertThat(result).containsOnly(peter);
}

Expand All @@ -331,19 +338,27 @@ public void findsPersonInAgeRangeCorrectlyOrderByLastName() {

@Test
public void findsPersonInAgeRangeAndNameCorrectly() {
Iterable<IndexedPerson> it = repository.findByAgeBetweenAndLastName(40, 45, "Matthews");
CombinedQueryParam ageBetween = CombinedQueryParam.of(40, 45);
CombinedQueryParam lastNames = CombinedQueryParam.of("Matthews");
Iterable<IndexedPerson> it = repository.findByAgeBetweenAndLastName(ageBetween, lastNames);
assertThat(it).hasSize(0);

Iterable<IndexedPerson> result = repository.findByAgeBetweenAndLastName(20, 26, "Smith");
ageBetween = CombinedQueryParam.of(20, 26);
lastNames = CombinedQueryParam.of("Smith");
Iterable<IndexedPerson> result = repository.findByAgeBetweenAndLastName(ageBetween, lastNames);
assertThat(result).hasSize(1).contains(billy);
}

@Test
public void findsPersonInAgeRangeOrNameCorrectly() {
Iterable<IndexedPerson> it = repository.findByAgeBetweenOrLastName(40, 45, "James");
CombinedQueryParam ageBetween = CombinedQueryParam.of(40, 45);
CombinedQueryParam lastNames = CombinedQueryParam.of("James");
Iterable<IndexedPerson> it = repository.findByAgeBetweenOrLastName(ageBetween, lastNames);
assertThat(it).containsExactlyInAnyOrder(john, peter, tricia);

Iterable<IndexedPerson> result = repository.findByAgeBetweenOrLastName(20, 26, "Macintosh");
ageBetween = CombinedQueryParam.of(20, 26);
lastNames = CombinedQueryParam.of("Macintosh");
Iterable<IndexedPerson> result = repository.findByAgeBetweenOrLastName(ageBetween, lastNames);
assertThat(result).containsExactlyInAnyOrder(billy, peter);
}

Expand Down
Loading

0 comments on commit bde8b05

Please sign in to comment.