LDAP-281: Now supporting different types of criteria in generated queries.
This commit is contained in:
@@ -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"
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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<LdapQuery, ContainerC
|
||||
|
||||
@Override
|
||||
protected ContainerCriteria create(Part part, Iterator<Object> 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<Object> 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<LdapQuery, ContainerC
|
||||
|
||||
@Override
|
||||
protected ContainerCriteria and(Part part, ContainerCriteria base, Iterator<Object> iterator) {
|
||||
return base.and(getAttribute(part)).is(iterator.next().toString());
|
||||
ConditionCriteria criteria = base.and(getAttribute(part));
|
||||
|
||||
return appendCondition(part, iterator, criteria);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -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<UnitTestPersonRepository> 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());
|
||||
}
|
||||
}
|
||||
@@ -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<UnitTestPerson> {
|
||||
List<UnitTestPerson> findByFullName(String name);
|
||||
List<UnitTestPerson> findByFullNameNot(String name);
|
||||
List<UnitTestPerson> findByFullNameLike(String name);
|
||||
List<UnitTestPerson> findByFullNameNotLike(String name);
|
||||
List<UnitTestPerson> findByFullNameStartsWith(String name);
|
||||
List<UnitTestPerson> findByFullNameEndsWith(String name);
|
||||
List<UnitTestPerson> findByFullNameContains(String name);
|
||||
List<UnitTestPerson> findByFullNameGreaterThanEqual(String name);
|
||||
List<UnitTestPerson> findByFullNameLessThanEqual(String name);
|
||||
List<UnitTestPerson> findByFullNameIsNotNull();
|
||||
List<UnitTestPerson> findByFullNameIsNull();
|
||||
|
||||
List<UnitTestPerson> findByFullNameAndLastName(String fullName, String lastName);
|
||||
List<UnitTestPerson> findByFullNameAndLastNameNot(String fullName, String lastName);
|
||||
|
||||
}
|
||||
13
core/src/test/resources/query-test.xml
Normal file
13
core/src/test/resources/query-test.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ldap="http://www.springframework.org/schema/ldap"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
|
||||
|
||||
<ldap:context-source url="ldap://localhost:389"
|
||||
username="cn=Admin"
|
||||
password="secret" />
|
||||
<ldap:ldap-template />
|
||||
|
||||
<ldap:repositories base-package="org.springframework.ldap.repository.query" />
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user