diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/swap/InMemorySwapStore.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/swap/InMemorySwapStore.java new file mode 100644 index 0000000000..05a919bb26 --- /dev/null +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/swap/InMemorySwapStore.java @@ -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 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); + } + +} diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/swap/SwapJobImpl.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/swap/SwapJobImpl.java index 06209df6d7..26f27891b0 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/swap/SwapJobImpl.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/swap/SwapJobImpl.java @@ -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()); + } } diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/swap/SwapStore.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/swap/SwapStore.java index 4394adca11..dc82c35706 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/swap/SwapStore.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/swap/SwapStore.java @@ -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); diff --git a/services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/bridge/swap/InMemorySwapStoreTest.java b/services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/bridge/swap/InMemorySwapStoreTest.java new file mode 100644 index 0000000000..34553ae287 --- /dev/null +++ b/services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/bridge/swap/InMemorySwapStoreTest.java @@ -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"); + } + +} \ No newline at end of file