SGF-392 - Add support for OQL Query statement extensions in Repository Query methods via Annotations.

Adding integration tests to test the use of the custom OQL Query Annotation extensions in Repository Query methods against GemFire's actual OQL Query engine.
This commit is contained in:
John Blum
2015-08-13 13:16:20 -07:00
parent 23d05aeaee
commit bdbf101fd7
7 changed files with 237 additions and 8 deletions

View File

@@ -74,6 +74,10 @@ public class Customer {
this.lastName = lastName;
}
public String getName() {
return String.format("%1$s %2$s", getFirstName(), getLastName());
}
protected static boolean equalsIgnoreNull(final Object obj1, final Object obj2) {
return (obj1 == null ? obj2 == null : obj1.equals(obj2));
}
@@ -88,7 +92,7 @@ public class Customer {
return false;
}
final Customer that = (Customer) obj;
Customer that = (Customer) obj;
return equalsIgnoreNull(this.getId(), that.getId())
&& ObjectUtils.nullSafeEquals(this.getFirstName(), that.getFirstName())

View File

@@ -23,10 +23,11 @@ import org.springframework.data.gemfire.repository.GemfireRepository;
import org.springframework.data.gemfire.repository.Query;
/**
* Sample repository interface managing {@link Person}s.
* Sample Repository interface managing {@link Person}s.
*
* @author Oliver Gierke
* @author David Turanski
* @author John Blum
*/
public interface PersonRepository extends GemfireRepository<Person, Long> {

View File

@@ -33,6 +33,9 @@ import org.springframework.data.gemfire.repository.Query;
@SuppressWarnings("unused")
public interface UserRepository extends GemfireRepository<User, String> {
@Query("SELECT count(*) FROM /Users u WHERE u.username LIKE $1")
Integer countUsersByUsernameLike(String username);
//@Query("SELECT DISTINCT * FROM /Users u WHERE u.active = true")
List<User> findDistinctByActiveTrue();
@@ -41,10 +44,9 @@ public interface UserRepository extends GemfireRepository<User, String> {
List<User> findDistinctByUsernameLike(String username);
// NOTE unfortunately, the 'NOT LIKE' operator is unsupported in GemFire's Query/OQL syntax
//List<User> findDistinctByUsernameNotLike(String username);
@Query("SELECT count(*) FROM /Users u WHERE u.username LIKE $1")
Integer countUsersByUsernameLike(String username);
/*
//NOTE unfortunately, the 'NOT LIKE' operator is unsupported in GemFire's Query/OQL syntax
List<User> findDistinctByUsernameNotLike(String username);
*/
}

View File

@@ -85,7 +85,7 @@ public class UserRepositoryQueriesIntegrationTest {
}
protected static int toIntValue(final Integer value) {
return (value == null ? 0 : value.intValue());
return (value == null ? 0 : value);
}
@Before

View File

@@ -0,0 +1,55 @@
/*
* 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 java.util.List;
import org.springframework.data.gemfire.repository.GemfireRepository;
import org.springframework.data.gemfire.repository.Query;
import org.springframework.data.gemfire.repository.query.annotation.Hint;
import org.springframework.data.gemfire.repository.query.annotation.Import;
import org.springframework.data.gemfire.repository.query.annotation.Limit;
import org.springframework.data.gemfire.repository.query.annotation.Trace;
/**
* The UsingQueryAnnotationExtensionsInUserRepository class...
*
* @author John Blum
* @see org.springframework.data.gemfire.repository.GemfireRepository
* @see org.springframework.data.gemfire.repository.query.annotation.Hint
* @see org.springframework.data.gemfire.repository.query.annotation.Import
* @see org.springframework.data.gemfire.repository.query.annotation.Limit
* @see org.springframework.data.gemfire.repository.query.annotation.Trace
* @see org.springframework.data.gemfire.repository.sample.User
* @since 1.7.0
*/
@SuppressWarnings("unused")
public interface UsingQueryAnnotationExtensionsInUserRepository extends GemfireRepository<User, String> {
@Trace
@Limit(10)
@Hint("IdIdx")
@Import("org.springframework.data.gemfire.repository.sample.User")
@Query("<HINT 'UsernameIdx'> SELECT DISTINCT * FROM /Users x WHERE x.username LIKE $1 ORDER BY x.username ASC LIMIT 1")
List<User> findBy(String username);
@Trace
@Limit(5)
@Hint("UsernameIdx")
List<User> findDistinctByUsernameLikeOrderByUsernameAsc(String username);
}

View File

@@ -0,0 +1,135 @@
/*
* 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.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.gemstone.gemfire.cache.Region;
/**
* The UsingQueryAnnotationExtensionsInUserRepositoryIntegrationTest class is a test suite of test cases testing
* the contract and functionality of the Spring Data Commons Repository abstraction, Spring Data GemFire Repository
* extention with custom OQL Query annotations.
*
* @author John Blum
* @see org.junit.Test
* @see org.junit.runner.RunWith
* @see org.springframework.data.gemfire.repository.GemfireRepository
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
* @see com.gemstone.gemfire.cache.Region
* @since 1.7.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@SuppressWarnings("unused")
public class UsingQueryAnnotationExtensionsInUserRepositoryIntegrationTest {
@Resource(name = "Users")
private Region<String, User> users;
@Autowired
private UsingQueryAnnotationExtensionsInUserRepository userRepository;
protected static User create(String username) {
return new User(username);
}
protected static List<User> users(String... usernames) {
List<User> users = new ArrayList<User>(usernames.length);
for (String username : usernames) {
users.add(create(username));
}
return users;
}
protected User put(User user) {
return put(users, user);
}
protected User put(Region<String, User> users, User user) {
users.put(user.getUsername(), user);
return user;
}
@Before
public void setup() {
assertThat(users, is(notNullValue()));
if (users.isEmpty()) {
assertThat(users.size(), is(equalTo(0)));
put(create("jonDoe"));
put(create("joeDoe"));
put(create("janeDoe"));
put(create("cookieDoe"));
put(create("froDoe"));
put(create("pieDoe"));
put(create("sourDoe"));
put(create("toeDoe"));
assertThat(users.size(), is(equalTo(8)));
}
}
@Test
public void queryUsingFindBy() {
List<User> users = userRepository.findBy("j%Doe");
assertThat(users, is(notNullValue()));
assertThat(users.isEmpty(), is(false));
assertThat(users.size(), is(equalTo(1)));
assertThat(users.get(0), is(equalTo(create("janeDoe"))));
}
@Test
public void queryUsingFindByUsernameOrderedAndLimited() {
List<User> users = userRepository.findDistinctByUsernameLikeOrderByUsernameAsc("%o%Doe");
assertThat(users, is(notNullValue()));
assertThat(users.isEmpty(), is(false));
assertThat(users.size(), is(equalTo(5)));
assertThat(users, is(equalTo(users("cookieDoe", "froDoe", "joeDoe", "jonDoe", "sourDoe"))));
}
@Test
public void queryUsingFindByUsernameOrderedAndUnderLimit() {
List<User> users = userRepository.findDistinctByUsernameLikeOrderByUsernameAsc("%oo%Doe");
assertThat(users, is(notNullValue()));
assertThat(users.isEmpty(), is(false));
assertThat(users.size(), is(equalTo(1)));
assertThat(users.get(0), is(equalTo(create("cookieDoe"))));
}
}

View File

@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:gfe="http://www.springframework.org/schema/gemfire"
xmlns:gfe-data="http://www.springframework.org/schema/data/gemfire"
xmlns:repo="http://www.springframework.org/schema/data/repository"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
http://www.springframework.org/schema/data/gemfire http://www.springframework.org/schema/data/gemfire/spring-data-gemfire.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
">
<util:properties id="gemfireProperties">
<prop key="name">UsingQueryAnnotationExtensionsInUserRepositoryIntegrationTest</prop>
<prop key="mcast-port">0</prop>
<prop key="log-level">config</prop>
</util:properties>
<gfe:cache properties-ref="gemfireProperties"/>
<gfe:replicated-region id="Users" persistent="false"/>
<gfe:index id="UsernameIdx" expression="username" from="/Users" type="PRIMARY_KEY"/>
<gfe-data:repositories base-package="org.springframework.data.gemfire.repository.sample">
<repo:include-filter type="assignable" expression="org.springframework.data.gemfire.repository.sample.UsingQueryAnnotationExtensionsInUserRepository"/>
</gfe-data:repositories>
</beans>