|
| 1 | +package org.gitlab4j.api; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertNotNull; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | +import static org.junit.Assume.assumeTrue; |
| 8 | + |
| 9 | +import java.io.File; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.nio.file.StandardCopyOption; |
| 15 | +import java.util.Optional; |
| 16 | + |
| 17 | +import org.gitlab4j.api.models.Branch; |
| 18 | +import org.gitlab4j.api.models.Project; |
| 19 | +import org.gitlab4j.api.models.RepositoryFile; |
| 20 | +import org.junit.AfterClass; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.BeforeClass; |
| 23 | +import org.junit.FixMethodOrder; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runners.MethodSorters; |
| 26 | + |
| 27 | +/** |
| 28 | + * In order for these tests to run you must set the following properties in test-gitlab4j.properties |
| 29 | + * |
| 30 | + * TEST_NAMESPACE |
| 31 | + * TEST_PROJECT_NAME |
| 32 | + * TEST_HOST_URL |
| 33 | + * TEST_PRIVATE_TOKEN |
| 34 | + * |
| 35 | + * If any of the above are NULL, all tests in this class will be skipped. |
| 36 | + * |
| 37 | + * NOTE: &FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that testCreate() is executed first. |
| 38 | + */ |
| 39 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 40 | +public class TestRepositoryFileApi { |
| 41 | + |
| 42 | + // The following needs to be set to your test repository |
| 43 | + private static final String TEST_PROJECT_NAME; |
| 44 | + private static final String TEST_NAMESPACE; |
| 45 | + private static final String TEST_HOST_URL; |
| 46 | + private static final String TEST_PRIVATE_TOKEN; |
| 47 | + |
| 48 | + static { |
| 49 | + TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE"); |
| 50 | + TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME"); |
| 51 | + TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL"); |
| 52 | + TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN"); |
| 53 | + } |
| 54 | + |
| 55 | + private static final String TEST_BRANCH_NAME = "feature/test_branch_for_files"; |
| 56 | + private static final String TEST_FILEPATH = "test-file.txt"; |
| 57 | + private static GitLabApi gitLabApi; |
| 58 | + |
| 59 | + public TestRepositoryFileApi() { |
| 60 | + super(); |
| 61 | + } |
| 62 | + |
| 63 | + @BeforeClass |
| 64 | + public static void setup() { |
| 65 | + |
| 66 | + String problems = ""; |
| 67 | + if (TEST_NAMESPACE == null || TEST_NAMESPACE.trim().isEmpty()) { |
| 68 | + problems += "TEST_NAMESPACE cannot be empty\n"; |
| 69 | + } |
| 70 | + |
| 71 | + if (TEST_PROJECT_NAME == null || TEST_PROJECT_NAME.trim().isEmpty()) { |
| 72 | + problems += "TEST_PROJECT_NAME cannot be empty\n"; |
| 73 | + } |
| 74 | + |
| 75 | + if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) { |
| 76 | + problems += "TEST_HOST_URL cannot be empty\n"; |
| 77 | + } |
| 78 | + |
| 79 | + if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().isEmpty()) { |
| 80 | + problems += "TEST_PRIVATE_TOKEN cannot be empty\n"; |
| 81 | + } |
| 82 | + |
| 83 | + if (problems.isEmpty()) { |
| 84 | + gitLabApi = new GitLabApi(TEST_HOST_URL, TEST_PRIVATE_TOKEN); |
| 85 | + teardown(); |
| 86 | + } else { |
| 87 | + System.err.print(problems); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @AfterClass |
| 92 | + public static void teardown() { |
| 93 | + if (gitLabApi != null) { |
| 94 | + |
| 95 | + try { |
| 96 | + Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); |
| 97 | + |
| 98 | + try { |
| 99 | + gitLabApi.getRepositoryFileApi().deleteFile(TEST_FILEPATH, project.getId(), TEST_BRANCH_NAME, "Cleanup test files."); |
| 100 | + } catch (GitLabApiException ignore) { |
| 101 | + } |
| 102 | + |
| 103 | + try { |
| 104 | + gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH_NAME); |
| 105 | + } catch (GitLabApiException ignore) { |
| 106 | + } |
| 107 | + |
| 108 | + } catch (GitLabApiException ignore) { |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Before |
| 114 | + public void beforeMethod() { |
| 115 | + assumeTrue(gitLabApi != null); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testRawFileViaFile() throws GitLabApiException, IOException { |
| 120 | + |
| 121 | + Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); |
| 122 | + assertNotNull(project); |
| 123 | + |
| 124 | + File file = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README", null); |
| 125 | + assertTrue(file.length() > 0); |
| 126 | + file.delete(); |
| 127 | + |
| 128 | + file = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README", new File(".")); |
| 129 | + assertTrue(file.length() > 0); |
| 130 | + file.delete(); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testRepositoryFileViaInputStream() throws GitLabApiException, IOException { |
| 135 | + |
| 136 | + Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); |
| 137 | + assertNotNull(project); |
| 138 | + |
| 139 | + InputStream in = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README"); |
| 140 | + |
| 141 | + Path target = Files.createTempFile(TEST_PROJECT_NAME + "-README", ""); |
| 142 | + Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING); |
| 143 | + |
| 144 | + assertTrue(target.toFile().length() > 0); |
| 145 | + Files.delete(target); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void testRepositoryFileGetFile() throws GitLabApiException, IOException { |
| 150 | + |
| 151 | + Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); |
| 152 | + assertNotNull(project); |
| 153 | + |
| 154 | + RepositoryFile fileInfo = gitLabApi.getRepositoryFileApi().getFileInfo(project.getId(), "README", "master"); |
| 155 | + assertNotNull(fileInfo); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testRepositoryFileGetOptionalFile() throws GitLabApiException, IOException { |
| 160 | + |
| 161 | + Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); |
| 162 | + assertNotNull(project); |
| 163 | + |
| 164 | + Optional<RepositoryFile> fileInfo = gitLabApi.getRepositoryFileApi().getOptionalFileInfo(project.getId(), "README", "master"); |
| 165 | + assertNotNull(fileInfo.get()); |
| 166 | + |
| 167 | + fileInfo = gitLabApi.getRepositoryFileApi().getOptionalFileInfo(project.getId(), "I-DONT-EXIST", "master"); |
| 168 | + assertFalse(fileInfo.isPresent()); |
| 169 | + |
| 170 | + GitLabApiException e = GitLabApi.getOptionalException(fileInfo); |
| 171 | + assertNotNull(e); |
| 172 | + assertEquals(404, e.getHttpStatus()); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + public void testCreateFileAndDeleteFile() throws GitLabApiException { |
| 177 | + |
| 178 | + Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); |
| 179 | + assertNotNull(project); |
| 180 | + |
| 181 | + Branch branch = gitLabApi.getRepositoryApi().createBranch(project.getId(), TEST_BRANCH_NAME, "master"); |
| 182 | + assertNotNull(branch); |
| 183 | + |
| 184 | + RepositoryFile file = new RepositoryFile(); |
| 185 | + file.setFilePath(TEST_FILEPATH); |
| 186 | + file.setContent("This is a test file."); |
| 187 | + RepositoryFile createdFile = gitLabApi.getRepositoryFileApi().createFile(file, project.getId(), TEST_BRANCH_NAME, "Testing createFile()."); |
| 188 | + assertNotNull(createdFile); |
| 189 | + |
| 190 | + gitLabApi.getRepositoryFileApi().deleteFile(TEST_FILEPATH, project.getId(), TEST_BRANCH_NAME, "Testing deleteFile()."); |
| 191 | + gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH_NAME); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + public void testCreateFileWithEmptyContent() throws GitLabApiException { |
| 196 | + |
| 197 | + Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); |
| 198 | + assertNotNull(project); |
| 199 | + |
| 200 | + Branch branch = gitLabApi.getRepositoryApi().createBranch(project.getId(), TEST_BRANCH_NAME, "master"); |
| 201 | + assertNotNull(branch); |
| 202 | + |
| 203 | + RepositoryFile file = new RepositoryFile(); |
| 204 | + file.setFilePath(TEST_FILEPATH); |
| 205 | + file.setContent(""); |
| 206 | + RepositoryFile createdFile = gitLabApi.getRepositoryFileApi().createFile(file, project.getId(), TEST_BRANCH_NAME, "Testing createFile()."); |
| 207 | + assertNotNull(createdFile); |
| 208 | + |
| 209 | + gitLabApi.getRepositoryFileApi().deleteFile(TEST_FILEPATH, project.getId(), TEST_BRANCH_NAME, "Testing deleteFile()."); |
| 210 | + gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH_NAME); |
| 211 | + } |
| 212 | +} |
0 commit comments