Polishing.

Reuse CRUD metdata from repository information and add some tests for the persister factory.
This commit is contained in:
Christoph Strobl
2022-05-11 10:36:27 +02:00
committed by Oliver Drotbohm
parent e5540b460a
commit f08c473b43
3 changed files with 114 additions and 9 deletions

View File

@@ -29,7 +29,6 @@ import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.reactivestreams.Publisher;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.core.io.Resource;
@@ -207,7 +206,7 @@ public class ResourceReaderRepositoryPopulator implements RepositoryPopulator, A
RepositoryInformation repositoryInformation = repositories.getRequiredRepositoryInformation(domainType);
Object repository = repositories.getRepositoryFor(domainType).orElseThrow(
() -> new IllegalArgumentException(String.format("No repository found for domain type: %s", domainType)));
() -> new IllegalStateException(String.format("No repository found for domain type: %s", domainType)));
if (repositoryInformation.isReactiveRepository()) {
return repository instanceof ReactiveCrudRepository ? new ReactiveCrudRepositoryPersister(repository)
@@ -244,13 +243,13 @@ public class ResourceReaderRepositoryPopulator implements RepositoryPopulator, A
private final Object repository;
public ReflectivePersister(RepositoryMetadata metadata, Object repository) {
this.methods = new DefaultCrudMethods(metadata);
this.methods = metadata.getCrudMethods();
this.repository = repository;
}
@Override
public void save(Object object) {
doPersist(object);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2022 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.
@@ -43,6 +43,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
* Unit tests for {@link ResourceReaderRepositoryPopulator} using reactive repositories.
*
* @author Mark Paluch
* @author Christoph Strobl
*/
@SpringJUnitConfig(classes = ReactiveResourceReaderRepositoryPopulatorUnitTests.ReactiveSampleConfiguration.class)
class ReactiveResourceReaderRepositoryPopulatorUnitTests {
@@ -64,7 +65,7 @@ class ReactiveResourceReaderRepositoryPopulatorUnitTests {
this.resource = mock(Resource.class);
}
@Test
@Test // GH-2558
void storesSingleUsingReactiveRepositoryObjectCorrectly() throws Exception {
ReactivePerson reference = new ReactivePerson();
@@ -74,7 +75,7 @@ class ReactiveResourceReaderRepositoryPopulatorUnitTests {
verify(personRepository).save(reference);
}
@Test
@Test // GH-2558
void storesSingleUsingSimpleReactiveRepositoryObjectCorrectly() throws Exception {
ReactiveContact reference = new ReactiveContact();
@@ -84,7 +85,7 @@ class ReactiveResourceReaderRepositoryPopulatorUnitTests {
verify(contactRepository).save(reference);
}
@Test
@Test // GH-2558
void storesSingleUsingRxJavaRepositoryObjectCorrectly() throws Exception {
ReactiveUser reference = new ReactiveUser();
@@ -105,7 +106,7 @@ class ReactiveResourceReaderRepositoryPopulatorUnitTests {
verify(publisher, times(1)).publishEvent(event);
}
private RepositoryPopulator setUpReferenceAndInitialize(Object reference, ApplicationEventPublisher publish)
private RepositoryPopulator setUpReferenceAndInitialize(Object reference, ApplicationEventPublisher publisher)
throws Exception {
when(reader.readFrom(any(), any())).thenReturn(reference);

View File

@@ -0,0 +1,105 @@
/*
* Copyright 2022 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
*
* https://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.init;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.init.ResourceReaderRepositoryPopulator.RepositoryPersisterFactory;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.data.repository.support.Repositories;
/**
* @author Christoph Strobl
*/
@ExtendWith(MockitoExtension.class)
class RepositoryPersisterFactoryUnitTests {
@Mock Repositories repositories;
@Mock RepositoryInformation repoInfo;
RepositoryPersisterFactory factory;
@BeforeEach
void beforeEach() {
factory = new RepositoryPersisterFactory(repositories);
}
@Test // GH-2558
void errorsOnNoRepoFoundForType() {
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> factory.getPersisterFor(Object.class))
.withMessageContaining("No repository found");
}
@Test // GH-2558
void usesCrudRepoPersisterForNonReactiveCrudRepo() {
CrudRepository<?, ?> crudRepository = mock(CrudRepository.class);
when(repositories.getRequiredRepositoryInformation(any())).thenReturn(repoInfo);
when(repoInfo.isReactiveRepository()).thenReturn(false);
when(repositories.getRepositoryFor(Mockito.any())).thenReturn(Optional.of(crudRepository));
assertThat(factory.getPersisterFor(Object.class))
.satisfies(it -> it.getClass().getName().contains("CrudRepositoryPersister"));
}
@Test // GH-2558
void usesReactiveCrudRepoPersisterForReactiveCrudRepo() {
ReactiveCrudRepository<?, ?> crudRepository = mock(ReactiveCrudRepository.class);
when(repositories.getRequiredRepositoryInformation(any())).thenReturn(repoInfo);
when(repoInfo.isReactiveRepository()).thenReturn(true);
when(repositories.getRepositoryFor(Mockito.any())).thenReturn(Optional.of(crudRepository));
assertThat(factory.getPersisterFor(Object.class))
.satisfies(it -> it.getClass().getName().contains("ReactiveCrudRepositoryPersister"));
}
@Test // GH-2558
void usesReflectiveRepoPersisterForNonReactiveNonCrudRepo() {
Repository<?, ?> repository = mock(Repository.class);
when(repositories.getRequiredRepositoryInformation(any())).thenReturn(repoInfo);
when(repoInfo.isReactiveRepository()).thenReturn(false);
when(repositories.getRepositoryFor(Mockito.any())).thenReturn(Optional.of(repository));
assertThat(factory.getPersisterFor(Object.class))
.satisfies(it -> it.getClass().getName().contains("ReflectivePersister"));
}
@Test // GH-2558
void usesReactiveReflectiveRepoPersisterForReactiveNonCrudRepo() {
Repository<?, ?> repository = mock(Repository.class);
when(repositories.getRequiredRepositoryInformation(any())).thenReturn(repoInfo);
when(repoInfo.isReactiveRepository()).thenReturn(true);
when(repositories.getRepositoryFor(Mockito.any())).thenReturn(Optional.of(repository));
assertThat(factory.getPersisterFor(Object.class))
.satisfies(it -> it.getClass().getName().contains("ReflectiveReactivePersister"));
}
}