Add additional integration tests testing the auto-configuration of GemfireTemplate beans for Regions with existing named and Region-based GemfireTemplates.

Resolves gh-31.
This commit is contained in:
John Blum
2019-04-12 17:08:34 -07:00
parent b9922f8c1b
commit cf4df83333
3 changed files with 217 additions and 0 deletions

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.geode.boot.autoconfigure;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
@@ -37,6 +38,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.util.CollectionUtils;
import org.springframework.util.StringUtils;
/**
@@ -90,6 +92,8 @@ public class RegionTemplateAutoConfiguration {
Optional.ofNullable(applicationContext)
.filter(it -> bean instanceof Region)
.filter(it -> !it.containsBean(regionTemplateName))
.filter(it -> isGemfireTemplateWithRegionNotPresent(it, (Region) bean))
.map(ConfigurableApplicationContext::getBeanFactory)
.filter(SingletonBeanRegistry.class::isInstance)
.map(SingletonBeanRegistry.class::cast)
@@ -116,8 +120,20 @@ public class RegionTemplateAutoConfiguration {
.ifPresent(rootRegions -> rootRegions.stream()
.filter(Objects::nonNull)
.filter(region -> !regionTemplateNames.contains(toRegionTemplateName(region.getName())))
.filter(region -> !applicationContext.containsBean(toRegionTemplateName(region.getName())))
.filter(region -> isGemfireTemplateWithRegionNotPresent(applicationContext, region))
.forEach(region -> beanFactory.registerSingleton(toRegionTemplateName(region.getName()),
new GemfireTemplate(region))));
}
}
private boolean isGemfireTemplateWithRegionNotPresent(ApplicationContext applicationContext, Region region) {
Map<String, GemfireTemplate> gemfireTemplateBeans =
applicationContext.getBeansOfType(GemfireTemplate.class, false, false);
return CollectionUtils.nullSafeMap(gemfireTemplateBeans).values().stream()
.map(GemfireTemplate::getRegion)
.noneMatch(templateRegion -> templateRegion.equals(region));
}
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright 2019 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.geode.boot.autoconfigure.template;
import static org.assertj.core.api.Assertions.assertThat;
import javax.annotation.Resource;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects;
import org.springframework.geode.boot.autoconfigure.ContinuousQueryAutoConfiguration;
import org.springframework.geode.boot.autoconfigure.RegionTemplateAutoConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration tests for {@link RegionTemplateAutoConfiguration} using explicitly declared {@link Region}
* bean definition and existing, "named" {@link GemfireTemplate} in a Spring {@link ApplicationContext}.
*
* @author John Blum
* @see org.junit.Test
* @see org.apache.geode.cache.Region
* @see org.springframework.boot.autoconfigure.SpringBootApplication
* @see org.springframework.boot.test.context.SpringBootTest
* @see org.springframework.data.gemfire.GemfireTemplate
* @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects
* @see org.springframework.geode.boot.autoconfigure.RegionTemplateAutoConfiguration
* @see org.springframework.test.context.junit4.SpringRunner
* @since 1.0.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@SuppressWarnings("unused")
public class ExistingRegionTemplateByNameAutoConfigurationIntegrationTests {
@Autowired
private GemfireTemplate exampleTemplate;
@Resource(name = "Example")
private Region<Object, Object> example;
@Test
public void exampleRegionTemplateIsPresent() {
assertThat(this.exampleTemplate).isNotNull();
assertThat(this.exampleTemplate.getRegion()).isEqualTo(this.example);
}
@EnableGemFireMockObjects
@SpringBootApplication(exclude = ContinuousQueryAutoConfiguration.class)
static class TestConfiguration {
@Bean("Example")
public ClientRegionFactoryBean<Object, Object> exampleRegion(GemFireCache gemfireCache) {
ClientRegionFactoryBean<Object, Object> exampleRegion = new ClientRegionFactoryBean<>();
exampleRegion.setCache(gemfireCache);
exampleRegion.setClose(false);
exampleRegion.setShortcut(ClientRegionShortcut.LOCAL);
return exampleRegion;
}
@Bean
@DependsOn("Example")
public GemfireTemplate exampleTemplate(GemFireCache gemfireCache) {
return new GemfireTemplate(gemfireCache.getRegion("/Example"));
}
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright 2019 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.geode.boot.autoconfigure.template;
import static org.assertj.core.api.Assertions.assertThat;
import javax.annotation.Resource;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects;
import org.springframework.geode.boot.autoconfigure.ContinuousQueryAutoConfiguration;
import org.springframework.geode.boot.autoconfigure.RegionTemplateAutoConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration tests for {@link RegionTemplateAutoConfiguration} using explicitly declared {@link Region}
* bean definition and existing {@link GemfireTemplate} {@link Region} in a Spring {@link ApplicationContext}.
*
* @author John Blum
* @see org.junit.Test
* @see org.apache.geode.cache.Region
* @see org.springframework.boot.autoconfigure.SpringBootApplication
* @see org.springframework.boot.test.context.SpringBootTest
* @see org.springframework.data.gemfire.GemfireTemplate
* @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects
* @see org.springframework.geode.boot.autoconfigure.RegionTemplateAutoConfiguration
* @see org.springframework.test.context.junit4.SpringRunner
* @since 1.0.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@SuppressWarnings("unused")
public class ExistingRegionTemplateByRegionAutoConfigurationIntegrationTests {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private GemfireTemplate testTemplate;
@Resource(name = "Example")
private Region<Object, Object> example;
@Test
public void exampleRegionTemplateIsNotPresent() {
assertThat(this.applicationContext.containsBean("exampleTemplate")).isFalse();
}
@Test
public void testRegionTemplateIsPresent() {
assertThat(this.testTemplate).isNotNull();
assertThat(this.testTemplate.getRegion()).isEqualTo(this.example);
}
@EnableGemFireMockObjects
@SpringBootApplication(exclude = ContinuousQueryAutoConfiguration.class)
static class TestConfiguration {
@Bean("Example")
public ClientRegionFactoryBean<Object, Object> exampleRegion(GemFireCache gemfireCache) {
ClientRegionFactoryBean<Object, Object> clientRegion = new ClientRegionFactoryBean<>();
clientRegion.setCache(gemfireCache);
clientRegion.setClose(false);
clientRegion.setShortcut(ClientRegionShortcut.LOCAL);
return clientRegion;
}
@Bean("TestTemplate")
@DependsOn("Example")
GemfireTemplate testTemplate(GemFireCache gemfireCache) {
return new GemfireTemplate(gemfireCache.getRegion("/Example"));
}
}
}