From aa81fcb9f2360c202086d0292fb62814874f90c6 Mon Sep 17 00:00:00 2001 From: Mattias Hellborg Arthursson Date: Fri, 8 Nov 2013 10:29:17 +0100 Subject: [PATCH] LDAP-281: Now supporting different types of criteria in generated queries. --- core/build.gradle | 4 +- .../repository/query/LdapQueryCreator.java | 46 +++++- .../PartTreeLdapRepositoryQueryTest.java | 143 ++++++++++++++++++ .../query/UnitTestPersonRepository.java | 43 ++++++ core/src/test/resources/query-test.xml | 13 ++ 5 files changed, 244 insertions(+), 5 deletions(-) create mode 100644 core/src/test/java/org/springframework/ldap/repository/query/PartTreeLdapRepositoryQueryTest.java create mode 100644 core/src/test/java/org/springframework/ldap/repository/query/UnitTestPersonRepository.java create mode 100644 core/src/test/resources/query-test.xml diff --git a/core/build.gradle b/core/build.gradle index 1cb69792..749ea8fe 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -26,6 +26,8 @@ dependencies { "commons-lang:commons-lang:$commonsLangVersion", "gsbase:gsbase:$gsbaseVersion", "org.mockito:mockito-core:$mockitoVersion", - "org.slf4j:slf4j-log4j12:$slf4jVersion" + "org.slf4j:slf4j-log4j12:$slf4jVersion", + "org.springframework:spring-test:$springVersion" + } diff --git a/core/src/main/java/org/springframework/ldap/repository/query/LdapQueryCreator.java b/core/src/main/java/org/springframework/ldap/repository/query/LdapQueryCreator.java index b2191c56..435e7491 100644 --- a/core/src/main/java/org/springframework/ldap/repository/query/LdapQueryCreator.java +++ b/core/src/main/java/org/springframework/ldap/repository/query/LdapQueryCreator.java @@ -24,6 +24,7 @@ import org.springframework.data.repository.query.parser.AbstractQueryCreator; import org.springframework.data.repository.query.parser.Part; import org.springframework.data.repository.query.parser.PartTree; import org.springframework.ldap.odm.core.ObjectDirectoryMapper; +import org.springframework.ldap.query.ConditionCriteria; import org.springframework.ldap.query.ContainerCriteria; import org.springframework.ldap.query.LdapQuery; @@ -55,9 +56,44 @@ public class LdapQueryCreator extends AbstractQueryCreator iterator) { - return query() - .where(getAttribute(part)) - .is(iterator.next().toString()); + ConditionCriteria criteria = query().where(getAttribute(part)); + + return appendCondition(part, iterator, criteria); + } + + private ContainerCriteria appendCondition(Part part, Iterator iterator, ConditionCriteria criteria) { + Part.Type type = part.getType(); + + String value = null; + if(iterator.hasNext()){ + value = iterator.next().toString(); + } + switch (type) { + case NEGATING_SIMPLE_PROPERTY: + return criteria.not().is(value); + case SIMPLE_PROPERTY: + return criteria.is(value); + case STARTING_WITH: + return criteria.like(value + "*"); + case ENDING_WITH: + return criteria.like("*" + value); + case CONTAINING: + return criteria.like("*" + value + "*"); + case LIKE: + return criteria.like(value); + case NOT_LIKE: + return criteria.not().like(value); + case GREATER_THAN_EQUAL: + return criteria.gte(value); + case LESS_THAN_EQUAL: + return criteria.lte(value); + case IS_NOT_NULL: + return criteria.isPresent(); + case IS_NULL: + return criteria.not().isPresent(); + } + + throw new IllegalArgumentException(String.format("%s queries are not supported for LDAP repositories", type)); } private String getAttribute(Part part) { @@ -71,7 +107,9 @@ public class LdapQueryCreator extends AbstractQueryCreator iterator) { - return base.and(getAttribute(part)).is(iterator.next().toString()); + ConditionCriteria criteria = base.and(getAttribute(part)); + + return appendCondition(part, iterator, criteria); } @Override diff --git a/core/src/test/java/org/springframework/ldap/repository/query/PartTreeLdapRepositoryQueryTest.java b/core/src/test/java/org/springframework/ldap/repository/query/PartTreeLdapRepositoryQueryTest.java new file mode 100644 index 00000000..401e5b66 --- /dev/null +++ b/core/src/test/java/org/springframework/ldap/repository/query/PartTreeLdapRepositoryQueryTest.java @@ -0,0 +1,143 @@ +package org.springframework.ldap.repository.query; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.odm.core.impl.UnitTestPerson; +import org.springframework.ldap.query.LdapQuery; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; + +import java.lang.reflect.Method; + +import static org.junit.Assert.assertEquals; + +/** + * @author Mattias Hellborg Arthursson + */ +@ContextConfiguration("classpath:/query-test.xml") +public class PartTreeLdapRepositoryQueryTest extends AbstractJUnit4SpringContextTests { + + @Autowired + private LdapTemplate ldapTemplate; + private Class targetClass; + private DefaultRepositoryMetadata repositoryMetadata; + + @Before + public void prepareTest() { + targetClass = UnitTestPersonRepository.class; + repositoryMetadata = new DefaultRepositoryMetadata(targetClass); + } + + @Test + public void testFindByFullName() throws NoSuchMethodException { + assertFilterForMethod( + targetClass.getMethod("findByFullName", String.class), + "(cn=John Doe)", + "John Doe"); + } + + @Test + public void testFindByFullNameLike() throws NoSuchMethodException { + assertFilterForMethod( + targetClass.getMethod("findByFullNameLike", String.class), + "(cn=*John*)", + "*John*"); + } + + @Test + public void testFindByFullNameStartsWith() throws NoSuchMethodException { + assertFilterForMethod( + targetClass.getMethod("findByFullNameStartsWith", String.class), + "(cn=John*)", + "John"); + } + + @Test + public void testFindByFullNameEndsWith() throws NoSuchMethodException { + assertFilterForMethod( + targetClass.getMethod("findByFullNameEndsWith", String.class), + "(cn=*John)", + "John"); + } + + @Test + public void testFindByFullNameContains() throws NoSuchMethodException { + assertFilterForMethod( + targetClass.getMethod("findByFullNameContains", String.class), + "(cn=*John*)", + "John"); + } + + @Test + public void testFindByFullNameGreaterThanEqual() throws NoSuchMethodException { + assertFilterForMethod( + targetClass.getMethod("findByFullNameGreaterThanEqual", String.class), + "(cn>=John)", + "John"); + } + + @Test + public void testFindByFullNameLessThanEqual() throws NoSuchMethodException { + assertFilterForMethod( + targetClass.getMethod("findByFullNameLessThanEqual", String.class), + "(cn<=John)", + "John"); + } + + @Test + public void testFindByFullNameIsNotNull() throws NoSuchMethodException { + assertFilterForMethod( + targetClass.getMethod("findByFullNameIsNotNull"), + "(cn=*)"); + } + + @Test + public void testFindByFullNameIsNull() throws NoSuchMethodException { + assertFilterForMethod( + targetClass.getMethod("findByFullNameIsNull"), + "(!(cn=*))"); + } + + @Test + public void testFindByFullNameNot() throws NoSuchMethodException { + assertFilterForMethod( + targetClass.getMethod("findByFullNameNot", String.class), + "(!(cn=John Doe))", + "John Doe"); + } + + @Test + public void testFindByFullNameNotLike() throws NoSuchMethodException { + assertFilterForMethod( + targetClass.getMethod("findByFullNameNotLike", String.class), + "(!(cn=*John*))", + "*John*"); + } + + @Test + public void testFindByFullNameAndLastName() throws NoSuchMethodException { + assertFilterForMethod( + targetClass.getMethod("findByFullNameAndLastName", String.class, String.class), + "(&(cn=John Doe)(sn=Doe))", + "John Doe", "Doe"); + } + + @Test + public void testFindByFullNameAndLastNameNot() throws NoSuchMethodException { + assertFilterForMethod( + targetClass.getMethod("findByFullNameAndLastNameNot", String.class, String.class), + "(&(cn=John Doe)(!(sn=Doe)))", + "John Doe", "Doe"); + } + + private void assertFilterForMethod(Method targetMethod, String expectedFilter, Object... expectedParams) { + LdapQueryMethod queryMethod = new LdapQueryMethod(targetMethod, repositoryMetadata); + PartTreeLdapRepositoryQuery tested = new PartTreeLdapRepositoryQuery(queryMethod, UnitTestPerson.class, ldapTemplate); + + LdapQuery query = tested.createQuery(expectedParams); + assertEquals(expectedFilter, query.filter().encode()); + } +} diff --git a/core/src/test/java/org/springframework/ldap/repository/query/UnitTestPersonRepository.java b/core/src/test/java/org/springframework/ldap/repository/query/UnitTestPersonRepository.java new file mode 100644 index 00000000..04ce36b8 --- /dev/null +++ b/core/src/test/java/org/springframework/ldap/repository/query/UnitTestPersonRepository.java @@ -0,0 +1,43 @@ +/* + * Copyright 2005-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.ldap.repository.query; + +import org.springframework.ldap.odm.core.impl.UnitTestPerson; +import org.springframework.ldap.repository.LdapRepository; + +import java.util.List; + +/** + * @author Mattias Hellborg Arthursson + */ +public interface UnitTestPersonRepository extends LdapRepository { + List findByFullName(String name); + List findByFullNameNot(String name); + List findByFullNameLike(String name); + List findByFullNameNotLike(String name); + List findByFullNameStartsWith(String name); + List findByFullNameEndsWith(String name); + List findByFullNameContains(String name); + List findByFullNameGreaterThanEqual(String name); + List findByFullNameLessThanEqual(String name); + List findByFullNameIsNotNull(); + List findByFullNameIsNull(); + + List findByFullNameAndLastName(String fullName, String lastName); + List findByFullNameAndLastNameNot(String fullName, String lastName); + +} diff --git a/core/src/test/resources/query-test.xml b/core/src/test/resources/query-test.xml new file mode 100644 index 00000000..7302e56f --- /dev/null +++ b/core/src/test/resources/query-test.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file