DATAKV-95 - Added exists(…) method to QuerydslKeyValueRepository.

Renamed QueryDsl… classes to Querydsl… for consistency. Restructured test cases for repositories to make better use of generics.
This commit is contained in:
Oliver Gierke
2015-02-03 11:22:03 +01:00
parent b8de114457
commit 8f1bc111b4
7 changed files with 255 additions and 186 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@ package org.springframework.data.keyvalue.repository.support;
import static org.hamcrest.collection.IsArrayWithSize.*;
import static org.junit.Assert.*;
import static org.springframework.data.keyvalue.repository.support.KeyValueQueryDslUtils.*;
import static org.springframework.data.keyvalue.repository.support.KeyValueQuerydslUtils.*;
import org.hamcrest.collection.IsArrayContainingInOrder;
import org.junit.Before;
@@ -34,10 +34,13 @@ import com.mysema.query.types.OrderSpecifier;
import com.mysema.query.types.path.PathBuilder;
/**
* Unit tests for {@link KeyValueQuerydslUtils}.
*
* @author Christoph Strobl
* @author Thomas Darimont
* @author Oliver Gierke
*/
public class QueryDslUtilsUnitTests {
public class KeyValueQuerydslUtilsUnitTests {
private EntityPath<Person> path;
private PathBuilder<Person> builder;

View File

@@ -0,0 +1,174 @@
/*
* Copyright 2014-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.map;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import org.hamcrest.collection.IsCollectionWithSize;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.keyvalue.Person;
import org.springframework.data.keyvalue.core.KeyValueOperations;
import org.springframework.data.keyvalue.core.KeyValueTemplate;
import org.springframework.data.keyvalue.repository.KeyValueRepository;
import org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactory;
import org.springframework.data.repository.CrudRepository;
/**
* Base class for test cases for repository implementations.
*
* @author Christoph Strobl
* @author Oliver Gierke
*/
public abstract class AbstractRepositoryUnitTests<T extends AbstractRepositoryUnitTests.PersonRepository> {
protected static final Person CERSEI = new Person("cersei", 19);
protected static final Person JAIME = new Person("jaime", 19);
protected static final Person TYRION = new Person("tyrion", 17);
protected static List<Person> LENNISTERS = Arrays.asList(CERSEI, JAIME, TYRION);
protected T repository;
protected KeyValueRepositoryFactory factory;
@Before
public void setup() {
KeyValueOperations operations = new KeyValueTemplate(new MapKeyValueAdapter());
KeyValueRepositoryFactory keyValueRepositoryFactory = new KeyValueRepositoryFactory(operations);
this.repository = getRepository(keyValueRepositoryFactory);
}
/**
* @see DATACMNS-525
*/
@Test
public void findBy() {
repository.save(LENNISTERS);
assertThat(repository.findByAge(19), hasItems(CERSEI, JAIME));
}
/**
* @see DATACMNS-525
*/
@Test
public void combindedFindUsingAnd() {
repository.save(LENNISTERS);
assertThat(repository.findByFirstnameAndAge(JAIME.getFirstname(), 19), hasItem(JAIME));
}
/**
* @see DATACMNS-525
*/
@Test
public void findPage() {
repository.save(LENNISTERS);
Page<Person> page = repository.findByAge(19, new PageRequest(0, 1));
assertThat(page.hasNext(), is(true));
assertThat(page.getTotalElements(), is(2L));
assertThat(page.getContent(), IsCollectionWithSize.hasSize(1));
Page<Person> next = repository.findByAge(19, page.nextPageable());
assertThat(next.hasNext(), is(false));
assertThat(next.getTotalElements(), is(2L));
assertThat(next.getContent(), IsCollectionWithSize.hasSize(1));
}
/**
* @see DATACMNS-525
*/
@Test
public void findByConnectingOr() {
repository.save(LENNISTERS);
assertThat(repository.findByAgeOrFirstname(19, TYRION.getFirstname()), hasItems(CERSEI, JAIME, TYRION));
}
/**
* @see DATACMNS-525
*/
@Test
public void singleEntityExecution() {
repository.save(LENNISTERS);
assertThat(repository.findByAgeAndFirstname(TYRION.getAge(), TYRION.getFirstname()), is(TYRION));
}
/**
* @see DATACMNS-525
*/
@Test
public void findAllShouldRespectSort() {
repository.save(LENNISTERS);
assertThat(
repository.findAll(new Sort(new Sort.Order(Direction.ASC, "age"), new Sort.Order(Direction.DESC, "firstname"))),
contains(TYRION, JAIME, CERSEI));
}
/**
* @see DATACMNS-525
*/
@Test
public void derivedFinderShouldRespectSort() {
repository.save(LENNISTERS);
List<Person> result = repository.findByAgeGreaterThanOrderByAgeAscFirstnameDesc(2);
assertThat(result, contains(TYRION, JAIME, CERSEI));
}
protected abstract T getRepository(KeyValueRepositoryFactory factory);
public static interface PersonRepository extends CrudRepository<Person, String>, KeyValueRepository<Person, String> {
List<Person> findByAge(int age);
List<Person> findByFirstname(String firstname);
List<Person> findByFirstnameAndAge(String firstname, int age);
Page<Person> findByAge(int age, Pageable page);
List<Person> findByAgeOrFirstname(int age, String firstname);
Person findByAgeAndFirstname(int age, String firstname);
List<Person> findByAgeGreaterThanOrderByAgeAscFirstnameDesc(int age);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,13 +27,19 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.keyvalue.Person;
import org.springframework.data.keyvalue.QPerson;
import org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactory;
import org.springframework.data.keyvalue.repository.support.QuerydslKeyValueRepository;
import org.springframework.data.map.QuerydslKeyValueRepositoryUnitTests.QPersonRepository;
import org.springframework.data.querydsl.QSort;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
/**
* Unit tests for {@link QuerydslKeyValueRepository}.
*
* @author Christoph Strobl
* @author Oliver Gierke
*/
public class QueryDslMapRepositoryUnitTests extends SimpleKeyValueRepositoryUnitTests {
public class QuerydslKeyValueRepositoryUnitTests extends AbstractRepositoryUnitTests<QPersonRepository> {
/**
* @see DATACMNS-525
@@ -43,7 +49,7 @@ public class QueryDslMapRepositoryUnitTests extends SimpleKeyValueRepositoryUnit
repository.save(LENNISTERS);
Person result = getQPersonRepo().findOne(QPerson.person.firstname.eq(CERSEI.getFirstname()));
Person result = repository.findOne(QPerson.person.firstname.eq(CERSEI.getFirstname()));
assertThat(result, is(CERSEI));
}
@@ -55,7 +61,7 @@ public class QueryDslMapRepositoryUnitTests extends SimpleKeyValueRepositoryUnit
repository.save(LENNISTERS);
Iterable<Person> result = getQPersonRepo().findAll(QPerson.person.age.eq(CERSEI.getAge()));
Iterable<Person> result = repository.findAll(QPerson.person.age.eq(CERSEI.getAge()));
assertThat(result, containsInAnyOrder(CERSEI, JAIME));
}
@@ -66,7 +72,7 @@ public class QueryDslMapRepositoryUnitTests extends SimpleKeyValueRepositoryUnit
public void findWithPaginationWorksCorrectly() {
repository.save(LENNISTERS);
Page<Person> page1 = getQPersonRepo().findAll(QPerson.person.age.eq(CERSEI.getAge()), new PageRequest(0, 1));
Page<Person> page1 = repository.findAll(QPerson.person.age.eq(CERSEI.getAge()), new PageRequest(0, 1));
assertThat(page1.getTotalElements(), is(2L));
assertThat(page1.getContent(), hasSize(1));
@@ -88,7 +94,7 @@ public class QueryDslMapRepositoryUnitTests extends SimpleKeyValueRepositoryUnit
repository.save(LENNISTERS);
Iterable<Person> result = getQPersonRepo().findAll(QPerson.person.age.eq(CERSEI.getAge()),
Iterable<Person> result = repository.findAll(QPerson.person.age.eq(CERSEI.getAge()),
QPerson.person.firstname.desc());
assertThat(result, contains(JAIME, CERSEI));
@@ -102,8 +108,8 @@ public class QueryDslMapRepositoryUnitTests extends SimpleKeyValueRepositoryUnit
repository.save(LENNISTERS);
Iterable<Person> result = getQPersonRepo().findAll(QPerson.person.age.eq(CERSEI.getAge()),
new PageRequest(0, 10, Direction.DESC, "firstname"));
Iterable<Person> result = repository.findAll(QPerson.person.age.eq(CERSEI.getAge()), new PageRequest(0, 10,
Direction.DESC, "firstname"));
assertThat(result, contains(JAIME, CERSEI));
}
@@ -116,8 +122,8 @@ public class QueryDslMapRepositoryUnitTests extends SimpleKeyValueRepositoryUnit
repository.save(LENNISTERS);
Iterable<Person> result = getQPersonRepo().findAll(QPerson.person.age.eq(CERSEI.getAge()),
new PageRequest(0, 10, new QSort(QPerson.person.firstname.desc())));
Iterable<Person> result = repository.findAll(QPerson.person.age.eq(CERSEI.getAge()), new PageRequest(0, 10,
new QSort(QPerson.person.firstname.desc())));
assertThat(result, contains(JAIME, CERSEI));
}
@@ -130,7 +136,7 @@ public class QueryDslMapRepositoryUnitTests extends SimpleKeyValueRepositoryUnit
repository.save(LENNISTERS);
Iterable<Person> result = getQPersonRepo().findAll(new QSort(QPerson.person.firstname.desc()));
Iterable<Person> result = repository.findAll(new QSort(QPerson.person.firstname.desc()));
assertThat(result, contains(TYRION, JAIME, CERSEI));
}
@@ -143,22 +149,31 @@ public class QueryDslMapRepositoryUnitTests extends SimpleKeyValueRepositoryUnit
repository.save(LENNISTERS);
Iterable<Person> result = getQPersonRepo().findAll((QSort) null);
Iterable<Person> result = repository.findAll((QSort) null);
assertThat(result, containsInAnyOrder(TYRION, JAIME, CERSEI));
}
/**
* @see DATAKV-95
*/
@Test
public void executesExistsCorrectly() {
repository.save(LENNISTERS);
assertThat(repository.exists(QPerson.person.age.eq(CERSEI.getAge())), is(true));
}
/*
* (non-Javadoc)
* @see org.springframework.data.map.SimpleKeyValueRepositoryUnitTests#getRepository(org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactory)
*/
@Override
protected Class<? extends PersonRepository> getRepositoryClass() {
return QPersonRepository.class;
}
QPersonRepository getQPersonRepo() {
return ((QPersonRepository) repository);
}
static interface QPersonRepository extends PersonRepository, QueryDslPredicateExecutor<Person> {
protected QPersonRepository getRepository(KeyValueRepositoryFactory factory) {
return factory.getRepository(QPersonRepository.class);
}
static interface QPersonRepository extends org.springframework.data.map.AbstractRepositoryUnitTests.PersonRepository,
QueryDslPredicateExecutor<Person> {}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,156 +15,19 @@
*/
package org.springframework.data.map;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.*;
import static org.hamcrest.collection.IsIterableContainingInOrder.*;
import static org.hamcrest.core.Is.*;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import org.hamcrest.collection.IsCollectionWithSize;
import org.hamcrest.collection.IsIterableContainingInOrder;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.keyvalue.Person;
import org.springframework.data.keyvalue.core.KeyValueTemplate;
import org.springframework.data.keyvalue.repository.KeyValueRepository;
import org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactory;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.keyvalue.repository.support.SimpleKeyValueRepository;
import org.springframework.data.map.AbstractRepositoryUnitTests.PersonRepository;
/**
* Unit tests for {@link SimpleKeyValueRepository}.
*
* @author Christoph Strobl
* @author Oliver Gierke
*/
public class SimpleKeyValueRepositoryUnitTests {
public class SimpleKeyValueRepositoryUnitTests extends AbstractRepositoryUnitTests<PersonRepository> {
protected static final Person CERSEI = new Person("cersei", 19);
protected static final Person JAIME = new Person("jaime", 19);
protected static final Person TYRION = new Person("tyrion", 17);
protected static List<Person> LENNISTERS = Arrays.asList(CERSEI, JAIME, TYRION);
protected PersonRepository repository;
protected KeyValueTemplate template = new KeyValueTemplate(new MapKeyValueAdapter());
@Before
public void setup() {
this.repository = new KeyValueRepositoryFactory(template).getRepository(getRepositoryClass());
protected PersonRepository getRepository(KeyValueRepositoryFactory factory) {
return factory.getRepository(PersonRepository.class);
}
/**
* @see DATACMNS-525
*/
@Test
public void findBy() {
this.repository.save(LENNISTERS);
assertThat(this.repository.findByAge(19), containsInAnyOrder(CERSEI, JAIME));
}
/**
* @see DATACMNS-525
*/
@Test
public void combindedFindUsingAnd() {
this.repository.save(LENNISTERS);
assertThat(this.repository.findByFirstnameAndAge(JAIME.getFirstname(), 19), containsInAnyOrder(JAIME));
}
/**
* @see DATACMNS-525
*/
@Test
public void findPage() {
this.repository.save(LENNISTERS);
Page<Person> page = this.repository.findByAge(19, new PageRequest(0, 1));
assertThat(page.hasNext(), is(true));
assertThat(page.getTotalElements(), is(2L));
assertThat(page.getContent(), IsCollectionWithSize.hasSize(1));
Page<Person> next = this.repository.findByAge(19, page.nextPageable());
assertThat(next.hasNext(), is(false));
assertThat(next.getTotalElements(), is(2L));
assertThat(next.getContent(), IsCollectionWithSize.hasSize(1));
}
/**
* @see DATACMNS-525
*/
@Test
public void findByConnectingOr() {
this.repository.save(LENNISTERS);
assertThat(this.repository.findByAgeOrFirstname(19, TYRION.getFirstname()),
containsInAnyOrder(CERSEI, JAIME, TYRION));
}
/**
* @see DATACMNS-525
*/
@Test
public void singleEntityExecution() {
this.repository.save(LENNISTERS);
assertThat(this.repository.findByAgeAndFirstname(TYRION.getAge(), TYRION.getFirstname()), is(TYRION));
}
/**
* @see DATACMNS-525
*/
@Test
public void findAllShouldRespectSort() {
this.repository.save(LENNISTERS);
assertThat(this.repository.findAll(new Sort(new Sort.Order(Direction.ASC, "age"), new Sort.Order(Direction.DESC,
"firstname"))), IsIterableContainingInOrder.contains(TYRION, JAIME, CERSEI));
}
/**
* @see DATACMNS-525
*/
@Test
public void derivedFinderShouldRespectSort() {
repository.save(LENNISTERS);
List<Person> result = repository.findByAgeGreaterThanOrderByAgeAscFirstnameDesc(2);
assertThat(result, contains(TYRION, JAIME, CERSEI));
}
protected Class<? extends PersonRepository> getRepositoryClass() {
return PersonRepository.class;
}
public static interface PersonRepository extends CrudRepository<Person, String>, KeyValueRepository<Person, String> {
List<Person> findByAge(int age);
List<Person> findByFirstname(String firstname);
List<Person> findByFirstnameAndAge(String firstname, int age);
Page<Person> findByAge(int age, Pageable page);
List<Person> findByAgeOrFirstname(int age, String firstname);
Person findByAgeAndFirstname(int age, String firstname);
List<Person> findByAgeGreaterThanOrderByAgeAscFirstnameDesc(int age);
}
}