Skip to content

Commit 2267922

Browse files
authored
Feature/protected branch and example test properties (#156)
* Minor mods to protected branch functionality. * Cleaned up example-test-gitlab4j.properties and related tests.
1 parent e535066 commit 2267922

14 files changed

+125
-50
lines changed

example-test-gitlab4j.properties

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

src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.List;
1010

1111
public class ProtectedBranchesApi extends AbstractApi {
12+
1213
public ProtectedBranchesApi(GitLabApi gitLabApi) {
1314
super(gitLabApi);
1415
}

src/main/java/org/gitlab4j/api/RepositoryApi.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import javax.ws.rs.core.Response;
1616

1717
import org.gitlab4j.api.GitLabApi.ApiVersion;
18-
import org.gitlab4j.api.models.AccessLevel;
1918
import org.gitlab4j.api.models.Branch;
2019
import org.gitlab4j.api.models.CompareResults;
2120
import org.gitlab4j.api.models.Tag;

src/main/java/org/gitlab4j/api/models/BranchAccessLevelDetail.java renamed to src/main/java/org/gitlab4j/api/models/BranchAccessLevel.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
@XmlRootElement
88
@XmlAccessorType(XmlAccessType.FIELD)
9-
public class BranchAccessLevelDetail {
10-
private String accessLevel;
9+
public class BranchAccessLevel {
10+
11+
private AccessLevel accessLevel;
1112
private String accessLevelDescription;
1213

13-
public String getAccessLevel() {
14+
public AccessLevel getAccessLevel() {
1415
return this.accessLevel;
1516
}
1617

17-
public void setAccessLevel(String accessLevel) {
18+
public void setAccessLevel(AccessLevel accessLevel) {
1819
this.accessLevel = accessLevel;
1920
}
2021

src/main/java/org/gitlab4j/api/models/ProtectedBranch.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
@XmlRootElement
1010
@XmlAccessorType(XmlAccessType.FIELD)
1111
public class ProtectedBranch {
12+
1213
private String name;
13-
private List<BranchAccessLevelDetail> pushAccessLevels;
14-
private List<BranchAccessLevelDetail> mergeAccessLevels;
14+
private List<BranchAccessLevel> pushAccessLevels;
15+
private List<BranchAccessLevel> mergeAccessLevels;
1516

1617
public String getName() {
1718
return this.name;
@@ -21,19 +22,19 @@ public void setName(String name) {
2122
this.name = name;
2223
}
2324

24-
public List<BranchAccessLevelDetail> getPushAccessLevels() {
25+
public List<BranchAccessLevel> getPushAccessLevels() {
2526
return this.pushAccessLevels;
2627
}
2728

28-
public void setPushAccessLevels(List<BranchAccessLevelDetail> pushAccessLevels) {
29+
public void setPushAccessLevels(List<BranchAccessLevel> pushAccessLevels) {
2930
this.pushAccessLevels = pushAccessLevels;
3031
}
3132

32-
public List<BranchAccessLevelDetail> getMergeAccessLevels() {
33+
public List<BranchAccessLevel> getMergeAccessLevels() {
3334
return this.mergeAccessLevels;
3435
}
3536

36-
public void setMergeAccessLevels(List<BranchAccessLevelDetail> mergeAccessLevels) {
37+
public void setMergeAccessLevels(List<BranchAccessLevel> mergeAccessLevels) {
3738
this.mergeAccessLevels = mergeAccessLevels;
3839
}
3940

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: 10 additions & 7 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.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertTrue;
6+
import static org.junit.Assume.assumeTrue;
7+
8+
import java.util.List;
9+
310
import org.gitlab4j.api.models.Branch;
411
import org.gitlab4j.api.models.Project;
512
import org.gitlab4j.api.models.ProtectedBranch;
@@ -10,13 +17,6 @@
1017
import org.junit.Test;
1118
import org.junit.runners.MethodSorters;
1219

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-
2020
/**
2121
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
2222
*
@@ -154,6 +154,9 @@ public void testProtectBranch() throws GitLabApiException {
154154
assertNotNull(project);
155155

156156
ProtectedBranch branch = gitLabApi.getProtectedBranchesApi().protectBranch(project.getId(), TEST_BRANCH_NAME);
157+
assertNotNull(branch);
158+
assertEquals(TEST_BRANCH_NAME, branch.getName());
159+
157160
List<ProtectedBranch> branches = gitLabApi.getProtectedBranchesApi().getProtectedBranches(project.getId());
158161
assertNotNull(branches);
159162
assertTrue(branches.stream()

0 commit comments

Comments
 (0)