mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-01 05:11:34 +02:00
Added integration test directory and wrote FileUtil and tests.
This commit is contained in:
@@ -24,7 +24,21 @@ public class SnapshotAPIState {
|
||||
private Map<String, SnapshotPushResult> push;
|
||||
private Map<String, SnapshotPostbackRequest> postback;
|
||||
|
||||
public SnapshotAPIState(Map<String, SnapshotGetDocResult> getDoc,
|
||||
Map<String, SnapshotGetSavedVersResult> getSavedVers,
|
||||
Map<String, Map<Integer, SnapshotGetForVersionResult>> getForVers,
|
||||
Map<String, SnapshotPushResult> push,
|
||||
Map<String, SnapshotPostbackRequest> postback) {
|
||||
|
||||
this.getDoc = getDoc;
|
||||
this.getSavedVers = getSavedVers;
|
||||
this.getForVers = getForVers;
|
||||
this.push = push;
|
||||
this.postback = postback;
|
||||
}
|
||||
|
||||
public SnapshotAPIState() {
|
||||
|
||||
getDoc = new HashMap<String, SnapshotGetDocResult>();
|
||||
getDoc.put("1826rqgsdb", new SnapshotGetDocResult(243, "2014-11-30T18:40:58Z", "jdleesmiller+1@gmail.com", "John+1"));
|
||||
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
package uk.ac.ic.wlgitbridge.test.state;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import uk.ac.ic.wlgitbridge.test.response.push.data.SnapshotPushResult;
|
||||
import uk.ac.ic.wlgitbridge.test.response.push.data.SnapshotPushResultOutOfDate;
|
||||
import uk.ac.ic.wlgitbridge.test.response.push.data.SnapshotPushResultSuccess;
|
||||
import uk.ac.ic.wlgitbridge.test.response.push.postback.*;
|
||||
import uk.ac.ic.wlgitbridge.test.response.push.postback.invalidfile.InvalidFileError;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocResult;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotAttachment;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotData;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotFile;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionResult;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotGetSavedVersResult;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotInfo;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by Winston on 11/01/15.
|
||||
*/
|
||||
public class SnapshotAPIStateBuilder {
|
||||
|
||||
private final JsonArray projects;
|
||||
|
||||
private Map<String, SnapshotGetDocResult> getDoc = new HashMap<String, SnapshotGetDocResult>();
|
||||
private Map<String, SnapshotGetSavedVersResult> getSavedVers = new HashMap<String, SnapshotGetSavedVersResult>();
|
||||
private Map<String, Map<Integer, SnapshotGetForVersionResult>> getForVers = new HashMap<String, Map<Integer, SnapshotGetForVersionResult>>();
|
||||
private Map<String, SnapshotPushResult> push = new HashMap<String, SnapshotPushResult>();
|
||||
private Map<String, SnapshotPostbackRequest> postback = new HashMap<String, SnapshotPostbackRequest>();
|
||||
|
||||
public SnapshotAPIStateBuilder(InputStream stream) {
|
||||
projects = new Gson().fromJson(new InputStreamReader(stream), JsonArray.class);
|
||||
}
|
||||
|
||||
public SnapshotAPIState build() {
|
||||
for (JsonElement project : projects) {
|
||||
addProject(project.getAsJsonObject());
|
||||
}
|
||||
return new SnapshotAPIState(getDoc, getSavedVers, getForVers, push, postback);
|
||||
}
|
||||
|
||||
private void addProject(JsonObject project) {
|
||||
String projectName = project.get("project").getAsString();
|
||||
addGetDocForProject(projectName, project.get("getDoc").getAsJsonObject());
|
||||
addGetSavedVersForProject(projectName, project.get("getSavedVers").getAsJsonArray());
|
||||
addGetForVersForProject(projectName, project.get("getForVers").getAsJsonArray());
|
||||
addPushForProject(projectName, project.get("push").getAsString());
|
||||
addPostbackForProject(projectName, project.get("postback").getAsJsonObject());
|
||||
}
|
||||
|
||||
private void addGetDocForProject(String projectName, JsonObject jsonGetDoc) {
|
||||
getDoc.put(projectName,
|
||||
new SnapshotGetDocResult(jsonGetDoc.get("versionID").getAsInt(),
|
||||
jsonGetDoc.get("createdAt").getAsString(),
|
||||
jsonGetDoc.get("email").getAsString(),
|
||||
jsonGetDoc.get("name").getAsString()));
|
||||
}
|
||||
|
||||
private void addGetSavedVersForProject(String projectName, JsonArray jsonGetSavedVers) {
|
||||
List<SnapshotInfo> savedVers = new LinkedList<SnapshotInfo>();
|
||||
for (JsonElement ver : jsonGetSavedVers) {
|
||||
savedVers.add(getSnapshotInfo(ver.getAsJsonObject()));
|
||||
}
|
||||
getSavedVers.put(projectName, new SnapshotGetSavedVersResult(savedVers));
|
||||
}
|
||||
|
||||
private SnapshotInfo getSnapshotInfo(JsonObject jsonSnapshotInfo) {
|
||||
return new SnapshotInfo(jsonSnapshotInfo.get("versionID").getAsInt(),
|
||||
jsonSnapshotInfo.get("comment").getAsString(),
|
||||
jsonSnapshotInfo.get("email").getAsString(),
|
||||
jsonSnapshotInfo.get("name").getAsString(),
|
||||
jsonSnapshotInfo.get("createdAt").getAsString());
|
||||
}
|
||||
|
||||
private void addGetForVersForProject(String projectName, JsonArray jsonGetForVers) {
|
||||
Map<Integer, SnapshotGetForVersionResult> forVers = new HashMap<Integer, SnapshotGetForVersionResult>();
|
||||
for (JsonElement forVer : jsonGetForVers) {
|
||||
JsonObject forVerObj = forVer.getAsJsonObject();
|
||||
forVers.put(forVerObj.get("versionID").getAsInt(),
|
||||
new SnapshotGetForVersionResult(new SnapshotData(getSrcs(forVerObj.get("srcs").getAsJsonArray()),
|
||||
getAtts(forVerObj.get("atts").getAsJsonArray()))));
|
||||
}
|
||||
getForVers.put(projectName, forVers);
|
||||
}
|
||||
|
||||
private List<SnapshotFile> getSrcs(JsonArray jsonSrcs) {
|
||||
List<SnapshotFile> srcs = new LinkedList<SnapshotFile>();
|
||||
for (JsonElement src : jsonSrcs) {
|
||||
srcs.add(getSrc(src.getAsJsonObject()));
|
||||
}
|
||||
return srcs;
|
||||
}
|
||||
|
||||
private SnapshotFile getSrc(JsonObject jsonSrc) {
|
||||
return new SnapshotFile(jsonSrc.get("content").getAsString(),
|
||||
jsonSrc.get("path").getAsString());
|
||||
}
|
||||
|
||||
private List<SnapshotAttachment> getAtts(JsonArray jsonAtts) {
|
||||
List<SnapshotAttachment> atts = new LinkedList<SnapshotAttachment>();
|
||||
for (JsonElement att : jsonAtts) {
|
||||
atts.add(getAtt(att.getAsJsonObject()));
|
||||
}
|
||||
return atts;
|
||||
}
|
||||
|
||||
private SnapshotAttachment getAtt(JsonObject jsonAtt) {
|
||||
return new SnapshotAttachment(jsonAtt.get("url").getAsString(),
|
||||
jsonAtt.get("path").getAsString());
|
||||
}
|
||||
|
||||
private void addPushForProject(String projectName, String jsonPush) {
|
||||
SnapshotPushResult p;
|
||||
if (jsonPush.equals("success")) {
|
||||
p = new SnapshotPushResultSuccess();
|
||||
} else if (jsonPush.equals("outOfDate")) {
|
||||
p = new SnapshotPushResultOutOfDate();
|
||||
} else {
|
||||
throw new IllegalArgumentException("invalid push");
|
||||
}
|
||||
push.put(projectName, p);
|
||||
}
|
||||
|
||||
private void addPostbackForProject(String projectName, JsonObject jsonPostback) {
|
||||
SnapshotPostbackRequest p;
|
||||
String type = jsonPostback.get("type").getAsString();
|
||||
if (type.equals("success")) {
|
||||
p = new SnapshotPostbackRequestSuccess(jsonPostback.get("versionID").getAsInt());
|
||||
} else if (type.equals("outOfDate")) {
|
||||
p = new SnapshotPostbackRequestOutOfDate();
|
||||
} else if (type.equals("invalidFiles")) {
|
||||
p = new SnapshotPostbackRequestInvalidFiles(new LinkedList<InvalidFileError>());
|
||||
} else if (type.equals("invalidProject")) {
|
||||
p = new SnapshotPostbackRequestInvalidProject(new LinkedList<String>());
|
||||
} else if (type.equals("error")) {
|
||||
p = new SnapshotPostbackRequestError();
|
||||
} else {
|
||||
throw new IllegalArgumentException("invalid postback type");
|
||||
}
|
||||
postback.put(projectName, p);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package uk.ac.ic.wlgitbridge.test.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Created by Winston on 11/01/15.
|
||||
*/
|
||||
public class FileUtil {
|
||||
|
||||
public static boolean gitDirectoriesAreEqual(Path dir1, Path dir2) {
|
||||
Set<String> dir1Contents = getAllFilesRecursivelyInDirectoryApartFrom(dir1, dir1.resolve(".git"));
|
||||
Set<String> dir2Contents = getAllFilesRecursivelyInDirectoryApartFrom(dir2, dir2.resolve(".git"));
|
||||
return dir1Contents.equals(dir2Contents) && directoryContentsEqual(dir1Contents, dir1, dir2);
|
||||
}
|
||||
|
||||
static boolean directoryContentsEqual(Set<String> dirContents, Path dir1, Path dir2) {
|
||||
for (String file : dirContents) {
|
||||
Path path1 = dir1.resolve(file);
|
||||
Path path2 = dir2.resolve(file);
|
||||
if (!path1.toFile().isDirectory() && !path2.toFile().isDirectory() && !fileContentsEqual(path1, path2)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean fileContentsEqual(Path first, Path second) {
|
||||
try {
|
||||
byte[] firstContents = Files.readAllBytes(first);
|
||||
byte[] secondContents = Files.readAllBytes(second);
|
||||
return Arrays.equals(firstContents, secondContents);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
static Set<String> getAllFilesRecursivelyInDirectoryApartFrom(Path dir, Path excluded) {
|
||||
if (!dir.toFile().isDirectory()) {
|
||||
throw new IllegalArgumentException("need a directory");
|
||||
}
|
||||
return getAllFilesRecursively(dir, dir, excluded);
|
||||
}
|
||||
|
||||
static Set<String> getAllFilesRecursively(Path baseDir, Path dir, Path excluded) {
|
||||
Set<String> files = new HashSet<String>();
|
||||
for (File file : dir.toFile().listFiles()) {
|
||||
if (!file.equals(excluded.toFile())) {
|
||||
files.add(baseDir.relativize(file.toPath()).toString());
|
||||
if (file.isDirectory()) {
|
||||
files.addAll(getAllFilesRecursively(baseDir, file.toPath(), excluded));
|
||||
}
|
||||
}
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user