First integration test.

This commit is contained in:
Winston Li
2015-01-11 11:23:33 +00:00
parent 3a532a76df
commit 3f67e62846
13 changed files with 199 additions and 31 deletions

View File

@@ -3,13 +3,15 @@ package uk.ac.ic.wlgitbridge.test;
import uk.ac.ic.wlgitbridge.test.server.MockSnapshotServer;
import uk.ac.ic.wlgitbridge.test.state.SnapshotAPIState;
import java.io.File;
/**
* Created by Winston on 10/01/15.
*/
public class Main {
public static void main(String[] args) {
MockSnapshotServer server = new MockSnapshotServer();
MockSnapshotServer server = new MockSnapshotServer(new File("/Users/Roxy/Code/java/writelatex-git-bridge"));
server.setState(new SnapshotAPIState());
server.start();
}

View File

@@ -25,17 +25,19 @@ public class MockSnapshotRequestHandler extends AbstractHandler {
@Override
public void handle(String target, final Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
boolean handled;
try {
final SnapshotResponse snapshotResponse = responseBuilder.buildWithTarget(target, baseRequest.getMethod());
response.getWriter().println(snapshotResponse.respond());
new PostbackThread(baseRequest.getReader(), snapshotResponse.postback()).startIfNotNull();
handled = true;
} catch (InvalidAPICallException e) {
Util.printStackTrace(e);
handled = false;
} catch (RuntimeException e) {
Util.printStackTrace(e);
handled = true;
}
baseRequest.setHandled(true);
baseRequest.setHandled(handled);
}
}

View File

@@ -2,10 +2,14 @@ package uk.ac.ic.wlgitbridge.test.server;
import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.ResourceHandler;
import uk.ac.ic.wlgitbridge.test.response.SnapshotResponseBuilder;
import uk.ac.ic.wlgitbridge.test.state.SnapshotAPIState;
import uk.ac.ic.wlgitbridge.util.Util;
import java.io.File;
/**
* Created by Winston on 09/01/15.
*/
@@ -15,10 +19,23 @@ public class MockSnapshotServer {
private final SnapshotResponseBuilder responseBuilder;
private int port;
public MockSnapshotServer() {
public MockSnapshotServer(File resourceBase) {
server = new Server(60000);
responseBuilder = new SnapshotResponseBuilder();
server.setHandler(new MockSnapshotRequestHandler(responseBuilder));
server.setHandler(getHandlerForResourceBase(resourceBase));
}
private HandlerCollection getHandlerForResourceBase(File resourceBase) {
HandlerCollection handlers = new HandlerCollection();
handlers.addHandler(new MockSnapshotRequestHandler(responseBuilder));
handlers.addHandler(resourceHandlerWithBase(resourceBase));
return handlers;
}
private ResourceHandler resourceHandlerWithBase(File resourceBase) {
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setResourceBase(resourceBase.getAbsolutePath());
return resourceHandler;
}
public void start() {

View File

@@ -0,0 +1,10 @@
package uk.ac.ic.wlgitbridge.test.state;
/**
* Created by Winston on 11/01/15.
*/
public class SnapshotAPIStateManager {
}

View File

@@ -1,5 +1,11 @@
package uk.ac.ic.wlgitbridge.test.util;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.NoHeadException;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
@@ -13,6 +19,20 @@ import java.util.Set;
*/
public class FileUtil {
public static boolean currentCommitsAreEqual(Path dir1, Path dir2) {
try {
RevCommit commit1 = new Git(new FileRepositoryBuilder().setWorkTree(dir1.toFile().getAbsoluteFile()).build()).log().call().iterator().next();
RevCommit commit2 = new Git(new FileRepositoryBuilder().setWorkTree(dir2.toFile().getAbsoluteFile()).build()).log().call().iterator().next();
return commit1.equals(commit2);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (NoHeadException e) {
return false;
} catch (GitAPIException e) {
throw new RuntimeException(e);
}
}
public static boolean gitDirectoriesAreEqual(Path dir1, Path dir2) {
Set<String> dir1Contents = getAllFilesRecursivelyInDirectoryApartFrom(dir1, dir1.resolve(".git"));
Set<String> dir2Contents = getAllFilesRecursivelyInDirectoryApartFrom(dir2, dir2.resolve(".git"));
@@ -34,13 +54,19 @@ public class FileUtil {
try {
byte[] firstContents = Files.readAllBytes(first);
byte[] secondContents = Files.readAllBytes(second);
return Arrays.equals(firstContents, secondContents);
boolean equals = Arrays.equals(firstContents, secondContents);
if (!equals) {
System.out.println("Not equal: (" + first + ", " + second + ")");
System.out.println(first + ": " + new String(firstContents));
System.out.println(second + ": " + new String(secondContents));
}
return equals;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static Set<String> getAllFilesRecursivelyInDirectoryApartFrom(Path dir, Path excluded) {
public static Set<String> getAllFilesRecursivelyInDirectoryApartFrom(Path dir, Path excluded) {
if (!dir.toFile().isDirectory()) {
throw new IllegalArgumentException("need a directory");
}