forked from gitlab4j/gitlab4j-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestImportExportApi.java
185 lines (153 loc) · 6.4 KB
/
TestImportExportApi.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Greg Messner <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.gitlab4j.api;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import java.io.File;
import java.util.Optional;
import org.gitlab4j.api.models.ExportStatus;
import org.gitlab4j.api.models.ImportStatus;
import org.gitlab4j.api.models.Project;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* In order for these tests to run you must set the following properties in ~/test-gitlab4j.properties
*
* TEST_NAMESPACE
* TEST_PROJECT_NAME
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
*
* If any of the above are NULL, all tests in this class will be skipped.
*/
@Tag("integration")
@ExtendWith(SetupIntegrationTestExtension.class)
@org.junit.jupiter.api.Disabled("Integration tests are disabled, see https://github.com/gitlab4j/gitlab4j-api/issues/1165")
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestImportExportApi extends AbstractIntegrationTest {
private static final String TEST_IMPORT_PROJECT_NAME = "test-import-project";
private static GitLabApi gitLabApi;
private static Project testProject;
public TestImportExportApi() {
super();
}
@BeforeAll
public static void setup() {
// Must setup the connection to the GitLab test server
gitLabApi = baseTestSetup();
testProject = getTestProject();
deleteAllTestProjects();
}
@AfterAll
public static void teardown() throws GitLabApiException {
deleteAllTestProjects();
}
private static void deleteAllTestProjects() {
if (gitLabApi == null) {
return;
}
try {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_IMPORT_PROJECT_NAME);
gitLabApi.getProjectApi().deleteProject(project);
} catch (GitLabApiException ignore) {}
}
@BeforeEach
public void beforeMethod() {
assumeTrue(gitLabApi != null);
}
@Test
public void testExportDownloadAndImport() throws GitLabApiException {
// Arrange
assumeTrue(testProject != null);
// Act
gitLabApi.getImportExportApi().scheduleExport(testProject);
// Wait up to 40 seconds for the export to complete
System.out.print("Waiting for export to complete");
int retries = 0;
while (true) {
System.out.print(".");
ExportStatus exportStatus = gitLabApi.getImportExportApi().getExportStatus(testProject);
if (exportStatus.getExportStatus() == ExportStatus.Status.FINISHED) {
System.out.println("done");
break;
}
if (retries >= 6) {
System.out.println("aborting!");
fail("Project export is taking too long, failing test.");
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
retries++;
}
File exportDownload = null;
try {
System.out.println("Downloading exported project");
exportDownload = gitLabApi.getImportExportApi().downloadExport(testProject, null);
assertNotNull(exportDownload);
assertTrue(exportDownload.length() > 2000, "length is not as expected. Current value: " + exportDownload.length());
ImportStatus importStatus = gitLabApi.getImportExportApi().startImport(null, exportDownload,
TEST_IMPORT_PROJECT_NAME, true, null);
assertNotNull(importStatus);
Long newProjectId = importStatus.getId();
// Wait up to 40 seconds for the import to complete
System.out.print("Waiting for import to complete");
retries = 0;
while (true) {
System.out.print(".");
importStatus = gitLabApi.getImportExportApi().getImportStatus(newProjectId);
if (importStatus.getImportStatus() == ImportStatus.Status.FINISHED) {
System.out.println("done");
break;
}
if (retries >= 6) {
System.out.println("aborting!");
fail("Project import is taking too long, failing test.");
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
retries++;
}
Optional<Project> newProject = gitLabApi.getProjectApi().getOptionalProject(newProjectId);
assertTrue(newProject.isPresent());
assertEquals(newProjectId, newProject.get().getId());
assertEquals(TEST_IMPORT_PROJECT_NAME, newProject.get().getName());
} finally {
if (exportDownload != null) {
exportDownload.delete();
}
}
}
}