From edb12fc523300f2044244f2d3efb5124cad351e9 Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 12 Sep 2019 16:54:55 -0700 Subject: [PATCH] Add shared Spring @Configuration class for enabling (client/Pool) subscriptions. This class will be used by both the ContinuousQueryAutoConfiguration class and the SpringSessionAutoConfiguration class. --- .../EnableSubscriptionConfiguration.java | 63 ++++++++ ...bleSubscriptionConfigurationUnitTests.java | 148 ++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/support/EnableSubscriptionConfiguration.java create mode 100644 spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/support/EnableSubscriptionConfigurationUnitTests.java diff --git a/spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/support/EnableSubscriptionConfiguration.java b/spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/support/EnableSubscriptionConfiguration.java new file mode 100644 index 00000000..f3c2c3b0 --- /dev/null +++ b/spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/support/EnableSubscriptionConfiguration.java @@ -0,0 +1,63 @@ +/* + * 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.support; + +import java.util.Optional; +import java.util.Set; + +import org.apache.geode.cache.client.Pool; + +import org.apache.shiro.util.CollectionUtils; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.gemfire.config.annotation.ClientCacheConfigurer; +import org.springframework.data.gemfire.config.annotation.PoolConfigurer; + +/** + * A Spring {@link Configuration} class used to enable subscription on the Apache Geode & Pivotal GemFire + * {@literal DEFAULT} {@link Pool} as well as the SDG {@literal gemfirePool} {@link Pool}, only. + * + * @author John Blum + * @see org.apache.geode.cache.client.Pool + * @see org.springframework.context.annotation.Bean + * @see org.springframework.context.annotation.Configuration + * @see org.springframework.data.gemfire.config.annotation.ClientCacheConfigurer + * @see org.springframework.data.gemfire.config.annotation.PoolConfigurer + * @since 1.2.0 + */ +@Configuration +@SuppressWarnings("unused") +public class EnableSubscriptionConfiguration { + + private static final String DEFAULT_POOL_NAME = "DEFAULT"; + private static final String GEMFIRE_POOL_NAME = "gemfirePool"; + + private static final Set POOL_NAMES = CollectionUtils.asSet(DEFAULT_POOL_NAME, GEMFIRE_POOL_NAME); + + @Bean + public ClientCacheConfigurer enableSubscriptionClientCacheConfigurer() { + return (beanName, clientCacheFactoryBean) -> clientCacheFactoryBean.setSubscriptionEnabled(true); + } + + @Bean + public PoolConfigurer enableSubscriptionPoolConfigurer() { + + return (beanName, poolFactoryBean) -> Optional.ofNullable(beanName) + .filter(POOL_NAMES::contains) + .ifPresent(poolName -> poolFactoryBean.setSubscriptionEnabled(true)); + } +} diff --git a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/support/EnableSubscriptionConfigurationUnitTests.java b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/support/EnableSubscriptionConfigurationUnitTests.java new file mode 100644 index 00000000..4d39f580 --- /dev/null +++ b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/support/EnableSubscriptionConfigurationUnitTests.java @@ -0,0 +1,148 @@ +/* + * 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.support; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException; + +import java.util.Optional; + +import org.junit.Test; + +import org.springframework.data.gemfire.client.ClientCacheFactoryBean; +import org.springframework.data.gemfire.client.PoolFactoryBean; +import org.springframework.util.ReflectionUtils; + +/** + * Unit Tests for {@link EnableSubscriptionConfiguration}. + * + * @author John Blum + * @see org.junit.Test + * @see org.springframework.data.gemfire.client.ClientCacheFactoryBean + * @see org.springframework.data.gemfire.client.PoolFactoryBean + * @since 1.2.0 + */ +public class EnableSubscriptionConfigurationUnitTests { + + private EnableSubscriptionConfiguration configuration = new EnableSubscriptionConfiguration(); + + @SuppressWarnings("all") + private T getFieldValue(Object target, String fieldName) { + + return Optional.ofNullable(target) + .map(Object::getClass) + .map(targetType -> ReflectionUtils.findField(targetType, fieldName)) + .map(field -> { + ReflectionUtils.makeAccessible(field); + return field; + }) + .map(field -> (T) ReflectionUtils.getField(field, target)) + .orElseThrow(() -> newIllegalArgumentException("Unable to get value of field [%s] on object of type [%s]", + fieldName, target != null ? target.getClass().getName() : null)); + } + + @Test + public void subscriptionEnabledForClientCachePool() { + + ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean(); + + assertThat(clientCacheFactoryBean).isNotNull(); + assertThat(Boolean.TRUE.equals(clientCacheFactoryBean.getSubscriptionEnabled())).isFalse(); + + this.configuration.enableSubscriptionClientCacheConfigurer() + .configure("testClientCache", clientCacheFactoryBean); + + assertThat(clientCacheFactoryBean.getSubscriptionEnabled()).isTrue(); + } + + @Test + public void subscriptionEnabledForDefaultPool() { + + PoolFactoryBean poolFactoryBean = new PoolFactoryBean(); + + assertThat(poolFactoryBean).isNotNull(); + assertThat(this.getFieldValue(poolFactoryBean, "subscriptionEnabled")).isFalse(); + + this.configuration.enableSubscriptionPoolConfigurer().configure("DEFAULT", poolFactoryBean); + + assertThat(this.getFieldValue(poolFactoryBean, "subscriptionEnabled")).isTrue(); + } + + @Test + public void subscriptionEnabledForGemFirePool() { + + PoolFactoryBean poolFactoryBean = new PoolFactoryBean(); + + assertThat(poolFactoryBean).isNotNull(); + assertThat(this.getFieldValue(poolFactoryBean, "subscriptionEnabled")).isFalse(); + + this.configuration.enableSubscriptionPoolConfigurer().configure("gemfirePool", poolFactoryBean); + + assertThat(this.getFieldValue(poolFactoryBean, "subscriptionEnabled")).isTrue(); + } + + @Test + public void subscriptionEnabledOnGemFirePool() { + + PoolFactoryBean poolFactoryBean = new PoolFactoryBean(); + + assertThat(poolFactoryBean).isNotNull(); + assertThat(this.getFieldValue(poolFactoryBean, "subscriptionEnabled")).isFalse(); + + this.configuration.enableSubscriptionPoolConfigurer().configure("gemfirePool", poolFactoryBean); + + assertThat(this.getFieldValue(poolFactoryBean, "subscriptionEnabled")).isTrue(); + } + + @Test + public void subscriptionNotEnabledForGeodePool() { + + PoolFactoryBean poolFactoryBean = new PoolFactoryBean(); + + assertThat(poolFactoryBean).isNotNull(); + assertThat(this.getFieldValue(poolFactoryBean, "subscriptionEnabled")).isFalse(); + + this.configuration.enableSubscriptionPoolConfigurer().configure("geodePool", poolFactoryBean); + + assertThat(this.getFieldValue(poolFactoryBean, "subscriptionEnabled")).isFalse(); + } + + @Test + public void subscriptionNotEnabledForNonPreciseDefaultPoolCase() { + + PoolFactoryBean poolFactoryBean = new PoolFactoryBean(); + + assertThat(poolFactoryBean).isNotNull(); + assertThat(this.getFieldValue(poolFactoryBean, "subscriptionEnabled")).isFalse(); + + this.configuration.enableSubscriptionPoolConfigurer().configure("Default", poolFactoryBean); + + assertThat(this.getFieldValue(poolFactoryBean, "subscriptionEnabled")).isFalse(); + } + + @Test + public void subscriptionNotEnabledForTestPool() { + + PoolFactoryBean poolFactoryBean = new PoolFactoryBean(); + + assertThat(poolFactoryBean).isNotNull(); + assertThat(this.getFieldValue(poolFactoryBean, "subscriptionEnabled")).isFalse(); + + this.configuration.enableSubscriptionPoolConfigurer().configure("TEST", poolFactoryBean); + + assertThat(this.getFieldValue(poolFactoryBean, "subscriptionEnabled")).isFalse(); + } +}