Skip to content

Commit d4906b7

Browse files
committed
Cleaned up example-test-gitlab4j.properties and releated tests.
1 parent 44eb844 commit d4906b7

9 files changed

+83
-37
lines changed

example-test-gitlab4j.properties

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
1-
# Rename this file to test-gitlab4j.properties with proper property values.
1+
# To test GitLab4J-API you must have access to a gitlab server.
2+
#
3+
# Copy this file to test-gitlab4j.properties and update the property
4+
# values to match the gitlab server you are testing against.
5+
#
6+
# The TEST_PRIVATE_TOKEN and TEST_USERNAME should belong to an admin user
7+
#
8+
9+
# The following values must be set or testing will fail
210
TEST_NAMESPACE=some-namespace
311
TEST_PROJECT_NAME=some-project-name
412
TEST_HOST_URL=some-gitlab-url
5-
TEST_PRIVATE_TOKEN=some-private-token
13+
TEST_PRIVATE_TOKEN=admin-private-token
14+
TEST_ACCESS_TOKEN=some-access-token
15+
TEST_USERNAME=admin-user
16+
17+
# To test the GroupApi, set these to an existing group and group-project, and group member
18+
TEST_GROUP=
19+
TEST_GROUP_PROJECT=
20+
TEST_GROUP_MEMBER_USERNAME=
21+
22+
# To test oauth2Login, set these properties
23+
TEST_LOGIN_USERNAME=
24+
TEST_LOGIN_PASSWORD=
25+
26+
# To test sudo capability provide a username to sudo as
27+
TEST_SUDO_AS_USERNAME=
28+
29+
# To test using GitLab4J-API with a proxy, set the following properties
30+
TEST_PROXY_URI=
31+
TEST_PROXY_USERNAME=
32+
TEST_PROXY_PASSWORD=
33+

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public void testGetVersion() throws GitLabApiException {
7777
@Test
7878
public void testProxyConnection() throws GitLabApiException {
7979
assumeTrue(TEST_PROXY_URI != null && TEST_PROXY_USERNAME != null && TEST_PROXY_PASSWORD != null);
80+
assumeTrue(TEST_PROXY_URI.length() > 0 && TEST_PROXY_USERNAME.length() > 0 && TEST_PROXY_PASSWORD.length() > 0);
8081

8182
// Setup a GitLabApi instance to use a proxy
8283
Map<String, Object> clientConfig = ProxyClientConfig.createProxyClientConfig(TEST_PROXY_URI, TEST_PROXY_USERNAME, TEST_PROXY_PASSWORD);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.gitlab4j.api.models.Project;
5555
import org.gitlab4j.api.models.ProjectHook;
5656
import org.gitlab4j.api.models.ProjectUser;
57+
import org.gitlab4j.api.models.ProtectedBranch;
5758
import org.gitlab4j.api.models.Session;
5859
import org.gitlab4j.api.models.Snippet;
5960
import org.gitlab4j.api.models.SshKey;
@@ -266,6 +267,17 @@ public void testProjectHook() {
266267
}
267268
}
268269

270+
@Test
271+
public void testProtectedBranch() {
272+
273+
try {
274+
ProtectedBranch protectedBranch = makeFakeApiCall(ProtectedBranch.class, "protected-branch");
275+
assertTrue(compareJson(protectedBranch, "protected-branch"));
276+
} catch (Exception e) {
277+
e.printStackTrace();
278+
}
279+
}
280+
269281
@Test
270282
public void testKey() {
271283

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@
1515
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
1616
*
1717
* TEST_HOST_URL
18-
* TEST_USERNAME
19-
* TEST_PASSWORD
18+
* TEST_LOGIN_USERNAME
19+
* TEST_LOGIN_PASSWORD
2020
* TEST_PRIVATE_TOKEN
2121
*
2222
* If any of the above are NULL, all tests in this class will be skipped.
2323
*/
2424
public class TestGitLabLogin {
2525

2626
// The following needs to be set to your test repository
27-
private static final String TEST_USERNAME;
28-
private static final String TEST_PASSWORD;
27+
private static final String TEST_LOGIN_USERNAME;
28+
private static final String TEST_LOGIN_PASSWORD;
2929
private static final String TEST_HOST_URL;
3030
private static final String TEST_PRIVATE_TOKEN;
3131
static {
32-
TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME");
33-
TEST_PASSWORD = TestUtils.getProperty("TEST_PASSWORD");
32+
TEST_LOGIN_USERNAME = TestUtils.getProperty("TEST_LOGIN_USERNAME");
33+
TEST_LOGIN_PASSWORD = TestUtils.getProperty("TEST_LOGIN_PASSWORD");
3434
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
3535
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
3636
}
@@ -47,12 +47,12 @@ public static void setup() {
4747

4848
problems = "";
4949

50-
if (TEST_USERNAME == null || TEST_USERNAME.trim().length() == 0) {
51-
problems += "TEST_USERNAME cannot be empty\n";
50+
if (TEST_LOGIN_USERNAME == null || TEST_LOGIN_USERNAME.trim().length() == 0) {
51+
problems += "TEST_LOGIN_USERNAME cannot be empty\n";
5252
}
5353

54-
if (TEST_PASSWORD == null || TEST_PASSWORD.trim().length() == 0) {
55-
problems += "TEST_PASSWORD cannot be empty\n";
54+
if (TEST_LOGIN_PASSWORD == null || TEST_LOGIN_PASSWORD.trim().length() == 0) {
55+
problems += "TEST_LOGIN_PASSWORD cannot be empty\n";
5656
}
5757

5858
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) {
@@ -93,7 +93,7 @@ public void beforeMethod() {
9393
public void testSession() throws GitLabApiException {
9494

9595
assumeTrue(hasSession);
96-
GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V4, TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD);
96+
GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V4, TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD);
9797
assertNotNull(gitLabApi);
9898
assertNotNull(gitLabApi.getSession());
9999
assertEquals(TEST_PRIVATE_TOKEN, gitLabApi.getSession().getPrivateToken());
@@ -104,7 +104,7 @@ public void testSession() throws GitLabApiException {
104104
public void testSessionV3() throws GitLabApiException {
105105

106106
assumeTrue(hasSession);
107-
GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V3, TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD);
107+
GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V3, TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD);
108108
assertNotNull(gitLabApi);
109109
assertNotNull(gitLabApi.getSession());
110110
assertEquals(TEST_PRIVATE_TOKEN, gitLabApi.getSession().getPrivateToken());
@@ -114,7 +114,7 @@ public void testSessionV3() throws GitLabApiException {
114114
public void testSessionFallover() throws GitLabApiException {
115115

116116
assumeFalse(hasSession);
117-
GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V4, TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD);
117+
GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V4, TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD);
118118
assertNotNull(gitLabApi);
119119
Version version = gitLabApi.getVersion();
120120
assertNotNull(version);
@@ -123,7 +123,7 @@ public void testSessionFallover() throws GitLabApiException {
123123
@Test
124124
public void testOauth2Login() throws GitLabApiException {
125125

126-
GitLabApi gitLabApi = GitLabApi.oauth2Login(TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD, null, null, true);
126+
GitLabApi gitLabApi = GitLabApi.oauth2Login(TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD, null, null, true);
127127
assertNotNull(gitLabApi);
128128
Version version = gitLabApi.getVersion();
129129
assertNotNull(version);

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ public static void setup() {
8383
problems += "Problem fetching test group, error=" + gle.getMessage() + "\n";
8484
}
8585

86-
try {
87-
testUser = gitLabApi.getUserApi().getUser(TEST_GROUP_MEMBER_USERNAME);
88-
} catch (GitLabApiException gle) {
89-
problems += "Problem fetching test user, error=" + gle.getMessage() + "\n";
86+
if (TEST_GROUP_MEMBER_USERNAME != null && TEST_GROUP_MEMBER_USERNAME.length() > 0) {
87+
try {
88+
testUser = gitLabApi.getUserApi().getUser(TEST_GROUP_MEMBER_USERNAME);
89+
} catch (GitLabApiException gle) {
90+
problems += "Problem fetching test user, error=" + gle.getMessage() + "\n";
91+
}
9092
}
9193
}
9294

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package org.gitlab4j.api;
22

3+
import static org.junit.Assert.assertNotNull;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.junit.Assume.assumeTrue;
6+
7+
import java.util.List;
8+
39
import org.gitlab4j.api.models.Branch;
410
import org.gitlab4j.api.models.Project;
511
import org.gitlab4j.api.models.ProtectedBranch;
@@ -10,13 +16,6 @@
1016
import org.junit.Test;
1117
import org.junit.runners.MethodSorters;
1218

13-
import java.util.List;
14-
15-
import static org.junit.Assert.assertFalse;
16-
import static org.junit.Assert.assertNotNull;
17-
import static org.junit.Assert.assertTrue;
18-
import static org.junit.Assume.assumeTrue;
19-
2019
/**
2120
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
2221
*
@@ -157,6 +156,6 @@ public void testProtectBranch() throws GitLabApiException {
157156
List<ProtectedBranch> branches = gitLabApi.getProtectedBranchesApi().getProtectedBranches(project.getId());
158157
assertNotNull(branches);
159158
assertTrue(branches.stream()
160-
.anyMatch((protectedBranch) -> protectedBranch.getName().equals(TEST_BRANCH_NAME)));
159+
.anyMatch((protectedBranch) -> branch.getName().equals(TEST_BRANCH_NAME)));
161160
}
162161
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ public static void setup() {
8686

8787
if (problems.isEmpty()) {
8888
gitLabApi = new GitLabApi(ApiVersion.V3, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
89+
teardown();
8990
} else {
9091
System.err.print(problems);
9192
}
9293
}
9394

9495
@AfterClass
95-
public static void teardown() throws GitLabApiException {
96+
public static void teardown() {
9697
if (gitLabApi != null) {
9798

9899
try {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818

1919
/**
2020
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
21-
*
22-
* TEST_HOOK_URL
21+
*
2322
* TEST_HOST_URL
2423
* TEST_PRIVATE_TOKEN
25-
*
24+
*
2625
* If any of the above are NULL, all tests in this class will be skipped.
2726
*/
2827
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@@ -34,7 +33,7 @@ public class TestSystemHooksApi {
3433
private static final String TEST_PRIVATE_TOKEN;
3534
static {
3635
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
37-
TEST_HOOK_URL = TestUtils.getProperty("TEST_HOOK_URL");
36+
TEST_HOOK_URL = "http://hook.example.com/hook/callback";
3837
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
3938
}
4039

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727

2828
/**
2929
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
30-
*
30+
*
3131
* TEST_HOST_URL
3232
* TEST_PRIVATE_TOKEN
3333
* TEST_USERNAME
34-
*
34+
*
3535
* If any of the above are NULL, all tests in this class will be skipped.
3636
*
3737
* TEST_SUDO_AS_USERNAME
@@ -56,7 +56,11 @@ public class TestUserApi {
5656
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
5757
TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME");
5858
TEST_SUDO_AS_USERNAME = TestUtils.getProperty("TEST_SUDO_AS_USERNAME");
59-
TEST_SSH_KEY = TestUtils.getProperty("TEST_SSH_KEY");
59+
TEST_SSH_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCvbkmGRaANy2nmLrfYa9LkjMqjs9twYZXQKUPK18j" +
60+
"BWmNgnAm818IikxjfFit3Gqnnh9zdNzlzUYs2osmfdHwRLeFY3hKVR6WckGYVroQuV5ArUA4+oME+IIQ2soCv/" +
61+
"vNWfEmp2N1mpBTwi2mIYKurCKv6UpIpGK9D+ezNk5H0waVTK8EvZ/ey69Nu7C7RsbTYeyi5WY/jaUG5JbsEeKY" +
62+
"IW/2DIlUts7gcB2hzXtt7r7+6DLx82Vb+S2jPZu2JQaB4zfgS7LQgzHUy1aAAgUUpuAbvWzuGHKO0p551Ru4qi" +
63+
"tyXN2+OUVXcYAsuIIdGGB0wLvTDgiOOSZWnSE+sg6XX [email protected]";
6064
}
6165

6266
private static final String TEST_IMPERSONATION_TOKEN_NAME = "token1";
@@ -149,7 +153,7 @@ public void testGetOptionalUser() throws GitLabApiException {
149153
@Test
150154
public void testSudoAsUser() throws GitLabApiException {
151155

152-
assumeTrue(TEST_SUDO_AS_USERNAME != null);
156+
assumeTrue(TEST_SUDO_AS_USERNAME != null && TEST_SUDO_AS_USERNAME.length() > 0);
153157

154158
try {
155159

0 commit comments

Comments
 (0)