Rename modules.

Rename geode-spring-boot to spring-geode.

Rename geode-spring-boot-autoconfigure to spring-geode-autoconfigure.

Rename geode-spring-boot-starter to spring-geode-starter.

Rename gemfire-spring-boot-starter to spring-gemfire-starter.

Resolves GitHub Issue #6.
This commit is contained in:
John Blum
2018-06-19 18:35:09 -07:00
parent c830817ed3
commit 330dc7fec3
84 changed files with 5 additions and 5 deletions

View File

@@ -0,0 +1,69 @@
/*
* 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.geode.config.annotation;
import static org.assertj.core.api.Assertions.assertThat;
import org.apache.geode.cache.GemFireCache;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.gemfire.config.annotation.PeerCacheApplication;
import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport;
import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration tests for {@link UseDistributedSystemId} and {@link DistributedSystemIdConfiguration}.
*
* @author John Blum
* @see org.junit.Test
* @see org.apache.geode.cache.GemFireCache
* @see org.springframework.data.gemfire.config.annotation.PeerCacheApplication
* @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport
* @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects
* @see org.springframework.geode.config.annotation.DistributedSystemIdConfiguration
* @see org.springframework.geode.config.annotation.UseDistributedSystemId
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringRunner
* @since 1.0.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration
@SuppressWarnings("unused")
public class DistributedSystemIdConfigurationIntegrationTests extends IntegrationTestsSupport {
@Autowired
private GemFireCache gemfireCache;
@Test
public void distributedSystemIdWasConfiguredCorrectly() {
assertThat(this.gemfireCache).isNotNull();
assertThat(this.gemfireCache.getDistributedSystem()).isNotNull();
assertThat(this.gemfireCache.getDistributedSystem().getProperties()).isNotNull();
assertThat(this.gemfireCache.getDistributedSystem().getProperties().getProperty("distributed-system-id"))
.isEqualTo("42");
}
@PeerCacheApplication
@EnableGemFireMockObjects
@UseDistributedSystemId(42)
static class TestConfiguration { }
}

View File

@@ -0,0 +1,126 @@
/*
* 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.geode.config.annotation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.client.ClientCache;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.gemfire.client.ClientCacheFactoryBean;
import org.springframework.data.gemfire.config.annotation.ClientCacheApplication;
import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport;
import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration tests for {@link EnableDurableClient} and {@link DurableClientConfiguration}.
*
* @author John Blum
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.apache.geode.cache.GemFireCache
* @see org.apache.geode.cache.client.ClientCache
* @see org.springframework.context.ConfigurableApplicationContext
* @see org.springframework.data.gemfire.client.ClientCacheFactoryBean
* @see org.springframework.data.gemfire.config.annotation.ClientCacheApplication
* @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport
* @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects
* @see org.springframework.geode.config.annotation.DurableClientConfiguration
* @see org.springframework.geode.config.annotation.EnableDurableClient
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringRunner
* @since 1.0.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration
@SuppressWarnings("unused")
public class DurableClientConfigurationIntegrationTests extends IntegrationTestsSupport {
private static final AtomicReference<ConfigurableApplicationContext> applicationContextReference =
new AtomicReference<>(null);
private static final AtomicReference<ClientCache> clientCacheReference =
new AtomicReference<>(null);
@Autowired
private ConfigurableApplicationContext applicationContext;
@Autowired
private GemFireCache gemfireCache;
@Autowired
private ClientCacheFactoryBean clientCacheFactoryBean;
@AfterClass
public static void closeApplicationContext() {
Optional.ofNullable(applicationContextReference.get()).ifPresent(ConfigurableApplicationContext::close);
ClientCache clientCache = clientCacheReference.get();
assertThat(clientCache).isNotNull();
verify(clientCache, times(1)).close(eq(true));
}
@Test
public void durableClientWasConfiguredSuccessfully() {
assertThat(this.gemfireCache).isInstanceOf(ClientCache.class);
assertThat(this.gemfireCache.getDistributedSystem()).isNotNull();
assertThat(this.gemfireCache.getDistributedSystem().getProperties()).isNotNull();
assertThat(this.gemfireCache.getDistributedSystem().getProperties().getProperty("durable-client-id"))
.isEqualTo("abc123");
assertThat(this.gemfireCache.getDistributedSystem().getProperties().getProperty("durable-client-timeout"))
.isEqualTo("600");
applicationContextReference.set(this.applicationContext);
clientCacheReference.set((ClientCache) this.gemfireCache);
}
@Test
public void setClientCacheFactoryBeanSetsKeepAliveOnClose() {
assertThat(this.clientCacheFactoryBean).isNotNull();
assertThat(this.clientCacheFactoryBean.isKeepAlive()).isTrue();
}
@Test
public void setClientCacheFactoryBeanSetsReadyForEventOnContextRefreshedEvent() {
assertThat(this.clientCacheFactoryBean).isNotNull();
assertThat(this.clientCacheFactoryBean.isReadyForEvents()).isTrue();
}
@ClientCacheApplication
@EnableGemFireMockObjects
@EnableDurableClient(id = "abc123", timeout = 600)
static class TestConfiguration { }
}

View File

@@ -0,0 +1,68 @@
/*
* 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.geode.config.annotation;
import static org.assertj.core.api.Assertions.assertThat;
import org.apache.geode.cache.GemFireCache;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.gemfire.config.annotation.ClientCacheApplication;
import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport;
import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration tests for {@link UseGroups} and {@link GroupsConfiguration}.
*
* @author John Blum
* @see org.junit.Test
* @see org.apache.geode.cache.GemFireCache
* @see org.springframework.data.gemfire.config.annotation.ClientCacheApplication
* @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport
* @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringRunner
* @since 1.0.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration
@SuppressWarnings("unused")
public class GroupsConfigurationIntegrationTests extends IntegrationTestsSupport {
@Autowired
private GemFireCache gemfireCache;
@Test
public void groupsAreConfiguredCorrectly() {
assertThat(this.gemfireCache).isNotNull();
assertThat(this.gemfireCache.getDistributedSystem()).isNotNull();
assertThat(this.gemfireCache.getDistributedSystem().getProperties()).isNotNull();
assertThat(this.gemfireCache.getDistributedSystem().getProperties().getProperty("groups"))
.isEqualTo("MockGroup,TestGroup");
}
@ClientCacheApplication
@EnableGemFireMockObjects
@UseGroups({ "MockGroup", "TestGroup" })
static class TestConfiguration { }
}

View File

@@ -0,0 +1,66 @@
/*
* 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.geode.config.annotation;
import static org.assertj.core.api.Assertions.assertThat;
import org.apache.geode.cache.GemFireCache;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.gemfire.config.annotation.ClientCacheApplication;
import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport;
import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration tests for {@link UseMemberName} and {@link MemberNameConfiguration}.
*
* @author John Blum
* @see org.apache.geode.cache.GemFireCache
* @see org.springframework.data.gemfire.config.annotation.ClientCacheApplication
* @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport
* @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringRunner
* @since 1.0.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration
@SuppressWarnings("unused")
public class MemberNameConfigurationIntegrationTests extends IntegrationTestsSupport {
@Autowired
private GemFireCache gemfireCache;
@Test
public void memberNameWasConfiguredCorrectly() {
assertThat(this.gemfireCache).isNotNull();
assertThat(this.gemfireCache.getDistributedSystem()).isNotNull();
assertThat(this.gemfireCache.getDistributedSystem().getProperties()).isNotNull();
assertThat(this.gemfireCache.getDistributedSystem().getProperties().getProperty("name"))
.isEqualTo("TestClient");
}
@ClientCacheApplication
@EnableGemFireMockObjects
@UseMemberName("TestClient")
static class TestConfiguration { }
}

View File

@@ -0,0 +1,515 @@
/*
* 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.geode.core.env;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import java.net.URL;
import java.util.Arrays;
import java.util.Properties;
import java.util.Set;
import org.junit.Test;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.geode.core.env.support.CloudCacheService;
import org.springframework.geode.core.env.support.Service;
import org.springframework.geode.core.env.support.User;
/**
* Unit tests for {@link VcapPropertySource}.
*
* @author John Blum
* @see java.util.Properties
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.core.env.Environment
* @see org.springframework.core.env.PropertiesPropertySource
* @see org.springframework.core.env.PropertySource
* @see org.springframework.geode.core.env.VcapPropertySource
* @since 1.0.0
*/
public class VcapPropertySourceUnitTests {
@Test
public void fromEnvironmentIsSuccessful() {
ConfigurableEnvironment mockEnvironment = mock(ConfigurableEnvironment.class);
MutablePropertySources propertySources = spy(new MutablePropertySources());
PropertySource mockVcapPropertySource = mock(EnumerablePropertySource.class);
when(mockEnvironment.getPropertySources()).thenReturn(propertySources);
doReturn(mockVcapPropertySource).when(propertySources).get(eq("vcap"));
when(mockVcapPropertySource.getName()).thenReturn("vcap");
when(mockVcapPropertySource.containsProperty(anyString())).thenReturn(true);
VcapPropertySource propertySource = VcapPropertySource.from(mockEnvironment);
assertThat(propertySource).isNotNull();
assertThat(propertySource.getSource()).isEqualTo(mockVcapPropertySource);
verify(mockEnvironment, times(1)).getPropertySources();
verify(propertySources, times(1)).get(eq("vcap"));
verify(mockVcapPropertySource, times(1))
.containsProperty(eq("vcap.application.name"));
verify(mockVcapPropertySource, times(1))
.containsProperty(eq("vcap.application.uris"));
}
@Test(expected = IllegalArgumentException.class)
public void fromNonConfigurableEnvironmentThrowsIllegalArgumentException() {
Environment mockEnvironment = mock(Environment.class);
try {
VcapPropertySource.from(mockEnvironment);
}
catch (IllegalArgumentException expected) {
assertThat(expected)
.hasMessage("Environment was not configurable or does not contain an enumerable [vcap] PropertySource");
assertThat(expected).hasNoCause();
throw expected;
}
finally {
verifyZeroInteractions(mockEnvironment);
}
}
@Test(expected = IllegalArgumentException.class)
public void fromConfigurableEnvironmentWithNoVcapPropertySourceThrowsIllegalArgumentException() {
ConfigurableEnvironment mockEnvironment = mock(ConfigurableEnvironment.class);
MutablePropertySources propertySources = spy(new MutablePropertySources());
when(mockEnvironment.getPropertySources()).thenReturn(propertySources);
doReturn(null).when(propertySources).get(anyString());
try {
VcapPropertySource.from(mockEnvironment);
}
catch (IllegalArgumentException expected) {
assertThat(expected)
.hasMessage("Environment was not configurable or does not contain an enumerable [vcap] PropertySource");
assertThat(expected).hasNoCause();
throw expected;
}
finally {
verify(mockEnvironment, times(1)).getPropertySources();
verify(propertySources, times(1)).get(eq("vcap"));
}
}
@Test(expected = IllegalArgumentException.class)
public void fromConfigurableEnvironmentWithNonEnumerableVcapPropertySourceThrowsIllegalArgumentException() {
ConfigurableEnvironment mockEnvironment = mock(ConfigurableEnvironment.class);
MutablePropertySources propertySources = spy(new MutablePropertySources());
PropertySource mockPropertySource = mock(PropertySource.class);
when(mockEnvironment.getPropertySources()).thenReturn(propertySources);
doReturn(mockPropertySource).when(propertySources).get(eq("vcap"));
when(mockPropertySource.getName()).thenReturn("vcap");
try {
VcapPropertySource.from(mockEnvironment);
}
catch (IllegalArgumentException expected) {
assertThat(expected)
.hasMessage("A valid EnumerablePropertySource named [vcap] with VCAP properties is required",
mockEnvironment);
assertThat(expected).hasNoCause();
throw expected;
}
finally {
verify(mockEnvironment, times(1)).getPropertySources();
verify(propertySources, times(1)).get(eq("vcap"));
verify(mockPropertySource, times(1)).getName();
verifyNoMoreInteractions(mockPropertySource);
}
}
@Test(expected = IllegalArgumentException.class)
public void fromConfigurableEnvironmentWithEnumerableVcapPropertySourceHavingNoRequiredPropertiesThrowsIllegalArgumentException() {
ConfigurableEnvironment mockEnvironment = mock(ConfigurableEnvironment.class);
MutablePropertySources propertySources = spy(new MutablePropertySources());
PropertySource mockPropertySource = mock(EnumerablePropertySource.class);
when(mockEnvironment.getPropertySources()).thenReturn(propertySources);
doReturn(mockPropertySource).when(propertySources).get(eq("vcap"));
when(mockPropertySource.getName()).thenReturn("vcap");
when(mockPropertySource.containsProperty(eq("vcap.application.name"))).thenReturn(true);
when(mockPropertySource.containsProperty(eq("vcap.application.uris"))).thenReturn(false);
try {
VcapPropertySource.from(mockEnvironment);
}
catch (IllegalArgumentException expected) {
assertThat(expected)
.hasMessage("A valid EnumerablePropertySource named [vcap] with VCAP properties is required",
mockEnvironment);
assertThat(expected).hasNoCause();
throw expected;
}
finally {
verify(mockEnvironment, times(1)).getPropertySources();
verify(propertySources, times(1)).get(eq("vcap"));
verify(mockPropertySource, times(1)).getName();
verify(mockPropertySource, times(1))
.containsProperty(eq("vcap.application.name"));
verify(mockPropertySource, times(1))
.containsProperty(eq("vcap.application.uris"));
}
}
@Test
public void fromPropertiesIsSuccessful() {
Properties vcap = new Properties();
vcap.setProperty("vcap.application.name", "testApp");
vcap.setProperty("vcap.application.uris", "boot-app.apps.cloud.net");
VcapPropertySource propertySource = VcapPropertySource.from(vcap);
assertThat(propertySource).isNotNull();
assertThat(propertySource.getSource()).isInstanceOf(PropertiesPropertySource.class);
assertThat(propertySource.getProperty("vcap.application.name")).isEqualTo("testApp");
assertThat(propertySource.getProperty("vcap.application.uris")).isEqualTo("boot-app.apps.cloud.net");
}
@Test(expected = IllegalArgumentException.class)
public void fromPropertiesHavingNoRequiredProperties() {
try {
VcapPropertySource.from(new Properties());
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Properties are required");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test(expected = IllegalArgumentException.class)
public void fromNullPropertiesThrowsIllegalArgumentException() {
try {
VcapPropertySource.from((Properties) null);
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Properties are required");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test
public void findAllVcapApplicationPropertiesIsSuccessful() {
EnumerablePropertySource mockPropertySource = mock(EnumerablePropertySource.class);
String[] propertyNames = {
"vcap.services.jblum-pcc.credentials.locators",
"vcap.services.jblum-pcc.credentials.users",
"vcap.application.host",
"vcap.application.name",
"vcap.services.jblum-pcc.name",
"vcap.application.port",
"vcap.application.space_name",
"vcap.services.jblum-pcc.plan",
"vcap.application.uris",
"vcap.services.jblum-pcc.tags"
};
when(mockPropertySource.getName()).thenReturn("vcap");
when(mockPropertySource.containsProperty(anyString())).thenAnswer(invocation ->
Arrays.asList(propertyNames).contains(invocation.<String>getArgument(0)));
when(mockPropertySource.getPropertyNames()).thenReturn(propertyNames);
VcapPropertySource propertySource = VcapPropertySource.from(mockPropertySource);
assertThat(propertySource).isNotNull();
assertThat(propertySource.getSource()).isEqualTo(mockPropertySource);
Set<String> vcapApplicationProperties = propertySource.findAllVcapApplicationProperties();
assertThat(vcapApplicationProperties).isNotNull();
assertThat(vcapApplicationProperties).hasSize(5);
assertThat(vcapApplicationProperties)
.containsExactlyInAnyOrder("vcap.application.host", "vcap.application.name", "vcap.application.port",
"vcap.application.space_name", "vcap.application.uris");
verify(mockPropertySource, times(1)).getName();
verify(mockPropertySource, times(1))
.containsProperty(eq("vcap.application.name"));
verify(mockPropertySource, times(1))
.containsProperty(eq("vcap.application.uris"));
verify(mockPropertySource, times(1)).getPropertyNames();
}
@Test
public void findAllVcapServicesPropertiesIsSuccessful() {
EnumerablePropertySource mockPropertySource = mock(EnumerablePropertySource.class);
String[] propertyNames = {
"vcap.services.jblum-pcc.credentials.locators",
"vcap.services.jblum-pcc.credentials.users",
"vcap.application.host",
"vcap.application.name",
"vcap.services.jblum-pcc.name",
"vcap.application.port",
"vcap.application.space_name",
"vcap.services.jblum-pcc.plan",
"vcap.application.uris",
"vcap.services.jblum-pcc.tags"
};
when(mockPropertySource.getName()).thenReturn("vcap");
when(mockPropertySource.containsProperty(anyString())).thenAnswer(invocation ->
Arrays.asList(propertyNames).contains(invocation.<String>getArgument(0)));
when(mockPropertySource.getPropertyNames()).thenReturn(propertyNames);
VcapPropertySource propertySource = VcapPropertySource.from(mockPropertySource);
assertThat(propertySource).isNotNull();
assertThat(propertySource.getSource()).isEqualTo(mockPropertySource);
Set<String> vcapApplicationProperties = propertySource.findAllVcapServicesProperties();
assertThat(vcapApplicationProperties).isNotNull();
assertThat(vcapApplicationProperties).hasSize(5);
assertThat(vcapApplicationProperties)
.containsExactlyInAnyOrder("vcap.services.jblum-pcc.credentials.locators",
"vcap.services.jblum-pcc.credentials.users", "vcap.services.jblum-pcc.name",
"vcap.services.jblum-pcc.plan", "vcap.services.jblum-pcc.tags");
verify(mockPropertySource, times(1)).getName();
verify(mockPropertySource, times(1))
.containsProperty(eq("vcap.application.name"));
verify(mockPropertySource, times(1))
.containsProperty(eq("vcap.application.uris"));
verify(mockPropertySource, times(1)).getPropertyNames();
}
@Test
public void findFirstCloudCacheServiceNameReturnsServiceName() {
Properties vcap = new Properties();
vcap.setProperty("vcap.application.name", "boot-example");
vcap.setProperty("vcap.services.test-pcc.name", "test-pcc");
vcap.setProperty("vcap.services.test-pcc.plan", "small");
vcap.setProperty("vcap.services.test-pcc.tags", "pivotal,database,cloudcache,gemfire");
vcap.setProperty("vcap.application.space_name", "outerspace");
vcap.setProperty("vcap.services.jblum-pcc.name", "jblum-pcc");
vcap.setProperty("vcap.services.jblum-pcc.plan", "small");
vcap.setProperty("vcap.services.jblum-pcc.tags", "cloudcache,database,gemfire,pivotal");
vcap.setProperty("vcap.application.uris", "boot-example.boot-apps.apps.cloud.net");
vcap.setProperty("vcap.services.a-pcc.name", "a-pcc");
vcap.setProperty("vcap.services.a-pcc.plan", "huge");
vcap.setProperty("vcap.services.a-pcc.tags", "pivotal,cloudcache,database");
VcapPropertySource propertySource = VcapPropertySource.from(vcap);
assertThat(propertySource).isNotNull();
assertThat(propertySource.findFirstCloudCacheServiceName()).isEqualTo("jblum-pcc");
}
@Test(expected = IllegalStateException.class)
public void findFirstCloudCacheServiceNameWithInvalidTagsThrowsIllegalStateException() {
Properties vcap = new Properties();
vcap.setProperty("vcap.application.name", "boot-example");
vcap.setProperty("vcap.services.test-pcc.name", "test-pcc");
vcap.setProperty("vcap.services.test-pcc.plan", "small");
vcap.setProperty("vcap.services.test-pcc.tags", "pivotal,gemfire,database");
vcap.setProperty("vcap.application.space_name", "outerspace");
vcap.setProperty("vcap.services.a-pcc.tags", "pivotal,cloudcache,database");
vcap.setProperty("vcap.application.uris", "boot-example.boot-apps.apps.cloud.net");
VcapPropertySource propertySource = VcapPropertySource.from(vcap);
assertThat(propertySource).isNotNull();
try {
propertySource.findFirstCloudCacheServiceName();
}
catch (IllegalStateException expected) {
assertThat(expected).hasMessage("No service with tags [cloudcache, gemfire] was found");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test(expected = IllegalStateException.class)
public void findFirstCloudCacheServiceNameWithNoTagsThrowsIllegalStateException() {
Properties vcap = new Properties();
vcap.setProperty("vcap.application.name", "boot-example");
vcap.setProperty("vcap.services.test-pcc.name", "test-pcc");
vcap.setProperty("vcap.services.test-pcc.plan", "small");
vcap.setProperty("vcap.application.space_name", "outerspace");
vcap.setProperty("vcap.application.uris", "boot-example.boot-apps.apps.cloud.net");
VcapPropertySource propertySource = VcapPropertySource.from(vcap);
assertThat(propertySource).isNotNull();
try {
propertySource.findFirstCloudCacheServiceName();
}
catch (IllegalStateException expected) {
assertThat(expected).hasMessage("No service with tags [cloudcache, gemfire] was found");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test
public void findFirstCloudCacheServiceReturnsCloudCacheService() throws Exception {
URL gfshUrl = new URL("http://skullbox:7070/v1/gemfire");
Properties vcap = new Properties();
vcap.setProperty("vcap.services.test-pcc.name", "test-pcc");
vcap.setProperty("vcap.services.test-pcc.plan", "huge");
vcap.setProperty("vcap.application.name", "boot-example");
vcap.setProperty("vcap.services.test-pcc.credentials.locators", "sandbox[1234],toolbox,xbox[6789]");
vcap.setProperty("vcap.application.space_name", "outerspace");
vcap.setProperty("vcap.services.test-pcc.credentials.urls.gfsh", gfshUrl.toExternalForm());
vcap.setProperty("vcap.application.uris", "boot-example.boot-apps.apps.cloud.net");
vcap.setProperty("vcap.services.test-pcc.tags", "pivotal,cloudcache , database, gemfire ");
VcapPropertySource propertySource = VcapPropertySource.from(vcap);
assertThat(propertySource).isNotNull();
CloudCacheService cloudCacheService = propertySource.findFirstCloudCacheService();
assertThat(cloudCacheService).isNotNull();
assertThat(cloudCacheService.getGfshUrl().orElse(null)).isEqualTo(gfshUrl);
assertThat(cloudCacheService.getLocatorList()).containsExactly(
CloudCacheService.Locator.newLocator("sandbox", 1234),
CloudCacheService.Locator.newLocator("toolbox", 10334),
CloudCacheService.Locator.newLocator("xbox", 6789)
);
}
@Test
public void findFirstUserByRoleClusterOperatorReturnsUser() {
Properties vcap = new Properties();
vcap.setProperty("vcap.application.name", "boot-example");
vcap.setProperty("vcap.services.test-pcc.name", "test-pcc");
vcap.setProperty("vcap.services.test-pcc.credentials.users[0].username", "jdoe");
vcap.setProperty("vcap.services.test-pcc.credentials.users[0].roles", "developer,poweruser,seaswab");
vcap.setProperty("vcap.services.test-pcc.credentials.users[0].password", "test");
vcap.setProperty("vcap.services.test-pcc.tags", "pivotal,cloudcache , database, gemfire ");
vcap.setProperty("vcap.application.space_name", "outerspace");
vcap.setProperty("vcap.services.a-pcc.name", "a-pcc");
vcap.setProperty("vcap.services.a-pcc.credentials.users[0].username", "admin");
vcap.setProperty("vcap.services.a-pcc.credentials.users[0].roles", "cluster_admin");
vcap.setProperty("vcap.services.a-pcc.credentials.users[0].password", "p@55w0rd");
vcap.setProperty("vcap.services.a-pcc.credentials.users[1].username", "root");
vcap.setProperty("vcap.services.a-pcc.credentials.users[1].roles", "cluster_operator");
vcap.setProperty("vcap.services.a-pcc.credentials.users[1].password", "p@55w0rd");
vcap.setProperty("vcap.services.a-pcc.tags", "pivotal,cloudcache,database");
vcap.setProperty("vcap.application.uris", "boot-example.boot-apps.apps.cloud.net");
vcap.setProperty("vcap.services.jblum-pcc.name", "jblum-pcc");
vcap.setProperty("vcap.services.jblum-pcc.credentials.users[0].username", "majorTom");
vcap.setProperty("vcap.services.jblum-pcc.credentials.users[0].roles", "cluster_operator,ground_contoller");
vcap.setProperty("vcap.services.jblum-pcc.credentials.users[0].password", "s3cUr3");
vcap.setProperty("vcap.services.jblum-pcc.credentials.users[1].username", "jimbo");
vcap.setProperty("vcap.services.jblum-pcc.credentials.users[1].roles", "cluster_fuck");
vcap.setProperty("vcap.services.jblum-pcc.credentials.users[1].password", "p@55!t");
vcap.setProperty("vcap.services.jblum-pcc.credentials.users[2].username", "buster");
vcap.setProperty("vcap.services.jblum-pcc.credentials.users[2].roles", "cluster_operator");
vcap.setProperty("vcap.services.jblum-pcc.credentials.users[2].password", "p@55!t");
vcap.setProperty("vcap.services.jblum-pcc.tags", "pivotal,gemfire,database");
VcapPropertySource propertySource = VcapPropertySource.from(vcap);
assertThat(propertySource).isNotNull();
assertThat(propertySource.findFirstUserByRoleClusterOperator(Service.with("test-pcc")).isPresent()).isFalse();
User root = propertySource.findFirstUserByRoleClusterOperator(Service.with("a-pcc")).orElse(null);
assertThat(root).isNotNull();
assertThat(root.getName()).isEqualTo("root");
assertThat(root.getPassword().orElse(null)).isEqualTo("p@55w0rd");
assertThat(root.getRole().orElse(null).isClusterOperator()).isTrue();
User majorTom = propertySource.findFirstUserByRoleClusterOperator(Service.with("jblum-pcc")).orElse(null);
assertThat(majorTom).isNotNull();
assertThat(majorTom.getName()).isEqualTo("majorTom");
assertThat(majorTom.getPassword().orElse(null)).isEqualTo("s3cUr3");
assertThat(majorTom.getRole().orElse(null).isClusterOperator()).isTrue();
}
}

View File

@@ -0,0 +1,242 @@
/*
* 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.geode.core.env.support;
import static org.assertj.core.api.Assertions.assertThat;
import java.net.URL;
import java.util.List;
import org.junit.Test;
/**
* Unit tests for {@link CloudCacheService}.
*
* @author John Blum
* @see org.junit.Test
* @see org.springframework.geode.core.env.support.CloudCacheService
* @since 1.0.0
*/
public class CloudCacheServiceUnitTests {
@Test
public void withServiceNameLocatorsAndUrlReturnsNewCloudCacheService() throws Exception {
URL gfshUrl = new URL("http://localhost:7070/v1/gemfire");
CloudCacheService service = CloudCacheService.with("gemfire")
.withLocators("boombox[123],cardboardbox[456],mailbox[789],xbox[40404]")
.withGfshUrl(gfshUrl);
assertThat(service).isNotNull();
assertThat(service.getName()).isEqualTo("gemfire");
assertThat(service.getGfshUrl().orElse(null)).isEqualTo(gfshUrl);
assertThat(service.getLocators().orElse(null))
.isEqualTo("boombox[123],cardboardbox[456],mailbox[789],xbox[40404]");
List<CloudCacheService.Locator> locators = service.getLocatorList();
assertThat(locators).isNotNull();
assertThat(locators).hasSize(4);
assertThat(locators.get(0)).isEqualTo(CloudCacheService.Locator.newLocator("boombox", 123));
assertThat(locators.get(1)).isEqualTo(CloudCacheService.Locator.newLocator("cardboardbox", 456));
assertThat(locators.get(2)).isEqualTo(CloudCacheService.Locator.newLocator("mailbox", 789));
assertThat(locators.get(3)).isEqualTo(CloudCacheService.Locator.newLocator("xbox", 40404));
}
@Test
public void parseLocatorWithSingleLetterHostnameAndPort() {
CloudCacheService.Locator locator = CloudCacheService.Locator.parse("x [10336]");
assertThat(locator).isNotNull();
assertThat(locator.getHost()).isEqualTo("x");
assertThat(locator.getPort()).isEqualTo(10336);
}
@Test
public void parseLocatorWithNoHostnameAndPort() {
CloudCacheService.Locator locator = CloudCacheService.Locator.parse(" [1 234] ");
assertThat(locator).isNotNull();
assertThat(locator.getHost()).isEqualTo(CloudCacheService.Locator.DEFAULT_LOCATOR_HOST);
assertThat(locator.getPort()).isEqualTo(1234);
}
@Test
public void parseLocatorWithHostnameAndNoPort() {
CloudCacheService.Locator locator = CloudCacheService.Locator.parse(" chatterbox ");
assertThat(locator).isNotNull();
assertThat(locator.getHost()).isEqualTo("chatterbox");
assertThat(locator.getPort()).isEqualTo(CloudCacheService.Locator.DEFAULT_LOCATOR_PORT);
}
private void testParseLocatorWithInvalidHostPort(String hostPort) {
try {
CloudCacheService.Locator.parse(hostPort);
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Locator host/port [%s] is not valid", hostPort);
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test(expected = IllegalArgumentException.class)
public void parseLocatorWithBlankHostPort() {
testParseLocatorWithInvalidHostPort(" ");
}
@Test(expected = IllegalArgumentException.class)
public void parseLocatorWithEmptyHostPort() {
testParseLocatorWithInvalidHostPort("");
}
@Test(expected = IllegalArgumentException.class)
public void parseLocatorWithNullHostPort() {
testParseLocatorWithInvalidHostPort(null);
}
@Test
public void parseLocatorsWithMultipleLocatorHostsPorts() {
List<CloudCacheService.Locator> locators =
CloudCacheService.Locator.parseLocators(" jukebox[12345], matchbox [6789] ");
assertThat(locators).isNotNull();
assertThat(locators).hasSize(2);
assertThat(locators).containsExactly(
CloudCacheService.Locator.newLocator("jukebox", 12345),
CloudCacheService.Locator.newLocator("matchbox", 6789)
);
}
@Test
public void parseLocatorsWithNoLocatorHostPort() {
List<CloudCacheService.Locator> locators = CloudCacheService.Locator.parseLocators(" ");
assertThat(locators).isNotNull();
assertThat(locators).isEmpty();
}
@Test
public void parseLocatorsWithSingleLocatorHostPort() {
List<CloudCacheService.Locator> locators = CloudCacheService.Locator.parseLocators("skullbox[2345]");
assertThat(locators).isNotNull();
assertThat(locators).hasSize(1);
assertThat(locators).containsExactly(CloudCacheService.Locator.newLocator("skullbox", 2345));
}
@Test
public void newLocatorWithHostAndPort() {
CloudCacheService.Locator locator = CloudCacheService.Locator.newLocator("toybox", 8008);
assertThat(locator).isNotNull();
assertThat(locator.getHost()).isEqualTo("toybox");
assertThat(locator.getPort()).isEqualTo(8008);
}
@Test
public void newLocatorWithHostname() {
CloudCacheService.Locator locator = CloudCacheService.Locator.newLocator("unbox");
assertThat(locator).isNotNull();
assertThat(locator.getHost()).isEqualTo("unbox");
assertThat(locator.getPort()).isEqualTo(CloudCacheService.Locator.DEFAULT_LOCATOR_PORT);
}
@Test
public void newLocatorWithPort() {
CloudCacheService.Locator locator = CloudCacheService.Locator.newLocator(6789);
assertThat(locator).isNotNull();
assertThat(locator.getHost()).isEqualTo(CloudCacheService.Locator.DEFAULT_LOCATOR_HOST);
assertThat(locator.getPort()).isEqualTo(6789);
}
@Test
public void newLocatorWithDefaultHostPort() {
CloudCacheService.Locator locator = CloudCacheService.Locator.newLocator();
assertThat(locator).isNotNull();
assertThat(locator.getHost()).isEqualTo(CloudCacheService.Locator.DEFAULT_LOCATOR_HOST);
assertThat(locator.getPort()).isEqualTo(CloudCacheService.Locator.DEFAULT_LOCATOR_PORT);
}
private void testNewLocatorWithInvalidHost(String host) {
try {
CloudCacheService.Locator.newLocator(host);
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Host [%s] is required", host);
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test(expected = IllegalArgumentException.class)
public void newLocatorWithBlankHostnameThrowsIllegalArgumentException() {
testNewLocatorWithInvalidHost(" ");
}
@Test(expected = IllegalArgumentException.class)
public void newLocatorWithEmptyHostnameThrowsIllegalArgumentException() {
testNewLocatorWithInvalidHost("");
}
@Test(expected = IllegalArgumentException.class)
public void newLocatorWithNullHostnameThrowsIllegalArgumentException() {
testNewLocatorWithInvalidHost(null);
}
@Test(expected = IllegalArgumentException.class)
public void newLocatorWithInvalidPortNumberThrowsIllegalArgumentException() {
try {
CloudCacheService.Locator.newLocator(-2345);
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Port [-2345] must be greater than equal to 0");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test
public void locatorToStringPrintsHostPort() {
assertThat(CloudCacheService.Locator.newLocator("skullbox", 1234).toString())
.isEqualTo("skullbox[1234]");
}
}

View File

@@ -0,0 +1,75 @@
/*
* 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.geode.core.env.support;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
/**
* Unit tests for {@link Service}.
*
* @author John Blum
* @see org.junit.Test
* @see org.springframework.geode.core.env.support.Service
* @since 1.0.0
*/
public class ServiceUnitTests {
@Test
public void withNameReturnsNewService() {
Service service = Service.with("test");
assertThat(service).isNotNull();
assertThat(service.getName()).isEqualTo("test");
}
private void testWithInvalidNameThrowsIllegalArgumentException(String name) {
try {
Service.with(name);
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Service name [%s] is required", name);
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test(expected = IllegalArgumentException.class)
public void withBlankServiceNameThrowsIllegalArgumentException() {
testWithInvalidNameThrowsIllegalArgumentException(" ");
}
@Test(expected = IllegalArgumentException.class)
public void withEmptyServiceNameThrowsIllegalArgumentException() {
testWithInvalidNameThrowsIllegalArgumentException("");
}
@Test(expected = IllegalArgumentException.class)
public void withNullServiceNameThrowsIllegalArgumentException() {
testWithInvalidNameThrowsIllegalArgumentException("");
}
@Test
public void toStringReturnsServiceName() {
assertThat(Service.with("test").toString()).isEqualTo("test");
}
}

View File

@@ -0,0 +1,206 @@
/*
* 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.geode.core.env.support;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import org.junit.Test;
/**
* Unit tests for {@link User}.
*
* @author John Blum
* @see org.junit.Test
* @see org.springframework.geode.core.env.support.User
* @since 1.0.0
*/
public class UserUnitTests {
@Test
public void withNameReturnsNewUser() {
User user = User.with("root");
assertThat(user).isNotNull();
assertThat(user.getName()).isEqualTo("root");
assertThat(user.getPassword().isPresent()).isFalse();
assertThat(user.getRole().isPresent()).isFalse();
}
@Test
public void withNamePasswordAndRoleReturnsNewUser() {
User jdoe = User.with("jdoe")
.withPassword("p@55w0rd!")
.withRole(User.Role.CLUSTER_OPERATOR);
assertThat(jdoe).isNotNull();
assertThat(jdoe.getName()).isEqualTo("jdoe");
assertThat(jdoe.getPassword().orElse(null)).isEqualTo("p@55w0rd!");
assertThat(jdoe.getRole().orElse(null)).isEqualTo(User.Role.CLUSTER_OPERATOR);
}
private void testWithInvalidNameThrowsIllegalArgumentException(String name) {
try {
User.with(name);
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("User name [%s] is required", name);
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test(expected = IllegalArgumentException.class)
public void withBlankUserNameThrowsIllegalArgumentException() {
testWithInvalidNameThrowsIllegalArgumentException(" ");
}
@Test(expected = IllegalArgumentException.class)
public void withEmptyUserNameThrowsIllegalArgumentException() {
testWithInvalidNameThrowsIllegalArgumentException("");
}
@Test(expected = IllegalArgumentException.class)
public void withNullUserNameThrowsIllegalArgumentException() {
testWithInvalidNameThrowsIllegalArgumentException(null);
}
@Test
public void compareToReturnsEqualValue() {
assertThat(User.with("root").compareTo(User.with("root"))).isEqualTo(0);
}
@Test
public void compareToReturnsNegativeValue() {
assertThat(User.with("jdoe").compareTo(User.with("root"))).isLessThan(0);
}
@Test
public void compareToReturnsPositiveValue() {
assertThat(User.with("root").compareTo(User.with("jdoe"))).isGreaterThan(0);
}
@Test
@SuppressWarnings("all")
public void equalsObjectsReturnsFalse() {
assertThat(User.with("admin").equals("admin")).isFalse();
}
@Test
public void equalsWithDifferentObjectsReturnsFalse() {
User admin = User.with("admin").withRole(User.Role.CLUSTER_OPERATOR);
User root = User.with("root").withRole(User.Role.CLUSTER_OPERATOR);
assertThat(root.equals(admin)).isFalse();
}
@Test
public void equalsWithEqualObjectsReturnsTrue() {
User root = User.with("root").withRole(User.Role.CLUSTER_OPERATOR);
User rootToo = User.with("root").withPassword("test").withRole(User.Role.DEVELOPER);
assertThat(root.equals(rootToo)).isTrue();
}
@Test
@SuppressWarnings("all")
public void equalsWithIdenticalObjectsReturnsTrue() {
User root = User.with("root");
assertThat(root.equals(root)).isTrue();
}
@Test
public void hashCodeIsCorrect() {
User user = User.with("root");
int hashCode = user.hashCode();
assertThat(hashCode).isNotZero();
assertThat(hashCode).isEqualTo(user.hashCode());
user.withPassword("test").withRole(User.Role.DEVELOPER);
assertThat(user.hashCode()).isEqualTo(hashCode);
assertThat(user.hashCode()).isNotEqualTo(User.with("anotherUser").hashCode());
}
@Test
public void toStringReturnsUserName() {
assertThat(User.with("root").toString()).isEqualTo("root");
}
@Test
public void roleOfEmptyNameReturnsNull() {
assertThat(User.Role.of("")).isNull();
assertThat(User.Role.of(" ")).isNull();
}
@Test
public void roleOfInvalidNameReturnsNull() {
assertThat(User.Role.of("invalid")).isNull();
}
@Test
public void roleOfNulReturnsNull() {
assertThat(User.Role.of(null)).isNull();
}
@Test
public void roleOfRoleNamesEqualsRole() {
Arrays.stream(User.Role.values()).forEach(role -> {
assertThat(User.Role.of(role.name())).isEqualTo(role);
assertThat(User.Role.of(role.toString())).isEqualTo(role);
});
}
@Test
public void isClusterOperator() {
assertThat(User.Role.CLUSTER_OPERATOR.isClusterOperator()).isTrue();
}
@Test
public void isNotClusterOperator() {
assertThat(User.Role.DEVELOPER.isClusterOperator()).isFalse();
}
@Test
public void isDeveloper() {
assertThat(User.Role.DEVELOPER.isDeveloper()).isTrue();
}
@Test
public void isNotDeveloper() {
assertThat(User.Role.CLUSTER_OPERATOR.isDeveloper()).isFalse();
}
@Test
public void toStringReturnsLowercaseName() {
Arrays.stream(User.Role.values())
.forEach(role -> assertThat(role.toString()).isEqualTo(role.name().toLowerCase()));
}
}

View File

@@ -0,0 +1,81 @@
/*
* 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.geode.core.util;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newRuntimeException;
import org.junit.Test;
/**
* Unit tests for {@link ObjectUtils}.
*
* @author John Blum
* @see org.junit.Test
* @see org.springframework.geode.core.util.ObjectUtils
* @since 1.0.0
*/
public class ObjectUtilsUnitTests {
@Test
public void doOperationSafelyReturnsResult() {
assertThat(ObjectUtils.doOperationSafely(() -> "test")).isEqualTo("test");
}
@Test
public void doOperationSafelyReturnsDefaultValue() {
assertThat(ObjectUtils.doOperationSafely(() -> { throw newRuntimeException("test"); },
"default value")).isEqualTo("default value");
}
@Test(expected = IllegalStateException.class)
public void doOperationSafelyThrowsIllegalStateException() {
try {
ObjectUtils.doOperationSafely(() -> { throw newRuntimeException("test"); }, null);
}
catch (IllegalStateException expected) {
assertThat(expected).hasMessage("Failed to execute operation");
assertThat(expected).hasCauseInstanceOf(RuntimeException.class);
assertThat(expected.getCause()).hasMessage("test");
assertThat(expected.getCause()).hasNoCause();
throw expected;
}
}
@Test
public void returnValueThrowOnNullWithNonNullValueReturnsValue() {
assertThat(ObjectUtils.returnValueThrowOnNull("test")).isEqualTo("test");
}
@Test(expected = RuntimeException.class)
public void returnValueThrowOnNullWithNullValueThrowsException() {
try {
ObjectUtils.returnValueThrowOnNull(null, newRuntimeException("test"));
}
catch (RuntimeException expected) {
assertThat(expected).hasMessage("test");
assertThat(expected).hasNoCause();
throw expected;
}
}
}

View File

@@ -0,0 +1,152 @@
/*
* 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.geode.function.support;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.concurrent.TimeUnit;
import edu.umd.cs.mtc.MultithreadedTestCase;
import edu.umd.cs.mtc.TestFramework;
import org.apache.geode.cache.execute.FunctionException;
import org.apache.geode.distributed.DistributedMember;
import org.junit.Before;
import org.junit.Test;
/**
* Unit tests for {@link AbstractResultCollector}.
*
* @author John Blum
* @see org.junit.Test
* @see org.springframework.geode.function.support.AbstractResultCollector
* @since 1.0.0
*/
public class AbstractResultCollectorUnitTests {
private AbstractResultCollector<Object, Object> resultCollector;
private static <T, S> AbstractResultCollector<T, S> newResultCollector() {
return newResultCollector(() -> {});
}
private static <T, S> AbstractResultCollector<T, S> newResultCollector(Runnable runnable) {
return new AbstractResultCollector<T, S>() {
@Override
public synchronized S getResult() throws FunctionException {
runnable.run();
return super.getResult();
}
@Override
@SuppressWarnings("unchecked")
public void addResult(DistributedMember memberID, T resultOfSingleExecution) {
setResult((S) resultOfSingleExecution);
}
};
}
@Before
public void setup() {
this.resultCollector = newResultCollector();
}
@Test
public void clearResultClearsResult() {
this.resultCollector.setResult("test");
assertThat(this.resultCollector.getResult()).isEqualTo("test");
this.resultCollector.clearResults();
assertThat(this.resultCollector.getResult()).isNull();
}
@Test
public void getResultReturnsResult() {
this.resultCollector.setResult("test");
assertThat(this.resultCollector.getResult()).isEqualTo("test");
}
@Test
public void getResultReturnsResultWithinTimeout() throws Throwable {
TestFramework.runOnce(new ReturnsResultWithinTimeoutMultithreadedTestCase());
}
@Test
public void resultsHaveEnded() {
this.resultCollector.endResults();
assertThat(this.resultCollector.hasResultsEnded()).isTrue();
assertThat(this.resultCollector.hasResultsNotEnded()).isFalse();
}
@Test
public void resultsHaveNotEnded() {
assertThat(this.resultCollector.hasResultsEnded()).isFalse();
assertThat(this.resultCollector.hasResultsNotEnded()).isTrue();
}
@SuppressWarnings("unused")
static class ReturnsResultWithinTimeoutMultithreadedTestCase extends MultithreadedTestCase {
private long startTimestamp;
private AbstractResultCollector<Object, Object> resultCollector;
@Override
public void initialize() {
super.initialize();
this.resultCollector = newResultCollector(() -> waitForTick(1));
this.startTimestamp = System.currentTimeMillis();
}
public void thread1() throws InterruptedException {
Thread.currentThread().setName("ResultCollector.getResult()");
assertThat(this.resultCollector.getResult(500, TimeUnit.MILLISECONDS)).isEqualTo("test");
}
public void thread2() {
Thread.currentThread().setName("ResultCollector.setResult(..)");
waitForTick(1);
this.resultCollector.setResult("test");
}
@Override
public void finish() {
long endTimestamp = System.currentTimeMillis();
assertThat(endTimestamp).isGreaterThan(this.startTimestamp);
assertThat(endTimestamp - this.startTimestamp).isLessThan(TimeUnit.SECONDS.toMillis(2));
}
}
}

View File

@@ -0,0 +1,130 @@
/*
* 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.geode.function.support;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import org.apache.geode.distributed.DistributedMember;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
/**
* Unit tests for {@link SingleResultReturningCollector}.
*
* @author John Blum
* @see org.junit.Test
* @see org.mockito.Mock
* @see org.mockito.Mockito
* @see org.mockito.junit.MockitoJUnitRunner
* @see org.springframework.geode.function.support.SingleResultReturningCollector
* @since 1.0.0
*/
@RunWith(MockitoJUnitRunner.class)
public class SingleResultReturningCollectorUnitTests {
@Mock
private DistributedMember mockDistributedMember;
private SingleResultReturningCollector<Object> resultCollector;
@Before
public void setup() {
this.resultCollector = new SingleResultReturningCollector<>();
}
@Test
public void addResultWithNullIsSafe() {
assertThat(this.resultCollector.getResult()).isNull();
this.resultCollector.addResult(this.mockDistributedMember, null);
assertThat(this.resultCollector.getResult()).isNull();
}
@Test
public void addResultWithSingleObjectReturnsObject() {
assertThat(this.resultCollector.getResult()).isNull();
this.resultCollector.addResult(this.mockDistributedMember, "test");
assertThat(this.resultCollector.getResult()).isEqualTo("test");
}
@Test
public void addResultWithIterableReturnsFirstElement() {
Iterable<String> list = Arrays.asList("one", "two", "three");
assertThat(this.resultCollector.getResult()).isNull();
this.resultCollector.addResult(this.mockDistributedMember, list);
assertThat(this.resultCollector.getResult()).isEqualTo("one");
}
@Test
@SuppressWarnings("unchecked")
public void addResultWithIterableReturningNullIteratorIsSafe() {
Iterable<Object> mockIterable = mock(Iterable.class);
when(mockIterable.iterator()).thenReturn(null);
assertThat(this.resultCollector.getResult()).isNull();
this.resultCollector.addResult(mockDistributedMember, mockIterable);
assertThat(this.resultCollector.getResult()).isNull();
}
@Test
public void addResultWithIterableOfListsReturnsFirstElementInListOne() {
Iterable<String> listOne = Arrays.asList("one", "two", "three");
Iterable<String> listTwo = Arrays.asList("four", "five", "six");
assertThat(this.resultCollector.getResult()).isNull();
this.resultCollector.addResult(this.mockDistributedMember, Arrays.asList(listOne, listTwo));
assertThat(this.resultCollector.getResult()).isEqualTo("one");
}
@Test
public void addResultWithIterableOfListsOfListsReturnsFirstElementInListOne() {
Iterable<String> listOne = Arrays.asList("one", "two", "three");
Iterable<String> listTwo = Arrays.asList("four", "five", "six");
Iterable<String> listThree = Arrays.asList("seven", "eight", "nine");
assertThat(this.resultCollector.getResult()).isNull();
this.resultCollector.addResult(this.mockDistributedMember,
Arrays.asList(Arrays.asList(listOne, listTwo), listThree));
assertThat(this.resultCollector.getResult()).isEqualTo("one");
}
}

View File

@@ -0,0 +1,93 @@
/*
* 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.geode.security.support;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import org.apache.geode.cache.GemFireCache;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.gemfire.config.annotation.EnableSecurity;
import org.springframework.data.gemfire.config.annotation.PeerCacheApplication;
import org.springframework.data.gemfire.support.GemfireBeanFactoryLocator;
import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport;
import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects;
import org.springframework.geode.core.util.ObjectUtils;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration tests for {@link SecurityManagerProxy}.
*
* @author John Blum
* @see org.apache.geode.cache.GemFireCache
* @see org.apache.geode.security.SecurityManager
* @see org.springframework.context.annotation.Bean
* @see org.springframework.data.gemfire.config.annotation.EnableSecurity
* @see org.springframework.data.gemfire.config.annotation.PeerCacheApplication
* @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport
* @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects
* @see org.springframework.test.annotation.DirtiesContext
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringRunner
* @since 1.0.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration
@DirtiesContext
@SuppressWarnings("unused")
public class SecurityManagerProxyIntegrationTests extends IntegrationTestsSupport {
private static final String GEMFIRE_LOG_LEVEL = "error";
@BeforeClass
@AfterClass
public static void cleanUpBeanFactoryLocatorReferences() throws Exception {
GemfireBeanFactoryLocator.clear();
ObjectUtils.doOperationSafely(() -> {
SecurityManagerProxy.getInstance().destroy();
return true;
}, true);
}
@Autowired
private org.apache.geode.security.SecurityManager mockSecurityManager;
@Test
public void securityManagerProxyWasConfiguredWithMockSecurityManager() {
assertThat(SecurityManagerProxy.getInstance().getSecurityManager()).isEqualTo(this.mockSecurityManager);
}
@EnableGemFireMockObjects
@PeerCacheApplication(logLevel = GEMFIRE_LOG_LEVEL, useBeanFactoryLocator = true)
@EnableSecurity(securityManagerClassName = "org.springframework.geode.security.support.SecurityManagerProxy")
static class TestConfiguration {
@Bean
org.apache.geode.security.SecurityManager mockSecurityManager(GemFireCache gemfireCache) {
return mock(org.apache.geode.security.SecurityManager.class);
}
}
}

View File

@@ -0,0 +1,150 @@
/*
* 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.geode.security.support;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.security.Principal;
import java.util.Properties;
import org.apache.geode.security.ResourcePermission;
import org.junit.Test;
/**
* Unit tests for {@link SecurityManagerProxy}
*
* @author John Blum
* @see java.security.Principal
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.geode.security.support.SecurityManagerProxy
* @since 1.0.0
*/
public class SecurityManagerProxyUnitTests {
@Test
public void setAndGetSecurityManager() {
org.apache.geode.security.SecurityManager mockSecurityManager =
mock(org.apache.geode.security.SecurityManager.class);
SecurityManagerProxy securityManagerProxy = new SecurityManagerProxy();
securityManagerProxy.setSecurityManager(mockSecurityManager);
assertThat(securityManagerProxy.getSecurityManager()).isEqualTo(mockSecurityManager);
}
@Test(expected = IllegalArgumentException.class)
public void setSecurityManagerToNullThrowsIllegalArgumentException() {
try {
new SecurityManagerProxy().setSecurityManager(null);
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("SecurityManager must not be null");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test(expected = IllegalStateException.class)
public void getSecurityManagerWhenUninitializedThrowsIllegalStateException() {
try {
new SecurityManagerProxy().getSecurityManager();
}
catch (IllegalStateException expected) {
assertThat(expected).hasMessage("No SecurityManager configured");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test
public void authenticateDelegatesToConfiguredSecurityManager() {
Properties securityProperties = new Properties();
org.apache.geode.security.SecurityManager mockSecurityManager =
mock(org.apache.geode.security.SecurityManager.class);
when(mockSecurityManager.authenticate(any(Properties.class))).thenReturn("TestUser");
SecurityManagerProxy securityManagerProxy = new SecurityManagerProxy();
securityManagerProxy.setSecurityManager(mockSecurityManager);
assertThat(securityManagerProxy.getSecurityManager()).isEqualTo(mockSecurityManager);
assertThat(securityManagerProxy.authenticate(securityProperties)).isEqualTo("TestUser");
verify(mockSecurityManager, times(1)).authenticate(eq(securityProperties));
}
@Test
public void authorizeDelegatesToConfiguredSecurityManager() {
Principal mockPrincipal = mock(Principal.class);
ResourcePermission resourcePermission =
new ResourcePermission(ResourcePermission.Resource.DATA, ResourcePermission.Operation.READ);
org.apache.geode.security.SecurityManager mockSecurityManager =
mock(org.apache.geode.security.SecurityManager.class);
when(mockSecurityManager.authorize(any(Object.class), any(ResourcePermission.class))).thenReturn(true);
SecurityManagerProxy securityManagerProxy = new SecurityManagerProxy();
securityManagerProxy.setSecurityManager(mockSecurityManager);
assertThat(securityManagerProxy.getSecurityManager()).isEqualTo(mockSecurityManager);
assertThat(securityManagerProxy.authorize(mockPrincipal, resourcePermission)).isTrue();
verify(mockSecurityManager, times(1))
.authorize(eq(mockPrincipal), eq(resourcePermission));
}
@Test
public void closeDelegatesToConfiguredSecurityManager() {
org.apache.geode.security.SecurityManager mockSecurityManager =
mock(org.apache.geode.security.SecurityManager.class);
when(mockSecurityManager.authorize(any(Object.class), any(ResourcePermission.class))).thenReturn(true);
SecurityManagerProxy securityManagerProxy = new SecurityManagerProxy();
securityManagerProxy.setSecurityManager(mockSecurityManager);
assertThat(securityManagerProxy.getSecurityManager()).isEqualTo(mockSecurityManager);
securityManagerProxy.close();
verify(mockSecurityManager, times(1)).close();
}
}