|
| 1 | +package org.gitlab4j.api; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assume.assumeTrue; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.gitlab4j.api.GitLabApi.ApiVersion; |
| 10 | +import org.gitlab4j.api.models.AccessLevel; |
| 11 | +import org.gitlab4j.api.models.Group; |
| 12 | +import org.gitlab4j.api.models.Member; |
| 13 | +import org.gitlab4j.api.models.User; |
| 14 | +import org.junit.AfterClass; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.BeforeClass; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +/** |
| 20 | + * In order for these tests to run you must set the following properties in test-gitlab4j.properties |
| 21 | + * |
| 22 | + * TEST_HOST_URL |
| 23 | + * TEST_PRIVATE_TOKEN |
| 24 | + * TEST_USERNAME |
| 25 | + * TEST_GROUP |
| 26 | + * TEST_GROUP_MEMBER_USERNAME |
| 27 | + * |
| 28 | + * If any of the above are NULL, all tests in this class will be skipped. |
| 29 | + * |
| 30 | + */ |
| 31 | +public class TestGroupApi { |
| 32 | + |
| 33 | + // The following needs to be set to your test repository |
| 34 | + private static final String TEST_HOST_URL; |
| 35 | + private static final String TEST_PRIVATE_TOKEN; |
| 36 | + private static final String TEST_USERNAME; |
| 37 | + private static final String TEST_GROUP; |
| 38 | + private static final String TEST_GROUP_MEMBER_USERNAME; |
| 39 | + static { |
| 40 | + TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL"); |
| 41 | + TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN"); |
| 42 | + TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME"); |
| 43 | + TEST_GROUP = TestUtils.getProperty("TEST_GROUP"); |
| 44 | + TEST_GROUP_MEMBER_USERNAME = TestUtils.getProperty("TEST_GROUP_MEMBER_USERNAME"); |
| 45 | + } |
| 46 | + |
| 47 | + private static GitLabApi gitLabApi; |
| 48 | + private static Group testGroup; |
| 49 | + private static User testUser; |
| 50 | + |
| 51 | + public TestGroupApi() { |
| 52 | + super(); |
| 53 | + } |
| 54 | + |
| 55 | + @BeforeClass |
| 56 | + public static void setup() { |
| 57 | + |
| 58 | + String problems = ""; |
| 59 | + if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) { |
| 60 | + problems += "TEST_HOST_URL cannot be empty\n"; |
| 61 | + } |
| 62 | + |
| 63 | + if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().length() == 0) { |
| 64 | + problems += "TEST_PRIVATE_TOKEN cannot be empty\n"; |
| 65 | + } |
| 66 | + |
| 67 | + if (TEST_USERNAME == null || TEST_USERNAME.trim().length() == 0) { |
| 68 | + problems += "TEST_USER_NAME cannot be empty\n"; |
| 69 | + } |
| 70 | + |
| 71 | + if (problems.isEmpty()) { |
| 72 | + |
| 73 | + gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN); |
| 74 | + try { |
| 75 | + List<Group> groups = gitLabApi.getGroupApi().getGroups(TEST_GROUP); |
| 76 | + testGroup = groups.get(0); |
| 77 | + } catch (GitLabApiException gle) { |
| 78 | + problems += "Problem fetching test group, error=" + gle.getMessage() + "\n"; |
| 79 | + } |
| 80 | + |
| 81 | + try { |
| 82 | + testUser = gitLabApi.getUserApi().getUser(TEST_GROUP_MEMBER_USERNAME); |
| 83 | + } catch (GitLabApiException gle) { |
| 84 | + problems += "Problem fetching test user, error=" + gle.getMessage() + "\n"; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (problems.isEmpty()) { |
| 89 | + gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN); |
| 90 | + } else { |
| 91 | + System.err.print(problems); |
| 92 | + } |
| 93 | + |
| 94 | + removeGroupMember(); |
| 95 | + } |
| 96 | + |
| 97 | + @AfterClass |
| 98 | + public static void teardown() { |
| 99 | + removeGroupMember(); |
| 100 | + } |
| 101 | + |
| 102 | + private static void removeGroupMember() { |
| 103 | + |
| 104 | + if (gitLabApi != null) { |
| 105 | + |
| 106 | + if (testGroup != null && testUser != null) { |
| 107 | + try { |
| 108 | + gitLabApi.getGroupApi().removeMember(testGroup.getId(), testUser.getId()); |
| 109 | + } catch (GitLabApiException ignore) { |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Before |
| 116 | + public void beforeMethod() { |
| 117 | + assumeTrue(gitLabApi != null); |
| 118 | + assumeTrue(testGroup != null && testUser != null); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testMemberOperations() throws GitLabApiException { |
| 123 | + |
| 124 | + Member member = gitLabApi.getGroupApi().addMember(testGroup.getId(), testUser.getId(), AccessLevel.DEVELOPER); |
| 125 | + assertNotNull(member); |
| 126 | + assertEquals(testUser.getId(), member.getId()); |
| 127 | + |
| 128 | + gitLabApi.getGroupApi().removeMember(testGroup.getId(), testUser.getId()); |
| 129 | + } |
| 130 | +} |
0 commit comments