Add and test InMemorySwapStore

This commit is contained in:
Winston Li 2016-08-23 23:33:01 +01:00 committed by Michael Mazour
parent f036ff2c8b
commit 4b014826d3
4 changed files with 153 additions and 2 deletions

View file

@ -0,0 +1,50 @@
package uk.ac.ic.wlgitbridge.bridge.swap;
import org.apache.commons.io.IOUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* Created by winston on 23/08/2016.
*/
public class InMemorySwapStore implements SwapStore {
private final Map<String, byte[]> store;
public InMemorySwapStore() {
store = new HashMap<>();
}
@Override
public void upload(
String projectName,
InputStream uploadStream,
long contentLength
) throws IOException {
store.put(
projectName,
IOUtils.toByteArray(uploadStream, contentLength)
);
}
@Override
public InputStream openDownloadStream(String projectName) {
byte[] buf = store.get(projectName);
if (buf == null) {
throw new IllegalArgumentException(
"no such project in swap store: " + projectName
);
}
return new ByteArrayInputStream(buf);
}
@Override
public void remove(String projectName) {
store.remove(projectName);
}
}

View file

@ -3,6 +3,7 @@ package uk.ac.ic.wlgitbridge.bridge.swap;
import uk.ac.ic.wlgitbridge.bridge.db.DBStore;
import uk.ac.ic.wlgitbridge.bridge.lock.ProjectLock;
import uk.ac.ic.wlgitbridge.bridge.repo.RepoStore;
import uk.ac.ic.wlgitbridge.util.Log;
import java.time.Duration;
import java.util.Timer;
@ -52,7 +53,8 @@ public class SwapJobImpl implements SwapJob {
}
private void doSwap() {
swaps.incrementAndGet();
Log.info("Running {}th swap", swaps.getAndIncrement());
}
}

View file

@ -1,5 +1,6 @@
package uk.ac.ic.wlgitbridge.bridge.swap;
import java.io.IOException;
import java.io.InputStream;
/**
@ -7,7 +8,11 @@ import java.io.InputStream;
*/
public interface SwapStore {
void upload(String projectName, InputStream uploadStream, long contentLength);
void upload(
String projectName,
InputStream uploadStream,
long contentLength
) throws IOException;
InputStream openDownloadStream(String projectName);

View file

@ -0,0 +1,94 @@
package uk.ac.ic.wlgitbridge.bridge.swap;
import org.apache.commons.io.IOUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import static org.junit.Assert.assertArrayEquals;
/**
* Created by winston on 23/08/2016.
*/
public class InMemorySwapStoreTest {
private final InMemorySwapStore swapStore = new InMemorySwapStore();
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void downloadingNonExistentFileThrows() {
exception.expect(IllegalArgumentException.class);
swapStore.openDownloadStream("asdf");
}
@Test
public void canDownloadUploadedFiles() throws IOException {
byte[] proj1Contents = "helloproj1".getBytes();
byte[] proj2Contents = "asdfproj2".getBytes();
swapStore.upload(
"proj1",
new ByteArrayInputStream(proj1Contents),
proj1Contents.length
);
swapStore.upload(
"proj2",
new ByteArrayInputStream(proj2Contents),
proj2Contents.length
);
assertArrayEquals(
proj1Contents,
IOUtils.toByteArray(swapStore.openDownloadStream("proj1"))
);
assertArrayEquals(
proj2Contents,
IOUtils.toByteArray(swapStore.openDownloadStream("proj2"))
);
}
@Test
public void uploadingForTheSameProjectOverwritesTheFile() throws IOException {
byte[] proj1Contents = "helloproj1".getBytes();
byte[] proj1NewContents = "goodbyeproj1".getBytes();
swapStore.upload(
"proj1",
new ByteArrayInputStream(proj1Contents),
proj1Contents.length
);
assertArrayEquals(
proj1Contents,
IOUtils.toByteArray(swapStore.openDownloadStream("proj1"))
);
swapStore.upload(
"proj1",
new ByteArrayInputStream(proj1NewContents),
proj1NewContents.length
);
assertArrayEquals(
proj1NewContents,
IOUtils.toByteArray(swapStore.openDownloadStream("proj1"))
);
}
@Test
public void canRemoveFiles() throws IOException {
byte[] projContents = "total garbage".getBytes();
swapStore.upload(
"proj",
new ByteArrayInputStream(projContents),
projContents.length
);
assertArrayEquals(
projContents,
IOUtils.toByteArray(swapStore.openDownloadStream("proj"))
);
swapStore.remove("proj");
exception.expect(IllegalArgumentException.class);
swapStore.openDownloadStream("proj");
}
}