5
5
import static org .junit .Assert .fail ;
6
6
7
7
import java .util .Arrays ;
8
+ import java .util .Optional ;
8
9
10
+ import org .gitlab4j .api .models .Group ;
11
+ import org .gitlab4j .api .models .Project ;
12
+ import org .gitlab4j .api .models .RepositoryFile ;
13
+ import org .gitlab4j .api .models .User ;
14
+ import org .gitlab4j .api .models .Visibility ;
9
15
import org .gitlab4j .api .utils .AccessTokenUtils ;
10
16
import org .junit .AfterClass ;
11
17
import org .junit .BeforeClass ;
15
21
import com .googlecode .junittoolbox .SuiteClasses ;
16
22
import com .googlecode .junittoolbox .WildcardPatternSuite ;
17
23
24
+ /**
25
+ * This test suite implementation will check for the users, projects, groups, and repo files needed for testing
26
+ * and create them if they do not exist. It will also create temporary personal access tokens needed for testing.
27
+ *
28
+ * <p>NOTE: This class relies on a minimal amount of the GitLab4J-API library to set things up,
29
+ * so if there are any failures the test suite will fail. Consider it the first integration tests
30
+ * that are being performed.</p>
31
+ */
18
32
@ RunWith (WildcardPatternSuite .class )
19
33
@ SuiteClasses ({"**/Test*.class" })
20
34
@ IncludeCategory (IntegrationTest .class )
21
35
public class IntegrationTestSuite implements PropertyConstants {
22
36
23
- private static final String TEST_LOGIN_USERNAME = HelperUtils .getProperty (LOGIN_USERNAME_KEY );
24
- private static final String TEST_LOGIN_PASSWORD = HelperUtils .getProperty (LOGIN_PASSWORD_KEY );
25
- private static final String TEST_HOST_URL = HelperUtils .getProperty (HOST_URL_KEY );
37
+ private static final String TEST_HOST_URL = HelperUtils .getProperty (HOST_URL_KEY , "http://localhost:8090" );
38
+ private static final String TEST_LOGIN_USERNAME = HelperUtils .getProperty (LOGIN_USERNAME_KEY , "gitlab4j" );
39
+ private static final String TEST_LOGIN_PASSWORD = HelperUtils .getProperty (LOGIN_PASSWORD_KEY , "ChangeMeNow" );
40
+
41
+ private static final String TEST_PROJECT_NAME = HelperUtils .getProperty (PROJECT_NAME_KEY , "test-project" );
42
+ private static final String TEST_GROUP = HelperUtils .getProperty (GROUP_KEY , "test-group" );
43
+ private static final String TEST_GROUP_PROJECT_NAME = HelperUtils .getProperty (GROUP_PROJECT_KEY , "test-group-project" );
26
44
27
- protected static final String TEST_PROJECT_NAME = HelperUtils .getProperty (PROJECT_NAME_KEY );
28
- protected static final String TEST_NAMESPACE = HelperUtils .getProperty (NAMESPACE_KEY );
45
+ protected static final String TEST_NAMESPACE = HelperUtils .getProperty (NAMESPACE_KEY , TEST_LOGIN_USERNAME );
29
46
30
47
protected static final String TEST_PRIVATE_TOKEN_NAME = "GitLab4J Test Private Token - " + HelperUtils .getRandomInt (1000 );
31
48
protected static String TEST_PRIVATE_TOKEN = HelperUtils .getProperty (PRIVATE_TOKEN_KEY );
@@ -60,6 +77,22 @@ public static void suiteSetup() throws GitLabApiException {
60
77
fail (problems );
61
78
}
62
79
80
+ seedData ();
81
+ createAccessTokens ();
82
+ }
83
+
84
+ @ AfterClass
85
+ public static void suiteTeardown () throws GitLabApiException {
86
+
87
+ System .out .println ("********************************************************" );
88
+ System .out .println ("* Test Suite Teardown *" );
89
+ System .out .println ("********************************************************" );
90
+
91
+ revokeAccessTokens ();
92
+ }
93
+
94
+ private static void createAccessTokens () throws GitLabApiException {
95
+
63
96
// If the private token is not in the properties, create it
64
97
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN .trim ().isEmpty ()) {
65
98
@@ -87,12 +120,7 @@ public static void suiteSetup() throws GitLabApiException {
87
120
}
88
121
}
89
122
90
- @ AfterClass
91
- public static void suiteTeardown () throws GitLabApiException {
92
-
93
- System .out .println ("********************************************************" );
94
- System .out .println ("* Test Suite Teardown *" );
95
- System .out .println ("********************************************************" );
123
+ private static void revokeAccessTokens () throws GitLabApiException {
96
124
97
125
if (createdPrivateToken && TEST_PRIVATE_TOKEN != null ) {
98
126
try {
@@ -112,4 +140,150 @@ public static void suiteTeardown() throws GitLabApiException {
112
140
} catch (Exception ignore ) {}
113
141
}
114
142
}
143
+
144
+ /**
145
+ * This method will check for the users, prjects, groups, and repo files needed for testing
146
+ * and create them if they do not exist.
147
+ *
148
+ * @throws GitLabApiException if any error occurs
149
+ */
150
+ private static void seedData () throws GitLabApiException {
151
+
152
+ // Use OAUTH2 and the provided admin credentials to create the user to run the tests as if it doesn't exist
153
+ String username = HelperUtils .getProperty (ADMIN_USERNAME_KEY );
154
+ if (username == null || username .trim ().isEmpty ()) {
155
+ username = System .getProperty (ADMIN_USERNAME_KEY );
156
+ username = (username == null || username .trim ().isEmpty () ? "root" : username );
157
+ }
158
+
159
+ String password = HelperUtils .getProperty (ADMIN_PASSWORD_KEY );
160
+ if (password == null || password .trim ().isEmpty ()) {
161
+ password = System .getProperty (ADMIN_PASSWORD_KEY );
162
+ password = (password == null || password .trim ().isEmpty () ? "password" : password );
163
+ }
164
+
165
+ GitLabApi gitLabApi = GitLabApi .oauth2Login (TEST_HOST_URL , username , password , null , null , true );
166
+
167
+ // If the tester user doen't exists, create it
168
+ Optional <User > optionalUser = gitLabApi .getUserApi ().getOptionalUser (TEST_LOGIN_USERNAME );
169
+ if (!optionalUser .isPresent ()) {
170
+ User userSettings = new User ()
171
+ .withUsername (TEST_LOGIN_USERNAME )
172
+ .withEmail (TEST_LOGIN_USERNAME + "@gitlab4j.org" )
173
+ .withName ("GitLab4J Tester" )
174
+ .withSkipConfirmation (true )
175
+ .withIsAdmin (true );
176
+ gitLabApi .getUserApi ().createUser (userSettings , TEST_LOGIN_PASSWORD , false );
177
+ System .out .format ("Created %s user (%s)%n" , userSettings .getName (), userSettings .getUsername ());
178
+ }
179
+
180
+ // The reset of the operations will use the test user to do things,
181
+ // so use OAUTH2 to get the GitLabApi instance
182
+ gitLabApi = GitLabApi .oauth2Login (TEST_HOST_URL , TEST_LOGIN_USERNAME , TEST_LOGIN_PASSWORD , null , null , true );
183
+
184
+ // Create the sudo as user if it does not exists
185
+ username = HelperUtils .getProperty (SUDO_AS_USERNAME_KEY , "user1" );
186
+ optionalUser = gitLabApi .getUserApi ().getOptionalUser (username );
187
+ if (!optionalUser .isPresent ()) {
188
+ User userSettings = new User ()
189
+ .withUsername (username )
190
+ .withEmail (username + "@gitlab4j.org" )
191
+ .withName ("Test User" )
192
+ .withSkipConfirmation (true )
193
+ .withIsAdmin (false );
194
+ gitLabApi .getUserApi ().createUser (userSettings , TEST_LOGIN_PASSWORD , false );
195
+ System .out .format ("Created %s user (%s)%n" , userSettings .getName (), userSettings .getUsername ());
196
+ }
197
+
198
+ // Create the test project
199
+ Optional <Project > optionalProject = gitLabApi .getProjectApi ().getOptionalProject (TEST_NAMESPACE , TEST_PROJECT_NAME );
200
+ Project testProject = optionalProject .orElse (null );
201
+ if (testProject == null ) {
202
+
203
+ Project projectSettings = new Project ()
204
+ .withName (TEST_PROJECT_NAME )
205
+ .withDefaultBranch ("master" )
206
+ .withPublic (true )
207
+ .withInitializeWithReadme (true );
208
+ testProject = gitLabApi .getProjectApi ().createProject (projectSettings );
209
+ System .out .format ("Created %s project%n" , projectSettings .getName ());
210
+
211
+ // Update the contents of README.md, so we have at minimum 2 commits
212
+ RepositoryFile repoFile = new RepositoryFile ();
213
+ repoFile .setFilePath ("README.md" );
214
+ repoFile .setContent ("This is a test project used to test GitLab4J-API." );
215
+ gitLabApi .getRepositoryFileApi ().updateFile (testProject , repoFile , "master" , "Updated contents" );
216
+ System .out .format ("Updated content of %s repository file%n" , repoFile .getFilePath ());
217
+
218
+ // Create a file in a subdirectory
219
+ repoFile .setFilePath (TEST_PROJECT_SUBDIRECTORY_PATH );
220
+ gitLabApi .getRepositoryFileApi ().createFile (testProject , repoFile , "master" , "Initial commit." );
221
+ System .out .format ("Created %s repository file%n" , repoFile .getFilePath ());
222
+
223
+ } else if (!gitLabApi .getRepositoryFileApi ().getOptionalFile (testProject , "README.md" , "master" ).isPresent ()) {
224
+
225
+ // Create the README.md file since it does not exists
226
+ RepositoryFile repoFile = new RepositoryFile ();
227
+ repoFile .setFilePath ("README.md" );
228
+ repoFile .setContent ("" );
229
+ gitLabApi .getRepositoryFileApi ().createFile (testProject , repoFile , "master" , "Initial commit." );
230
+ System .out .format ("Created %s repository file%n" , repoFile .getFilePath ());
231
+
232
+ // Update the contents so we have at minimum 2 commits
233
+ repoFile .encodeAndSetContent ("This is a test project used to test GitLab4J-API." );
234
+ gitLabApi .getRepositoryFileApi ().updateFile (testProject , repoFile , "master" , "Updated contents" );
235
+ System .out .format ("Updated content of %s repository file%n" , repoFile .getFilePath ());
236
+ }
237
+
238
+ // Create the test group if it does not exist
239
+ Optional <Group > optionalGroup = gitLabApi .getGroupApi ().getOptionalGroup (TEST_GROUP );
240
+ Group testGroup = optionalGroup .orElse (null );
241
+ if (testGroup == null ) {
242
+ Group groupSettings = new Group ()
243
+ .withName ("Test Group" )
244
+ .withPath (TEST_GROUP )
245
+ .withDescription ("Test Group" )
246
+ .withVisibility (Visibility .PUBLIC );
247
+ testGroup = gitLabApi .getGroupApi ().addGroup (groupSettings );
248
+ System .out .format ("Created %s group (%s)%n" , groupSettings .getName (), groupSettings .getPath ());
249
+ }
250
+
251
+ // Create the test project in the test group namespace if it does not exist
252
+ optionalProject = gitLabApi .getProjectApi ().getOptionalProject (TEST_GROUP , TEST_GROUP_PROJECT_NAME );
253
+ testProject = optionalProject .orElse (null );
254
+ if (testProject == null ) {
255
+
256
+ Project projectSettings = new Project ()
257
+ .withName (TEST_GROUP_PROJECT_NAME )
258
+ .withDefaultBranch ("master" )
259
+ .withPublic (true )
260
+ .withInitializeWithReadme (true );
261
+ Project groupProject = gitLabApi .getProjectApi ().createProject (projectSettings );
262
+ System .out .format ("Created %s project%n" , projectSettings .getName ());
263
+
264
+ // Update the contents of README.md, so we have at minimum 2 commits
265
+ RepositoryFile repoFile = new RepositoryFile ();
266
+ repoFile .setFilePath ("README.md" );
267
+ repoFile .encodeAndSetContent ("This is a test project used to test GitLab4J-API." );
268
+ gitLabApi .getRepositoryFileApi ().updateFile (groupProject , repoFile , "master" , "Updated contents" );
269
+ System .out .format ("Updated content of %s repository file%n" , repoFile .getFilePath ());
270
+
271
+ gitLabApi .getGroupApi ().transferProject (testGroup , groupProject );
272
+ System .out .format ("Transfered %s project to %s group%n" , TEST_GROUP_PROJECT_NAME , TEST_GROUP );
273
+
274
+ } else if (!gitLabApi .getRepositoryFileApi ().getOptionalFile (testProject , "README.md" , "master" ).isPresent ()) {
275
+
276
+ // Create the README.md file since it does not exists
277
+ RepositoryFile repoFile = new RepositoryFile ();
278
+ repoFile .setFilePath ("README.md" );
279
+ repoFile .setContent ("" );
280
+ gitLabApi .getRepositoryFileApi ().createFile (testProject , repoFile , "master" , "Initial commit." );
281
+ System .out .format ("Created %s repository file in %s%n" , repoFile .getFilePath (), TEST_GROUP_PROJECT_NAME );
282
+
283
+ // Update the contents so we have at minimum 2 commits
284
+ repoFile .encodeAndSetContent ("This is a test project used to test GitLab4J-API." );
285
+ gitLabApi .getRepositoryFileApi ().updateFile (testProject , repoFile , "master" , "Updated contents" );
286
+ System .out .format ("Updated %s repository file in %s%n" , repoFile .getFilePath (), TEST_GROUP_PROJECT_NAME );
287
+ }
288
+ }
115
289
}
0 commit comments