From c9bc360ef4151a4ab98d492237f5180085c89886 Mon Sep 17 00:00:00 2001 From: John Blum Date: Wed, 19 Jan 2022 16:24:26 -0800 Subject: [PATCH] Add Integration Test for collocated PARTITION Regions configured with SDG Java config (PartitionedRegionFactoryBean). Resolves gh-566. --- ...ionsJavaConfigurationIntegrationTests.java | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 spring-data-geode/src/test/java/org/springframework/data/gemfire/CollocatedRegionsJavaConfigurationIntegrationTests.java diff --git a/spring-data-geode/src/test/java/org/springframework/data/gemfire/CollocatedRegionsJavaConfigurationIntegrationTests.java b/spring-data-geode/src/test/java/org/springframework/data/gemfire/CollocatedRegionsJavaConfigurationIntegrationTests.java new file mode 100644 index 00000000..a119de77 --- /dev/null +++ b/spring-data-geode/src/test/java/org/springframework/data/gemfire/CollocatedRegionsJavaConfigurationIntegrationTests.java @@ -0,0 +1,144 @@ +/* + * Copyright 2021 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.gemfire; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.apache.geode.cache.DataPolicy; +import org.apache.geode.cache.GemFireCache; +import org.apache.geode.cache.PartitionAttributes; +import org.apache.geode.cache.Region; +import org.apache.geode.cache.RegionAttributes; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.DependsOn; +import org.springframework.data.gemfire.config.annotation.PeerCacheApplication; +import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport; +import org.springframework.data.gemfire.tests.unit.annotation.GemFireUnitTest; +import org.springframework.data.gemfire.util.RegionUtils; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Integration Tests for Apache Geode {@literal collocated} {@link DataPolicy#PARTITION} {@link Region Regions} + * using SDG Java configuration. + * + * @author John Blum + * @see org.junit.Test + * @see org.mockito.Mockito + * @see org.apache.geode.cache.GemFireCache + * @see org.apache.geode.cache.Region + * @see org.springframework.data.gemfire.PartitionedRegionFactoryBean + * @see org.springframework.data.gemfire.config.annotation.PeerCacheApplication + * @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport + * @see org.springframework.data.gemfire.tests.unit.annotation.GemFireUnitTest + * @see org.springframework.test.context.ContextConfiguration + * @see org.springframework.test.context.junit4.SpringRunner + * @since 2.7.0 + */ +@RunWith(SpringRunner.class) +@ContextConfiguration +@GemFireUnitTest +@SuppressWarnings("unused") +public class CollocatedRegionsJavaConfigurationIntegrationTests extends IntegrationTestsSupport { + + @Autowired + @Qualifier("A") + private Region a; + + @Autowired + @Qualifier("B") + private Region b; + + private void assertRegion(Region region, String name, int redundantCopies) { + assertRegion(region, name, null, redundantCopies); + } + + private void assertRegion(Region region, String name, String collocatedWith, int redundantCopies) { + + assertThat(region).isNotNull(); + assertThat(region.getName()).isEqualTo(name); + assertThat(region.getFullPath()).isEqualTo(RegionUtils.toRegionPath(name)); + assertThat(region.getAttributes()).isNotNull(); + assertThat(region.getAttributes().getDataPolicy()).isEqualTo(DataPolicy.PARTITION); + assertThat(region.getAttributes().getPartitionAttributes()).isNotNull(); + assertThat(region.getAttributes().getPartitionAttributes().getColocatedWith()).isEqualTo(collocatedWith); + assertThat(region.getAttributes().getPartitionAttributes().getRedundantCopies()).isEqualTo(redundantCopies); + } + + @Test + public void collocatedPartitionRegionConfigurationIsCorrect() { + + assertRegion(this.a, "A", 0); + assertRegion(this.b, "B", "A", 0); + } + + @PeerCacheApplication + static class TestConfiguration { + + @Bean("B") + @DependsOn("A") + PartitionedRegionFactoryBean partTwoRegion(GemFireCache cache, + @Qualifier("partTwoRegionAttributes") RegionAttributes partTwoRegionAttributes) { + + PartitionedRegionFactoryBean partTwoRegion = new PartitionedRegionFactoryBean<>(); + + partTwoRegion.setCache(cache); + partTwoRegion.setPersistent(false); + partTwoRegion.setAttributes(partTwoRegionAttributes); + + return partTwoRegion; + } + + @Bean + RegionAttributesFactoryBean partTwoRegionAttributes( + @Qualifier("partTwoPartitionAttributes") PartitionAttributes partTwoPartitionAttributes) { + + RegionAttributesFactoryBean partTwoRegionAttributes = new RegionAttributesFactoryBean<>(); + + partTwoRegionAttributes.setPartitionAttributes(partTwoPartitionAttributes); + + return partTwoRegionAttributes; + } + + @Bean + PartitionAttributesFactoryBean partTwoPartitionAttributes() { + + PartitionAttributesFactoryBean partTwoPartitionAttributes = + new PartitionAttributesFactoryBean<>(); + + partTwoPartitionAttributes.setColocatedWith("A"); + + return partTwoPartitionAttributes; + } + + @Bean("A") + public PartitionedRegionFactoryBean partOneRegion(GemFireCache gemfireCache) { + + PartitionedRegionFactoryBean partOneRegion = new PartitionedRegionFactoryBean<>(); + + partOneRegion.setCache(gemfireCache); + partOneRegion.setPersistent(false); + + return partOneRegion; + } + } +}