DATAGEODE-196 - Add support for dynamic HTTP client port configuration based on protocol/scheme.

This commit is contained in:
John Blum
2019-05-20 15:12:32 -07:00
parent ac8da3b238
commit 1b4eec323a
9 changed files with 245 additions and 38 deletions

View File

@@ -28,6 +28,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.gemfire.util.NetworkUtils;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
@@ -55,7 +56,7 @@ public class RestHttpGemfireAdminTemplateBuilderUnitTests {
private ClientCache mockClientCache;
@SuppressWarnings("unchecked")
private <T> T resolveFieldValue(Object target, String fieldName) throws NoSuchFieldException {
private <T> T getFieldValue(Object target, String fieldName) throws NoSuchFieldException {
Field field = ReflectionUtils.findField(target.getClass(), fieldName, ClientHttpRequestFactory.class);
@@ -100,7 +101,7 @@ public class RestHttpGemfireAdminTemplateBuilderUnitTests {
.isInstanceOf(InterceptingClientHttpRequestFactory.class);
ClientHttpRequestFactory clientHttpRequestFactory =
resolveFieldValue(template.<RestTemplate>getRestOperations().getRequestFactory(), "requestFactory");
getFieldValue(template.<RestTemplate>getRestOperations().getRequestFactory(), "requestFactory");
assertThat(clientHttpRequestFactory).isInstanceOf(FollowRedirectsSimpleClientHttpRequestFactory.class);
assertThat(((FollowRedirectsSimpleClientHttpRequestFactory) clientHttpRequestFactory).isFollowRedirects())
@@ -114,7 +115,7 @@ public class RestHttpGemfireAdminTemplateBuilderUnitTests {
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Port [%d] must be greater than 0 and less than 65536", port);
assertThat(expected).hasMessage(NetworkUtils.INVALID_NO_EPHEMERAL_PORT_MESSAGE, port);
assertThat(expected).hasNoCause();
throw expected;
@@ -139,7 +140,7 @@ public class RestHttpGemfireAdminTemplateBuilderUnitTests {
.build();
assertThat(template).isNotNull();
assertThat(template.getManagementRestApiUrl()).isEqualTo("https://localhost:7070/gemfire/v1");
assertThat(template.getManagementRestApiUrl()).isEqualTo("https://localhost/gemfire/v1");
}
@Test

View File

@@ -65,9 +65,14 @@ import org.springframework.web.client.RestTemplate;
* @see org.junit.Test
* @see org.mockito.Mock
* @see org.mockito.Mockito
* @see org.apache.geode.cache.Region
* @see org.apache.geode.cache.client.ClientCache
* @see org.apache.geode.cache.query.Index
* @see org.springframework.data.gemfire.config.admin.remote.RestHttpGemfireAdminTemplate
* @see org.springframework.http.HttpHeaders
* @see org.springframework.http.client.ClientHttpRequestFactory
* @see org.springframework.http.client.ClientHttpRequestInterceptor
* @see org.springframework.http.client.InterceptingClientHttpRequestFactory
* @see org.springframework.web.client.RestOperations
* @see org.springframework.web.client.RestTemplate
* @since 2.0.0
@@ -111,7 +116,7 @@ public class RestHttpGemfireAdminTemplateUnitTests {
}
@SuppressWarnings("unchecked")
private <T> T resolveFieldValue(Object target, String fieldName) throws NoSuchFieldException {
private <T> T getFieldValue(Object target, String fieldName) throws NoSuchFieldException {
Field field = ReflectionUtils.findField(target.getClass(), fieldName, ClientHttpRequestFactory.class);
@@ -134,12 +139,11 @@ public class RestHttpGemfireAdminTemplateUnitTests {
assertThat(template).isNotNull();
assertThat(template.getClientCache()).isSameAs(this.mockClientCache);
assertThat(template.getManagementRestApiUrl())
.isEqualTo(String.format(RestHttpGemfireAdminTemplate.MANAGEMENT_REST_API_URL_TEMPLATE,
RestHttpGemfireAdminTemplate.DEFAULT_SCHEME, RestHttpGemfireAdminTemplate.DEFAULT_HOST,
RestHttpGemfireAdminTemplate.DEFAULT_PORT));
.isEqualTo(String.format(RestHttpGemfireAdminTemplate.MANAGEMENT_REST_API_NO_PORT_URL_TEMPLATE,
RestHttpGemfireAdminTemplate.DEFAULT_SCHEME, RestHttpGemfireAdminTemplate.DEFAULT_HOST));
assertThat(template.<RestOperations>getRestOperations()).isInstanceOf(RestTemplate.class);
RestTemplate restTemplate = (RestTemplate) template.getRestOperations();
RestTemplate restTemplate = template.getRestOperations();
ClientHttpRequestFactory clientHttpRequestFactory = restTemplate.getRequestFactory();
@@ -176,7 +180,7 @@ public class RestHttpGemfireAdminTemplateUnitTests {
assertThat(clientHttpRequestFactory).isInstanceOf(InterceptingClientHttpRequestFactory.class);
clientHttpRequestFactory = this.resolveFieldValue(clientHttpRequestFactory, "requestFactory");
clientHttpRequestFactory = this.getFieldValue(clientHttpRequestFactory, "requestFactory");
assertThat(clientHttpRequestFactory).isInstanceOf(FollowRedirectsSimpleClientHttpRequestFactory.class);
assertThat(((FollowRedirectsSimpleClientHttpRequestFactory) clientHttpRequestFactory).isFollowRedirects())
@@ -251,9 +255,9 @@ public class RestHttpGemfireAdminTemplateUnitTests {
.isEqualTo(String.format(RestHttpGemfireAdminTemplate.MANAGEMENT_REST_API_URL_TEMPLATE,
"https", "cardboardbox", 443));
assertThat(this.template.resolveManagementRestApiUrl("ftp", "jambox", 21))
assertThat(this.template.resolveManagementRestApiUrl("ftp", "lunchbox", 21))
.isEqualTo(String.format(RestHttpGemfireAdminTemplate.MANAGEMENT_REST_API_URL_TEMPLATE,
"ftp", "jambox", 21));
"ftp", "lunchbox", 21));
assertThat(this.template.resolveManagementRestApiUrl("sftp", "mailbox", 22))
.isEqualTo(String.format(RestHttpGemfireAdminTemplate.MANAGEMENT_REST_API_URL_TEMPLATE,
@@ -264,6 +268,26 @@ public class RestHttpGemfireAdminTemplateUnitTests {
"smtp", "skullbox", 25));
}
@Test
public void resolvesManagementRestApiUrlCorrectlyWhenInvalidPortIsGiven() {
assertThat(this.template.resolveManagementRestApiUrl("https", "box", -1))
.isEqualTo(String.format(RestHttpGemfireAdminTemplate.MANAGEMENT_REST_API_NO_PORT_URL_TEMPLATE,
"https", "box"));
assertThat(this.template.resolveManagementRestApiUrl("http", "dropbox", 0))
.isEqualTo(String.format(RestHttpGemfireAdminTemplate.MANAGEMENT_REST_API_NO_PORT_URL_TEMPLATE,
"http", "dropbox"));
assertThat(this.template.resolveManagementRestApiUrl("https", "jambox", 65536))
.isEqualTo(String.format(RestHttpGemfireAdminTemplate.MANAGEMENT_REST_API_NO_PORT_URL_TEMPLATE,
"https", "jambox"));
assertThat(this.template.resolveManagementRestApiUrl("http", "shoebox", 101123))
.isEqualTo(String.format(RestHttpGemfireAdminTemplate.MANAGEMENT_REST_API_NO_PORT_URL_TEMPLATE,
"http", "shoebox"));
}
@Test
@SuppressWarnings("unchecked")
public void createIndexCallsGemFireManagementRestApi() {
@@ -276,7 +300,7 @@ public class RestHttpGemfireAdminTemplateUnitTests {
assertThat(requestEntity).isNotNull();
assertThat(requestEntity.getMethod()).isEqualTo(HttpMethod.POST);
assertThat(requestEntity.getUrl()).isEqualTo(URI.create("https://localhost:7070/gemfire/v1/indexes"));
assertThat(requestEntity.getUrl()).isEqualTo(URI.create("https://localhost/gemfire/v1/indexes"));
HttpHeaders headers = requestEntity.getHeaders();
@@ -316,7 +340,7 @@ public class RestHttpGemfireAdminTemplateUnitTests {
assertThat(requestEntity).isNotNull();
assertThat(requestEntity.getMethod()).isEqualTo(HttpMethod.POST);
assertThat(requestEntity.getUrl()).isEqualTo(URI.create("https://localhost:7070/gemfire/v1/regions"));
assertThat(requestEntity.getUrl()).isEqualTo(URI.create("https://localhost/gemfire/v1/regions"));
HttpHeaders headers = requestEntity.getHeaders();

View File

@@ -90,7 +90,7 @@ public class ClusterConfigurationWithClientHttpRequestInterceptorsIntegrationTes
private List<ClientHttpRequestInterceptor> clientHttpRequestInterceptors;
@SuppressWarnings("unchecked")
private <T> T resolveFieldValue(Object target, String fieldName) throws NoSuchFieldException {
private <T> T getFieldValue(Object target, String fieldName) throws NoSuchFieldException {
Field field = ReflectionUtils.findField(target.getClass(), fieldName);
@@ -138,7 +138,7 @@ public class ClusterConfigurationWithClientHttpRequestInterceptorsIntegrationTes
RestHttpGemfireAdminTemplate template = schemaObjectContext.getGemfireAdminOperations();
RestTemplate restTemplate = resolveFieldValue(template, "restTemplate");
RestTemplate restTemplate = getFieldValue(template, "restTemplate");
assertThat(restTemplate).isNotNull();
assertThat(restTemplate.getInterceptors())

View File

@@ -68,10 +68,15 @@ import org.springframework.web.client.RestTemplate;
* @see org.junit.Test
* @see org.mockito.Mock
* @see org.mockito.Mockito
* @see org.apache.geode.cache.Cache
* @see org.apache.geode.cache.client.ClientCache
* @see org.springframework.beans.factory.BeanFactory
* @see org.springframework.core.env.Environment
* @see org.springframework.core.type.AnnotationMetadata
* @see org.springframework.data.gemfire.config.admin.GemfireAdminOperations
* @see org.springframework.data.gemfire.config.annotation.ClusterConfigurationConfiguration
* @see org.springframework.data.gemfire.config.annotation.EnableClusterConfiguration
* @see org.springframework.web.client.RestTemplate
* @since 2.0.1
*/
public class EnableClusterConfigurationUnitTests {
@@ -99,7 +104,7 @@ public class EnableClusterConfigurationUnitTests {
}
@SuppressWarnings("unchecked")
private <T> T resolveFieldValue(Object target, String fieldName) throws NoSuchFieldException {
private <T> T getFieldValue(Object target, String fieldName) throws NoSuchFieldException {
Field field = ReflectionUtils.findField(target.getClass(), fieldName);
@@ -509,7 +514,7 @@ public class EnableClusterConfigurationUnitTests {
RestHttpGemfireAdminTemplate template = (RestHttpGemfireAdminTemplate) operations;
RestTemplate restTemplate = resolveFieldValue(template, "restTemplate");
RestTemplate restTemplate = getFieldValue(template, "restTemplate");
assertThat(restTemplate).isNotNull();
assertThat(restTemplate.getInterceptors()).isEmpty();
@@ -547,14 +552,13 @@ public class EnableClusterConfigurationUnitTests {
assertThat(configuration.resolveManagementRequireHttps()).isTrue();
assertThat(configuration.resolveManagementUseHttp()).isTrue();
GemfireAdminOperations operations =
configuration.resolveGemfireAdminOperations(environment, mockClientCache);
GemfireAdminOperations operations = configuration.resolveGemfireAdminOperations(environment, mockClientCache);
assertThat(operations).isInstanceOf(RestHttpGemfireAdminTemplate.class);
RestHttpGemfireAdminTemplate template = (RestHttpGemfireAdminTemplate) operations;
RestTemplate restTemplate = resolveFieldValue(template, "restTemplate");
RestTemplate restTemplate = getFieldValue(template, "restTemplate");
assertThat(restTemplate).isNotNull();
assertThat(restTemplate.getInterceptors()).isEmpty();
@@ -598,21 +602,20 @@ public class EnableClusterConfigurationUnitTests {
assertThat(configuration.resolveManagementRequireHttps()).isFalse();
assertThat(configuration.resolveManagementUseHttp()).isTrue();
GemfireAdminOperations operations =
configuration.resolveGemfireAdminOperations(environment, mockClientCache);
GemfireAdminOperations operations = configuration.resolveGemfireAdminOperations(environment, mockClientCache);
assertThat(operations).isInstanceOf(RestHttpGemfireAdminTemplate.class);
RestHttpGemfireAdminTemplate template = (RestHttpGemfireAdminTemplate) operations;
RestTemplate restTemplate = resolveFieldValue(template, "restTemplate");
RestTemplate restTemplate = getFieldValue(template, "restTemplate");
assertThat(restTemplate).isNotNull();
assertThat(restTemplate.getInterceptors()).containsExactly(mockInterceptorOne, mockInterceptorTwo);
assertThat(restTemplate.getRequestFactory()).isInstanceOf(InterceptingClientHttpRequestFactory.class);
FollowRedirectsSimpleClientHttpRequestFactory clientHttpRequestFactory =
resolveFieldValue(restTemplate.getRequestFactory(), "requestFactory");
getFieldValue(restTemplate.getRequestFactory(), "requestFactory");
assertThat(clientHttpRequestFactory.isFollowRedirects()).isTrue();
@@ -624,4 +627,43 @@ public class EnableClusterConfigurationUnitTests {
.getProperty(eq(ClusterConfigurationConfiguration.HTTP_FOLLOW_REDIRECTS_PROPERTY), eq(Boolean.class),
eq(ClusterConfigurationConfiguration.DEFAULT_HTTP_FOLLOW_REDIRECTS));
}
@Test
public void resolvesNewRestHttpGemfireAdminOperationsAvoidsSettingInvalidPort() throws Exception {
ClientCache mockClientCache = mock(ClientCache.class);
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);
assertThat(configuration.resolveManagementHttpHost()).isEqualTo("skullbox");
assertThat(configuration.resolveManagementHttpPort()).isEqualTo(-1);
assertThat(configuration.resolveManagementUseHttp()).isTrue();
GemfireAdminOperations operations = configuration.resolveGemfireAdminOperations(environment, mockClientCache);
assertThat(operations).isInstanceOf(RestHttpGemfireAdminTemplate.class);
RestHttpGemfireAdminTemplate template = (RestHttpGemfireAdminTemplate) operations;
assertThat(this.<String>getFieldValue(template, "managementRestApiUrl"))
.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));
}
}

View File

@@ -104,7 +104,7 @@ public class EnableClusterConfigurationWithSecurityIntegrationTests {
private ClusterSchemaObjectInitializer initializer;
@SuppressWarnings("unchecked")
private <T> T resolveFieldValue(Object target, String fieldName) throws NoSuchFieldException {
private <T> T getFieldValue(Object target, String fieldName) throws NoSuchFieldException {
Field field = ReflectionUtils.findField(target.getClass(), fieldName);
@@ -151,7 +151,7 @@ public class EnableClusterConfigurationWithSecurityIntegrationTests {
RestHttpGemfireAdminTemplate template = schemaObjectContext.getGemfireAdminOperations();
RestTemplate restTemplate = resolveFieldValue(template, "restTemplate");
RestTemplate restTemplate = getFieldValue(template, "restTemplate");
assertThat(restTemplate).isNotNull();
assertThat(restTemplate.getInterceptors()).containsExactly(this.securityAwareClientHttpRequestInterceptor,

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2018 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.util;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
/**
* Unit Tests for {@link NetworkUtils}.
*
* @author John Blum
* @see org.junit.Test
* @see org.springframework.data.gemfire.util.NetworkUtils
* @since 2.2.0
*/
public class NetworkUtilsUnitTests {
@Test
public void invalidPortMessageIsCorrect() {
assertThat(String.format(NetworkUtils.INVALID_PORT_MESSAGE, -1))
.isEqualTo("Port [-1] must be greater than equal to 0 and less than 65536");
}
@Test
public void invalidNonEphemeralPortMessageIsCorrect() {
assertThat(String.format(NetworkUtils.INVALID_NO_EPHEMERAL_PORT_MESSAGE, -1))
.isEqualTo("Port [-1] must be greater than 0 and less than 65536");
}
@Test
public void withValidPortsReturnsTrue() {
for (int port = 0; port < 65536; port++) {
assertThat(NetworkUtils.isValidPort(port)).isTrue();
}
}
@Test
public void withInvalidPortsReturnsFalse() {
assertThat(NetworkUtils.isValidPort(-21)).isFalse();
assertThat(NetworkUtils.isValidPort(-1)).isFalse();
assertThat(NetworkUtils.isValidPort(65536)).isFalse();
assertThat(NetworkUtils.isValidPort(99199)).isFalse();
}
@Test
public void withValidNonEphemeralPortsReturnsTrue() {
for (int port = 1; port < 65536; port++) {
assertThat(NetworkUtils.isValidNonEphemeralPort(port)).isTrue();
}
}
@Test
public void withInvalidNonEphemeralPortsReturnsFalse() {
assertThat(NetworkUtils.isValidNonEphemeralPort(-21)).isFalse();
assertThat(NetworkUtils.isValidNonEphemeralPort(-1)).isFalse();
assertThat(NetworkUtils.isValidNonEphemeralPort(0)).isFalse();
assertThat(NetworkUtils.isValidNonEphemeralPort(65536)).isFalse();
assertThat(NetworkUtils.isValidNonEphemeralPort(99199)).isFalse();
}
}