DATALDAP-65 - Fix query execution without predicates using Querydsl.
Query execution without a predicate now no longer fails but falls back to LdapOperations.findAll(…). Previously, we did not initialize the filter of LdapQuery which caused an IllegalStateException in LdapOperations.find(…).
This commit is contained in:
@@ -20,6 +20,7 @@ import static org.springframework.ldap.query.LdapQueryBuilder.*;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.ldap.filter.AbsoluteTrueFilter;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -75,12 +76,22 @@ public class QueryDslLdapQuery<K> implements FilteredClause<QueryDslLdapQuery<K>
|
||||
*/
|
||||
@Override
|
||||
public QueryDslLdapQuery<K> where(Predicate... o) {
|
||||
|
||||
if (o == null) {
|
||||
return this;
|
||||
}
|
||||
return queryMixin.where(o);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<K> list() {
|
||||
return (List<K>) ldapOperations.find(buildQuery(), entityType);
|
||||
|
||||
LdapQuery ldapQuery = buildQuery();
|
||||
if (ldapQuery.filter() instanceof AbsoluteTrueFilter) {
|
||||
return (List<K>) ldapOperations.findAll(entityType);
|
||||
}
|
||||
|
||||
return (List<K>) ldapOperations.find(ldapQuery, entityType);
|
||||
}
|
||||
|
||||
public K uniqueResult() {
|
||||
@@ -88,7 +99,8 @@ public class QueryDslLdapQuery<K> implements FilteredClause<QueryDslLdapQuery<K>
|
||||
}
|
||||
|
||||
LdapQuery buildQuery() {
|
||||
return query().filter(filterGenerator.handle(queryMixin.getMetadata().getWhere()));
|
||||
}
|
||||
|
||||
Predicate where = queryMixin.getMetadata().getWhere();
|
||||
return where != null ? query().filter(filterGenerator.handle(where)) : query().filter(new AbsoluteTrueFilter());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2018 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.ldap.repository.support;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.ldap.filter.AbsoluteTrueFilter;
|
||||
import org.springframework.ldap.filter.EqualsFilter;
|
||||
import org.springframework.ldap.odm.core.impl.DefaultObjectDirectoryMapper;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link QueryDslLdapQuery}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class QueryDslLdapQueryUnitTests {
|
||||
|
||||
@Mock LdapOperations ldapOperations;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
when(ldapOperations.getObjectDirectoryMapper()).thenReturn(new DefaultObjectDirectoryMapper());
|
||||
}
|
||||
|
||||
@Test // DATALDAP-65
|
||||
public void shouldCreateFilter() {
|
||||
|
||||
QueryDslLdapQuery<UnitTestPerson> query = new QueryDslLdapQuery<UnitTestPerson>(ldapOperations,
|
||||
UnitTestPerson.class);
|
||||
|
||||
LdapQuery ldapQuery = query.where(QPerson.person.fullName.eq("foo")).buildQuery();
|
||||
|
||||
assertThat(ldapQuery.filter()).isInstanceOf(EqualsFilter.class);
|
||||
}
|
||||
|
||||
@Test // DATALDAP-65
|
||||
public void shouldCreateEmptyFilter() {
|
||||
|
||||
QueryDslLdapQuery<UnitTestPerson> query = new QueryDslLdapQuery<UnitTestPerson>(ldapOperations,
|
||||
UnitTestPerson.class);
|
||||
|
||||
LdapQuery ldapQuery = query.buildQuery();
|
||||
|
||||
assertThat(ldapQuery.filter()).isInstanceOf(AbsoluteTrueFilter.class);
|
||||
}
|
||||
|
||||
@Test // DATALDAP-65
|
||||
public void shouldCreateEmptyFilterFromWhereNull() {
|
||||
|
||||
QueryDslLdapQuery<UnitTestPerson> query = new QueryDslLdapQuery<UnitTestPerson>(ldapOperations, QPerson.person);
|
||||
|
||||
LdapQuery ldapQuery = query.where(null).buildQuery();
|
||||
|
||||
assertThat(ldapQuery.filter()).isInstanceOf(AbsoluteTrueFilter.class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user