Skip to content

Commit

Permalink
add validation for query methods
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr committed Feb 8, 2024
1 parent 1f555ba commit 5f127b5
Show file tree
Hide file tree
Showing 12 changed files with 1,167 additions and 130 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ public interface CriteriaDefinition {
*/
String getKey();

enum AerospikeMapCriteria {
enum AerospikeMapQueryCriteria {
KEY, VALUE, VALUE_CONTAINING
}

enum AerospikeNullQueryCriteria {
NULL
}

enum AerospikeMetadata {
SINCE_UPDATE_TIME, LAST_UPDATE_TIME, VOID_TIME, TTL, RECORD_SIZE_ON_DISK, RECORD_SIZE_IN_MEMORY
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,26 @@
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.util.StringUtils;

import java.io.File;
import java.net.InetAddress;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.time.ZoneId;
import java.time.temporal.Temporal;
import java.util.Arrays;
import java.util.Currency;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Pattern;

import static org.springframework.util.ClassUtils.isPrimitiveOrWrapper;

/**
* Utility class containing useful methods for interacting with Aerospike across the entire implementation
Expand Down Expand Up @@ -104,4 +120,31 @@ public static Optional<Integer> getIntegerProperty(String property) {
public static boolean allArrayElementsAreNull(Object[] array) {
return Arrays.stream(array).allMatch(Objects::isNull);
}

/**
* Checking that at least one of the arguments is of the following type: a primitive or primitive wrapper, an Enum,
* a String or other CharSequence, a Number, a Date, a Temporal, a UUID, a URI, a URL, a Locale, or a Class
*/
public static boolean isSimpleValueType(Class<?> type) {
return (Void.class != type && void.class != type &&
(isPrimitiveOrWrapper(type) ||
Enum.class.isAssignableFrom(type) ||
CharSequence.class.isAssignableFrom(type) ||
Number.class.isAssignableFrom(type) ||
Date.class.isAssignableFrom(type) ||
Temporal.class.isAssignableFrom(type) ||
ZoneId.class.isAssignableFrom(type) ||
TimeZone.class.isAssignableFrom(type) ||
File.class.isAssignableFrom(type) ||
Path.class.isAssignableFrom(type) ||
Charset.class.isAssignableFrom(type) ||
Currency.class.isAssignableFrom(type) ||
InetAddress.class.isAssignableFrom(type) ||
URI.class == type ||
URL.class == type ||
UUID.class == type ||
Locale.class == type ||
Pattern.class == type ||
Class.class == type));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public void findByListValueInRange() {
@Test
public void findByMapKeysContaining() {
Query query = QueryUtils.createQueryForMethodWithArgs("findByStringMapContaining", "key1",
CriteriaDefinition.AerospikeMapCriteria.KEY);
CriteriaDefinition.AerospikeMapQueryCriteria.KEY);
Stream<Person> result = template.find(query, Person.class);

assertThat(result)
Expand All @@ -376,7 +376,7 @@ public void findByMapKeysContaining() {
@Test
public void findByMapValuesContaining() {
Query query = QueryUtils.createQueryForMethodWithArgs("findByStringMapContaining", "val2",
CriteriaDefinition.AerospikeMapCriteria.VALUE);
CriteriaDefinition.AerospikeMapQueryCriteria.VALUE);
Stream<Person> result = template.find(query, Person.class);

assertThat(result)
Expand Down Expand Up @@ -436,7 +436,7 @@ public void findByMapKeyValueStartsWith() {
@Test
public void findByMapKeyValueContains() {
Query query = QueryUtils.createQueryForMethodWithArgs("findByStringMapContaining", "key4", "al",
CriteriaDefinition.AerospikeMapCriteria.VALUE_CONTAINING);
CriteriaDefinition.AerospikeMapQueryCriteria.VALUE_CONTAINING);
Stream<Person> result = template.find(query, Person.class);

assertThat(result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ public void findByMapKeysContaining() {
reactiveTemplate.insertAll(persons).blockLast();

Query query = QueryUtils.createQueryForMethodWithArgs("findByStringMapContaining", "key1",
CriteriaDefinition.AerospikeMapCriteria.KEY);
CriteriaDefinition.AerospikeMapQueryCriteria.KEY);

List<Person> result = reactiveTemplate.find(query, Person.class)
.subscribeOn(Schedulers.parallel())
Expand All @@ -459,7 +459,7 @@ public void findByMapValuesContaining() {
reactiveTemplate.insertAll(persons).blockLast();

Query query = QueryUtils.createQueryForMethodWithArgs("findByStringMapContaining", "val1",
CriteriaDefinition.AerospikeMapCriteria.VALUE);
CriteriaDefinition.AerospikeMapQueryCriteria.VALUE);

List<Person> result = reactiveTemplate.find(query, Person.class)
.subscribeOn(Schedulers.parallel())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
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.AerospikeMapCriteria.VALUE;
import static org.springframework.data.aerospike.repository.query.CriteriaDefinition.AerospikeMapQueryCriteria.VALUE;
import static org.springframework.data.aerospike.repository.query.CriteriaDefinition.AerospikeMetadata.SINCE_UPDATE_TIME;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand Down Expand Up @@ -247,7 +247,7 @@ public void findPersonInAgeRangeCorrectly() {
@Test
public void findByMapKeysContaining() {
List<IndexedPerson> results = reactiveRepository.findByStringMapContaining("key1",
CriteriaDefinition.AerospikeMapCriteria.KEY).subscribeOn(Schedulers.parallel()).collectList().block();
CriteriaDefinition.AerospikeMapQueryCriteria.KEY).subscribeOn(Schedulers.parallel()).collectList().block();

assertThat(results).contains(luc, petra);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
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.AerospikeMapCriteria.VALUE;
import static org.springframework.data.aerospike.repository.query.CriteriaDefinition.AerospikeMapQueryCriteria.VALUE;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class IndexedPersonRepositoryQueryTests extends BaseBlockingIntegrationTests {
Expand Down Expand Up @@ -352,7 +352,7 @@ void findByMapKeysContaining() {
assertThat(billy.getStringMap()).containsKey("key1");

List<IndexedPerson> persons = repository.findByStringMapContaining("key1",
CriteriaDefinition.AerospikeMapCriteria.KEY);
CriteriaDefinition.AerospikeMapQueryCriteria.KEY);
assertThat(persons).contains(billy);
}

Expand All @@ -361,7 +361,7 @@ void findByMapKeysNotContaining() {
assertThat(billy.getStringMap()).containsKey("key1");

List<IndexedPerson> persons = repository.findByStringMapNotContaining("key3",
CriteriaDefinition.AerospikeMapCriteria.KEY);
CriteriaDefinition.AerospikeMapQueryCriteria.KEY);
assertThat(persons).contains(billy);
}

Expand Down
Loading

0 comments on commit 5f127b5

Please sign in to comment.