Skip to content

Commit cf5cb33

Browse files
committed
Mods to support PropertyConstants and use of personal access tokens created by test setup (#311).
1 parent ea90e56 commit cf5cb33

16 files changed

+128
-102
lines changed

src/main/java/org/gitlab4j/api/utils/FileUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.io.Reader;
56
import java.util.Scanner;
67

78
import javax.ws.rs.core.Response;
@@ -75,4 +76,23 @@ public static String readFileContents(File file) throws IOException {
7576
return (in.next());
7677
}
7778
}
79+
80+
/**
81+
* Reads the content of a Reader instance and returns it as a String.
82+
*
83+
* @param reader
84+
* @return the content of a Reader instance as a String
85+
* @throws IOException
86+
*/
87+
public static String getReaderContentAsString(Reader reader) throws IOException {
88+
89+
int count;
90+
final char[] buffer = new char[2048];
91+
final StringBuilder out = new StringBuilder();
92+
while ((count = reader.read(buffer, 0, buffer.length)) >= 0) {
93+
out.append(buffer, 0, count);
94+
}
95+
96+
return (out.toString());
97+
}
7898
}

src/test/java/org/gitlab4j/api/AbstractIntegrationTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,13 @@
1616
* TEST_HOST_URL
1717
* TEST_PRIVATE_TOKEN
1818
*/
19-
public class AbstractIntegrationTest {
19+
public class AbstractIntegrationTest implements PropertyConstants {
2020

2121
// Get the values of the minimum required test properties.
22-
protected static final String TEST_PROJECT_NAME;
23-
protected static final String TEST_NAMESPACE;
24-
protected static final String TEST_HOST_URL;
25-
protected static final String TEST_PRIVATE_TOKEN;
26-
static {
27-
TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
28-
TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME");
29-
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
30-
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
31-
}
22+
protected static final String TEST_PROJECT_NAME = HelperUtils.getProperty(PROJECT_NAME_KEY);
23+
protected static final String TEST_NAMESPACE = HelperUtils.getProperty(NAMESPACE_KEY);
24+
protected static final String TEST_HOST_URL = HelperUtils.getProperty(HOST_URL_KEY);
25+
protected static String TEST_PRIVATE_TOKEN;
3226

3327
protected static class BaseTestResources {
3428
protected GitLabApi gitLabApi;
@@ -76,6 +70,10 @@ protected static GitLabApi baseTestSetup() {
7670
problems += "TEST_HOST_URL cannot be empty\n";
7771
}
7872

73+
if (TEST_PRIVATE_TOKEN == null) {
74+
TEST_PRIVATE_TOKEN = HelperUtils.getProperty(PRIVATE_TOKEN_KEY);
75+
}
76+
7977
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().isEmpty()) {
8078
problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
8179
}
Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package org.gitlab4j.api;
22

3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.fail;
6+
7+
import java.util.Arrays;
8+
9+
import org.gitlab4j.api.utils.AccessTokenUtils;
310
import org.junit.BeforeClass;
411
import org.junit.experimental.categories.Categories.IncludeCategory;
512
import org.junit.runner.RunWith;
@@ -10,14 +17,57 @@
1017
@RunWith(WildcardPatternSuite.class)
1118
@SuiteClasses({"**/Test*.class"})
1219
@IncludeCategory(IntegrationTest.class)
13-
public class IntegrationTestSuite {
20+
public class IntegrationTestSuite implements PropertyConstants {
21+
22+
private static final String TEST_LOGIN_USERNAME = HelperUtils.getProperty(LOGIN_USERNAME_KEY);
23+
private static final String TEST_LOGIN_PASSWORD = HelperUtils.getProperty(LOGIN_PASSWORD_KEY);
24+
private static final String TEST_HOST_URL = HelperUtils.getProperty(HOST_URL_KEY);
25+
26+
protected static final String TEST_PROJECT_NAME = HelperUtils.getProperty(PROJECT_NAME_KEY);
27+
protected static final String TEST_NAMESPACE = HelperUtils.getProperty(NAMESPACE_KEY);
28+
29+
protected static String TEST_PRIVATE_TOKEN;
30+
protected static String TEST_ACCESS_TOKEN;
31+
private static String problems = "";
1432

1533
@BeforeClass
16-
public static void suiteSetup() {
34+
public static void suiteSetup() throws GitLabApiException {
35+
1736
System.out.println("********************************************************");
18-
System.out.println(" Test Suite Setup");
37+
System.out.println("* Test Suite Setup *");
1938
System.out.println("********************************************************");
2039

21-
// TODO Create default test resources if not present
40+
if (TEST_LOGIN_USERNAME == null || TEST_LOGIN_USERNAME.trim().isEmpty()) {
41+
problems += "TEST_LOGIN_USERNAME cannot be empty\n";
42+
}
43+
44+
if (TEST_LOGIN_PASSWORD == null || TEST_LOGIN_PASSWORD.trim().isEmpty()) {
45+
problems += "TEST_LOGIN_PASSWORD cannot be empty\n";
46+
}
47+
48+
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) {
49+
problems += "TEST_HOST_URL cannot be empty\n";
50+
}
51+
52+
if (!problems.isEmpty()) {
53+
fail(problems);
54+
}
55+
56+
// Create a new personal access token for both the private and access tokens
57+
TEST_PRIVATE_TOKEN = AccessTokenUtils.createPersonalAccessToken(
58+
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
59+
"GitLab4J Test Private Token", Arrays.asList("api", "sudo"));
60+
System.out.println("Created private token: " + TEST_PRIVATE_TOKEN);
61+
assertNotNull(TEST_PRIVATE_TOKEN);
62+
assertFalse(TEST_PRIVATE_TOKEN.trim().isEmpty());
63+
HelperUtils.setProperty(PRIVATE_TOKEN_KEY, TEST_PRIVATE_TOKEN);
64+
65+
TEST_ACCESS_TOKEN = AccessTokenUtils.createPersonalAccessToken(
66+
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
67+
"GitLab4J Test Access Token", Arrays.asList("api", "sudo"));
68+
System.out.println("Created private token: " + TEST_ACCESS_TOKEN);
69+
assertNotNull(TEST_ACCESS_TOKEN);
70+
assertFalse(TEST_ACCESS_TOKEN.trim().isEmpty());
71+
HelperUtils.setProperty(ACCESS_TOKEN_KEY, TEST_ACCESS_TOKEN);
2272
}
2373
}

src/test/java/org/gitlab4j/api/TestAccessToken.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
public class TestAccessToken extends AbstractIntegrationTest {
2626

2727
// TEST_ACCESS_TOKEN must be defined to run this test
28-
private static final String TEST_ACCESS_TOKEN = TestUtils.getProperty("TEST_ACCESS_TOKEN");
28+
private static final String TEST_ACCESS_TOKEN = HelperUtils.getProperty("TEST_ACCESS_TOKEN");
2929
private static GitLabApi gitLabApi;
3030

3131
public TestAccessToken() {

src/test/java/org/gitlab4j/api/TestAvatarUpload.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public class TestAvatarUpload extends AbstractIntegrationTest {
3535
private static final String TEST_PROXY_USERNAME;
3636
private static final String TEST_PROXY_PASSWORD;
3737
static {
38-
TEST_PROXY_URI = TestUtils.getProperty("TEST_PROXY_URI");
39-
TEST_PROXY_USERNAME = TestUtils.getProperty("TEST_PROXY_USERNAME");
40-
TEST_PROXY_PASSWORD = TestUtils.getProperty("TEST_PROXY_PASSWORD");
38+
TEST_PROXY_URI = HelperUtils.getProperty("TEST_PROXY_URI");
39+
TEST_PROXY_USERNAME = HelperUtils.getProperty("TEST_PROXY_USERNAME");
40+
TEST_PROXY_PASSWORD = HelperUtils.getProperty("TEST_PROXY_PASSWORD");
4141
}
4242

4343
private static final String AVATAR_FILENAME = "avatar.png";

src/test/java/org/gitlab4j/api/TestDeployKeysApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
public class TestDeployKeysApi extends AbstractIntegrationTest {
2121

2222
// The following needs to be set to your test repository
23-
private static final String TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME");
23+
private static final String TEST_USERNAME = HelperUtils.getProperty(USERNAME_KEY);
2424

2525
private static GitLabApi gitLabApi;
2626

src/test/java/org/gitlab4j/api/TestFileUpload.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,9 @@
3131
public class TestFileUpload extends AbstractIntegrationTest {
3232

3333
// The following needs to be set to your test repository
34-
private static final String TEST_PROXY_URI;
35-
private static final String TEST_PROXY_USERNAME;
36-
private static final String TEST_PROXY_PASSWORD;
37-
static {
38-
TEST_PROXY_URI = TestUtils.getProperty("TEST_PROXY_URI");
39-
TEST_PROXY_USERNAME = TestUtils.getProperty("TEST_PROXY_USERNAME");
40-
TEST_PROXY_PASSWORD = TestUtils.getProperty("TEST_PROXY_PASSWORD");
41-
}
34+
private static final String TEST_PROXY_URI = HelperUtils.getProperty("TEST_PROXY_URI");
35+
private static final String TEST_PROXY_USERNAME = HelperUtils.getProperty("TEST_PROXY_USERNAME");
36+
private static final String TEST_PROXY_PASSWORD = HelperUtils.getProperty("TEST_PROXY_PASSWORD");
4237

4338
private static GitLabApi gitLabApi;
4439

src/test/java/org/gitlab4j/api/TestGitLabApi.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,9 @@
2525
public class TestGitLabApi extends AbstractIntegrationTest {
2626

2727
// The following needs to be set to your test repository
28-
private static final String TEST_PROXY_URI;
29-
private static final String TEST_PROXY_USERNAME;
30-
private static final String TEST_PROXY_PASSWORD;
31-
static {
32-
TEST_PROXY_URI = TestUtils.getProperty("TEST_PROXY_URI");
33-
TEST_PROXY_USERNAME = TestUtils.getProperty("TEST_PROXY_USERNAME");
34-
TEST_PROXY_PASSWORD = TestUtils.getProperty("TEST_PROXY_PASSWORD");
35-
}
28+
private static final String TEST_PROXY_URI = HelperUtils.getProperty(PROXY_URI_KEY);
29+
private static final String TEST_PROXY_USERNAME = HelperUtils.getProperty(PROXY_USERNAME_KEY);
30+
private static final String TEST_PROXY_PASSWORD = HelperUtils.getProperty(PROXY_PASSWORD_KEY);
3631

3732
private static GitLabApi gitLabApi;
3833

src/test/java/org/gitlab4j/api/TestGitLabLogin.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,13 @@
2323
* If any of the above are NULL, all tests in this class will be skipped.
2424
*/
2525
@Category(IntegrationTest.class)
26-
public class TestGitLabLogin {
26+
public class TestGitLabLogin implements PropertyConstants {
2727

2828
// The following needs to be set to your test repository
29-
private static final String TEST_LOGIN_USERNAME;
30-
private static final String TEST_LOGIN_PASSWORD;
31-
private static final String TEST_HOST_URL;
32-
private static final String TEST_PRIVATE_TOKEN;
33-
static {
34-
TEST_LOGIN_USERNAME = TestUtils.getProperty("TEST_LOGIN_USERNAME");
35-
TEST_LOGIN_PASSWORD = TestUtils.getProperty("TEST_LOGIN_PASSWORD");
36-
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
37-
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
38-
}
29+
private static final String TEST_LOGIN_USERNAME = HelperUtils.getProperty(LOGIN_USERNAME_KEY);
30+
private static final String TEST_LOGIN_PASSWORD = HelperUtils.getProperty(LOGIN_PASSWORD_KEY);
31+
private static final String TEST_HOST_URL = HelperUtils.getProperty(HOST_URL_KEY);
32+
private static final String TEST_PRIVATE_TOKEN = HelperUtils.getProperty(PRIVATE_TOKEN_KEY);
3933

4034
private static String problems = "";
4135
private static boolean hasSession;

src/test/java/org/gitlab4j/api/TestGroupApi.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,9 @@
3838
public class TestGroupApi extends AbstractIntegrationTest {
3939

4040
// The following needs to be set to your test repository
41-
private static final String TEST_USERNAME;
42-
private static final String TEST_GROUP;
43-
private static final String TEST_GROUP_MEMBER_USERNAME;
44-
static {
45-
TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME");
46-
TEST_GROUP = TestUtils.getProperty("TEST_GROUP");
47-
TEST_GROUP_MEMBER_USERNAME = TestUtils.getProperty("TEST_GROUP_MEMBER_USERNAME");
48-
}
41+
private static final String TEST_USERNAME = HelperUtils.getProperty(USERNAME_KEY);
42+
private static final String TEST_GROUP = HelperUtils.getProperty(GROUP_KEY);
43+
private static final String TEST_GROUP_MEMBER_USERNAME = HelperUtils.getProperty(GROUP_MEMBER_USERNAME_KEY);
4944

5045
private static GitLabApi gitLabApi;
5146
private static Group testGroup;

0 commit comments

Comments
 (0)