From bdbf101fd7f3e920f28634bb24cd0667855fa1e2 Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 13 Aug 2015 13:16:20 -0700 Subject: [PATCH] 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. --- .../gemfire/repository/sample/Customer.java | 6 +- .../repository/sample/PersonRepository.java | 3 +- .../repository/sample/UserRepository.java | 12 +- .../UserRepositoryQueriesIntegrationTest.java | 2 +- ...yAnnotationExtensionsInUserRepository.java | 55 +++++++ ...nsionsInUserRepositoryIntegrationTest.java | 135 ++++++++++++++++++ ...nUserRepositoryIntegrationTest-context.xml | 32 +++++ 7 files changed, 237 insertions(+), 8 deletions(-) create mode 100644 src/test/java/org/springframework/data/gemfire/repository/sample/UsingQueryAnnotationExtensionsInUserRepository.java create mode 100644 src/test/java/org/springframework/data/gemfire/repository/sample/UsingQueryAnnotationExtensionsInUserRepositoryIntegrationTest.java create mode 100644 src/test/resources/org/springframework/data/gemfire/repository/sample/UsingQueryAnnotationExtensionsInUserRepositoryIntegrationTest-context.xml diff --git a/src/test/java/org/springframework/data/gemfire/repository/sample/Customer.java b/src/test/java/org/springframework/data/gemfire/repository/sample/Customer.java index 6dab9cc1..5532e5c0 100644 --- a/src/test/java/org/springframework/data/gemfire/repository/sample/Customer.java +++ b/src/test/java/org/springframework/data/gemfire/repository/sample/Customer.java @@ -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()) diff --git a/src/test/java/org/springframework/data/gemfire/repository/sample/PersonRepository.java b/src/test/java/org/springframework/data/gemfire/repository/sample/PersonRepository.java index 0dcc5085..da5df52c 100644 --- a/src/test/java/org/springframework/data/gemfire/repository/sample/PersonRepository.java +++ b/src/test/java/org/springframework/data/gemfire/repository/sample/PersonRepository.java @@ -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 { diff --git a/src/test/java/org/springframework/data/gemfire/repository/sample/UserRepository.java b/src/test/java/org/springframework/data/gemfire/repository/sample/UserRepository.java index 09c0a7bd..09b9677b 100644 --- a/src/test/java/org/springframework/data/gemfire/repository/sample/UserRepository.java +++ b/src/test/java/org/springframework/data/gemfire/repository/sample/UserRepository.java @@ -33,6 +33,9 @@ import org.springframework.data.gemfire.repository.Query; @SuppressWarnings("unused") public interface UserRepository extends GemfireRepository { + @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 findDistinctByActiveTrue(); @@ -41,10 +44,9 @@ public interface UserRepository extends GemfireRepository { List findDistinctByUsernameLike(String username); - // NOTE unfortunately, the 'NOT LIKE' operator is unsupported in GemFire's Query/OQL syntax - //List 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 findDistinctByUsernameNotLike(String username); +*/ } diff --git a/src/test/java/org/springframework/data/gemfire/repository/sample/UserRepositoryQueriesIntegrationTest.java b/src/test/java/org/springframework/data/gemfire/repository/sample/UserRepositoryQueriesIntegrationTest.java index ac0e6c40..14a58d04 100644 --- a/src/test/java/org/springframework/data/gemfire/repository/sample/UserRepositoryQueriesIntegrationTest.java +++ b/src/test/java/org/springframework/data/gemfire/repository/sample/UserRepositoryQueriesIntegrationTest.java @@ -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 diff --git a/src/test/java/org/springframework/data/gemfire/repository/sample/UsingQueryAnnotationExtensionsInUserRepository.java b/src/test/java/org/springframework/data/gemfire/repository/sample/UsingQueryAnnotationExtensionsInUserRepository.java new file mode 100644 index 00000000..046c6370 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/repository/sample/UsingQueryAnnotationExtensionsInUserRepository.java @@ -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 { + + @Trace + @Limit(10) + @Hint("IdIdx") + @Import("org.springframework.data.gemfire.repository.sample.User") + @Query(" SELECT DISTINCT * FROM /Users x WHERE x.username LIKE $1 ORDER BY x.username ASC LIMIT 1") + List findBy(String username); + + @Trace + @Limit(5) + @Hint("UsernameIdx") + List findDistinctByUsernameLikeOrderByUsernameAsc(String username); + +} diff --git a/src/test/java/org/springframework/data/gemfire/repository/sample/UsingQueryAnnotationExtensionsInUserRepositoryIntegrationTest.java b/src/test/java/org/springframework/data/gemfire/repository/sample/UsingQueryAnnotationExtensionsInUserRepositoryIntegrationTest.java new file mode 100644 index 00000000..8d1b270d --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/repository/sample/UsingQueryAnnotationExtensionsInUserRepositoryIntegrationTest.java @@ -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 users; + + @Autowired + private UsingQueryAnnotationExtensionsInUserRepository userRepository; + + protected static User create(String username) { + return new User(username); + } + + protected static List users(String... usernames) { + List users = new ArrayList(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 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 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 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 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")))); + } + +} diff --git a/src/test/resources/org/springframework/data/gemfire/repository/sample/UsingQueryAnnotationExtensionsInUserRepositoryIntegrationTest-context.xml b/src/test/resources/org/springframework/data/gemfire/repository/sample/UsingQueryAnnotationExtensionsInUserRepositoryIntegrationTest-context.xml new file mode 100644 index 00000000..124f9434 --- /dev/null +++ b/src/test/resources/org/springframework/data/gemfire/repository/sample/UsingQueryAnnotationExtensionsInUserRepositoryIntegrationTest-context.xml @@ -0,0 +1,32 @@ + + + + + UsingQueryAnnotationExtensionsInUserRepositoryIntegrationTest + 0 + config + + + + + + + + + + + + +