Refactor GemFireRepositoryFactoryInformationIntegraionTests.

Replace JUnit Hamcrest Assertions with AssertJ.

Simplify test configuration to use SDG Annotations.

Enable GemFire Mock Objects.
This commit is contained in:
John Blum
2021-02-16 12:51:16 -08:00
parent ef84134eb5
commit da819378b5

View File

@@ -14,52 +14,48 @@
* limitations under the License.
*
*/
package org.springframework.data.gemfire.repository;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.RegionAttributes;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.gemfire.CacheFactoryBean;
import org.springframework.data.gemfire.LocalRegionFactoryBean;
import org.springframework.data.gemfire.RegionAttributesFactoryBean;
import org.springframework.data.gemfire.config.annotation.ClientCacheApplication;
import org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
import org.springframework.data.gemfire.repository.sample.Person;
import org.springframework.data.gemfire.repository.sample.PersonRepository;
import org.springframework.data.gemfire.repository.support.GemfireRepositoryFactoryBean;
import org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.RepositoryFactoryInformation;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import example.app.model.User;
import example.app.repo.UserRepository;
/**
* Test suite of test cases testing that the GemFire-based {@link org.springframework.data.repository.Repository}
* factories, implementing the {@link RepositoryFactoryInformation} interface, can in fact be looked up in the
* Spring {@link ApplicationContext}.
* Integration Tests testing and asserting that Apache Geode-based {@link Repository} factories,
* implementing the {@link RepositoryFactoryInformation} interface, can in fact be looked up
* in the Spring {@link ApplicationContext}.
*
* @author John Blum
* @see org.springframework.context.ApplicationContext
* @see org.springframework.data.gemfire.config.annotation.ClientCacheApplication
* @see org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions
* @see org.springframework.data.gemfire.repository.config.EnableGemfireRepositories
* @see org.springframework.data.gemfire.repository.support.GemfireRepositoryFactoryBean
* @see org.springframework.data.repository.Repository
* @see org.springframework.data.repository.core.support.RepositoryFactoryInformation
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringRunner
* @since 1.9.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@RunWith(SpringRunner.class)
@ContextConfiguration
@SuppressWarnings("unused")
public class GemFireRepositoryFactoryInformationIntegrationTests {
@@ -68,71 +64,27 @@ public class GemFireRepositoryFactoryInformationIntegrationTests {
private ApplicationContext applicationContext;
@Test
public void canAccessRepositoryFactoryInformationFactoryBeans() {
public void applicationContextContainsUserRepositoryBean() {
assertThat(this.applicationContext.getBeanNamesForType(UserRepository.class)).contains("userRepository");
}
@Test
@SuppressWarnings("rawtypes")
public void canGetGemfireRepositoryBeansByRepositoryFactoryInformationType() {
Map<String, RepositoryFactoryInformation> repositoryFactories =
applicationContext.getBeansOfType(RepositoryFactoryInformation.class);
this.applicationContext.getBeansOfType(RepositoryFactoryInformation.class);
assertThat(repositoryFactories, is(notNullValue(Map.class)));
assertThat(repositoryFactories.size(), is(greaterThan(0)));
assertThat(repositoryFactories.keySet(), hasItem("&personRepository"));
assertThat(repositoryFactories.get("&personRepository"), is(instanceOf(GemfireRepositoryFactoryBean.class)));
assertThat(Arrays.asList(applicationContext.getBeanNamesForType(PersonRepository.class)),
hasItem("personRepository"));
assertThat(repositoryFactories).isNotNull();
assertThat(repositoryFactories).isNotEmpty();
assertThat(repositoryFactories).containsKeys("&userRepository");
assertThat(repositoryFactories.get("&userRepository")).isInstanceOf(GemfireRepositoryFactoryBean.class);
}
@Configuration
@EnableGemfireRepositories(basePackageClasses = { Person.class },
includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
value = org.springframework.data.gemfire.repository.sample.PersonRepository.class))
static class GemFireConfiguration {
@ClientCacheApplication(name = "GemFireRepositoryFactoryInformationIntegrationTests")
@EnableGemFireMockObjects
@EnableEntityDefinedRegions(basePackageClasses = User.class)
@EnableGemfireRepositories(basePackageClasses = UserRepository.class)
static class TestGeodeConfiguration { }
String applicationName() {
return GemFireRepositoryFactoryInformationIntegrationTests.class.getSimpleName();
}
Properties gemfireProperties() {
Properties gemfireProperties = new Properties();
gemfireProperties.setProperty("name", applicationName());
gemfireProperties.setProperty("log-level", "error");
return gemfireProperties;
}
@Bean
CacheFactoryBean gemfireCache() {
CacheFactoryBean gemfireCache = new CacheFactoryBean();
gemfireCache.setClose(true);
gemfireCache.setProperties(gemfireProperties());
return gemfireCache;
}
@Bean(name = "simple")
LocalRegionFactoryBean<Long, Person> simpleRegion(Cache gemfireCache,
RegionAttributes<Long, Person> simpleRegionAttributes) {
LocalRegionFactoryBean<Long, Person> simpleRegion = new LocalRegionFactoryBean<Long, Person>();
simpleRegion.setAttributes(simpleRegionAttributes);
simpleRegion.setCache(gemfireCache);
simpleRegion.setClose(false);
simpleRegion.setPersistent(false);
return simpleRegion;
}
@Bean
@SuppressWarnings("unchecked")
RegionAttributesFactoryBean simpleRegionAttributes() {
RegionAttributesFactoryBean simpleRegionAttributes = new RegionAttributesFactoryBean();
simpleRegionAttributes.setKeyConstraint(Long.class);
simpleRegionAttributes.setValueConstraint(Person.class);
return simpleRegionAttributes;
}
}
}