/*
* Copyright 2016-2017 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.repository.core.support;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import io.reactivex.Completable;
import io.reactivex.Maybe;
import reactor.core.publisher.Mono;
import rx.Single;
import java.io.Serializable;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.reactive.ReactiveSortingRepository;
/**
* Unit tests for {@link RepositoryFactorySupport} using reactive wrapper types.
*
* @author Mark Paluch
* @author Christoph Strobl
*/
@RunWith(MockitoJUnitRunner.Silent.class)
public class ReactiveWrapperRepositoryFactorySupportUnitTests {
public @Rule ExpectedException exception = ExpectedException.none();
DummyRepositoryFactory factory;
@Mock ReactiveSortingRepository<Object, Serializable> backingRepo;
@Mock ObjectRepositoryCustom customImplementation;
@Before
public void setUp() {
factory = new DummyRepositoryFactory(backingRepo);
}
@Test // DATACMNS-836
public void invokesCustomMethodIfItRedeclaresACRUDOne() {
ObjectRepository repository = factory.getRepository(ObjectRepository.class, customImplementation);
repository.findById(1);
verify(customImplementation, times(1)).findById(1);
verify(backingRepo, times(0)).findById(1);
}
@Test // DATACMNS-836
public void callsRxJava1MethodOnBaseImplementationWithExactArguments() {
Serializable id = 1L;
RxJava1ConvertingRepository repository = factory.getRepository(RxJava1ConvertingRepository.class);
repository.existsById(id);
repository.existsById((Long) id);
verify(backingRepo, times(2)).existsById(id);
}
@Test // DATACMNS-836
@SuppressWarnings("unchecked")
public void callsRxJava1MethodOnBaseImplementationWithTypeConversion() {
Single<Long> ids = Single.just(1L);
RxJava1ConvertingRepository repository = factory.getRepository(RxJava1ConvertingRepository.class);
repository.existsById(ids);
verify(backingRepo, times(1)).existsById(any(Mono.class));
}
@Test // DATACMNS-988
public void callsRxJava2MethodOnBaseImplementationWithExactArguments() {
Long id = 1L;
RxJava2ConvertingRepository repository = factory.getRepository(RxJava2ConvertingRepository.class);
repository.findById(id);
verify(backingRepo, times(1)).findById(id);
}
@Test // DATACMNS-988
public void callsRxJava2MethodOnBaseImplementationWithTypeConversion() {
Serializable id = 1L;
RxJava2ConvertingRepository repository = factory.getRepository(RxJava2ConvertingRepository.class);
repository.deleteById(id);
verify(backingRepo, times(1)).deleteById(id);
}
interface RxJava1ConvertingRepository extends Repository<Object, Long> {
Single<Boolean> existsById(Single<Long> id);
Single<Boolean> existsById(Serializable id);
Single<Boolean> existsById(Long id);
}
interface RxJava2ConvertingRepository extends Repository<Object, Long> {
Maybe<Boolean> findById(Serializable id);
Single<Boolean> existsById(Long id);
Completable deleteById(Serializable id);
}
interface ObjectRepository
extends Repository<Object, Object>, RepositoryFactorySupportUnitTests.ObjectRepositoryCustom {
}
interface ObjectRepositoryCustom {
Object findById(Object id);
}
}