WLGitBridgeIntegrationTest.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package uk.ac.ic.wlgitbridge;
  2. import org.eclipse.jgit.api.Git;
  3. import org.eclipse.jgit.api.errors.GitAPIException;
  4. import org.junit.Rule;
  5. import org.junit.Test;
  6. import org.junit.rules.TemporaryFolder;
  7. import uk.ac.ic.wlgitbridge.application.WLGitBridgeApplication;
  8. import uk.ac.ic.wlgitbridge.test.server.MockSnapshotServer;
  9. import uk.ac.ic.wlgitbridge.test.state.SnapshotAPIState;
  10. import uk.ac.ic.wlgitbridge.test.state.SnapshotAPIStateBuilder;
  11. import uk.ac.ic.wlgitbridge.test.util.FileUtil;
  12. import java.io.*;
  13. import java.nio.file.Path;
  14. import java.nio.file.Paths;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. import static org.junit.Assert.assertTrue;
  18. /**
  19. * Created by Winston on 11/01/15.
  20. */
  21. public class WLGitBridgeIntegrationTest {
  22. private Map<String, Map<String, SnapshotAPIState>> states =
  23. new HashMap<String, Map<String, SnapshotAPIState>>() {{
  24. put("canCloneARepository", new HashMap<String, SnapshotAPIState>() {{
  25. put("state", new SnapshotAPIStateBuilder(getResourceAsStream("/canCloneARepository/state/state.json")).build());
  26. }});
  27. put("canCloneMultipleRepositories", new HashMap<String, SnapshotAPIState>() {{
  28. put("state", new SnapshotAPIStateBuilder(getResourceAsStream("/canCloneMultipleRepositories/state/state.json")).build());
  29. }});
  30. put("canPullAModifiedTexFile", new HashMap<String, SnapshotAPIState>() {{
  31. put("base", new SnapshotAPIStateBuilder(getResourceAsStream("/canPullAModifiedTexFile/base/state.json")).build());
  32. put("withModifiedTexFile", new SnapshotAPIStateBuilder(getResourceAsStream("/canPullAModifiedTexFile/withModifiedTexFile/state.json")).build());
  33. }});
  34. }};
  35. @Rule
  36. public TemporaryFolder folder = new TemporaryFolder();
  37. @Test
  38. public void canCloneARepository() throws IOException, GitAPIException {
  39. MockSnapshotServer server = new MockSnapshotServer(3857, getResource("/canCloneARepository").toFile());
  40. server.start();
  41. server.setState(states.get("canCloneARepository").get("state"));
  42. WLGitBridgeApplication wlgb = new WLGitBridgeApplication(new String[] {
  43. makeConfigFile(33857, 3857)
  44. });
  45. wlgb.run();
  46. folder.create();
  47. File git = folder.newFolder();
  48. Git.cloneRepository()
  49. .setURI("http://127.0.0.1:33857/testproj.git")
  50. .setDirectory(git)
  51. .call()
  52. .close();
  53. wlgb.stop();
  54. assertTrue(FileUtil.gitDirectoriesAreEqual(getResource("/canCloneARepository/state/testproj"), git.toPath()));
  55. }
  56. @Test
  57. public void canCloneMultipleRepositories() throws IOException, GitAPIException {
  58. MockSnapshotServer server = new MockSnapshotServer(3858, getResource("/canCloneMultipleRepositories").toFile());
  59. server.start();
  60. server.setState(states.get("canCloneMultipleRepositories").get("state"));
  61. WLGitBridgeApplication wlgb = new WLGitBridgeApplication(new String[] {
  62. makeConfigFile(33858, 3858)
  63. });
  64. wlgb.run();
  65. folder.create();
  66. File testproj1 = folder.newFolder();
  67. Git.cloneRepository()
  68. .setURI("http://127.0.0.1:33858/testproj1.git")
  69. .setDirectory(testproj1)
  70. .call()
  71. .close();
  72. File testproj2 = folder.newFolder();
  73. Git.cloneRepository()
  74. .setURI("http://127.0.0.1:33858/testproj2.git")
  75. .setDirectory(testproj2)
  76. .call()
  77. .close();
  78. wlgb.stop();
  79. assertTrue(FileUtil.gitDirectoriesAreEqual(getResource("/canCloneMultipleRepositories/state/testproj1"), testproj1.toPath()));
  80. assertTrue(FileUtil.gitDirectoriesAreEqual(getResource("/canCloneMultipleRepositories/state/testproj2"), testproj2.toPath()));
  81. }
  82. @Test
  83. public void canPullAModifiedTexFile() throws IOException, GitAPIException {
  84. MockSnapshotServer server = new MockSnapshotServer(3859, getResource("/canPullAModifiedTexFile").toFile());
  85. server.start();
  86. server.setState(states.get("canPullAModifiedTexFile").get("base"));
  87. WLGitBridgeApplication wlgb = new WLGitBridgeApplication(new String[] {
  88. makeConfigFile(33859, 3859)
  89. });
  90. wlgb.run();
  91. folder.create();
  92. File git = folder.newFolder();
  93. Git base = Git.cloneRepository()
  94. .setURI("http://127.0.0.1:33859/testproj.git")
  95. .setDirectory(git)
  96. .call();
  97. assertTrue(FileUtil.gitDirectoriesAreEqual(getResource("/canPullAModifiedTexFile/base/testproj"), git.toPath()));
  98. server.setState(states.get("canPullAModifiedTexFile").get("withModifiedTexFile"));
  99. base.pull().call();
  100. base.close();
  101. wlgb.stop();
  102. assertTrue(FileUtil.gitDirectoriesAreEqual(getResource("/canPullAModifiedTexFile/withModifiedTexFile/testproj"), git.toPath()));
  103. }
  104. private String makeConfigFile(int port, int apiPort) throws IOException {
  105. File wlgb = folder.newFolder();
  106. File config = folder.newFile();
  107. PrintWriter writer = new PrintWriter(config);
  108. writer.println("{\n" +
  109. "\t\"port\": " + port + ",\n" +
  110. "\t\"rootGitDirectory\": \"" + wlgb.getAbsolutePath() + "\",\n" +
  111. "\t\"apiBaseUrl\": \"http://127.0.0.1:" + apiPort + "/api/v0\",\n" +
  112. "\t\"username\": \"\",\n" +
  113. "\t\"password\": \"\",\n" +
  114. "\t\"postbackBaseUrl\": \"http://127.0.0.1:" + port + "\",\n" +
  115. "\t\"serviceName\": \"Overleaf\"\n" +
  116. "}\n");
  117. writer.close();
  118. return config.getAbsolutePath();
  119. }
  120. private Path getResource(String path) {
  121. return Paths.get("src/test/resources/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest" + path);
  122. }
  123. private InputStream getResourceAsStream(String path) {
  124. try {
  125. return new FileInputStream(getResource(path).toFile());
  126. } catch (FileNotFoundException e) {
  127. throw new RuntimeException(e);
  128. }
  129. }
  130. }