Skip to content

Commit

Permalink
FMWK-337 Convert PersonRepositoryQueryTests to dedicated test classes (
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr authored Feb 26, 2024
1 parent dc6db3c commit bd1e162
Show file tree
Hide file tree
Showing 33 changed files with 2,637 additions and 2,757 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public boolean isDropCreateBehaviorUpdated() {
}

/**
* Since Aerospike Server ver. 6.3.0.0 find by POJO is supported.
* Since Aerospike Server ver. 6.3.0.0 find by Collection Data Types (Collection / Map / POJO) is supported.
*/
public boolean isFindByPojoSupported() {
public boolean isFindByCDTSupported() {
return ModuleDescriptor.Version.parse(getServerVersion())
.compareTo(SERVER_VERSION_6_3_0_0) >= 0;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.springframework.data.aerospike.repository;
package org.springframework.data.aerospike.repository.query;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -7,7 +7,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.aerospike.BaseBlockingIntegrationTests;
import org.springframework.data.aerospike.query.Qualifier;
import org.springframework.data.aerospike.repository.query.Query;
import org.springframework.data.aerospike.sample.DocumentByteArrayIdRepository;
import org.springframework.data.aerospike.sample.DocumentByteIdRepository;
import org.springframework.data.aerospike.sample.DocumentCharacterIdRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.springframework.data.aerospike.repository;
package org.springframework.data.aerospike.repository.query.findBy.indexed;

import com.aerospike.client.Value;
import com.aerospike.client.cdt.CTX;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.springframework.data.aerospike.repository.query.findBy.noindex;

import org.junit.jupiter.api.Test;
import org.springframework.data.aerospike.sample.Person;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.util.List;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for the CrudRepository queries API.
*/
public class CrudRepositoryQueryTests extends PersonRepositoryQueryTests {

@Test
void findAll() {
List<Person> result = (List<Person>) repository.findAll();
assertThat(result).containsExactlyInAnyOrderElementsOf(allPersons);
}

@Test
void findAll_Paginated() {
Page<Person> result = repository.findAll(PageRequest.of(1, 2, Sort.Direction.ASC, "lastname", "firstname"));
assertThat(result.isFirst()).isFalse();
assertThat(result.isLast()).isFalse();
}

@Test
void findAll_doesNotFindDeletedPersonByEntity() {
try {
repository.delete(dave);
List<Person> result = (List<Person>) repository.findAll();
assertThat(result)
.doesNotContain(dave)
.containsExactlyInAnyOrderElementsOf(
allPersons.stream().filter(person -> !person.equals(dave)).collect(Collectors.toList())
);
} finally {
repository.save(dave);
}
}

@Test
void findAll_doesNotFindPersonDeletedById() {
try {
repository.deleteById(dave.getId());
List<Person> result = (List<Person>) repository.findAll();
assertThat(result)
.doesNotContain(dave)
.hasSize(allPersons.size() - 1);
} finally {
repository.save(dave);
}
}

@Test
void deleteAllPersonsFromList() {
if (serverVersionSupport.isBatchWriteSupported()) {
// batch delete requires server ver. >= 6.0.0
repository.deleteAll(List.of(dave, carter));
} else {
List.of(dave, carter).forEach(repository::delete);
}
assertThat(repository.findAllById(List.of(dave.getId(), carter.getId()))).isEmpty();
repository.save(dave); // cleanup
repository.save(carter); // cleanup
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.springframework.data.aerospike.repository.query.findBy.noindex;

/**
* Tests for the "Ends with" repository query. Keywords: EndingWith, IsEndingWith, EndsWith.
*/
public class EndsWithTests extends PersonRepositoryQueryTests {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
package org.springframework.data.aerospike.repository.query.findBy.noindex;

import com.aerospike.client.Value;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.data.aerospike.BaseIntegrationTests;
import org.springframework.data.aerospike.sample.Address;
import org.springframework.data.aerospike.sample.Person;
import org.springframework.data.aerospike.utility.TestUtils;

import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* Tests for the "Equals" repository query. Keywords: Is, Equals (or no keyword).
*/
public class EqualsTests extends PersonRepositoryQueryTests {

@Test
void findByBooleanIntSimplePropertyEquals() {
boolean initialValue = Value.UseBoolBin;
Value.UseBoolBin = false; // save boolean as int
Person intBoolBinPerson = Person.builder().id(BaseIntegrationTests.nextId()).isActive(true).firstName("Test")
.build();
repository.save(intBoolBinPerson);

List<Person> persons = repository.findByIsActive(true);
assertThat(persons).contains(intBoolBinPerson);

Value.UseBoolBin = initialValue; // set back to the default value
repository.delete(intBoolBinPerson);
}

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

assertThatThrownBy(() -> negativeTestsRepository.findByIsActive())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.isActive EQ: invalid number of arguments, expecting one");

assertThatThrownBy(() -> negativeTestsRepository.findByIsActive(true, false))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.isActive EQ: invalid number of arguments, expecting one");

Value.UseBoolBin = initialValue; // set back to the default value
}

@Test
void findByBooleanSimplePropertyEquals() {
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")
.build();
repository.save(intBoolBinPerson);

List<Person> persons = repository.findByIsActive(true);
assertThat(persons).contains(intBoolBinPerson);

Value.UseBoolBin = initialValue; // set back to the default value
repository.delete(intBoolBinPerson);
}

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

assertThatThrownBy(() -> negativeTestsRepository.findByIsActive())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.isActive EQ: invalid number of arguments, expecting one");

assertThatThrownBy(() -> negativeTestsRepository.findByIsActive(true, false))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.isActive EQ: invalid number of arguments, expecting one");

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 findByNestedSimplePropertyEquals() {
String zipCode = "C012345";
Address address = new Address("Foo Street 1", 1, zipCode, "Bar");
dave.setAddress(address);
repository.save(dave);

carter.setFriend(dave);
repository.save(carter);

List<Person> result = repository.findByFriendAddressZipCode(zipCode);

assertThat(result).containsExactly(carter);

TestUtils.setFriendsToNull(repository, carter);
}

@Test
void findByNestedSimplePropertyEqualsNegativeTest() {
assertThatThrownBy(() -> negativeTestsRepository.findByFriendAddressZipCode())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Address.zipCode EQ: invalid number of arguments, expecting one");

assertThatThrownBy(() -> negativeTestsRepository.findByFriendAddressZipCodeEquals())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Address.zipCode EQ: invalid number of arguments, expecting one");
}

@Test
void findByCollectionEquals() {
if (serverVersionSupport.isFindByCDTSupported()) {
List<String> listToCompareWith = List.of("str0", "str1", "str2");
Assertions.assertThat(dave.getStrings()).isEqualTo(listToCompareWith);

List<Person> persons = repository.findByStringsEquals(listToCompareWith);
assertThat(persons).contains(dave);

// another way to call the method
List<Person> persons2 = repository.findByStrings(listToCompareWith);
assertThat(persons2).contains(dave);
}
}

@Test
void findByCollectionEquals_NegativeTest() {
assertThatThrownBy(() -> negativeTestsRepository.findByStrings())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.strings EQ: invalid number of arguments, expecting one");

assertThatThrownBy(() -> negativeTestsRepository.findByStringsEquals("string1", "string2"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.strings EQ: invalid number of arguments, expecting one");
}

@Test
void findByMapEquals() {
if (serverVersionSupport.isFindByCDTSupported()) {
Map<String, String> mapToCompareWith = Map.of("key1", "val1", "key2", "val2");
assertThat(boyd.getStringMap()).isEqualTo(mapToCompareWith);

List<Person> persons = repository.findByStringMapEquals(mapToCompareWith);
assertThat(persons).contains(boyd);

// another way to call the method
List<Person> persons2 = repository.findByStringMap(mapToCompareWith);
assertThat(persons2).contains(boyd);
}
}

@Test
void findByMapEquals_NegativeTest() {
assertThatThrownBy(() -> negativeTestsRepository.findByStringMapEquals("map1"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.stringMap EQ: invalid combination of arguments, expecting either a Map or a key-value" +
" pair");

assertThatThrownBy(() -> negativeTestsRepository.findByStringMap(100))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.stringMap EQ: invalid combination of arguments, expecting either a Map or a key-value" +
" pair");

assertThatThrownBy(() -> negativeTestsRepository.findByStringMapEquals(Map.of("key", "value"), Map.of("key",
"value")))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.stringMap EQ: invalid combination of arguments, expecting either a Map or a key-value" +
" pair");
}

@Test
void findByPOJOEquals() {
if (serverVersionSupport.isFindByCDTSupported()) {
alicia.setAddress(new Address("Foo Street 1", 1, "C0123", "Bar"));
repository.save(alicia);
oliver.setFriend(alicia);
repository.save(oliver);

List<Person> persons = repository.findByFriend(alicia);
assertThat(persons).containsOnly(oliver);

alicia.setAddress(null);
repository.save(alicia);
}
}

@Test
void findByNestedPOJOEquals() {
if (serverVersionSupport.isFindByCDTSupported()) {
Address address = new Address("Foo Street 1", 1, "C0123", "Bar");
dave.setAddress(address);
repository.save(dave);

carter.setFriend(dave);
repository.save(carter);

List<Person> result = repository.findByFriendAddress(address);

assertThat(result).contains(carter);
TestUtils.setFriendsToNull(repository, carter);
}
}

@Test
void findByNestedPojoEqualsNegativeTest() {
assertThatThrownBy(() -> negativeTestsRepository.findByFriendAddress())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.address EQ: invalid number of arguments, expecting one POJO");

assertThatThrownBy(() -> negativeTestsRepository.findByFriendAddressEquals())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.address EQ: invalid number of arguments, expecting one POJO");

assertThatThrownBy(() -> negativeTestsRepository.findByFriendAddress(100))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Person.address EQ: Type mismatch, expecting Address");
}
}
Loading

0 comments on commit bd1e162

Please sign in to comment.