From 226792226c15d01de64b6275f4eb5108b32d7fca Mon Sep 17 00:00:00 2001 From: Greg Messner Date: Mon, 12 Mar 2018 11:08:23 -0700 Subject: [PATCH] Feature/protected branch and example test properties (#156) * Minor mods to protected branch functionality. * Cleaned up example-test-gitlab4j.properties and related tests. --- example-test-gitlab4j.properties | 38 ++++++++++++++++--- .../gitlab4j/api/ProtectedBranchesApi.java | 1 + .../java/org/gitlab4j/api/RepositoryApi.java | 1 - ...evelDetail.java => BranchAccessLevel.java} | 9 +++-- .../gitlab4j/api/models/ProtectedBranch.java | 13 ++++--- .../java/org/gitlab4j/api/TestGitLabApi.java | 1 + .../org/gitlab4j/api/TestGitLabApiBeans.java | 12 ++++++ .../org/gitlab4j/api/TestGitLabLogin.java | 28 +++++++------- .../java/org/gitlab4j/api/TestGroupApi.java | 10 +++-- .../api/TestProtectedBranchesApi.java | 17 +++++---- .../org/gitlab4j/api/TestRepositoryApi.java | 3 +- .../org/gitlab4j/api/TestSystemHooksApi.java | 7 ++-- .../java/org/gitlab4j/api/TestUserApi.java | 12 ++++-- .../org/gitlab4j/api/protected-branch.json | 23 +++++++++++ 14 files changed, 125 insertions(+), 50 deletions(-) rename src/main/java/org/gitlab4j/api/models/{BranchAccessLevelDetail.java => BranchAccessLevel.java} (78%) create mode 100644 src/test/resources/org/gitlab4j/api/protected-branch.json diff --git a/example-test-gitlab4j.properties b/example-test-gitlab4j.properties index 0a634c5b..d7441a0b 100644 --- a/example-test-gitlab4j.properties +++ b/example-test-gitlab4j.properties @@ -1,5 +1,33 @@ -# Rename this file to test-gitlab4j.properties with proper property values. -TEST_NAMESPACE=some-namespace -TEST_PROJECT_NAME=some-project-name -TEST_HOST_URL=some-gitlab-url -TEST_PRIVATE_TOKEN=some-private-token +# +# To test GitLab4J-API, you must have access to a GitLab server. +# +# Copy this file to test-gitlab4j.properties and set the property +# values to match the GitLab server you are testing against. +# + +# REQUIRED: The following values must be set or the tests will fail. +# The TEST_PRIVATE_TOKEN and TEST_USERNAME must belong to an admin user. +TEST_PRIVATE_TOKEN= +TEST_ACCESS_TOKEN= +TEST_USERNAME= +TEST_NAMESPACE= +TEST_PROJECT_NAME= +TEST_HOST_URL= + +# OPTIONAL: To test the GroupApi, set these to an existing group name and group-project name, +# and the username of a group member +TEST_GROUP= +TEST_GROUP_PROJECT= +TEST_GROUP_MEMBER_USERNAME= + +# OPTIONAL: To test oauth2Login, set these properties +TEST_LOGIN_USERNAME= +TEST_LOGIN_PASSWORD= + +# OPTIONAL: To test sudo capability provide a username to sudo as +TEST_SUDO_AS_USERNAME= + +# OPTIONAL: To test using GitLab4J-API with a proxy, set the following properties +TEST_PROXY_URI= +TEST_PROXY_USERNAME= +TEST_PROXY_PASSWORD= diff --git a/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java b/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java index 85eeea10..7a8f9f80 100644 --- a/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java +++ b/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java @@ -9,6 +9,7 @@ import javax.ws.rs.core.Response; import java.util.List; public class ProtectedBranchesApi extends AbstractApi { + public ProtectedBranchesApi(GitLabApi gitLabApi) { super(gitLabApi); } diff --git a/src/main/java/org/gitlab4j/api/RepositoryApi.java b/src/main/java/org/gitlab4j/api/RepositoryApi.java index ad1cb54a..6efc3a11 100644 --- a/src/main/java/org/gitlab4j/api/RepositoryApi.java +++ b/src/main/java/org/gitlab4j/api/RepositoryApi.java @@ -15,7 +15,6 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.gitlab4j.api.GitLabApi.ApiVersion; -import org.gitlab4j.api.models.AccessLevel; import org.gitlab4j.api.models.Branch; import org.gitlab4j.api.models.CompareResults; import org.gitlab4j.api.models.Tag; diff --git a/src/main/java/org/gitlab4j/api/models/BranchAccessLevelDetail.java b/src/main/java/org/gitlab4j/api/models/BranchAccessLevel.java similarity index 78% rename from src/main/java/org/gitlab4j/api/models/BranchAccessLevelDetail.java rename to src/main/java/org/gitlab4j/api/models/BranchAccessLevel.java index ff0ef09b..ddc48132 100644 --- a/src/main/java/org/gitlab4j/api/models/BranchAccessLevelDetail.java +++ b/src/main/java/org/gitlab4j/api/models/BranchAccessLevel.java @@ -6,15 +6,16 @@ import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) -public class BranchAccessLevelDetail { - private String accessLevel; +public class BranchAccessLevel { + + private AccessLevel accessLevel; private String accessLevelDescription; - public String getAccessLevel() { + public AccessLevel getAccessLevel() { return this.accessLevel; } - public void setAccessLevel(String accessLevel) { + public void setAccessLevel(AccessLevel accessLevel) { this.accessLevel = accessLevel; } diff --git a/src/main/java/org/gitlab4j/api/models/ProtectedBranch.java b/src/main/java/org/gitlab4j/api/models/ProtectedBranch.java index 45cecc8a..0f119c28 100644 --- a/src/main/java/org/gitlab4j/api/models/ProtectedBranch.java +++ b/src/main/java/org/gitlab4j/api/models/ProtectedBranch.java @@ -9,9 +9,10 @@ import java.util.List; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class ProtectedBranch { + private String name; - private List pushAccessLevels; - private List mergeAccessLevels; + private List pushAccessLevels; + private List mergeAccessLevels; public String getName() { return this.name; @@ -21,19 +22,19 @@ public class ProtectedBranch { this.name = name; } - public List getPushAccessLevels() { + public List getPushAccessLevels() { return this.pushAccessLevels; } - public void setPushAccessLevels(List pushAccessLevels) { + public void setPushAccessLevels(List pushAccessLevels) { this.pushAccessLevels = pushAccessLevels; } - public List getMergeAccessLevels() { + public List getMergeAccessLevels() { return this.mergeAccessLevels; } - public void setMergeAccessLevels(List mergeAccessLevels) { + public void setMergeAccessLevels(List mergeAccessLevels) { this.mergeAccessLevels = mergeAccessLevels; } diff --git a/src/test/java/org/gitlab4j/api/TestGitLabApi.java b/src/test/java/org/gitlab4j/api/TestGitLabApi.java index 382ba9fa..04bf3d75 100644 --- a/src/test/java/org/gitlab4j/api/TestGitLabApi.java +++ b/src/test/java/org/gitlab4j/api/TestGitLabApi.java @@ -77,6 +77,7 @@ public class TestGitLabApi { @Test public void testProxyConnection() throws GitLabApiException { assumeTrue(TEST_PROXY_URI != null && TEST_PROXY_USERNAME != null && TEST_PROXY_PASSWORD != null); + assumeTrue(TEST_PROXY_URI.length() > 0 && TEST_PROXY_USERNAME.length() > 0 && TEST_PROXY_PASSWORD.length() > 0); // Setup a GitLabApi instance to use a proxy Map clientConfig = ProxyClientConfig.createProxyClientConfig(TEST_PROXY_URI, TEST_PROXY_USERNAME, TEST_PROXY_PASSWORD); diff --git a/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java b/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java index 7b0b243f..3ac6da93 100644 --- a/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java +++ b/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java @@ -54,6 +54,7 @@ import org.gitlab4j.api.models.Pipeline; import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.ProjectHook; import org.gitlab4j.api.models.ProjectUser; +import org.gitlab4j.api.models.ProtectedBranch; import org.gitlab4j.api.models.Session; import org.gitlab4j.api.models.Snippet; import org.gitlab4j.api.models.SshKey; @@ -266,6 +267,17 @@ public class TestGitLabApiBeans { } } + @Test + public void testProtectedBranch() { + + try { + ProtectedBranch protectedBranch = makeFakeApiCall(ProtectedBranch.class, "protected-branch"); + assertTrue(compareJson(protectedBranch, "protected-branch")); + } catch (Exception e) { + e.printStackTrace(); + } + } + @Test public void testKey() { diff --git a/src/test/java/org/gitlab4j/api/TestGitLabLogin.java b/src/test/java/org/gitlab4j/api/TestGitLabLogin.java index 7142a496..f8ddbe63 100644 --- a/src/test/java/org/gitlab4j/api/TestGitLabLogin.java +++ b/src/test/java/org/gitlab4j/api/TestGitLabLogin.java @@ -15,8 +15,8 @@ import org.junit.Test; * In order for these tests to run you must set the following properties in test-gitlab4j.properties * * TEST_HOST_URL - * TEST_USERNAME - * TEST_PASSWORD + * TEST_LOGIN_USERNAME + * TEST_LOGIN_PASSWORD * TEST_PRIVATE_TOKEN * * If any of the above are NULL, all tests in this class will be skipped. @@ -24,13 +24,13 @@ import org.junit.Test; public class TestGitLabLogin { // The following needs to be set to your test repository - private static final String TEST_USERNAME; - private static final String TEST_PASSWORD; + private static final String TEST_LOGIN_USERNAME; + private static final String TEST_LOGIN_PASSWORD; private static final String TEST_HOST_URL; private static final String TEST_PRIVATE_TOKEN; static { - TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME"); - TEST_PASSWORD = TestUtils.getProperty("TEST_PASSWORD"); + TEST_LOGIN_USERNAME = TestUtils.getProperty("TEST_LOGIN_USERNAME"); + TEST_LOGIN_PASSWORD = TestUtils.getProperty("TEST_LOGIN_PASSWORD"); TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL"); TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN"); } @@ -47,12 +47,12 @@ public class TestGitLabLogin { problems = ""; - if (TEST_USERNAME == null || TEST_USERNAME.trim().length() == 0) { - problems += "TEST_USERNAME cannot be empty\n"; + if (TEST_LOGIN_USERNAME == null || TEST_LOGIN_USERNAME.trim().length() == 0) { + problems += "TEST_LOGIN_USERNAME cannot be empty\n"; } - if (TEST_PASSWORD == null || TEST_PASSWORD.trim().length() == 0) { - problems += "TEST_PASSWORD cannot be empty\n"; + if (TEST_LOGIN_PASSWORD == null || TEST_LOGIN_PASSWORD.trim().length() == 0) { + problems += "TEST_LOGIN_PASSWORD cannot be empty\n"; } if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) { @@ -93,7 +93,7 @@ public class TestGitLabLogin { public void testSession() throws GitLabApiException { assumeTrue(hasSession); - GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V4, TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD); + GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V4, TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD); assertNotNull(gitLabApi); assertNotNull(gitLabApi.getSession()); assertEquals(TEST_PRIVATE_TOKEN, gitLabApi.getSession().getPrivateToken()); @@ -104,7 +104,7 @@ public class TestGitLabLogin { public void testSessionV3() throws GitLabApiException { assumeTrue(hasSession); - GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V3, TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD); + GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V3, TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD); assertNotNull(gitLabApi); assertNotNull(gitLabApi.getSession()); assertEquals(TEST_PRIVATE_TOKEN, gitLabApi.getSession().getPrivateToken()); @@ -114,7 +114,7 @@ public class TestGitLabLogin { public void testSessionFallover() throws GitLabApiException { assumeFalse(hasSession); - GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V4, TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD); + GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V4, TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD); assertNotNull(gitLabApi); Version version = gitLabApi.getVersion(); assertNotNull(version); @@ -123,7 +123,7 @@ public class TestGitLabLogin { @Test public void testOauth2Login() throws GitLabApiException { - GitLabApi gitLabApi = GitLabApi.oauth2Login(TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD, null, null, true); + GitLabApi gitLabApi = GitLabApi.oauth2Login(TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD, null, null, true); assertNotNull(gitLabApi); Version version = gitLabApi.getVersion(); assertNotNull(version); diff --git a/src/test/java/org/gitlab4j/api/TestGroupApi.java b/src/test/java/org/gitlab4j/api/TestGroupApi.java index 706a57a1..f7263bb8 100644 --- a/src/test/java/org/gitlab4j/api/TestGroupApi.java +++ b/src/test/java/org/gitlab4j/api/TestGroupApi.java @@ -83,10 +83,12 @@ public class TestGroupApi { problems += "Problem fetching test group, error=" + gle.getMessage() + "\n"; } - try { - testUser = gitLabApi.getUserApi().getUser(TEST_GROUP_MEMBER_USERNAME); - } catch (GitLabApiException gle) { - problems += "Problem fetching test user, error=" + gle.getMessage() + "\n"; + if (TEST_GROUP_MEMBER_USERNAME != null && TEST_GROUP_MEMBER_USERNAME.length() > 0) { + try { + testUser = gitLabApi.getUserApi().getUser(TEST_GROUP_MEMBER_USERNAME); + } catch (GitLabApiException gle) { + problems += "Problem fetching test user, error=" + gle.getMessage() + "\n"; + } } } diff --git a/src/test/java/org/gitlab4j/api/TestProtectedBranchesApi.java b/src/test/java/org/gitlab4j/api/TestProtectedBranchesApi.java index dfd5992b..94a69be5 100644 --- a/src/test/java/org/gitlab4j/api/TestProtectedBranchesApi.java +++ b/src/test/java/org/gitlab4j/api/TestProtectedBranchesApi.java @@ -1,5 +1,12 @@ package org.gitlab4j.api; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; + +import java.util.List; + import org.gitlab4j.api.models.Branch; import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.ProtectedBranch; @@ -10,13 +17,6 @@ import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; -import java.util.List; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeTrue; - /** * In order for these tests to run you must set the following properties in test-gitlab4j.properties * @@ -154,6 +154,9 @@ public class TestProtectedBranchesApi { assertNotNull(project); ProtectedBranch branch = gitLabApi.getProtectedBranchesApi().protectBranch(project.getId(), TEST_BRANCH_NAME); + assertNotNull(branch); + assertEquals(TEST_BRANCH_NAME, branch.getName()); + List branches = gitLabApi.getProtectedBranchesApi().getProtectedBranches(project.getId()); assertNotNull(branches); assertTrue(branches.stream() diff --git a/src/test/java/org/gitlab4j/api/TestRepositoryApi.java b/src/test/java/org/gitlab4j/api/TestRepositoryApi.java index bd80153d..ee4c30ce 100644 --- a/src/test/java/org/gitlab4j/api/TestRepositoryApi.java +++ b/src/test/java/org/gitlab4j/api/TestRepositoryApi.java @@ -86,13 +86,14 @@ public class TestRepositoryApi { if (problems.isEmpty()) { gitLabApi = new GitLabApi(ApiVersion.V3, TEST_HOST_URL, TEST_PRIVATE_TOKEN); + teardown(); } else { System.err.print(problems); } } @AfterClass - public static void teardown() throws GitLabApiException { + public static void teardown() { if (gitLabApi != null) { try { diff --git a/src/test/java/org/gitlab4j/api/TestSystemHooksApi.java b/src/test/java/org/gitlab4j/api/TestSystemHooksApi.java index 8c56c3bb..4ef27d5f 100644 --- a/src/test/java/org/gitlab4j/api/TestSystemHooksApi.java +++ b/src/test/java/org/gitlab4j/api/TestSystemHooksApi.java @@ -18,11 +18,10 @@ import org.junit.runners.MethodSorters; /** * In order for these tests to run you must set the following properties in test-gitlab4j.properties - * - * TEST_HOOK_URL + * * TEST_HOST_URL * TEST_PRIVATE_TOKEN - * + * * If any of the above are NULL, all tests in this class will be skipped. */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) @@ -34,7 +33,7 @@ public class TestSystemHooksApi { private static final String TEST_PRIVATE_TOKEN; static { TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL"); - TEST_HOOK_URL = TestUtils.getProperty("TEST_HOOK_URL"); + TEST_HOOK_URL = "http://hook.example.com/hook/callback"; TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN"); } diff --git a/src/test/java/org/gitlab4j/api/TestUserApi.java b/src/test/java/org/gitlab4j/api/TestUserApi.java index 15c081bf..516d6539 100644 --- a/src/test/java/org/gitlab4j/api/TestUserApi.java +++ b/src/test/java/org/gitlab4j/api/TestUserApi.java @@ -27,11 +27,11 @@ import org.junit.Test; /** * In order for these tests to run you must set the following properties in test-gitlab4j.properties - * + * * TEST_HOST_URL * TEST_PRIVATE_TOKEN * TEST_USERNAME - * + * * If any of the above are NULL, all tests in this class will be skipped. * * TEST_SUDO_AS_USERNAME @@ -56,7 +56,11 @@ public class TestUserApi { TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN"); TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME"); TEST_SUDO_AS_USERNAME = TestUtils.getProperty("TEST_SUDO_AS_USERNAME"); - TEST_SSH_KEY = TestUtils.getProperty("TEST_SSH_KEY"); + TEST_SSH_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCvbkmGRaANy2nmLrfYa9LkjMqjs9twYZXQKUPK18j" + + "BWmNgnAm818IikxjfFit3Gqnnh9zdNzlzUYs2osmfdHwRLeFY3hKVR6WckGYVroQuV5ArUA4+oME+IIQ2soCv/" + + "vNWfEmp2N1mpBTwi2mIYKurCKv6UpIpGK9D+ezNk5H0waVTK8EvZ/ey69Nu7C7RsbTYeyi5WY/jaUG5JbsEeKY" + + "IW/2DIlUts7gcB2hzXtt7r7+6DLx82Vb+S2jPZu2JQaB4zfgS7LQgzHUy1aAAgUUpuAbvWzuGHKO0p551Ru4qi" + + "tyXN2+OUVXcYAsuIIdGGB0wLvTDgiOOSZWnSE+sg6XX user@example.com"; } private static final String TEST_IMPERSONATION_TOKEN_NAME = "token1"; @@ -149,7 +153,7 @@ public class TestUserApi { @Test public void testSudoAsUser() throws GitLabApiException { - assumeTrue(TEST_SUDO_AS_USERNAME != null); + assumeTrue(TEST_SUDO_AS_USERNAME != null && TEST_SUDO_AS_USERNAME.length() > 0); try { diff --git a/src/test/resources/org/gitlab4j/api/protected-branch.json b/src/test/resources/org/gitlab4j/api/protected-branch.json new file mode 100644 index 00000000..2a64f1d5 --- /dev/null +++ b/src/test/resources/org/gitlab4j/api/protected-branch.json @@ -0,0 +1,23 @@ +{ + "name": "develop", + "push_access_levels": [ + { + "access_level": 40, + "access_level_description": "Masters" + }, + { + "access_level": 30, + "access_level_description": "Developer access" + } + ], + "merge_access_levels": [ + { + "access_level": 40, + "access_level_description": "Masters" + }, + { + "access_level": 30, + "access_level_description": "Developer access" + } + ] +} -- GitLab