SGF-408 - Provide support in the SDG XML namespace to load a pre-defined data set using GemFire Snapshot Service for development and testing purposes.

Refactored the SnapshotApplicationEvent into 2 separate ApplicationEvent classes to handle import and exports.
Refactored the SnapshotServiceFactoryBean class to handle import/export SnapshotApplicationEvents in onApplicationEvent(:SnapshotApplicationEvent).
Added integration tests for the archive file handling capabilities of the SnapshotServiceFactoryBean.SnapshotServiceAdapterSupport class.
Added additional unit tests.
(cherry picked from commit 35107aa12dc9d5f133697775c2535c1f9cd3cd87)

Signed-off-by: John Blum <jblum@pivotal.io>
This commit is contained in:
John Blum
2015-08-25 00:53:46 -07:00
parent e78169eda2
commit 447d55efbe
9 changed files with 390 additions and 83 deletions

View File

@@ -64,7 +64,7 @@ public class SnapshotApplicationEventTest {
@Test
public void constructSnapshotApplicationEventWithoutRegionOrSnapshotMetadata() {
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this);
SnapshotApplicationEvent event = new TestSnapshotApplicationEvent(this);
assertThat((SnapshotApplicationEventTest) event.getSource(), is(equalTo(this)));
assertThat(event.getRegionPath(), is(nullValue()));
@@ -78,7 +78,7 @@ public class SnapshotApplicationEventTest {
public void constructSnapshotApplicationEventWithRegionAndSnapshotMetadata() {
SnapshotMetadata eventSnapshotMetadata = newSnapshotMetadata();
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, "/Example", eventSnapshotMetadata);
SnapshotApplicationEvent event = new TestSnapshotApplicationEvent(this, "/Example", eventSnapshotMetadata);
assertThat((SnapshotApplicationEventTest) event.getSource(), is(equalTo(this)));
assertThat(event.getRegionPath(), is(equalTo("/Example")));
@@ -89,7 +89,7 @@ public class SnapshotApplicationEventTest {
@Test
public void constructSnapshotApplicationEventWithRegionButNoSnapshotMetadata() {
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, "/Example");
SnapshotApplicationEvent event = new TestSnapshotApplicationEvent(this, "/Example");
assertThat((SnapshotApplicationEventTest) event.getSource(), is(equalTo(this)));
assertThat(event.getRegionPath(), is(equalTo("/Example")));
@@ -104,7 +104,7 @@ public class SnapshotApplicationEventTest {
SnapshotMetadata eventSnapshotMetadataOne = newSnapshotMetadata();
SnapshotMetadata eventSnapshotMetadataTwo = newSnapshotMetadata();
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, eventSnapshotMetadataOne,
SnapshotApplicationEvent event = new TestSnapshotApplicationEvent(this, eventSnapshotMetadataOne,
eventSnapshotMetadataTwo);
assertThat((SnapshotApplicationEventTest) event.getSource(), is(equalTo(this)));
@@ -117,7 +117,7 @@ public class SnapshotApplicationEventTest {
@Test
public void matchesNullRegionIsFalse() {
assertThat(new SnapshotApplicationEvent(this).matches((Region) null), is(false));
assertThat(new TestSnapshotApplicationEvent(this).matches((Region) null), is(false));
}
@Test
@@ -126,7 +126,7 @@ public class SnapshotApplicationEventTest {
when(mockRegion.getFullPath()).thenReturn("/Example");
assertThat(new SnapshotApplicationEvent(this, "/Prototype").matches(mockRegion), is(false));
assertThat(new TestSnapshotApplicationEvent(this, "/Prototype").matches(mockRegion), is(false));
verify(mockRegion, times(1)).getFullPath();
}
@@ -137,14 +137,14 @@ public class SnapshotApplicationEventTest {
when(mockRegion.getFullPath()).thenReturn("/Example");
assertThat(new SnapshotApplicationEvent(this, "/Example").matches(mockRegion), is(true));
assertThat(new TestSnapshotApplicationEvent(this, "/Example").matches(mockRegion), is(true));
verify(mockRegion, times(1)).getFullPath();
}
@Test
public void matchesNonMatchingRegionPathsIsFalse() {
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, "/Example");
SnapshotApplicationEvent event = new TestSnapshotApplicationEvent(this, "/Example");
assertThat(event.getRegionPath(), is(equalTo("/Example")));
assertThat(event.matches("/Prototype"), is(false));
@@ -163,11 +163,23 @@ public class SnapshotApplicationEventTest {
@Test
public void matchesMatchingRegionPathsIsTrue() {
assertThat(new SnapshotApplicationEvent(this, "/Example").matches("/Example"), is(true));
assertThat(new SnapshotApplicationEvent(this, "/").matches("/"), is(true));
assertThat(new SnapshotApplicationEvent(this, " ").matches(" "), is(true));
assertThat(new SnapshotApplicationEvent(this, "").matches(""), is(true));
assertThat(new SnapshotApplicationEvent(this, (String) null).matches((String) null), is(true));
assertThat(new TestSnapshotApplicationEvent(this, "/Example").matches("/Example"), is(true));
assertThat(new TestSnapshotApplicationEvent(this, "/").matches("/"), is(true));
assertThat(new TestSnapshotApplicationEvent(this, " ").matches(" "), is(true));
assertThat(new TestSnapshotApplicationEvent(this, "").matches(""), is(true));
assertThat(new TestSnapshotApplicationEvent(this, (String) null).matches((String) null), is(true));
}
protected static final class TestSnapshotApplicationEvent<K, V> extends SnapshotApplicationEvent<K, V> {
public TestSnapshotApplicationEvent(Object source, SnapshotMetadata<K, V>... snapshotMetadata) {
super(source, snapshotMetadata);
}
public TestSnapshotApplicationEvent(Object source, String regionPath,
SnapshotMetadata<K, V>... snapshotMetadata) {
super(source, regionPath, snapshotMetadata);
}
}
}

View File

@@ -16,13 +16,89 @@
package org.springframework.data.gemfire;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotServiceAdapterSupport;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.gemfire.test.support.FileSystemUtils;
/**
* The SnapshotServiceFactoryBeanIntegrationTest class...
* The SnapshotServiceFactoryBeanIntegrationTest class is a test suite of test cases testing the file archive handling
* capabilities of the SnapshotServiceFactoryBean.SnapshotServiceAdpterSupport class.
*
* @author John Blum
* @see org.springframework.data.gemfire.
* @see org.junit.Test
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotServiceAdapterSupport
* @since 1.7.0
*/
public class SnapshotServiceFactoryBeanIntegrationTest {
SnapshotServiceAdapterSupport snapshotService = new TestSnapshotServiceAdapter();
protected List<String> toFilenames(File... files) {
List<String> filenames = new ArrayList<String>(files.length);
for (File file : files) {
filenames.add(file.getName());
}
return filenames;
}
@Test
public void handleNonArchiveFileLocation() {
File expectedFile = new File("/path/to/non-existing/snapshot/file.gfd");
File[] files = snapshotService.handleFileLocation(expectedFile);
assertThat(files, is(notNullValue()));
assertThat(files.length, is(equalTo(1)));
assertThat(files[0], is(equalTo(expectedFile)));
}
@Test
public void handleArchiveFileLocation() throws Exception {
File cacheSnapshotZipDirectory = null;
try {
File cacheSnapshotZip = new ClassPathResource("/cache_snapshot.zip").getFile();
File[] actualSnapshots = snapshotService.handleFileLocation(cacheSnapshotZip);
assertThat(actualSnapshots, is(notNullValue()));
assertThat(actualSnapshots.length, is(equalTo(3)));
assertThat(toFilenames(actualSnapshots).containsAll(Arrays.asList(
"accounts.snapshot", "address.snapshot", "people.snapshot")), is(true));
cacheSnapshotZipDirectory = new File(System.getProperty("java.io.tmpdir"),
cacheSnapshotZip.getName().replaceAll("\\.", "-"));
assertThat(cacheSnapshotZipDirectory.isDirectory(), is(true));
assertThat(cacheSnapshotZipDirectory.listFiles(FileSystemUtils.FileOnlyFilter.INSTANCE),
is(equalTo(actualSnapshots)));
}
finally {
if (cacheSnapshotZipDirectory != null && cacheSnapshotZipDirectory.isDirectory()) {
FileSystemUtils.deleteRecursive(cacheSnapshotZipDirectory);
}
}
}
protected static final class TestSnapshotServiceAdapter<K, V> extends SnapshotServiceAdapterSupport<K, V> {
@Override
protected File[] handleLocation(final SnapshotServiceFactoryBean.SnapshotMetadata<K, V> configuration) {
throw new UnsupportedOperationException("not implemented");
}
}
}

View File

@@ -370,12 +370,12 @@ public class SnapshotServiceFactoryBeanTest {
SnapshotMetadata eventSnapshotMetadata = newSnapshotMetadata(snapshotDat);
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, "/Example", eventSnapshotMetadata);
SnapshotApplicationEvent event = new ExportSnapshotApplicationEvent(this, "/Example", eventSnapshotMetadata);
assertThat(event.isRegionSnapshotEvent(), is(true));
assertThat(event.getSnapshotMetadata()[0], is(sameInstance(eventSnapshotMetadata)));
assertThat(factoryBean.getExports()[0], is(not(sameInstance(eventSnapshotMetadata))));
assertThat(factoryBean.getRegion(), is(sameInstance(mockRegion)));
assertThat(event.isRegionSnapshotEvent(), is(true));
assertThat(event.getSnapshotMetadata()[0], is(sameInstance(eventSnapshotMetadata)));
factoryBean.onApplicationEvent(event);
@@ -386,6 +386,40 @@ public class SnapshotServiceFactoryBeanTest {
eq(eventSnapshotMetadata.getFormat()), eq(mockSnapshotOptions));
}
@Test
public void onApplicationEventWhenMatchUsingFactorySnapshotMetadataPerformsImport() throws Exception {
final CacheSnapshotService mockSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService");
SnapshotOptions mockSnapshotOptions = mock(SnapshotOptions.class, "MockSnapshotOptions");
when(mockSnapshotService.createOptions()).thenReturn(mockSnapshotOptions);
when(mockSnapshotOptions.setFilter(any(SnapshotFilter.class))).thenReturn(mockSnapshotOptions);
SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean() {
@Override public SnapshotServiceAdapter getObject() throws Exception {
return new CacheSnapshotServiceAdapter(mockSnapshotService);
}
};
SnapshotMetadata expectedSnapshotMetadata = newSnapshotMetadata(snapshotDat);
factoryBean.setImports(toArray(expectedSnapshotMetadata));
SnapshotApplicationEvent event = new ImportSnapshotApplicationEvent(this);
assertThat(factoryBean.getImports()[0], is(equalTo(expectedSnapshotMetadata)));
assertThat(factoryBean.getRegion(), is(nullValue()));
assertThat(event.isCacheSnapshotEvent(), is(true));
assertThat(event.getSnapshotMetadata().length, is(equalTo(0)));
factoryBean.onApplicationEvent(event);
verify(mockSnapshotOptions, times(1)).setFilter(isNull(SnapshotFilter.class));
verify(mockSnapshotService, times(1)).createOptions();
verify(mockSnapshotService, times(1)).load(eq(new File[] { expectedSnapshotMetadata.getLocation() }),
eq(expectedSnapshotMetadata.getFormat()), eq(mockSnapshotOptions));
}
@Test
public void onApplicationEventWhenNoMatchDoesNotPerformExport() throws Exception {
final CacheSnapshotService mockSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService");
@@ -396,10 +430,13 @@ public class SnapshotServiceFactoryBeanTest {
}
};
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, "/Example");
factoryBean.setExports(toArray(newSnapshotMetadata()));
assertThat(event.isRegionSnapshotEvent(), is(true));
SnapshotApplicationEvent event = new ExportSnapshotApplicationEvent(this, "/Example");
assertThat(factoryBean.getExports().length, is(equalTo(1)));
assertThat(factoryBean.getRegion(), is(nullValue()));
assertThat(event.isRegionSnapshotEvent(), is(true));
factoryBean.onApplicationEvent(event);
@@ -409,32 +446,78 @@ public class SnapshotServiceFactoryBeanTest {
any(SnapshotOptions.class));
}
@Test
public void onApplicationEventWhenNoMatchDoesNotPerformImport() throws Exception {
Region mockRegion = mock(Region.class, "MockRegion");
when(mockRegion.getFullPath()).thenReturn("/Example");
final RegionSnapshotService mockSnapshotService = mock(RegionSnapshotService.class, "MockRegionSnapshotService");
SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean() {
@Override public SnapshotServiceAdapter getObject() throws Exception {
return new RegionSnapshotServiceAdapter(mockSnapshotService);
}
};
factoryBean.setImports(toArray(newSnapshotMetadata()));
factoryBean.setRegion(mockRegion);
SnapshotApplicationEvent event = new ImportSnapshotApplicationEvent(this, "/Test");
assertThat(factoryBean.getImports().length, is(equalTo(1)));
assertThat(factoryBean.getRegion(), is(equalTo(mockRegion)));
assertThat(event.isRegionSnapshotEvent(), is(true));
factoryBean.onApplicationEvent(event);
verify(mockRegion, times(1)).getFullPath();
verify(mockSnapshotService, never()).createOptions();
verify(mockSnapshotService, never()).load(any(File.class), any(SnapshotFormat.class));
verify(mockSnapshotService, never()).load(any(File.class), any(SnapshotFormat.class),
any(SnapshotOptions.class));
}
@Test
public void resolveSnapshotMetadataFromEvent() {
SnapshotMetadata eventSnapshotMetadata = newSnapshotMetadata(snapshotDat);
SnapshotMetadata exportSnapshotMetadata = newSnapshotMetadata();
SnapshotMetadata importSnapshotMetadata = newSnapshotMetadata(FileSystemUtils.USER_HOME);
factoryBean.setExports(toArray(exportSnapshotMetadata));
factoryBean.setImports(toArray(importSnapshotMetadata));
assertThat(factoryBean.getExports()[0], is(equalTo(exportSnapshotMetadata)));
assertThat(factoryBean.resolveSnapshotMetadata(new SnapshotApplicationEvent(
assertThat(factoryBean.getImports()[0], is(equalTo(importSnapshotMetadata)));
assertThat(factoryBean.resolveSnapshotMetadata(new ExportSnapshotApplicationEvent(
this, eventSnapshotMetadata))[0], is(equalTo(eventSnapshotMetadata)));
}
@Test
public void resolveSnapshotMetadataFromFactory() {
public void resolveExportSnapshotMetadataFromFactory() {
SnapshotMetadata exportSnapshotMetadata = newSnapshotMetadata();
factoryBean.setExports(toArray(exportSnapshotMetadata));
assertThat(factoryBean.getExports()[0], is(equalTo(exportSnapshotMetadata)));
assertThat(factoryBean.resolveSnapshotMetadata(new SnapshotApplicationEvent(this))[0],
assertThat(factoryBean.resolveSnapshotMetadata(new ExportSnapshotApplicationEvent(this))[0],
is(equalTo(exportSnapshotMetadata)));
}
@Test
public void resolveImportSnapshotMetadataFromFactory() {
SnapshotMetadata exportSnapshotMetadata = newSnapshotMetadata();
factoryBean.setImports(toArray(exportSnapshotMetadata));
assertThat(factoryBean.getImports()[0], is(equalTo(exportSnapshotMetadata)));
assertThat(factoryBean.resolveSnapshotMetadata(new ImportSnapshotApplicationEvent(this))[0],
is(equalTo(exportSnapshotMetadata)));
}
@Test
public void withCacheBasedSnapshotServiceOnCacheSnapshotEventIsMatch() {
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this);
SnapshotApplicationEvent event = new ExportSnapshotApplicationEvent(this);
assertThat(event.isCacheSnapshotEvent(), is(true));
assertThat(event.isRegionSnapshotEvent(), is(false));
@@ -443,7 +526,7 @@ public class SnapshotServiceFactoryBeanTest {
@Test
public void withCacheBasedSnapshotServiceOnRegionSnapshotEventIsNotAMatch() {
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, "/Example");
SnapshotApplicationEvent event = new ImportSnapshotApplicationEvent(this, "/Example");
assertThat(event.isCacheSnapshotEvent(), is(false));
assertThat(event.isRegionSnapshotEvent(), is(true));
@@ -452,7 +535,7 @@ public class SnapshotServiceFactoryBeanTest {
@Test
public void withRegionBasedSnapshotServiceOnRegionSnapshotEventIsMatch() {
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, "/Example");
SnapshotApplicationEvent event = new ExportSnapshotApplicationEvent(this, "/Example");
assertThat(event.isCacheSnapshotEvent(), is(false));
assertThat(event.isRegionSnapshotEvent(), is(true));
@@ -471,7 +554,7 @@ public class SnapshotServiceFactoryBeanTest {
@Test
public void withRegionBasedSnapshotServiceOnCacheSnapshotEventIsNotAMatch() {
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this);
SnapshotApplicationEvent event = new ImportSnapshotApplicationEvent(this);
assertThat(event.isCacheSnapshotEvent(), is(true));
assertThat(event.isRegionSnapshotEvent(), is(false));
@@ -1074,35 +1157,10 @@ public class SnapshotServiceFactoryBeanTest {
protected static class TestSnapshotServiceAdapter extends SnapshotServiceAdapterSupport<Object, Object> {
@Override
public SnapshotOptions<Object, Object> createOptions() {
throw new UnsupportedOperationException("not implemented");
}
@Override
protected File[] handleLocation(final SnapshotMetadata<Object, Object> configuration) {
throw new UnsupportedOperationException("not implemented");
}
@Override
public void load(final File directory, final SnapshotFormat format) {
throw new UnsupportedOperationException("not implemented");
}
@Override
public void load(SnapshotFormat format, SnapshotOptions<Object, Object> options, File... snapshots) {
throw new UnsupportedOperationException("not implemented");
}
@Override
public void save(final File location, final SnapshotFormat format) {
throw new UnsupportedOperationException("not implemented");
}
@Override
public void save(File location, SnapshotFormat format, SnapshotOptions<Object, Object> options) {
throw new UnsupportedOperationException("not implemented");
}
}
}