|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.repository.init; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 26 | +import org.mockito.Mock; |
| 27 | +import org.mockito.Mockito; |
| 28 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 29 | +import org.springframework.data.repository.CrudRepository; |
| 30 | +import org.springframework.data.repository.Repository; |
| 31 | +import org.springframework.data.repository.core.RepositoryInformation; |
| 32 | +import org.springframework.data.repository.init.ResourceReaderRepositoryPopulator.RepositoryPersisterFactory; |
| 33 | +import org.springframework.data.repository.reactive.ReactiveCrudRepository; |
| 34 | +import org.springframework.data.repository.support.Repositories; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Christoph Strobl |
| 38 | + */ |
| 39 | +@ExtendWith(MockitoExtension.class) |
| 40 | +class RepositoryPersisterFactoryUnitTests { |
| 41 | + |
| 42 | + @Mock Repositories repositories; |
| 43 | + @Mock RepositoryInformation repoInfo; |
| 44 | + RepositoryPersisterFactory factory; |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + void beforeEach() { |
| 48 | + factory = new RepositoryPersisterFactory(repositories); |
| 49 | + } |
| 50 | + |
| 51 | + @Test // GH-2558 |
| 52 | + void errorsOnNoRepoFoundForType() { |
| 53 | + |
| 54 | + assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> factory.getPersisterFor(Object.class)) |
| 55 | + .withMessageContaining("No repository found"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test // GH-2558 |
| 59 | + void usesCrudRepoPersisterForNonReactiveCrudRepo() { |
| 60 | + |
| 61 | + CrudRepository<?, ?> crudRepository = mock(CrudRepository.class); |
| 62 | + when(repositories.getRequiredRepositoryInformation(any())).thenReturn(repoInfo); |
| 63 | + when(repoInfo.isReactiveRepository()).thenReturn(false); |
| 64 | + when(repositories.getRepositoryFor(Mockito.any())).thenReturn(Optional.of(crudRepository)); |
| 65 | + |
| 66 | + assertThat(factory.getPersisterFor(Object.class)) |
| 67 | + .satisfies(it -> it.getClass().getName().contains("CrudRepositoryPersister")); |
| 68 | + } |
| 69 | + |
| 70 | + @Test // GH-2558 |
| 71 | + void usesReactiveCrudRepoPersisterForReactiveCrudRepo() { |
| 72 | + |
| 73 | + ReactiveCrudRepository<?, ?> crudRepository = mock(ReactiveCrudRepository.class); |
| 74 | + when(repositories.getRequiredRepositoryInformation(any())).thenReturn(repoInfo); |
| 75 | + when(repoInfo.isReactiveRepository()).thenReturn(true); |
| 76 | + when(repositories.getRepositoryFor(Mockito.any())).thenReturn(Optional.of(crudRepository)); |
| 77 | + |
| 78 | + assertThat(factory.getPersisterFor(Object.class)) |
| 79 | + .satisfies(it -> it.getClass().getName().contains("ReactiveCrudRepositoryPersister")); |
| 80 | + } |
| 81 | + |
| 82 | + @Test // GH-2558 |
| 83 | + void usesReflectiveRepoPersisterForNonReactiveNonCrudRepo() { |
| 84 | + |
| 85 | + Repository<?, ?> repository = mock(Repository.class); |
| 86 | + when(repositories.getRequiredRepositoryInformation(any())).thenReturn(repoInfo); |
| 87 | + when(repoInfo.isReactiveRepository()).thenReturn(false); |
| 88 | + when(repositories.getRepositoryFor(Mockito.any())).thenReturn(Optional.of(repository)); |
| 89 | + |
| 90 | + assertThat(factory.getPersisterFor(Object.class)) |
| 91 | + .satisfies(it -> it.getClass().getName().contains("ReflectivePersister")); |
| 92 | + } |
| 93 | + |
| 94 | + @Test // GH-2558 |
| 95 | + void usesReactiveReflectiveRepoPersisterForReactiveNonCrudRepo() { |
| 96 | + |
| 97 | + Repository<?, ?> repository = mock(Repository.class); |
| 98 | + when(repositories.getRequiredRepositoryInformation(any())).thenReturn(repoInfo); |
| 99 | + when(repoInfo.isReactiveRepository()).thenReturn(true); |
| 100 | + when(repositories.getRepositoryFor(Mockito.any())).thenReturn(Optional.of(repository)); |
| 101 | + |
| 102 | + assertThat(factory.getPersisterFor(Object.class)) |
| 103 | + .satisfies(it -> it.getClass().getName().contains("ReflectiveReactivePersister")); |
| 104 | + } |
| 105 | +} |
0 commit comments