WLGitBridgeIntegrationTest.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.Before;
  5. import org.junit.Rule;
  6. import org.junit.Test;
  7. import org.junit.rules.TemporaryFolder;
  8. import uk.ac.ic.wlgitbridge.application.WLGitBridgeApplication;
  9. import uk.ac.ic.wlgitbridge.test.server.MockSnapshotServer;
  10. import uk.ac.ic.wlgitbridge.test.state.SnapshotAPIState;
  11. import uk.ac.ic.wlgitbridge.test.state.SnapshotAPIStateBuilder;
  12. import uk.ac.ic.wlgitbridge.test.util.FileUtil;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.io.PrintWriter;
  17. import java.net.URISyntaxException;
  18. import java.nio.file.Path;
  19. import java.nio.file.Paths;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import static org.junit.Assert.assertTrue;
  23. /**
  24. * Created by Winston on 11/01/15.
  25. */
  26. public class WLGitBridgeIntegrationTest {
  27. private MockSnapshotServer server;
  28. private Map<String, Map<String, SnapshotAPIState>> states =
  29. new HashMap<String, Map<String, SnapshotAPIState>>() {{
  30. put("canCloneARepository", new HashMap<String, SnapshotAPIState>() {{
  31. put("state", new SnapshotAPIStateBuilder(getResourceAsStream("/canCloneARepository/state/state.json")).build());
  32. }});
  33. }};
  34. @Rule
  35. public TemporaryFolder folder = new TemporaryFolder();
  36. @Before
  37. public void startMockSnapshotAPIServer() throws URISyntaxException {
  38. server = new MockSnapshotServer(Paths.get(getClass().getResource("/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest/").toURI()).toFile());
  39. server.start();
  40. }
  41. @Test
  42. public void canCloneARepository() throws IOException, GitAPIException {
  43. server.setState(states.get("canCloneARepository").get("state"));
  44. WLGitBridgeApplication wlgb = new WLGitBridgeApplication(new String[] {
  45. makeConfigFile()
  46. });
  47. wlgb.run();
  48. folder.create();
  49. File git = folder.newFolder();
  50. Git.cloneRepository()
  51. .setURI("http://127.0.0.1:30080/testproj.git")
  52. .setDirectory(git)
  53. .call()
  54. .close();
  55. wlgb.stop();
  56. assertTrue(FileUtil.gitDirectoriesAreEqual(getResource("/canCloneARepository/state/git"), git.toPath()));
  57. }
  58. private String makeConfigFile() throws IOException {
  59. File wlgb = folder.newFolder();
  60. File config = folder.newFile();
  61. PrintWriter writer = new PrintWriter(config);
  62. writer.println("{\n" +
  63. "\t\"port\": 30080,\n" +
  64. "\t\"rootGitDirectory\": \"" + wlgb.getAbsolutePath() + "\",\n" +
  65. "\t\"apiBaseUrl\": \"http://127.0.0.1:60000/api/v0\",\n" +
  66. "\t\"username\": \"\",\n" +
  67. "\t\"password\": \"\",\n" +
  68. "\t\"postbackBaseUrl\": \"http://127.0.0.1:30080\",\n" +
  69. "\t\"serviceName\": \"Overleaf\"\n" +
  70. "}\n");
  71. writer.close();
  72. return config.getAbsolutePath();
  73. }
  74. private Path getResource(String path) {
  75. try {
  76. return Paths.get(getClass().getResource("/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest" + path).toURI());
  77. } catch (URISyntaxException e) {
  78. throw new RuntimeException(e);
  79. }
  80. }
  81. private InputStream getResourceAsStream(String path) {
  82. return getClass().getResourceAsStream("/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest" + path);
  83. }
  84. }