DATAGEODE-243 - Introduce RestTemplateConfigurer to configure the RestTemplate used when sending configuration metadata from client to server.
This commit is contained in:
@@ -19,6 +19,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isA;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -32,19 +33,21 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.geode.cache.Region;
|
||||
import org.apache.geode.cache.client.ClientCache;
|
||||
import org.apache.geode.cache.query.Index;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.apache.geode.cache.Region;
|
||||
import org.apache.geode.cache.client.ClientCache;
|
||||
import org.apache.geode.cache.query.Index;
|
||||
|
||||
import org.springframework.data.gemfire.IndexType;
|
||||
import org.springframework.data.gemfire.config.schema.definitions.IndexDefinition;
|
||||
import org.springframework.data.gemfire.config.schema.definitions.RegionDefinition;
|
||||
import org.springframework.data.gemfire.config.support.RestTemplateConfigurer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -104,7 +107,8 @@ public class RestHttpGemfireAdminTemplateUnitTests {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <T extends RestOperations> T newRestOperations(ClientHttpRequestFactory clientHttpRequestFactory,
|
||||
List<ClientHttpRequestInterceptor> clientHttpRequestInterceptors) {
|
||||
List<ClientHttpRequestInterceptor> clientHttpRequestInterceptors,
|
||||
List<RestTemplateConfigurer> restTemplateConfigurers) {
|
||||
|
||||
return (T) mockRestOperations;
|
||||
}
|
||||
@@ -225,7 +229,8 @@ public class RestHttpGemfireAdminTemplateUnitTests {
|
||||
ClientHttpRequestInterceptor mockInterceptorTwo = mock(ClientHttpRequestInterceptor.class);
|
||||
|
||||
RestTemplate restTemplate = new RestHttpGemfireAdminTemplate(this.mockClientCache)
|
||||
.newRestOperations(mockClientHttpRequestFactory, Arrays.asList(mockInterceptorOne, mockInterceptorTwo));
|
||||
.newRestOperations(mockClientHttpRequestFactory, Arrays.asList(mockInterceptorOne, mockInterceptorTwo),
|
||||
Collections.emptyList());
|
||||
|
||||
assertThat(restTemplate).isNotNull();
|
||||
assertThat(restTemplate.getInterceptors()).containsExactly(mockInterceptorOne, mockInterceptorTwo);
|
||||
@@ -239,13 +244,43 @@ public class RestHttpGemfireAdminTemplateUnitTests {
|
||||
ClientHttpRequestFactory mockClientHttpRequestFactory = mock(ClientHttpRequestFactory.class);
|
||||
|
||||
RestTemplate restTemplate = new RestHttpGemfireAdminTemplate(this.mockClientCache)
|
||||
.newRestOperations(mockClientHttpRequestFactory, Collections.emptyList());
|
||||
.newRestOperations(mockClientHttpRequestFactory, Collections.emptyList(), Collections.emptyList());
|
||||
|
||||
assertThat(restTemplate).isNotNull();
|
||||
assertThat(restTemplate.getInterceptors()).isEmpty();
|
||||
assertThat(restTemplate.getRequestFactory()).isSameAs(mockClientHttpRequestFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newRestOperationsWithRestTemplateConfigurersApplied() {
|
||||
|
||||
ClientHttpRequestFactory mockClientHttpRequestFactory = mock(ClientHttpRequestFactory.class);
|
||||
|
||||
RestTemplateConfigurer mockRestTemplateConfigurerOne = mock(RestTemplateConfigurer.class);
|
||||
RestTemplateConfigurer mockRestTemplateConfigurerTwo = mock(RestTemplateConfigurer.class);
|
||||
|
||||
Answer answer = invocation -> {
|
||||
|
||||
assertThat(invocation.getArgument(0, RestTemplate.class)).isInstanceOf(RestTemplate.class);
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
doAnswer(answer).when(mockRestTemplateConfigurerOne).configure(isA(RestTemplate.class));
|
||||
doAnswer(answer).when(mockRestTemplateConfigurerTwo).configure(isA(RestTemplate.class));
|
||||
|
||||
List<RestTemplateConfigurer> mockRestTemplateConfigurers =
|
||||
Arrays.asList(mockRestTemplateConfigurerOne, null, mockRestTemplateConfigurerTwo);
|
||||
|
||||
RestTemplate restTemplate = new RestHttpGemfireAdminTemplate(this.mockClientCache)
|
||||
.newRestOperations(mockClientHttpRequestFactory, Collections.emptyList(), mockRestTemplateConfigurers);
|
||||
|
||||
assertThat(restTemplate).isNotNull();
|
||||
|
||||
verify(mockRestTemplateConfigurerOne, times(1)).configure(eq(restTemplate));
|
||||
verify(mockRestTemplateConfigurerTwo, times(1)).configure(eq(restTemplate));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolvesManagementRestApiUrlCorrectly() {
|
||||
|
||||
|
||||
@@ -26,12 +26,12 @@ import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.geode.cache.client.ClientCache;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.apache.geode.cache.client.ClientCache;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
@@ -40,6 +40,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.data.gemfire.config.admin.GemfireAdminOperations;
|
||||
import org.springframework.data.gemfire.config.admin.remote.RestHttpGemfireAdminTemplate;
|
||||
import org.springframework.data.gemfire.config.support.RestTemplateConfigurer;
|
||||
import org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
|
||||
@@ -91,6 +92,20 @@ public class ClusterConfigurationWithClientHttpRequestInterceptorsIntegrationTes
|
||||
@Autowired
|
||||
private List<ClientHttpRequestInterceptor> clientHttpRequestInterceptors;
|
||||
|
||||
@Autowired
|
||||
private List<RestTemplateConfigurer> restTemplateConfigurers;
|
||||
|
||||
private RestTemplate theRestTemplate;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("testRestTemplateConfigurerOne")
|
||||
private RestTemplateConfigurer restTemplateConfigurerOne;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("testRestTemplateConfigurerTwo")
|
||||
private RestTemplateConfigurer restTemplateConfigurerTwo;
|
||||
|
||||
// TODO: Replace with STDG
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T getFieldValue(Object target, String fieldName) throws NoSuchFieldException {
|
||||
|
||||
@@ -108,7 +123,7 @@ public class ClusterConfigurationWithClientHttpRequestInterceptorsIntegrationTes
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
public void setupIsCorrect() {
|
||||
|
||||
assertThat(this.clientCache).isNotNull();
|
||||
assertThat(this.configuration).isNotNull();
|
||||
@@ -119,17 +134,18 @@ public class ClusterConfigurationWithClientHttpRequestInterceptorsIntegrationTes
|
||||
assertThat(this.clientHttpRequestInterceptors).hasSize(2);
|
||||
assertThat(this.clientHttpRequestInterceptors)
|
||||
.containsExactly(this.mockClientHttpRequestInterceptorTwo, this.mockClientHttpRequestInterceptorOne);
|
||||
assertThat(this.restTemplateConfigurerOne).isInstanceOf(TestRestTemplateConfigurer.class);
|
||||
assertThat(this.restTemplateConfigurerTwo).isInstanceOf(TestRestTemplateConfigurer.class);
|
||||
assertThat(this.restTemplateConfigurers).isNotNull();
|
||||
assertThat(this.restTemplateConfigurers).hasSize(2);
|
||||
assertThat(this.restTemplateConfigurers)
|
||||
.containsExactlyInAnyOrder(this.restTemplateConfigurerOne, restTemplateConfigurerTwo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configurationWasAutowiredWithUserDefinedClientHttpRequestInterceptors() {
|
||||
@Before
|
||||
public void restTemplateWasConfiguredCorrectly() throws Exception {
|
||||
|
||||
assertThat(this.configuration.resolveClientHttpRequestInterceptors())
|
||||
.isEqualTo(this.clientHttpRequestInterceptors);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clientHttpRequestInterceptorsRegistered() throws Exception {
|
||||
assertThat(this.initializer).isNotNull();
|
||||
|
||||
SchemaObjectContext schemaObjectContext = this.initializer.getSchemaObjectContext();
|
||||
|
||||
@@ -140,12 +156,43 @@ public class ClusterConfigurationWithClientHttpRequestInterceptorsIntegrationTes
|
||||
|
||||
RestHttpGemfireAdminTemplate template = schemaObjectContext.getGemfireAdminOperations();
|
||||
|
||||
RestTemplate restTemplate = getFieldValue(template, "restTemplate");
|
||||
this.theRestTemplate = getFieldValue(template, "restTemplate");
|
||||
|
||||
assertThat(restTemplate).isNotNull();
|
||||
assertThat(restTemplate.getInterceptors())
|
||||
.containsExactly(this.mockClientHttpRequestInterceptorTwo, this.mockClientHttpRequestInterceptorOne);
|
||||
assertThat(restTemplate.getRequestFactory()).isInstanceOf(InterceptingClientHttpRequestFactory.class);
|
||||
assertThat(this.theRestTemplate).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertRestTemplateConfigurersVisitedAndConfiguredTheClusterConfigurationRestTemplate() {
|
||||
|
||||
assertThat(((TestRestTemplateConfigurer) restTemplateConfigurerOne).getRestTemplate())
|
||||
.isEqualTo(this.theRestTemplate);
|
||||
|
||||
assertThat(((TestRestTemplateConfigurer) restTemplateConfigurerTwo).getRestTemplate())
|
||||
.isEqualTo(this.theRestTemplate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertUserDefinedCustomClientHttpRequestInterceptorsAreNotRegisteredByDefault() {
|
||||
|
||||
assertThat(this.theRestTemplate.getInterceptors())
|
||||
.doesNotContain(this.mockClientHttpRequestInterceptorTwo, this.mockClientHttpRequestInterceptorOne);
|
||||
|
||||
assertThat(this.theRestTemplate.getRequestFactory())
|
||||
.isNotInstanceOf(InterceptingClientHttpRequestFactory.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configurationWasAutowiredWithUserDefinedClientHttpRequestInterceptors() {
|
||||
|
||||
assertThat(this.configuration.resolveClientHttpRequestInterceptors(true))
|
||||
.isEqualTo(this.clientHttpRequestInterceptors);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configurationWasAutowiredWithUserDefinedRestTemplateConfigurers() {
|
||||
|
||||
assertThat(this.configuration.resolveRestTemplateConfigurers())
|
||||
.isEqualTo(this.restTemplateConfigurers);
|
||||
}
|
||||
|
||||
@ClientCacheApplication
|
||||
@@ -186,5 +233,29 @@ public class ClusterConfigurationWithClientHttpRequestInterceptorsIntegrationTes
|
||||
ClientHttpRequestInterceptor mockClientHttpRequestInterceptorTwo() {
|
||||
return mock(ClientHttpRequestInterceptor.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
TestRestTemplateConfigurer testRestTemplateConfigurerOne() {
|
||||
return new TestRestTemplateConfigurer();
|
||||
}
|
||||
|
||||
@Bean
|
||||
TestRestTemplateConfigurer testRestTemplateConfigurerTwo() {
|
||||
return new TestRestTemplateConfigurer();
|
||||
}
|
||||
}
|
||||
|
||||
private static final class TestRestTemplateConfigurer implements RestTemplateConfigurer {
|
||||
|
||||
private volatile RestTemplate restTemplate;
|
||||
|
||||
RestTemplate getRestTemplate() {
|
||||
return this.restTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ 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.doReturn;
|
||||
@@ -41,13 +40,13 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.apache.geode.cache.Cache;
|
||||
import org.apache.geode.cache.RegionShortcut;
|
||||
import org.apache.geode.cache.client.ClientCache;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.core.env.Environment;
|
||||
@@ -64,7 +63,7 @@ import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link EnableClusterConfiguration} annotation and the {@link ClusterConfigurationConfiguration} class.
|
||||
* Unit Tests for {@link EnableClusterConfiguration} annotation and the {@link ClusterConfigurationConfiguration} class.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.junit.Test
|
||||
@@ -88,6 +87,7 @@ public class EnableClusterConfigurationUnitTests {
|
||||
System.clearProperty(ClusterConfigurationConfiguration.HTTP_FOLLOW_REDIRECTS_PROPERTY);
|
||||
}
|
||||
|
||||
// TODO: Replace with STDG!
|
||||
private <T> ClusterConfigurationConfiguration autowire(ClusterConfigurationConfiguration target,
|
||||
String fieldName, T dependency) throws NoSuchFieldException {
|
||||
|
||||
@@ -130,6 +130,8 @@ public class EnableClusterConfigurationUnitTests {
|
||||
|
||||
annotationAttributes.put("host", "skullbox");
|
||||
annotationAttributes.put("port", 12345);
|
||||
annotationAttributes.put("enableInterceptors", true);
|
||||
annotationAttributes.put("followRedirects", true);
|
||||
annotationAttributes.put("requireHttps", false);
|
||||
annotationAttributes.put("serverRegionShortcut", RegionShortcut.PARTITION_PERSISTENT);
|
||||
annotationAttributes.put("useHttp", true);
|
||||
@@ -145,6 +147,8 @@ public class EnableClusterConfigurationUnitTests {
|
||||
|
||||
assertThat(configuration.getManagementHttpHost().orElse(null)).isEqualTo("skullbox");
|
||||
assertThat(configuration.getManagementHttpPort().orElse(0)).isEqualTo(12345);
|
||||
assertThat(configuration.getManagementHttpEnableInterceptors().orElse(false)).isTrue();
|
||||
assertThat(configuration.getManagementHttpFollowRedirects().orElse(false)).isTrue();
|
||||
assertThat(configuration.getManagementRequireHttps().orElse(true)).isFalse();
|
||||
assertThat(configuration.getManagementUseHttp().orElse(false)).isTrue();
|
||||
assertThat(configuration.getServerRegionShortcut().orElse(null)).isEqualTo(RegionShortcut.PARTITION_PERSISTENT);
|
||||
@@ -165,6 +169,8 @@ public class EnableClusterConfigurationUnitTests {
|
||||
|
||||
annotationAttributes.put("host", "skullbox");
|
||||
annotationAttributes.put("port", 12345);
|
||||
annotationAttributes.put("enableInterceptors", false);
|
||||
annotationAttributes.put("followRedirects", false);
|
||||
annotationAttributes.put("requireHttps", true);
|
||||
annotationAttributes.put("serverRegionShortcut", RegionShortcut.PARTITION_PERSISTENT);
|
||||
annotationAttributes.put("useHttp", false);
|
||||
@@ -179,6 +185,8 @@ public class EnableClusterConfigurationUnitTests {
|
||||
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.http.enable-interceptors"))).thenReturn(true);
|
||||
when(mockEnvironment.containsProperty(eq("spring.data.gemfire.management.http.follow-redirects"))).thenReturn(true);
|
||||
when(mockEnvironment.containsProperty(eq("spring.data.gemfire.management.require-https"))).thenReturn(true);
|
||||
when(mockEnvironment.containsProperty(eq("spring.data.gemfire.management.use-http"))).thenReturn(true);
|
||||
|
||||
@@ -191,6 +199,12 @@ public class EnableClusterConfigurationUnitTests {
|
||||
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.http.enable-interceptors"), eq(Boolean.class), any(Boolean.class)))
|
||||
.thenReturn(true);
|
||||
|
||||
when(mockEnvironment.getProperty(eq("spring.data.gemfire.management.http.follow-redirects"), eq(Boolean.class), any(Boolean.class)))
|
||||
.thenReturn(true);
|
||||
|
||||
when(mockEnvironment.getProperty(eq("spring.data.gemfire.management.require-https"), eq(Boolean.class), any(Boolean.class)))
|
||||
.thenReturn(false);
|
||||
|
||||
@@ -207,6 +221,8 @@ public class EnableClusterConfigurationUnitTests {
|
||||
|
||||
assertThat(configuration.getManagementHttpHost().orElse(null)).isEqualTo("cardboardBox");
|
||||
assertThat(configuration.getManagementHttpPort().orElse(0)).isEqualTo(11235);
|
||||
assertThat(configuration.getManagementHttpEnableInterceptors().orElse(false)).isTrue();
|
||||
assertThat(configuration.getManagementHttpFollowRedirects().orElse(false)).isTrue();
|
||||
assertThat(configuration.getManagementRequireHttps().orElse(true)).isFalse();
|
||||
assertThat(configuration.getManagementUseHttp().orElse(false)).isTrue();
|
||||
assertThat(configuration.getServerRegionShortcut().orElse(null)).isEqualTo(RegionShortcut.LOCAL);
|
||||
@@ -226,6 +242,12 @@ public class EnableClusterConfigurationUnitTests {
|
||||
verify(mockEnvironment, times(1))
|
||||
.containsProperty(eq("spring.data.gemfire.management.http.port"));
|
||||
|
||||
verify(mockEnvironment, times(1))
|
||||
.containsProperty(eq("spring.data.gemfire.management.http.enable-interceptors"));
|
||||
|
||||
verify(mockEnvironment, times(1))
|
||||
.containsProperty(eq("spring.data.gemfire.management.http.follow-redirects"));
|
||||
|
||||
verify(mockEnvironment, times(1))
|
||||
.containsProperty(eq("spring.data.gemfire.management.require-https"));
|
||||
|
||||
@@ -237,10 +259,16 @@ public class EnableClusterConfigurationUnitTests {
|
||||
eq(RegionShortcut.PARTITION_PERSISTENT));
|
||||
|
||||
verify(mockEnvironment, times(1))
|
||||
.getProperty(eq("spring.data.gemfire.management.http.host"), eq(String.class), anyString());
|
||||
.getProperty(eq("spring.data.gemfire.management.http.host"), eq(String.class), eq("skullbox"));
|
||||
|
||||
verify(mockEnvironment, times(1))
|
||||
.getProperty(eq("spring.data.gemfire.management.http.port"), eq(Integer.class), anyInt());
|
||||
.getProperty(eq("spring.data.gemfire.management.http.port"), eq(Integer.class), eq(12345));
|
||||
|
||||
verify(mockEnvironment, times(1))
|
||||
.getProperty(eq("spring.data.gemfire.management.http.enable-interceptors"), eq(Boolean.class), eq(false));
|
||||
|
||||
verify(mockEnvironment, times(1))
|
||||
.getProperty(eq("spring.data.gemfire.management.http.follow-redirects"), eq(Boolean.class), eq(false));
|
||||
|
||||
verify(mockEnvironment, times(1))
|
||||
.getProperty(eq("spring.data.gemfire.management.require-https"), eq(Boolean.class), eq(true));
|
||||
@@ -258,6 +286,8 @@ public class EnableClusterConfigurationUnitTests {
|
||||
|
||||
annotationAttributes.put("host", "postOfficeBox");
|
||||
annotationAttributes.put("port", 10101);
|
||||
annotationAttributes.put("enableInterceptors", false);
|
||||
annotationAttributes.put("followRedirects", false);
|
||||
annotationAttributes.put("requireHttps", false);
|
||||
annotationAttributes.put("serverRegionShortcut", RegionShortcut.REPLICATE);
|
||||
annotationAttributes.put("useHttp", true);
|
||||
@@ -272,9 +302,14 @@ public class EnableClusterConfigurationUnitTests {
|
||||
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.http.enable-interceptors"))).thenReturn(false);
|
||||
when(mockEnvironment.containsProperty(eq("spring.data.gemfire.management.http.follow-redirects"))).thenReturn(true);
|
||||
when(mockEnvironment.containsProperty(eq("spring.data.gemfire.management.require-https"))).thenReturn(false);
|
||||
when(mockEnvironment.containsProperty(eq("spring.data.gemfire.management.use-http"))).thenReturn(false);
|
||||
|
||||
when(mockEnvironment.getProperty(eq("spring.data.gemfire.management.http.follow-redirects"), eq(Boolean.class), any(Boolean.class)))
|
||||
.thenReturn(true);
|
||||
|
||||
when(mockEnvironment.getProperty(eq("spring.data.gemfire.management.http.host"), eq(String.class), any(String.class)))
|
||||
.thenReturn("shoebox");
|
||||
|
||||
@@ -291,6 +326,8 @@ public class EnableClusterConfigurationUnitTests {
|
||||
|
||||
assertThat(configuration.getManagementHttpHost().orElse(null)).isEqualTo("shoebox");
|
||||
assertThat(configuration.getManagementHttpPort().orElse(0)).isEqualTo(12480);
|
||||
assertThat(configuration.getManagementHttpEnableInterceptors().orElse(true)).isFalse();
|
||||
assertThat(configuration.getManagementHttpFollowRedirects().orElse(false)).isTrue();
|
||||
assertThat(configuration.getManagementRequireHttps().orElse(true)).isFalse();
|
||||
assertThat(configuration.getManagementUseHttp().orElse(false)).isTrue();
|
||||
assertThat(configuration.getServerRegionShortcut().orElse(null)).isEqualTo(RegionShortcut.REPLICATE);
|
||||
@@ -321,10 +358,16 @@ public class EnableClusterConfigurationUnitTests {
|
||||
any(RegionShortcut.class));
|
||||
|
||||
verify(mockEnvironment, times(1))
|
||||
.getProperty(eq("spring.data.gemfire.management.http.host"), eq(String.class), anyString());
|
||||
.getProperty(eq("spring.data.gemfire.management.http.host"), eq(String.class), eq("postOfficeBox"));
|
||||
|
||||
verify(mockEnvironment, times(1))
|
||||
.getProperty(eq("spring.data.gemfire.management.http.port"), eq(Integer.class), anyInt());
|
||||
.getProperty(eq("spring.data.gemfire.management.http.port"), eq(Integer.class), eq(10101));
|
||||
|
||||
verify(mockEnvironment, never())
|
||||
.getProperty(eq("spring.data.gemfire.management.http.enable-interceptors"), eq(Boolean.class), anyBoolean());
|
||||
|
||||
verify(mockEnvironment, times(1))
|
||||
.getProperty(eq("spring.data.gemfire.management.http.follow-redirects"), eq(Boolean.class), eq(false));
|
||||
|
||||
verify(mockEnvironment, never())
|
||||
.getProperty(eq("spring.data.gemfire.management.require-https"), eq(Boolean.class), anyBoolean());
|
||||
@@ -364,9 +407,10 @@ public class EnableClusterConfigurationUnitTests {
|
||||
assertThat(schemaObjectContext.getSchemaObjectCollector()).isInstanceOf(ComposableSchemaObjectCollector.class);
|
||||
assertThat(schemaObjectContext.getSchemaObjectDefiner()).isInstanceOf(ComposableSchemaObjectDefiner.class);
|
||||
|
||||
verify(configuration, never()).resolveClientHttpRequestInterceptors();
|
||||
verify(configuration, times(1))
|
||||
.resolveGemfireAdminOperations(eq(mockEnvironment), eq(mockClientCache));
|
||||
verify(configuration, never()).resolveClientHttpRequestInterceptors(anyBoolean());
|
||||
verify(configuration, never()).resolveRestTemplateConfigurers();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -407,7 +451,8 @@ public class EnableClusterConfigurationUnitTests {
|
||||
configuration = autowire(configuration, "clientHttpRequestInterceptors", clientHttpRequestInterceptors);
|
||||
configuration.setBeanFactory(mockBeanFactory);
|
||||
|
||||
assertThat(configuration.resolveClientHttpRequestInterceptors()).isEqualTo(clientHttpRequestInterceptors);
|
||||
assertThat(configuration.resolveClientHttpRequestInterceptors(true))
|
||||
.isEqualTo(clientHttpRequestInterceptors);
|
||||
|
||||
verifyZeroInteractions(mockBeanFactory);
|
||||
}
|
||||
@@ -433,7 +478,7 @@ public class EnableClusterConfigurationUnitTests {
|
||||
configuration.setBeanFactory(mockBeanFactory);
|
||||
|
||||
List<ClientHttpRequestInterceptor> actualClientHttpRequestInterceptors =
|
||||
configuration.resolveClientHttpRequestInterceptors();
|
||||
configuration.resolveClientHttpRequestInterceptors(true);
|
||||
|
||||
assertThat(actualClientHttpRequestInterceptors).isNotNull();
|
||||
assertThat(actualClientHttpRequestInterceptors).hasSize(expectedClientHttpRequestInterceptors.size());
|
||||
@@ -455,7 +500,7 @@ public class EnableClusterConfigurationUnitTests {
|
||||
configuration.setBeanFactory(mockBeanFactory);
|
||||
|
||||
List<ClientHttpRequestInterceptor> clientHttpRequestInterceptors =
|
||||
configuration.resolveClientHttpRequestInterceptors();
|
||||
configuration.resolveClientHttpRequestInterceptors(true);
|
||||
|
||||
assertThat(clientHttpRequestInterceptors).isNotNull();
|
||||
assertThat(clientHttpRequestInterceptors).isEmpty();
|
||||
@@ -496,14 +541,15 @@ public class EnableClusterConfigurationUnitTests {
|
||||
|
||||
ClientCache mockClientCache = mock(ClientCache.class);
|
||||
|
||||
ClusterConfigurationConfiguration configuration = spy(new ClusterConfigurationConfiguration());
|
||||
|
||||
doReturn(Collections.emptyList()).when(configuration).resolveClientHttpRequestInterceptors(anyBoolean());
|
||||
doReturn(Collections.emptyList()).when(configuration).resolveRestTemplateConfigurers();
|
||||
|
||||
Environment mockEnvironment = mock(Environment.class);
|
||||
|
||||
when(mockEnvironment.getProperty(anyString(), eq(Boolean.class), anyBoolean())).thenReturn(false);
|
||||
|
||||
ClusterConfigurationConfiguration configuration = spy(new ClusterConfigurationConfiguration());
|
||||
|
||||
doReturn(Collections.emptyList()).when(configuration).resolveClientHttpRequestInterceptors();
|
||||
|
||||
configuration.setManagementUseHttp(true);
|
||||
|
||||
assertThat(configuration.resolveManagementRequireHttps()).isTrue();
|
||||
@@ -528,29 +574,26 @@ public class EnableClusterConfigurationUnitTests {
|
||||
assertThat(clientHttpRequestFactory.isFollowRedirects()).isFalse();
|
||||
|
||||
verifyZeroInteractions(mockClientCache);
|
||||
|
||||
verify(configuration, times(1)).resolveClientHttpRequestInterceptors();
|
||||
|
||||
verify(mockEnvironment, times(1))
|
||||
.getProperty(eq(ClusterConfigurationConfiguration.HTTP_FOLLOW_REDIRECTS_PROPERTY), eq(Boolean.class),
|
||||
eq(ClusterConfigurationConfiguration.DEFAULT_HTTP_FOLLOW_REDIRECTS));
|
||||
verify(configuration, times(1)).resolveClientHttpRequestInterceptors(eq(false));
|
||||
verify(configuration, times(1)).resolveRestTemplateConfigurers();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolvesNewRestHttpGemfireAdminOperationsSetsFollowRedirectsWithProperty() throws Exception {
|
||||
|
||||
System.setProperty(ClusterConfigurationConfiguration.HTTP_FOLLOW_REDIRECTS_PROPERTY, Boolean.TRUE.toString());
|
||||
|
||||
ClientCache mockClientCache = mock(ClientCache.class);
|
||||
|
||||
Environment environment = spy(new StandardEnvironment());
|
||||
|
||||
ClusterConfigurationConfiguration configuration = spy(new ClusterConfigurationConfiguration());
|
||||
|
||||
doReturn(Collections.emptyList()).when(configuration).resolveClientHttpRequestInterceptors();
|
||||
doReturn(Collections.emptyList()).when(configuration).resolveClientHttpRequestInterceptors(anyBoolean());
|
||||
doReturn(Collections.emptyList()).when(configuration).resolveRestTemplateConfigurers();
|
||||
|
||||
Environment environment = spy(new StandardEnvironment());
|
||||
|
||||
configuration.setManagementHttpFollowRedirects(true);
|
||||
configuration.setManagementUseHttp(true);
|
||||
|
||||
assertThat(configuration.resolveManagementHttpFollowRedirects()).isTrue();
|
||||
assertThat(configuration.resolveManagementRequireHttps()).isTrue();
|
||||
assertThat(configuration.resolveManagementUseHttp()).isTrue();
|
||||
|
||||
@@ -572,12 +615,8 @@ public class EnableClusterConfigurationUnitTests {
|
||||
assertThat(clientHttpRequestFactory.isFollowRedirects()).isTrue();
|
||||
|
||||
verifyZeroInteractions(mockClientCache);
|
||||
|
||||
verify(configuration, times(1)).resolveClientHttpRequestInterceptors();
|
||||
|
||||
verify(environment, times(1))
|
||||
.getProperty(eq(ClusterConfigurationConfiguration.HTTP_FOLLOW_REDIRECTS_PROPERTY), eq(Boolean.class),
|
||||
eq(ClusterConfigurationConfiguration.DEFAULT_HTTP_FOLLOW_REDIRECTS));
|
||||
verify(configuration, times(1)).resolveClientHttpRequestInterceptors(eq(false));
|
||||
verify(configuration, times(1)).resolveRestTemplateConfigurers();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -591,13 +630,16 @@ public class EnableClusterConfigurationUnitTests {
|
||||
ClientHttpRequestInterceptor mockInterceptorOne = mock(ClientHttpRequestInterceptor.class);
|
||||
ClientHttpRequestInterceptor mockInterceptorTwo = mock(ClientHttpRequestInterceptor.class);
|
||||
|
||||
Environment environment = spy(new StandardEnvironment());
|
||||
|
||||
ClusterConfigurationConfiguration configuration = spy(new ClusterConfigurationConfiguration());
|
||||
|
||||
doReturn(Arrays.asList(mockInterceptorOne, mockInterceptorTwo))
|
||||
.when(configuration).resolveClientHttpRequestInterceptors();
|
||||
Environment environment = spy(new StandardEnvironment());
|
||||
|
||||
doReturn(Arrays.asList(mockInterceptorOne, mockInterceptorTwo))
|
||||
.when(configuration).resolveClientHttpRequestInterceptors(anyBoolean());
|
||||
|
||||
doReturn(Collections.emptyList()).when(configuration).resolveRestTemplateConfigurers();
|
||||
|
||||
configuration.setManagementHttpEnableInterceptors(true);
|
||||
configuration.setManagementRequireHttps(false);
|
||||
configuration.setManagementUseHttp(true);
|
||||
|
||||
@@ -622,12 +664,8 @@ public class EnableClusterConfigurationUnitTests {
|
||||
assertThat(clientHttpRequestFactory.isFollowRedirects()).isTrue();
|
||||
|
||||
verifyZeroInteractions(mockClientCache);
|
||||
|
||||
verify(configuration, times(1)).resolveClientHttpRequestInterceptors();
|
||||
|
||||
verify(environment, times(1))
|
||||
.getProperty(eq(ClusterConfigurationConfiguration.HTTP_FOLLOW_REDIRECTS_PROPERTY), eq(Boolean.class),
|
||||
eq(ClusterConfigurationConfiguration.DEFAULT_HTTP_FOLLOW_REDIRECTS));
|
||||
verify(configuration, times(1)).resolveClientHttpRequestInterceptors(eq(true));
|
||||
verify(configuration, times(1)).resolveRestTemplateConfigurers();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -635,14 +673,15 @@ public class EnableClusterConfigurationUnitTests {
|
||||
|
||||
ClientCache mockClientCache = mock(ClientCache.class);
|
||||
|
||||
ClusterConfigurationConfiguration configuration = spy(new ClusterConfigurationConfiguration());
|
||||
|
||||
doReturn(Collections.emptyList()).when(configuration).resolveClientHttpRequestInterceptors(anyBoolean());
|
||||
doReturn(Collections.emptyList()).when(configuration).resolveRestTemplateConfigurers();
|
||||
|
||||
Environment environment = mock(Environment.class);
|
||||
|
||||
when(environment.getProperty(anyString(), eq(Boolean.class), anyBoolean())).thenReturn(false);
|
||||
|
||||
ClusterConfigurationConfiguration configuration = spy(new ClusterConfigurationConfiguration());
|
||||
|
||||
doReturn(Collections.emptyList()).when(configuration).resolveClientHttpRequestInterceptors();
|
||||
|
||||
configuration.setManagementHttpHost("skullbox");
|
||||
configuration.setManagementHttpPort(-1);
|
||||
configuration.setManagementUseHttp(true);
|
||||
@@ -661,11 +700,7 @@ public class EnableClusterConfigurationUnitTests {
|
||||
.isEqualTo("https://skullbox/gemfire/v1");
|
||||
|
||||
verifyZeroInteractions(mockClientCache);
|
||||
|
||||
verify(configuration, times(1)).resolveClientHttpRequestInterceptors();
|
||||
|
||||
verify(environment, times(1))
|
||||
.getProperty(eq(ClusterConfigurationConfiguration.HTTP_FOLLOW_REDIRECTS_PROPERTY), eq(Boolean.class),
|
||||
eq(ClusterConfigurationConfiguration.DEFAULT_HTTP_FOLLOW_REDIRECTS));
|
||||
verify(configuration, times(1)).resolveClientHttpRequestInterceptors(eq(false));
|
||||
verify(configuration, times(1)).resolveRestTemplateConfigurers();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,12 +33,12 @@ import java.net.URI;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.geode.management.internal.security.ResourceConstants;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.apache.geode.management.internal.security.ResourceConstants;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
@@ -54,7 +54,6 @@ import org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockOb
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
@@ -95,16 +94,12 @@ public class EnableClusterConfigurationWithSecurityIntegrationTests {
|
||||
private Authenticator authenticator;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("loggingAwareClientHttpRequestInterceptor")
|
||||
private ClientHttpRequestInterceptor loggingAwareClientHttpRequestInterceptor;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("securityAwareClientHttpRequestInterceptor")
|
||||
private ClientHttpRequestInterceptor securityAwareClientHttpRequestInterceptor;
|
||||
private AutoConfiguredAuthenticationConfiguration configuration;
|
||||
|
||||
@Autowired
|
||||
private ClusterSchemaObjectInitializer initializer;
|
||||
|
||||
// TODO: Replace with STDG.
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T getFieldValue(Object target, String fieldName) throws NoSuchFieldException {
|
||||
|
||||
@@ -123,11 +118,8 @@ public class EnableClusterConfigurationWithSecurityIntegrationTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
|
||||
assertThat(this.authenticator).isNotNull();
|
||||
assertThat(this.initializer).isNotNull();
|
||||
assertThat(this.loggingAwareClientHttpRequestInterceptor).isNotNull();
|
||||
assertThat(this.securityAwareClientHttpRequestInterceptor).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -156,8 +148,7 @@ public class EnableClusterConfigurationWithSecurityIntegrationTests {
|
||||
RestTemplate restTemplate = getFieldValue(template, "restTemplate");
|
||||
|
||||
assertThat(restTemplate).isNotNull();
|
||||
assertThat(restTemplate.getInterceptors()).containsExactly(this.securityAwareClientHttpRequestInterceptor,
|
||||
this.loggingAwareClientHttpRequestInterceptor);
|
||||
assertThat(restTemplate.getInterceptors()).hasSize(2);
|
||||
assertThat(restTemplate.getRequestFactory()).isInstanceOf(InterceptingClientHttpRequestFactory.class);
|
||||
}
|
||||
|
||||
@@ -178,7 +169,7 @@ public class EnableClusterConfigurationWithSecurityIntegrationTests {
|
||||
when(mockHttpRequest.getHeaders()).thenReturn(httpHeaders);
|
||||
when(mockHttpRequest.getURI()).thenReturn(uri);
|
||||
|
||||
this.securityAwareClientHttpRequestInterceptor.intercept(mockHttpRequest, body, mockClientHttpRequestExecution);
|
||||
this.configuration.securityAwareClientHttpRequestInterceptor().intercept(mockHttpRequest, body, mockClientHttpRequestExecution);
|
||||
|
||||
assertThat(httpHeaders).containsKeys(ResourceConstants.USER_NAME, ResourceConstants.PASSWORD);
|
||||
assertThat(httpHeaders.getFirst(ResourceConstants.USER_NAME)).isEqualTo("skeletor");
|
||||
|
||||
Reference in New Issue
Block a user