DATACMNS-669 - Polishing.

Javadoc on MultiValueBinding. Hide QuerydslBindings.bind(…), ….include(…) and ….exclude(…) methods that take String arguments to reduce the probability of no query class being present.

Refined the value handling for empty request values which are represented as String[] containing an empty String. We now massage those into an empty Collection for MultiValueBindings and a null value for SingleValueInvocations.

We currently skip property values that match this pattern entirely but are prepared to allow a configuration flip switch to handle those scenarios in the future. We currently stick to skipping those values to prevent the bindings from having to deal with null values.

We now also allow the bindings to return null to indicate they don't want to get applied in the resulting predicate at all.

Switched to interface based customization to make it easier to use customizations with Spring Data REST. Added Querydsl-specific adapter of RepositoryInvoker to allow Spring Data REST to hook in the execution of the predicate obtained from request parameters.

Original pull request: #132.
This commit is contained in:
Oliver Gierke
2015-07-15 16:13:10 +02:00
parent dc49101850
commit cccfa5e5c5
14 changed files with 506 additions and 136 deletions

View File

@@ -0,0 +1,121 @@
/*
* Copyright 2015 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.querydsl;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Map;
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.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.support.RepositoryInvoker;
import org.springframework.util.MultiValueMap;
import com.mysema.query.types.Predicate;
/**
* Unit tests for {@link QuerydslRepositoryInvokerAdapter}.
*
* @author Oliver Gierke
* @soundtrack Emilie Nicolas - Grown Up
*/
@RunWith(MockitoJUnitRunner.class)
public class QuerydslRepositoryInvokerAdapterUnitTests {
@Mock RepositoryInvoker delegate;
@Mock QueryDslPredicateExecutor<Object> executor;
@Mock Predicate predicate;
QuerydslRepositoryInvokerAdapter adapter;
@Before
public void setUp() {
this.adapter = new QuerydslRepositoryInvokerAdapter(delegate, executor, predicate);
}
/**
* @see DATACMNS-669
*/
@Test
public void forwardsFindAllToExecutorWithPredicate() {
Sort sort = new Sort("firstname");
adapter.invokeFindAll(sort);
verify(executor, times(1)).findAll(predicate, sort);
verify(delegate, times(0)).invokeFindAll(sort);
}
/**
* @see DATACMNS-669
*/
@Test
public void forwardsFindAllWithPageableToExecutorWithPredicate() {
PageRequest pageable = new PageRequest(0, 10);
adapter.invokeFindAll(pageable);
verify(executor, times(1)).findAll(predicate, pageable);
verify(delegate, times(0)).invokeFindAll(pageable);
}
/**
* @see DATACMNS-669
*/
@Test
@SuppressWarnings({ "deprecation", "unchecked" })
public void forwardsMethodsToDelegate() {
adapter.hasDeleteMethod();
verify(delegate, times(1)).hasDeleteMethod();
adapter.hasFindAllMethod();
verify(delegate, times(1)).hasFindAllMethod();
adapter.hasFindOneMethod();
verify(delegate, times(1)).hasFindOneMethod();
adapter.hasSaveMethod();
verify(delegate, times(1)).hasSaveMethod();
adapter.invokeDelete(any(Serializable.class));
verify(delegate, times(1)).invokeDelete(any(Serializable.class));
adapter.invokeFindOne(any(Serializable.class));
verify(delegate, times(1)).invokeFindOne(any(Serializable.class));
adapter.invokeQueryMethod(any(Method.class), any(Map.class), any(Pageable.class), any(Sort.class));
verify(delegate, times(1)).invokeQueryMethod(any(Method.class), any(Map.class), any(Pageable.class),
any(Sort.class));
adapter.invokeQueryMethod(any(Method.class), (MultiValueMap<String, String>) any(MultiValueMap.class),
any(Pageable.class), any(Sort.class));
verify(delegate, times(1)).invokeQueryMethod(any(Method.class),
(MultiValueMap<String, String>) any(MultiValueMap.class), any(Pageable.class), any(Sort.class));
adapter.invokeSave(any());
verify(delegate, times(1)).invokeSave(any());
}
}