Fixes JIRA issue SGF-273 - OrderBy (static) and Sort parameter (dynamic) Repository Queries do not work.

This commit is contained in:
John Blum
2014-04-28 15:37:30 -07:00
parent 8fd0b0bf6d
commit 0c25e14be9
10 changed files with 310 additions and 99 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.data.gemfire.repository.query;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@@ -28,6 +29,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.domain.Sort;
import org.springframework.data.gemfire.repository.sample.Person;
import org.springframework.data.gemfire.repository.sample.RootUser;
@@ -45,14 +47,26 @@ public class QueryStringUnitTests {
@SuppressWarnings("rawtypes")
Region region;
protected Sort.Order createOrder(final String property) {
return createOrder(property, Sort.Direction.ASC);
}
protected Sort.Order createOrder(final String property, final Sort.Direction direction) {
return new Sort.Order(direction, property);
}
protected Sort createSort(Sort.Order... orders) {
return new Sort(orders);
}
// SGF-251
@Test
public void replacesDomainObjectWithRegionPathCorrectly() {
QueryString query = new QueryString("SELECT * FROM /Person p WHERE p.firstname = $1");
when(region.getFullPath()).thenReturn("/foo");
when(region.getFullPath()).thenReturn("/foo/bar");
assertThat(query.forRegion(Person.class, region).toString(), is("SELECT * FROM /foo p WHERE p.firstname = $1"));
assertThat(query.forRegion(Person.class, region).toString(), is("SELECT * FROM /foo/bar p WHERE p.firstname = $1"));
verify(region, never()).getName();
}
@@ -63,16 +77,16 @@ public class QueryStringUnitTests {
public void replacesDomainObjectWithPluralRegionPathCorrectly() {
QueryString query = new QueryString("SELECT * FROM /Persons p WHERE p.firstname = $1");
when(region.getFullPath()).thenReturn("/Persons");
when(region.getFullPath()).thenReturn("/People");
assertThat(query.forRegion(Person.class, region).toString(), is("SELECT * FROM /Persons p WHERE p.firstname = $1"));
assertThat(query.forRegion(Person.class, region).toString(), is("SELECT * FROM /People p WHERE p.firstname = $1"));
verify(region, never()).getName();
}
// SGF-252
@Test
public void replacesFullyQualifiedSubegionReferenceWithRegionPathCorrectly() {
public void replacesFullyQualifiedSubregionReferenceWithRegionPathCorrectly() {
QueryString query = new QueryString("SELECT * FROM //Local/Root/Users u WHERE u.username = $1");
when(region.getFullPath()).thenReturn("/Local/Root/Users");
@@ -96,4 +110,29 @@ public class QueryStringUnitTests {
assertThat(indexes, is((Iterable<Integer>) Arrays.asList(1, 2)));
}
@Test
public void addsOrderByClauseCorrectly() {
QueryString query = new QueryString("SELECT * FROM /People p WHERE p.lastName = $1")
.orderBy(createSort(createOrder("lastName", Sort.Direction.DESC), createOrder("firstName")));
assertEquals("SELECT * FROM /People p WHERE p.lastName = $1 ORDER BY lastName DESC, firstName ASC",
query.toString());
}
@Test
public void addsSingleOrderByClauseCorrectly() {
QueryString query = new QueryString("SELECT DISTINCT p.lastName FROM /People p WHERE p.firstName = $1")
.orderBy(createSort(createOrder("lastName")));
assertEquals("SELECT DISTINCT p.lastName FROM /People p WHERE p.firstName = $1 ORDER BY lastName ASC",
query.toString());
}
@Test
public void addsNoOrderByClauseCorrectly() {
QueryString query = new QueryString("SELECT * FROM /People p").orderBy(null);
assertEquals("SELECT * FROM /People p", query.toString());
}
}

View File

@@ -16,7 +16,9 @@
package org.springframework.data.gemfire.repository.sample;
import java.util.Collection;
import java.util.List;
import org.springframework.data.domain.Sort;
import org.springframework.data.gemfire.repository.GemfireRepository;
import org.springframework.data.gemfire.repository.Query;
@@ -53,4 +55,9 @@ public interface PersonRepository extends GemfireRepository<Person, Long> {
Collection<Person> findByLastnameEndingWith(String lastname);
Collection<Person> findByFirstnameContaining(String firstname);
List<Person> findDistinctByLastname(String lastName, Sort order);
List<Person> findDistinctPeopleByOrderByLastnameDesc(Sort order);
}

View File

@@ -0,0 +1,118 @@
/*
* Copyright 2010-2013 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.gemfire.repository.sample;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* The PersonRepositoryTest class...
*
* @author John Blum
* @see org.junit.Test
* @see org.junit.runner.RunWith
* @see org.springframework.data.gemfire.repository.sample.Person
* @see org.springframework.data.gemfire.repository.sample.PersonRepository
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
* @since 1.4.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SuppressWarnings("unused")
public class PersonRepositoryTest {
protected final AtomicLong ID_SEQUENCE = new AtomicLong(0l);
private Person cookieDoe = createPerson("Cookie", "Doe");
private Person janeDoe = createPerson("Jane", "Doe");
private Person jonDoe = createPerson("Jon", "Doe");
private Person pieDoe = createPerson("Pie", "Doe");
private Person jackHandy = createPerson("Jack", "Handy");
private Person sandyHandy = createPerson("Sandy", "Handy");
private Person imaPigg = createPerson("Ima", "Pigg");
@Autowired
private PersonRepository personRepo;
@Before
public void setup() {
if (personRepo.count() == 0) {
sandyHandy = personRepo.save(sandyHandy);
jonDoe = personRepo.save(jonDoe);
jackHandy = personRepo.save(jackHandy);
janeDoe = personRepo.save(janeDoe);
pieDoe = personRepo.save(pieDoe);
imaPigg = personRepo.save(imaPigg);
cookieDoe = personRepo.save(cookieDoe);
}
assertEquals(7l, personRepo.count());
}
protected Person createPerson(final String firstName, final String lastName) {
return new Person(ID_SEQUENCE.incrementAndGet(), firstName, lastName);
}
protected Sort.Order createOrder(final String property) {
return createOrder(property, Sort.Direction.ASC);
}
protected Sort.Order createOrder(final String property, final Sort.Direction direction) {
return new Sort.Order(direction, property);
}
protected Sort createSort(final Sort.Order... orders) {
return new Sort(orders);
}
@Test
public void testFindDistinctPeopleWithOrder() {
List<Person> actualPeople = personRepo.findDistinctPeopleByOrderByLastnameDesc(
createSort(createOrder("firstname")));
assertNotNull(actualPeople);
assertFalse(actualPeople.isEmpty());
assertEquals(7, actualPeople.size());
assertEquals(Arrays.asList(imaPigg, jackHandy, sandyHandy, cookieDoe, janeDoe, jonDoe, pieDoe), actualPeople);
}
@Test
public void testFindDistinctPersonWithNoOrder() {
List<Person> actualPeople = personRepo.findDistinctByLastname("Pigg", null);
assertNotNull(actualPeople);
assertFalse(actualPeople.isEmpty());
assertEquals(1, actualPeople.size());
assertEquals(String.format("Expected '%1$s'; but was '%2$s'", imaPigg, actualPeople.get(0)),
imaPigg, actualPeople.get(0));
}
}

View File

@@ -213,14 +213,14 @@ public class SubRegionRepositoryIntegrationTest {
assertNotNull(javaProgrammers);
assertFalse(javaProgrammers.isEmpty());
assertEquals(2, javaProgrammers.size());
assertTrue(javaProgrammers.containsAll(getProgrammers("JamesGosling", "JoshuaBloch")));
assertEquals(javaProgrammers, getProgrammers("JamesGosling", "JoshuaBloch"));
List<Programmer> groovyProgrammers = programmersRepo.findDistinctByProgrammingLanguageLikeOrderByUsernameAsc("Groovy");
assertNotNull(groovyProgrammers);
assertFalse(groovyProgrammers.isEmpty());
assertEquals(1, groovyProgrammers.size());
assertTrue(groovyProgrammers.containsAll(getProgrammers("JamesStrachan")));
assertEquals(groovyProgrammers, getProgrammers("JamesStrachan"));
programmersRepo.save(new Wrapper<Programmer, String>(createProgrammer("RodJohnson", "Java"), "RodJohnson"));
programmersRepo.save(new Wrapper<Programmer, String>(createProgrammer("GuillaumeLaforge", "Groovy"), "GuillaumeLaforge"));
@@ -231,21 +231,21 @@ public class SubRegionRepositoryIntegrationTest {
assertNotNull(javaProgrammers);
assertFalse(javaProgrammers.isEmpty());
assertEquals(3, javaProgrammers.size());
assertTrue(javaProgrammers.containsAll(getProgrammers("JamesGosling", "JoshuaBloch", "RodJohnson")));
assertEquals(javaProgrammers, getProgrammers("JamesGosling", "JoshuaBloch", "RodJohnson"));
groovyProgrammers = programmersRepo.findDistinctByProgrammingLanguageLikeOrderByUsernameAsc("Groovy");
assertNotNull(groovyProgrammers);
assertFalse(groovyProgrammers.isEmpty());
assertEquals(3, groovyProgrammers.size());
assertTrue(groovyProgrammers.containsAll(getProgrammers("GraemeRocher", "GuillaumeLaforge", "JamesStrachan")));
assertEquals(groovyProgrammers, getProgrammers("GraemeRocher", "GuillaumeLaforge", "JamesStrachan"));
List<Programmer> javaLikeProgrammers = programmersRepo.findDistinctByProgrammingLanguageLikeOrderByUsernameAsc("Java%");
assertNotNull(javaLikeProgrammers);
assertFalse(javaLikeProgrammers.isEmpty());
assertEquals(4, javaLikeProgrammers.size());
assertTrue(javaLikeProgrammers.containsAll(getProgrammers("BrendanEich", "JamesGosling", "JoshuaBloch", "RodJohnson")));
assertEquals(javaLikeProgrammers, getProgrammers("BrendanEich", "JamesGosling", "JoshuaBloch", "RodJohnson"));
List<Programmer> pseudoCodeProgrammers = programmersRepo.findDistinctByProgrammingLanguageLikeOrderByUsernameAsc("PseudoCode");