diff --git a/example-test-gitlab4j.properties b/example-test-gitlab4j.properties index 0a634c5b598fe2523c39c7d12098caa89dd6d94b..d7441a0b3be8609129d561a2ecada6b18292a3f7 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 85eeea1050d0ccf5763243ace3d162dbdebaeb1d..7a8f9f8031b38e37db13a474be3d87ab1a5df934 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 ad1cb54ae4dd35e55b8a78d280a4d63bf16d4c02..6efc3a115ea63825988cc850cb56a1e452dd04da 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 ff0ef09bc7adc22d57ce836239c16e133fe31a53..ddc4813279a6a165d7049b0649a001ecf1b1e971 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 45cecc8aecc40ff80a385c9b9b641f595855d521..0f119c28e80d605fbf7322636120432194a0f24d 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 382ba9fa3cf705947c2ece7d9191f06f688dfdc3..04bf3d75e202a05d1ec364e47379c0fe44cb3112 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 7b0b243f10d2d95b0354e71e6c73401c20c18579..3ac6da933ce1d05c3fe45ad13ead571ae4abbe61 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 7142a49638770bab91ef1aa5f8b5ad393a136add..f8ddbe639043477499a02412d915b1b9ef7aa571 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 706a57a1f64826dcfac2badbd28ef85906acf0d8..f7263bb84cd898d9672a07f00a6e39b6aa252c94 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 dfd5992b7c5211efee252b4bae99192355cc0310..94a69be58ec133fd0b7e88f823d11bbddee8d9dd 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 bd80153d084d45f41422ce1810fa5a713e4b33a5..ee4c30ce4c6776c3655e4de7345dcdc3c4a9cb93 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 8c56c3bb32c9d1bd5ebddb9422db640940f9c8bc..4ef27d5f5cbb65a64854f1058dda8752751f63f4 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 15c081bf25c731dd5b23ad085e6a120dbc6effc7..516d65398626251d635387e2e60ed418400ac7fe 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 0000000000000000000000000000000000000000..2a64f1d51eb40bb23ee79fc96056a288ab0dee54 --- /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" + } + ] +}