From b9922f8c1bc7d3111e3c7219d8688a3be0b783d7 Mon Sep 17 00:00:00 2001 From: John Blum Date: Fri, 12 Apr 2019 16:23:35 -0700 Subject: [PATCH] Add auto-configuration support for creating Templates per Cache Region. Resolves gh-31. --- .../RegionTemplateAutoConfiguration.java | 123 ++++++++++++++++ .../main/resources/META-INF/spring.factories | 1 + .../library/service/LibraryService.java | 47 ++++++ ...lateAutoConfigurationIntegrationTests.java | 136 ++++++++++++++++++ ...lateAutoConfigurationIntegrationTests.java | 89 ++++++++++++ ...lateAutoConfigurationIntegrationTests.java | 95 ++++++++++++ ...lateAutoConfigurationIntegrationTests.java | 76 ++++++++++ ...lateAutoConfigurationIntegrationTests.java | 91 ++++++++++++ .../src/test/resources/template-cache.xml | 9 ++ 9 files changed, 667 insertions(+) create mode 100644 spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/RegionTemplateAutoConfiguration.java create mode 100644 spring-geode-autoconfigure/src/test/java/example/library/service/LibraryService.java create mode 100644 spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/CachingDefinedRegionTemplateAutoConfigurationIntegrationTests.java create mode 100644 spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/DeclaredRegionTemplateAutoConfigurationIntegrationTests.java create mode 100644 spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/EntityDefinedRegionTemplateAutoConfigurationIntegrationTests.java create mode 100644 spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/NativeDefinedRegionTemplateAutoConfigurationIntegrationTests.java create mode 100644 spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/ServerDefinedRegionTemplateAutoConfigurationIntegrationTests.java create mode 100644 spring-geode-autoconfigure/src/test/resources/template-cache.xml diff --git a/spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/RegionTemplateAutoConfiguration.java b/spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/RegionTemplateAutoConfiguration.java new file mode 100644 index 00000000..f316f702 --- /dev/null +++ b/spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/RegionTemplateAutoConfiguration.java @@ -0,0 +1,123 @@ +/* + * 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; + +import java.util.Objects; +import java.util.Optional; +import java.util.Set; + +import org.apache.geode.cache.GemFireCache; +import org.apache.geode.cache.Region; +import org.apache.geode.internal.concurrent.ConcurrentHashSet; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.config.SingletonBeanRegistry; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +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.util.StringUtils; + +/** + * Spring Boot {@link EnableAutoConfiguration auto-configuration} class used to configure a {@link GemfireTemplate} + * for each Apache Geode / Pivotal GemFire {@link Region} declared/defined in + * the Spring {@link ConfigurableApplicationContext} in order to perform {@link Region} data access operations. + * + * @author John Blum + * @see org.apache.geode.cache.Region + * @see org.springframework.beans.factory.config.BeanPostProcessor + * @see org.springframework.beans.factory.config.SingletonBeanRegistry + * @see org.springframework.context.ConfigurableApplicationContext + * @see org.springframework.context.annotation.Bean + * @see org.springframework.context.annotation.Configuration + * @see org.springframework.data.gemfire.GemfireTemplate + * @since 1.0.0 + */ +@Configuration +@AutoConfigureAfter(ClientCacheAutoConfiguration.class) +@ConditionalOnBean(GemFireCache.class) +@ConditionalOnClass(GemfireTemplate.class) +@SuppressWarnings("unused") +public class RegionTemplateAutoConfiguration { + + private static final Set regionTemplateNames = new ConcurrentHashSet<>(); + + private String toRegionTemplateName(String regionName) { + return StringUtils.uncapitalize(regionName) + "Template"; + } + + @Bean + BeanPostProcessor regionTemplateBeanPostProcessor(ConfigurableApplicationContext applicationContext) { + + return new BeanPostProcessor() { + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + + if (bean instanceof Region) { + + String regionTemplateName = toRegionTemplateName(beanName); + + registerRegionTemplateBean(regionTemplateName, bean); + } + + return bean; + } + + @SuppressWarnings("all") + private void registerRegionTemplateBean(String regionTemplateName, Object bean) { + + Optional.ofNullable(applicationContext) + .filter(it -> bean instanceof Region) + .map(ConfigurableApplicationContext::getBeanFactory) + .filter(SingletonBeanRegistry.class::isInstance) + .map(SingletonBeanRegistry.class::cast) + .ifPresent(beanFactory -> { + beanFactory.registerSingleton(regionTemplateName, new GemfireTemplate((Region) bean)); + regionTemplateNames.add(regionTemplateName); + }); + } + }; + } + + @EventListener({ ContextRefreshedEvent.class }) + public void registerRemainingRegionTemplatesOnContextRefresh(ContextRefreshedEvent event) { + + ApplicationContext applicationContext = event.getApplicationContext(); + + if (applicationContext instanceof ConfigurableApplicationContext) { + + ConfigurableListableBeanFactory beanFactory = + ((ConfigurableApplicationContext) applicationContext).getBeanFactory(); + + Optional.ofNullable(applicationContext.getBean(GemFireCache.class)) + .map(GemFireCache::rootRegions) + .ifPresent(rootRegions -> rootRegions.stream() + .filter(Objects::nonNull) + .filter(region -> !regionTemplateNames.contains(toRegionTemplateName(region.getName()))) + .forEach(region -> beanFactory.registerSingleton(toRegionTemplateName(region.getName()), + new GemfireTemplate(region)))); + } + } +} diff --git a/spring-geode-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-geode-autoconfigure/src/main/resources/META-INF/spring.factories index e98b034f..ebfc8f1b 100644 --- a/spring-geode-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-geode-autoconfigure/src/main/resources/META-INF/spring.factories @@ -9,6 +9,7 @@ org.springframework.geode.boot.autoconfigure.FunctionExecutionAutoConfiguration, org.springframework.geode.boot.autoconfigure.GemFirePropertiesAutoConfiguration,\ org.springframework.geode.boot.autoconfigure.PdxSerializationAutoConfiguration,\ org.springframework.geode.boot.autoconfigure.PeerSecurityAutoConfiguration,\ +org.springframework.geode.boot.autoconfigure.RegionTemplateAutoConfiguration,\ org.springframework.geode.boot.autoconfigure.RepositoriesAutoConfiguration,\ org.springframework.geode.boot.autoconfigure.SpringSessionAutoConfiguration,\ org.springframework.geode.boot.autoconfigure.SpringSessionPropertiesAutoConfiguration,\ diff --git a/spring-geode-autoconfigure/src/test/java/example/library/service/LibraryService.java b/spring-geode-autoconfigure/src/test/java/example/library/service/LibraryService.java new file mode 100644 index 00000000..b8c231f8 --- /dev/null +++ b/spring-geode-autoconfigure/src/test/java/example/library/service/LibraryService.java @@ -0,0 +1,47 @@ +/* + * 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 example.library.service; + +import java.util.Collections; +import java.util.List; + +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +import example.app.model.Author; +import example.app.model.Book; + +/** + * The LibraryService class... + * + * @author John Blum + * @since 1.0.0 + */ +@Service +@SuppressWarnings("unused") +public class LibraryService { + + @Cacheable("BooksByAuthor") + public List findBooksByAuthor(Author author) { + return Collections.emptyList(); + } + + @Cacheable("BooksByYear") + public List findBooksByYear(int year) { + return Collections.emptyList(); + } +} diff --git a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/CachingDefinedRegionTemplateAutoConfigurationIntegrationTests.java b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/CachingDefinedRegionTemplateAutoConfigurationIntegrationTests.java new file mode 100644 index 00000000..a332f252 --- /dev/null +++ b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/CachingDefinedRegionTemplateAutoConfigurationIntegrationTests.java @@ -0,0 +1,136 @@ +/* + * 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 java.util.List; +import java.util.stream.Collectors; + +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.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.data.gemfire.GemfireTemplate; +import org.springframework.data.gemfire.config.annotation.EnableCachingDefinedRegions; +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; + +import example.app.NonBeanType; +import example.app.model.Author; +import example.app.model.Book; +import example.library.service.LibraryService; + +/** + * Integration tests for {@link RegionTemplateAutoConfiguration} using SDG's {@link EnableCachingDefinedRegions} + * annotation to define {@link Region Regions} and associated Templates. + * + * @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.config.annotation.EnableCachingDefinedRegions + * @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 CachingDefinedRegionTemplateAutoConfigurationIntegrationTests { + + @Autowired + private GemFireCache gemfireCache; + + @Autowired + @Qualifier("booksByAuthorTemplate") + private GemfireTemplate booksByAuthorTemplate; + + @Autowired + @Qualifier("cachedBooksTemplate") + private GemfireTemplate booksByTitleTemplate; + + @Autowired + @Qualifier("booksByYearTemplate") + private GemfireTemplate booksByYearTemplate; + + @Resource(name = "BooksByAuthor") + private Region> booksByAuthor; + + @Resource(name = "CachedBooks") + private Region booksByTitle; + + @Resource(name = "BooksByYear") + private Region> booksByYear; + + @Before + public void setup() { + + assertThat(this.gemfireCache).isNotNull(); + + assertThat(this.gemfireCache.rootRegions().stream() + .map(Region::getName) + .sorted() + .collect(Collectors.toList())).containsExactly("BooksByAuthor", "BooksByYear", "CachedBooks"); + } + + @Test + public void authorsBooksRegionTemplateIsPresent() { + + assertThat(this.booksByAuthorTemplate).isNotNull(); + assertThat(this.booksByAuthorTemplate.getRegion()).isEqualTo(this.booksByAuthor); + } + + @Test + public void booksByTitleRegionTemplateIsPresent() { + + assertThat(this.booksByTitleTemplate).isNotNull(); + assertThat(this.booksByTitleTemplate.getRegion()).isEqualTo(this.booksByTitle); + } + + @Test + public void booksByYearRegionTemplateIsPresent() { + + assertThat(this.booksByYearTemplate).isNotNull(); + assertThat(this.booksByYearTemplate.getRegion()).isEqualTo(this.booksByYear); + } + + @EnableGemFireMockObjects + @EnableCachingDefinedRegions(clientRegionShortcut = ClientRegionShortcut.LOCAL) + @SpringBootApplication(exclude = ContinuousQueryAutoConfiguration.class, + scanBasePackageClasses = { NonBeanType.class, LibraryService.class }) + static class TestApplicationConfiguration { + + @Bean + LibraryService libraryService() { + return new LibraryService(); + } + } +} diff --git a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/DeclaredRegionTemplateAutoConfigurationIntegrationTests.java b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/DeclaredRegionTemplateAutoConfigurationIntegrationTests.java new file mode 100644 index 00000000..ca1fd6a7 --- /dev/null +++ b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/DeclaredRegionTemplateAutoConfigurationIntegrationTests.java @@ -0,0 +1,89 @@ +/* + * 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.beans.factory.annotation.Qualifier; +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.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 definitions 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 DeclaredRegionTemplateAutoConfigurationIntegrationTests { + + @Autowired + @Qualifier("exampleTemplate") + private GemfireTemplate exampleTemplate; + + @Resource(name = "Example") + private Region exampleRegion; + + @Test + public void exampleRegionTemplateIsPresent() { + + assertThat(this.exampleTemplate).isNotNull(); + assertThat(this.exampleTemplate.getRegion()).isEqualTo(this.exampleRegion); + } + + @EnableGemFireMockObjects + @SpringBootApplication(exclude = ContinuousQueryAutoConfiguration.class) + static class TestGemFireConfiguration { + + @Bean("Example") + ClientRegionFactoryBean exampleRegion(GemFireCache gemfireCache) { + + ClientRegionFactoryBean exampleRegion = new ClientRegionFactoryBean<>(); + + exampleRegion.setCache(gemfireCache); + exampleRegion.setShortcut(ClientRegionShortcut.LOCAL); + + return exampleRegion; + } + } +} diff --git a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/EntityDefinedRegionTemplateAutoConfigurationIntegrationTests.java b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/EntityDefinedRegionTemplateAutoConfigurationIntegrationTests.java new file mode 100644 index 00000000..5be2285e --- /dev/null +++ b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/EntityDefinedRegionTemplateAutoConfigurationIntegrationTests.java @@ -0,0 +1,95 @@ +/* + * 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.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.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.gemfire.GemfireTemplate; +import org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions; +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; + +import example.app.model.Author; +import example.app.model.Book; +import example.app.model.ISBN; + +/** + * Integration tests for {@link RegionTemplateAutoConfiguration} using SDG's {@link EnableEntityDefinedRegions} + * annotation to define {@link Region Regions} and associated Templates. + * + * @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.config.annotation.EnableEntityDefinedRegions + * @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 EntityDefinedRegionTemplateAutoConfigurationIntegrationTests { + + @Autowired + @Qualifier("authorsTemplate") + private GemfireTemplate authorsTemplate; + + @Autowired + @Qualifier("booksTemplate") + private GemfireTemplate booksTemplate; + + @Resource(name = "Authors") + private Region authors; + + @Resource(name = "Books") + private Region books; + + @Test + public void authorsRegionTemplateIsPresent() { + + assertThat(this.authorsTemplate).isNotNull(); + assertThat(this.authorsTemplate.getRegion()).isEqualTo(this.authors); + } + + @Test + public void booksRegionTemplateIsPresent() { + + assertThat(this.booksTemplate).isNotNull(); + assertThat(this.booksTemplate.getRegion()).isEqualTo(this.books); + } + + @EnableGemFireMockObjects + @SpringBootApplication(exclude = ContinuousQueryAutoConfiguration.class) + @EnableEntityDefinedRegions(basePackageClasses = Book.class, clientRegionShortcut = ClientRegionShortcut.LOCAL) + static class TestApplicationConfiguration { } + +} diff --git a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/NativeDefinedRegionTemplateAutoConfigurationIntegrationTests.java b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/NativeDefinedRegionTemplateAutoConfigurationIntegrationTests.java new file mode 100644 index 00000000..e1fdc472 --- /dev/null +++ b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/NativeDefinedRegionTemplateAutoConfigurationIntegrationTests.java @@ -0,0 +1,76 @@ +/* + * 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 org.apache.geode.cache.Region; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +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.data.gemfire.GemfireTemplate; +import org.springframework.data.gemfire.config.annotation.EnableGemFireProperties; +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 natively declared {@link Region} + * definitions in GemFire/Geode {@literal cache.xml}. + * + * @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.config.annotation.EnableGemFireProperties + * @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 NativeDefinedRegionTemplateAutoConfigurationIntegrationTests { + + @Autowired + private ApplicationContext applicationContext; + + @Autowired + private GemfireTemplate exampleTemplate; + + @Test(expected = NoSuchBeanDefinitionException.class) + public void exampleRegionBeanIsNotPresent() { + this.applicationContext.getBean("Example", Region.class); + } + + @Test + public void exampleRegionTemplateIsPresent() { + + assertThat(this.exampleTemplate).isNotNull(); + assertThat(this.exampleTemplate.getRegion()).isNotNull(); + assertThat(this.exampleTemplate.getRegion().getName()).isEqualTo("Example"); + } + + @EnableGemFireProperties(cacheXmlFile = "template-cache.xml") + @SpringBootApplication(exclude = ContinuousQueryAutoConfiguration.class) + static class TestConfiguration { } + +} diff --git a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/ServerDefinedRegionTemplateAutoConfigurationIntegrationTests.java b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/ServerDefinedRegionTemplateAutoConfigurationIntegrationTests.java new file mode 100644 index 00000000..00b15502 --- /dev/null +++ b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/ServerDefinedRegionTemplateAutoConfigurationIntegrationTests.java @@ -0,0 +1,91 @@ +/* + * 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 java.io.IOException; + +import org.apache.geode.cache.GemFireCache; +import org.apache.geode.cache.Region; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.runner.RunWith; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.data.gemfire.LocalRegionFactoryBean; +import org.springframework.data.gemfire.config.annotation.CacheServerApplication; +import org.springframework.data.gemfire.tests.integration.ForkingClientServerIntegrationTestsSupport; +import org.springframework.geode.boot.autoconfigure.RegionTemplateAutoConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Integration tests for {@link RegionTemplateAutoConfiguration} using SDG's {@link EnableServerDefinedRegions} + * annotation to define {@link Region Regions} and associated Templates. + * + * @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.config.annotation.EnableServerDefinedRegions + * @see org.springframework.data.gemfire.tests.integration.ForkingClientServerIntegrationTestsSupport + * @see org.springframework.geode.boot.autoconfigure.RegionTemplateAutoConfiguration + * @see org.springframework.test.context.junit4.SpringRunner + * @since 1.1.0 + */ +@Ignore +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + classes = ServerDefinedRegionTemplateAutoConfigurationIntegrationTests.GemFireClientConfiguration.class) +// TODO enable and complete test class when SBDG is rebased on SD Lovelace +public class ServerDefinedRegionTemplateAutoConfigurationIntegrationTests + extends ForkingClientServerIntegrationTestsSupport { + + @BeforeClass + public static void startGemFireServer() throws IOException { + startGemFireServer(GemFireServerConfiguration.class); + } + + @SpringBootApplication + //@EnableServerDefinedRegions + static class GemFireClientConfiguration { } + + @CacheServerApplication + static class GemFireServerConfiguration { + + public static void main(String[] args) { + + AnnotationConfigApplicationContext applicationContext = + new AnnotationConfigApplicationContext(GemFireServerConfiguration.class); + + applicationContext.registerShutdownHook(); + } + + @Bean("ExampleServerRegion") + LocalRegionFactoryBean exampleServerRegion(GemFireCache gemfireCache) { + + LocalRegionFactoryBean exampleServerRegion = new LocalRegionFactoryBean<>(); + + exampleServerRegion.setCache(gemfireCache); + exampleServerRegion.setClose(false); + exampleServerRegion.setPersistent(false); + + return exampleServerRegion; + } + } +} diff --git a/spring-geode-autoconfigure/src/test/resources/template-cache.xml b/spring-geode-autoconfigure/src/test/resources/template-cache.xml new file mode 100644 index 00000000..638ce4fc --- /dev/null +++ b/spring-geode-autoconfigure/src/test/resources/template-cache.xml @@ -0,0 +1,9 @@ + + + + + +