forked from timols/java-gitlab-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitlabUploadTest.java
More file actions
60 lines (52 loc) · 1.97 KB
/
Copy pathGitlabUploadTest.java
File metadata and controls
60 lines (52 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package org.gitlab.api;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
import org.gitlab.api.models.GitlabProject;
import org.gitlab.api.models.GitlabUpload;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@Ignore
public class GitlabUploadTest {
private static final String TEST_URL = System.getProperty("TEST_URL", "http://localhost");
private static final String TEST_TOKEN = System.getProperty("TEST_TOKEN", "y0E5b9761b7y4qk");
private static final String TEST_PROJECT = System.getProperty("TEST_PROJECT", "user/project");
@Test
public void testUploadToProject() throws Exception {
GitlabAPI api = GitlabAPI.connect(TEST_URL, TEST_TOKEN);
String content = "test file content";
File tempFile = createTempFile(content);
try {
GitlabUpload upload = api.uploadFile(gitlabProject(api), tempFile);
Assert.assertNotNull(upload.getUrl());
} finally {
tempFile.delete();
}
}
private File createTempFile(String content) throws IOException {
File tempFile = File.createTempFile("upload-", ".txt");
InputStream is = new ByteArrayInputStream(content.getBytes());
OutputStream os = new FileOutputStream(tempFile);
try {
IOUtils.copy(is, os);
} finally {
is.close();
os.close();
}
return tempFile;
}
private GitlabProject gitlabProject(GitlabAPI api) throws IOException {
for (GitlabProject gitlabProject : api.getProjects()) {
String projetPath = String.format("%s/%s", gitlabProject.getNamespace().getPath(), gitlabProject.getPath());
if (projetPath.equals(TEST_PROJECT)) {
return gitlabProject;
}
}
throw new IllegalStateException();
}
}