From 932f24e32e22b2fa1ef8655cbeae75040dcaecf4 Mon Sep 17 00:00:00 2001 From: John Blum Date: Wed, 18 Oct 2017 09:26:23 -0700 Subject: [PATCH] SGF-681 - EnableClusterConfiguration annotation attributes are silently ignored. --- .../ClusterConfigurationConfiguration.java | 10 +- .../EnableClusterConfigurationUnitTests.java | 242 ++++++++++++++++++ 2 files changed, 247 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/springframework/data/gemfire/config/annotation/EnableClusterConfigurationUnitTests.java diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/ClusterConfigurationConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/ClusterConfigurationConfiguration.java index 89177eed..71bdf850 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/ClusterConfigurationConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/ClusterConfigurationConfiguration.java @@ -148,17 +148,17 @@ public class ClusterConfigurationConfiguration extends AbstractAnnotationConfigS AnnotationAttributes enableClusterConfigurationAttributes = getAnnotationAttributes(importMetadata); - setManagementHttpHost(resolveProperty(managementProperty("http.hostname"), - DEFAULT_MANAGEMENT_HTTP_HOST)); + setManagementHttpHost(resolveProperty(managementProperty("http.host"), + enableClusterConfigurationAttributes.getString("host"))); setManagementHttpPort(resolveProperty(managementProperty("http.port"), - DEFAULT_MANAGEMENT_HTTP_PORT)); + enableClusterConfigurationAttributes.getNumber("port"))); setManagementUseHttp(resolveProperty(managementProperty("use-http"), - DEFAULT_MANAGEMENT_USE_HTTP)); + enableClusterConfigurationAttributes.getBoolean("useHttp"))); setServerRegionShortcut(resolveProperty(clusterProperty("region.type"), - RegionShortcut.class, DEFAULT_SERVER_REGION_SHORTCUT)); + RegionShortcut.class, enableClusterConfigurationAttributes.getEnum("serverRegionShortcut"))); } } diff --git a/src/test/java/org/springframework/data/gemfire/config/annotation/EnableClusterConfigurationUnitTests.java b/src/test/java/org/springframework/data/gemfire/config/annotation/EnableClusterConfigurationUnitTests.java new file mode 100644 index 00000000..73e2fa8b --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/config/annotation/EnableClusterConfigurationUnitTests.java @@ -0,0 +1,242 @@ +/* + * Copyright 2017 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 + * + * http://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.config.annotation; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.geode.cache.RegionShortcut; +import org.junit.Test; +import org.springframework.core.env.Environment; +import org.springframework.core.type.AnnotationMetadata; + +/** + * Unit tests for {@link EnableClusterConfiguration} annotation and the {@link ClusterConfigurationConfiguration} class. + * + * @author John Blum + * @see org.junit.Test + * @see org.mockito.Mock + * @see org.mockito.Mockito + * @see org.springframework.core.env.Environment + * @see org.springframework.core.type.AnnotationMetadata + * @see org.springframework.data.gemfire.config.annotation.ClusterConfigurationConfiguration + * @see org.springframework.data.gemfire.config.annotation.EnableClusterConfiguration + * @since 2.0.1 + */ +public class EnableClusterConfigurationUnitTests { + + @Test + public void setImportMetadataFromAnnotationAttributes() { + + AnnotationMetadata mockImportMetadata = mock(AnnotationMetadata.class); + + Map annotationAttributes = new HashMap<>(); + + annotationAttributes.put("host", "skullbox"); + annotationAttributes.put("port", 12345); + annotationAttributes.put("serverRegionShortcut", RegionShortcut.PARTITION_PERSISTENT); + annotationAttributes.put("useHttp", true); + + when(mockImportMetadata.getAnnotationAttributes(EnableClusterConfiguration.class.getName())) + .thenReturn(annotationAttributes); + + when(mockImportMetadata.hasAnnotation(eq(EnableClusterConfiguration.class.getName()))).thenReturn(true); + + ClusterConfigurationConfiguration configuration = new ClusterConfigurationConfiguration(); + + configuration.setImportMetadata(mockImportMetadata); + + assertThat(configuration.getManagementHttpHost().orElse(null)).isEqualTo("skullbox"); + assertThat(configuration.getManagementHttpPort().orElse(0)).isEqualTo(12345); + assertThat(configuration.getManagementUseHttp().orElse(false)).isTrue(); + assertThat(configuration.getServerRegionShortcut().orElse(null)).isEqualTo(RegionShortcut.PARTITION_PERSISTENT); + + verify(mockImportMetadata, times(1)) + .getAnnotationAttributes(eq(EnableClusterConfiguration.class.getName())); + + verify(mockImportMetadata, times(1)) + .hasAnnotation(eq(EnableClusterConfiguration.class.getName())); + } + + @Test + public void setImportMetadataFromProperties() { + + AnnotationMetadata mockImportMetadata = mock(AnnotationMetadata.class); + + Map annotationAttributes = new HashMap<>(); + + annotationAttributes.put("host", "skullbox"); + annotationAttributes.put("port", 12345); + annotationAttributes.put("serverRegionShortcut", RegionShortcut.PARTITION_PERSISTENT); + annotationAttributes.put("useHttp", false); + + when(mockImportMetadata.getAnnotationAttributes(EnableClusterConfiguration.class.getName())) + .thenReturn(annotationAttributes); + + when(mockImportMetadata.hasAnnotation(eq(EnableClusterConfiguration.class.getName()))).thenReturn(true); + + Environment mockEnvironment = mock(Environment.class); + + when(mockEnvironment.containsProperty(eq("spring.data.gemfire.cluster.region.type"))).thenReturn(true); + when(mockEnvironment.containsProperty(eq("spring.data.gemfire.management.http.host"))).thenReturn(true); + when(mockEnvironment.containsProperty(eq("spring.data.gemfire.management.http.port"))).thenReturn(true); + when(mockEnvironment.containsProperty(eq("spring.data.gemfire.management.use-http"))).thenReturn(true); + + when(mockEnvironment.getProperty(eq("spring.data.gemfire.cluster.region.type"), eq(RegionShortcut.class), any(RegionShortcut.class))) + .thenReturn(RegionShortcut.LOCAL); + + when(mockEnvironment.getProperty(eq("spring.data.gemfire.management.http.host"), eq(String.class), any(String.class))) + .thenReturn("cardboardBox"); + + when(mockEnvironment.getProperty(eq("spring.data.gemfire.management.http.port"), eq(Integer.class), any(Integer.class))) + .thenReturn(11235); + + when(mockEnvironment.getProperty(eq("spring.data.gemfire.management.use-http"), eq(Boolean.class), any(Boolean.class))) + .thenReturn(true); + + when(mockEnvironment.resolveRequiredPlaceholders(anyString())) + .thenAnswer(invocation -> invocation.getArgument(0)); + + ClusterConfigurationConfiguration configuration = new ClusterConfigurationConfiguration(); + + configuration.setEnvironment(mockEnvironment); + configuration.setImportMetadata(mockImportMetadata); + + assertThat(configuration.getManagementHttpHost().orElse(null)).isEqualTo("cardboardBox"); + assertThat(configuration.getManagementHttpPort().orElse(0)).isEqualTo(11235); + assertThat(configuration.getManagementUseHttp().orElse(false)).isTrue(); + assertThat(configuration.getServerRegionShortcut().orElse(null)).isEqualTo(RegionShortcut.LOCAL); + + verify(mockImportMetadata, times(1)) + .getAnnotationAttributes(eq(EnableClusterConfiguration.class.getName())); + + verify(mockImportMetadata, times(1)) + .hasAnnotation(eq(EnableClusterConfiguration.class.getName())); + + verify(mockEnvironment, times(1)) + .containsProperty(eq("spring.data.gemfire.cluster.region.type")); + + verify(mockEnvironment, times(1)) + .containsProperty(eq("spring.data.gemfire.management.http.host")); + + verify(mockEnvironment, times(1)) + .containsProperty(eq("spring.data.gemfire.management.http.port")); + + verify(mockEnvironment, times(1)) + .containsProperty(eq("spring.data.gemfire.management.use-http")); + + verify(mockEnvironment, times(1)) + .getProperty(eq("spring.data.gemfire.cluster.region.type"), eq(RegionShortcut.class), + eq(RegionShortcut.PARTITION_PERSISTENT)); + + verify(mockEnvironment, times(1)) + .getProperty(eq("spring.data.gemfire.management.http.host"), eq(String.class), anyString()); + + verify(mockEnvironment, times(1)) + .getProperty(eq("spring.data.gemfire.management.http.port"), eq(Integer.class), anyInt()); + + verify(mockEnvironment, times(1)) + .getProperty(eq("spring.data.gemfire.management.use-http"), eq(Boolean.class), eq(false)); + } + + @Test + public void setImportMetadataFromAnnotationAttributesAndProperties() { + + AnnotationMetadata mockImportMetadata = mock(AnnotationMetadata.class); + + Map annotationAttributes = new HashMap<>(); + + annotationAttributes.put("host", "postOfficeBox"); + annotationAttributes.put("port", 10101); + annotationAttributes.put("serverRegionShortcut", RegionShortcut.REPLICATE); + annotationAttributes.put("useHttp", true); + + when(mockImportMetadata.getAnnotationAttributes(EnableClusterConfiguration.class.getName())) + .thenReturn(annotationAttributes); + + when(mockImportMetadata.hasAnnotation(eq(EnableClusterConfiguration.class.getName()))).thenReturn(true); + + Environment mockEnvironment = mock(Environment.class); + + when(mockEnvironment.containsProperty(eq("spring.data.gemfire.cluster.region.type"))).thenReturn(false); + when(mockEnvironment.containsProperty(eq("spring.data.gemfire.management.http.host"))).thenReturn(true); + when(mockEnvironment.containsProperty(eq("spring.data.gemfire.management.http.port"))).thenReturn(true); + when(mockEnvironment.containsProperty(eq("spring.data.gemfire.management.use-http"))).thenReturn(false); + + when(mockEnvironment.getProperty(eq("spring.data.gemfire.management.http.host"), eq(String.class), any(String.class))) + .thenReturn("shoeBox"); + + when(mockEnvironment.getProperty(eq("spring.data.gemfire.management.http.port"), eq(Integer.class), any(Integer.class))) + .thenReturn(12480); + + when(mockEnvironment.resolveRequiredPlaceholders(anyString())) + .thenAnswer(invocation -> invocation.getArgument(0)); + + ClusterConfigurationConfiguration configuration = new ClusterConfigurationConfiguration(); + + configuration.setEnvironment(mockEnvironment); + configuration.setImportMetadata(mockImportMetadata); + + assertThat(configuration.getManagementHttpHost().orElse(null)).isEqualTo("shoeBox"); + assertThat(configuration.getManagementHttpPort().orElse(0)).isEqualTo(12480); + assertThat(configuration.getManagementUseHttp().orElse(false)).isTrue(); + assertThat(configuration.getServerRegionShortcut().orElse(null)).isEqualTo(RegionShortcut.REPLICATE); + + verify(mockImportMetadata, times(1)) + .getAnnotationAttributes(eq(EnableClusterConfiguration.class.getName())); + + verify(mockImportMetadata, times(1)) + .hasAnnotation(eq(EnableClusterConfiguration.class.getName())); + + verify(mockEnvironment, times(1)) + .containsProperty(eq("spring.data.gemfire.cluster.region.type")); + + verify(mockEnvironment, times(1)) + .containsProperty(eq("spring.data.gemfire.management.http.host")); + + verify(mockEnvironment, times(1)) + .containsProperty(eq("spring.data.gemfire.management.http.port")); + + verify(mockEnvironment, times(1)) + .containsProperty(eq("spring.data.gemfire.management.use-http")); + + verify(mockEnvironment, never()) + .getProperty(eq("spring.data.gemfire.cluster.region.type"), eq(RegionShortcut.class), + any(RegionShortcut.class)); + + verify(mockEnvironment, times(1)) + .getProperty(eq("spring.data.gemfire.management.http.host"), eq(String.class), anyString()); + + verify(mockEnvironment, times(1)) + .getProperty(eq("spring.data.gemfire.management.http.port"), eq(Integer.class), anyInt()); + + verify(mockEnvironment, never()) + .getProperty(eq("spring.data.gemfire.management.use-http"), eq(Boolean.class), anyBoolean()); + } +}