|
| 1 | +/* |
| 2 | + * Copyright 2021 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.gemfire.config.annotation; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.ArgumentMatchers.eq; |
| 20 | +import static org.mockito.Mockito.inOrder; |
| 21 | +import static org.mockito.Mockito.spy; |
| 22 | +import static org.mockito.Mockito.times; |
| 23 | +import static org.mockito.Mockito.verify; |
| 24 | + |
| 25 | +import java.util.Optional; |
| 26 | + |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.jupiter.api.Order; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.mockito.InOrder; |
| 32 | + |
| 33 | +import org.apache.geode.cache.Cache; |
| 34 | +import org.apache.geode.cache.GemFireCache; |
| 35 | +import org.apache.geode.cache.wan.GatewayReceiver; |
| 36 | + |
| 37 | +import org.springframework.beans.factory.annotation.Autowired; |
| 38 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 39 | +import org.springframework.context.annotation.Bean; |
| 40 | +import org.springframework.data.gemfire.GemfireUtils; |
| 41 | +import org.springframework.data.gemfire.LocalRegionFactoryBean; |
| 42 | +import org.springframework.data.gemfire.PartitionedRegionFactoryBean; |
| 43 | +import org.springframework.data.gemfire.ReplicatedRegionFactoryBean; |
| 44 | +import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects; |
| 45 | +import org.springframework.data.gemfire.wan.GatewayReceiverFactoryBean; |
| 46 | +import org.springframework.data.gemfire.wan.support.InitializationSafeGatewayReceiver; |
| 47 | +import org.springframework.test.context.ContextConfiguration; |
| 48 | +import org.springframework.test.context.junit4.SpringRunner; |
| 49 | + |
| 50 | +/** |
| 51 | + * Integration Tests for {@link EnableGemFireInitializationSafety} annotation |
| 52 | + * and the {@link GemFireInitializationSafetyConfiguration} class. |
| 53 | + * |
| 54 | + * @author John Blum |
| 55 | + * @see org.junit.Test |
| 56 | + * @see org.mockito.Mockito |
| 57 | + * @see org.springframework.data.gemfire.config.annotation.EnableGemFireInitializationSafety |
| 58 | + * @see org.springframework.data.gemfire.config.annotation.GemFireInitializationSafetyConfiguration |
| 59 | + * @see org.springframework.test.context.ContextConfiguration |
| 60 | + * @see org.springframework.test.context.junit4.SpringRunner |
| 61 | + * @since 2.7.0 |
| 62 | + */ |
| 63 | +@RunWith(SpringRunner.class) |
| 64 | +@ContextConfiguration |
| 65 | +@SuppressWarnings("unused") |
| 66 | +public class EnableGemFireInitializationSafetyConfigurationWithWanGatewayIntegrationTests { |
| 67 | + |
| 68 | + @Autowired |
| 69 | + private Cache cache; |
| 70 | + |
| 71 | + @Autowired |
| 72 | + private GatewayReceiver gatewayReceiver; |
| 73 | + |
| 74 | + @Autowired |
| 75 | + @Qualifier("&GatewayReceiver") |
| 76 | + private GatewayReceiverFactoryBean gatewayReceiverFactoryBean; |
| 77 | + |
| 78 | + @Before |
| 79 | + public void assertCacheConfiguration() { |
| 80 | + |
| 81 | + assertThat(this.cache).isNotNull(); |
| 82 | + assertThat(this.cache.getName()) |
| 83 | + .isEqualTo(EnableGemFireInitializationSafetyConfigurationWithWanGatewayIntegrationTests.class.getSimpleName()); |
| 84 | + assertThat(this.cache.rootRegions()).hasSize(3); |
| 85 | + assertThat(this.cache.getGatewayReceivers()).hasSize(1); |
| 86 | + assertThat(this.gatewayReceiverFactoryBean).isNotNull(); |
| 87 | + assertThat(this.gatewayReceiverFactoryBean.isManualStart()).isTrue(); |
| 88 | + assertThat(this.gatewayReceiver).isInstanceOf(InitializationSafeGatewayReceiver.class); |
| 89 | + assertThat(this.gatewayReceiver.isManualStart()).isTrue(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void cacheRegionsInitializedBeforeGatewayReceiverStart() throws Exception { |
| 94 | + |
| 95 | + GatewayReceiver unwrappedMockGatewayReceiver = Optional.of(this.gatewayReceiver) |
| 96 | + .filter(InitializationSafeGatewayReceiver.class::isInstance) |
| 97 | + .map(InitializationSafeGatewayReceiver.class::cast) |
| 98 | + .map(InitializationSafeGatewayReceiver::getDelegate) |
| 99 | + .orElse(this.gatewayReceiver); |
| 100 | + |
| 101 | + InOrder order = inOrder(this.cache, this.gatewayReceiverFactoryBean, unwrappedMockGatewayReceiver); |
| 102 | + |
| 103 | + order.verify(this.gatewayReceiverFactoryBean, times(1)).setManualStart(eq(true)); |
| 104 | + order.verify(this.cache, times(2)).rootRegions(); |
| 105 | + verify(this.cache, times(1)).getRegion(GemfireUtils.toRegionPath("LocalRegion")); |
| 106 | + verify(this.cache, times(1)).getRegion(GemfireUtils.toRegionPath("PartitionRegion")); |
| 107 | + verify(this.cache, times(1)).getRegion(GemfireUtils.toRegionPath("ReplicateRegion")); |
| 108 | + order.verify(unwrappedMockGatewayReceiver, times(1)).start(); |
| 109 | + } |
| 110 | + |
| 111 | + @PeerCacheApplication(name = "EnableGemFireInitializationSafetyConfigurationWithWanGatewayIntegrationTests") |
| 112 | + @EnableGemFireInitializationSafety |
| 113 | + @EnableGemFireMockObjects |
| 114 | + static class TestConfiguration { |
| 115 | + |
| 116 | + @Order(2) |
| 117 | + @Bean("LocalRegion") |
| 118 | + LocalRegionFactoryBean<Object, Object> localRegion(GemFireCache gemfireCache) { |
| 119 | + |
| 120 | + LocalRegionFactoryBean<Object, Object> localRegionFactory = new LocalRegionFactoryBean<>(); |
| 121 | + |
| 122 | + localRegionFactory.setCache(gemfireCache); |
| 123 | + localRegionFactory.setPersistent(false); |
| 124 | + |
| 125 | + return localRegionFactory; |
| 126 | + } |
| 127 | + |
| 128 | + @Order(3) |
| 129 | + @Bean("PartitionRegion") |
| 130 | + PartitionedRegionFactoryBean<Object, Object> partitionRegion(GemFireCache gemfireCache) { |
| 131 | + |
| 132 | + PartitionedRegionFactoryBean<Object, Object> partitionRegionFactory = new PartitionedRegionFactoryBean<>(); |
| 133 | + |
| 134 | + partitionRegionFactory.setCache(gemfireCache); |
| 135 | + partitionRegionFactory.setPersistent(false); |
| 136 | + |
| 137 | + return partitionRegionFactory; |
| 138 | + } |
| 139 | + |
| 140 | + @Order(4) |
| 141 | + @Bean("ReplicateRegion") |
| 142 | + ReplicatedRegionFactoryBean<Object, Object> replicateRegion(GemFireCache gemfireCache) { |
| 143 | + |
| 144 | + ReplicatedRegionFactoryBean<Object, Object> replicateRegionFactory = new ReplicatedRegionFactoryBean<>(); |
| 145 | + |
| 146 | + replicateRegionFactory.setCache(gemfireCache); |
| 147 | + replicateRegionFactory.setPersistent(false); |
| 148 | + |
| 149 | + return replicateRegionFactory; |
| 150 | + } |
| 151 | + |
| 152 | + @Order(1) |
| 153 | + @Bean("GatewayReceiver") |
| 154 | + GatewayReceiverFactoryBean gatewayReceiverBean(Cache peerCache) { |
| 155 | + |
| 156 | + GatewayReceiverFactoryBean gatewayReceiverFactory = new GatewayReceiverFactoryBean(peerCache); |
| 157 | + |
| 158 | + gatewayReceiverFactory.setManualStart(false); |
| 159 | + |
| 160 | + return spy(gatewayReceiverFactory); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments