TestProjectApi.java 35.17 KiB
/*
 * The MIT License (MIT)
 * Copyright (c) 2017 Greg Messner <greg@messners.com>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package org.gitlab4j.api;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import jakarta.ws.rs.core.Response;
import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.AccessRequest;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProjectFetches;
import org.gitlab4j.api.models.ProjectFilter;
import org.gitlab4j.api.models.User;
import org.gitlab4j.api.models.Variable;
import org.gitlab4j.api.models.Visibility;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
/**
 * In order for these tests to run you must set the following properties in ~/test-gitlab4j.properties
 * TEST_NAMESPACE
 * TEST_PROJECT_NAME
 * TEST_HOST_URL
 * TEST_PRIVATE_TOKEN
 * TEST_GROUP_PROJECT
 * If any of the above are NULL, all tests in this class will be skipped.
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
* NOTE: &amp;FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that the tests are in the correct order */ @Tag("integration") @ExtendWith(SetupIntegrationTestExtension.class) @TestMethodOrder(MethodOrderer.MethodName.class) public class TestProjectApi extends AbstractIntegrationTest { // The following needs to be set to your test repository private static final String TEST_GROUP = HelperUtils.getProperty(GROUP_KEY); private static final String TEST_GROUP_PROJECT = HelperUtils.getProperty(GROUP_PROJECT_KEY); private static final String TEST_XFER_NAMESPACE = HelperUtils.getProperty(XFER_NAMESPACE_KEY); private static final String TEST_SUDO_AS_USERNAME = HelperUtils.getProperty(SUDO_AS_USERNAME_KEY); private static final String TEST_REQUEST_ACCESS_USERNAME = HelperUtils.getProperty(TEST_REQUEST_ACCESS_USERNAME_KEY); private static final String TEST_PROJECT_NAME_1 = "test-gitlab4j-create-project"; private static final String TEST_PROJECT_NAME_2 = "test-gitlab4j-create-project-2"; private static final String TEST_NAMESPACE_PROJECT_NAME = "test-gitlab4j-create-namespace-project"; private static final String TEST_PROJECT_NAME_UPDATE = "test-gitlab4j-create-project-update"; private static final String TEST_XFER_PROJECT_NAME = "test-gitlab4j-xfer-project"; private static final String TEST_VARIABLE_KEY_PREFIX = "TEST_VARIABLE_KEY_"; private static GitLabApi gitLabApi; private static Project testProject; private static User currentUser; public TestProjectApi() { super(); } @BeforeAll public static void setup() { // Must setup the connection to the GitLab test server gitLabApi = baseTestSetup(); testProject = getTestProject(); currentUser = getCurrentUser(); deleteAllTransientTestData(); } @AfterAll public static void teardown() throws GitLabApiException { deleteAllTransientTestData(); } private static void deleteAllTransientTestData() { if (gitLabApi == null) { return; } try { gitLabApi.getProjectApi().deleteProject(Project.getPathWithNammespace(TEST_NAMESPACE, TEST_PROJECT_NAME_1)); } catch (GitLabApiException ignore) {} try { gitLabApi.getProjectApi().deleteProject(Project.getPathWithNammespace(TEST_NAMESPACE, TEST_PROJECT_NAME_2)); } catch (GitLabApiException ignore) {} try { gitLabApi.getProjectApi().deleteProject(Project.getPathWithNammespace(TEST_NAMESPACE, TEST_PROJECT_NAME_UPDATE)); } catch (GitLabApiException ignore) {} try { gitLabApi.getProjectApi().deleteProject(Project.getPathWithNammespace(TEST_NAMESPACE, TEST_XFER_PROJECT_NAME)); } catch (GitLabApiException ignore) {} try { gitLabApi.getProjectApi().deleteProject(Project.getPathWithNammespace(TEST_NAMESPACE, TEST_NAMESPACE_PROJECT_NAME)); } catch (GitLabApiException ignore) {}
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
if (TEST_GROUP != null && TEST_PROJECT_NAME != null) { try { List<Group> groups = gitLabApi.getGroupApi().getGroups(TEST_GROUP); if (groups != null && groups.size() > 0) { gitLabApi.getProjectApi().unshareProject(testProject, groups.get(0).getId()); } List<Variable> variables = gitLabApi.getProjectApi().getVariables(testProject); if (variables != null) { for (Variable variable : variables) { if (variable.getKey().startsWith(TEST_VARIABLE_KEY_PREFIX)) { gitLabApi.getProjectApi().updateVariable(testProject, variable.getKey(), "EMPTY", false); gitLabApi.getProjectApi().deleteVariable(testProject, variable.getKey()); } } } } catch (GitLabApiException ignore) { } } if (TEST_GROUP != null && TEST_GROUP_PROJECT != null) { try { gitLabApi.getProjectApi().deleteProject(Project.getPathWithNammespace(TEST_NAMESPACE, TEST_GROUP_PROJECT)); } catch (GitLabApiException ignore) {} } if (TEST_XFER_NAMESPACE != null) { try { gitLabApi.getProjectApi().deleteProject(Project.getPathWithNammespace(TEST_XFER_NAMESPACE, TEST_XFER_PROJECT_NAME)); } catch (GitLabApiException ignore) {} } if (TEST_REQUEST_ACCESS_USERNAME != null) { Optional<User> user = gitLabApi.getUserApi().getOptionalUser(TEST_REQUEST_ACCESS_USERNAME); if (user.isPresent()) { Long userId = user.get().getId(); try { gitLabApi.getProjectApi().denyAccessRequest(testProject, userId); } catch (Exception e) { try { gitLabApi.getProjectApi().removeMember(testProject, userId); } catch (Exception ignore) {} } } } } @BeforeEach public void beforeMethod() { assumeTrue(gitLabApi != null); } @Test public void testProjectsNoAuth() throws GitLabApiException { GitLabApi gitLabApi = new GitLabApi(TEST_HOST_URL, ""); List<Project> projects = gitLabApi.getProjectApi().getProjects(1, 1); assertTrue(projects != null); gitLabApi.close(); } @Test public void testCreate() throws GitLabApiException { Project project = new Project() .withName(TEST_PROJECT_NAME_1) .withDescription("GitLab4J test project.") .withIssuesEnabled(true) .withMergeRequestsEnabled(true)
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
.withWikiEnabled(true) .withSnippetsEnabled(true) .withVisibility(Visibility.PUBLIC) .withTagList(Arrays.asList("tag1", "tag2")); Project newProject = gitLabApi.getProjectApi().createProject(project); assertNotNull(newProject); assertEquals(TEST_PROJECT_NAME_1, newProject.getName()); assertEquals(project.getDescription(), newProject.getDescription()); assertEquals(project.getIssuesEnabled(), newProject.getIssuesEnabled()); assertEquals(project.getMergeRequestsEnabled(), newProject.getMergeRequestsEnabled()); assertEquals(project.getWikiEnabled(), newProject.getWikiEnabled()); assertEquals(project.getSnippetsEnabled(), newProject.getSnippetsEnabled()); assertEquals(project.getTagList(), newProject.getTagList()); assertTrue(Visibility.PUBLIC == newProject.getVisibility() || Boolean.TRUE == newProject.getPublic()); } @Test public void testUpdate() throws GitLabApiException { Project project = new Project() .withName(TEST_PROJECT_NAME_UPDATE) .withDescription("GitLab4J test project.") .withIssuesEnabled(true) .withMergeRequestsEnabled(true) .withWikiEnabled(true) .withSnippetsEnabled(true) .withVisibility(Visibility.PUBLIC) .withTagList(Arrays.asList("tag1", "tag2")) .withMergeMethod(Project.MergeMethod.MERGE) .withSuggestionCommitMessage("SuggestionCommitMessageOriginal") .withRemoveSourceBranchAfterMerge(false); Project newProject = gitLabApi.getProjectApi().createProject(project); assertNotNull(newProject); assertEquals(project.getName(), newProject.getName()); assertEquals(project.getDescription(), newProject.getDescription()); assertEquals(project.getIssuesEnabled(), newProject.getIssuesEnabled()); assertEquals(project.getMergeRequestsEnabled(), newProject.getMergeRequestsEnabled()); assertEquals(project.getWikiEnabled(), newProject.getWikiEnabled()); assertEquals(project.getSnippetsEnabled(), newProject.getSnippetsEnabled()); assertEquals(project.getTagList(), newProject.getTagList()); assertTrue(Visibility.PUBLIC == newProject.getVisibility() || Boolean.TRUE == newProject.getPublic()); assertEquals(Project.MergeMethod.MERGE, newProject.getMergeMethod()); assertEquals(project.getSuggestionCommitMessage(), newProject.getSuggestionCommitMessage()); assertEquals(project.getRemoveSourceBranchAfterMerge(), newProject.getRemoveSourceBranchAfterMerge()); project = new Project() .withId(newProject.getId()) .withName(newProject.getName()) .withDescription("GitLab4J test updateProject()") .withIssuesEnabled(false) .withMergeRequestsEnabled(false) .withWikiEnabled(false) .withSnippetsEnabled(false) .withVisibility(Visibility.PRIVATE) .withMergeMethod(Project.MergeMethod.REBASE_MERGE) .withSuggestionCommitMessage("SuggestionCommitMessageUpdated") .withRemoveSourceBranchAfterMerge(true); Project updatedProject = gitLabApi.getProjectApi().updateProject(project); assertNotNull(updatedProject); assertEquals(project.getName(), newProject.getName()); assertEquals(project.getDescription(), updatedProject.getDescription()); assertEquals(project.getIssuesEnabled(), updatedProject.getIssuesEnabled()); assertEquals(project.getMergeRequestsEnabled(), updatedProject.getMergeRequestsEnabled()); assertEquals(project.getWikiEnabled(), updatedProject.getWikiEnabled()); assertEquals(project.getSnippetsEnabled(), updatedProject.getSnippetsEnabled()); assertTrue(Visibility.PRIVATE == updatedProject.getVisibility() || Boolean.FALSE == updatedProject.getPublic()); assertEquals(Project.MergeMethod.REBASE_MERGE, updatedProject.getMergeMethod());
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
assertEquals(project.getSuggestionCommitMessage(), updatedProject.getSuggestionCommitMessage()); assertEquals(true, updatedProject.getRemoveSourceBranchAfterMerge()); } @Test public void testListProjects() throws GitLabApiException { List<Project> projects = gitLabApi.getProjectApi().getProjects(); assertNotNull(projects); assertTrue(projects.size() >= 2); int matchCount = 0; for (Project project : projects) { if (TEST_PROJECT_NAME_1.equals(project.getName())) matchCount++; else if (TEST_PROJECT_NAME_2.equals(project.getName())) matchCount++; } assertEquals(2, matchCount); projects = gitLabApi.getProjectApi().getProjects(TEST_PROJECT_NAME_1); assertNotNull(projects); assertEquals(2, projects.size()); assertEquals(TEST_PROJECT_NAME_2, projects.get(0).getName()); assertEquals(TEST_PROJECT_NAME_1, projects.get(1).getName()); } @Test public void testListProjectsWithParams() throws GitLabApiException { List<Project> projects = gitLabApi.getProjectApi().getProjects(false, Visibility.PUBLIC, Constants.ProjectOrderBy.NAME, Constants.SortOrder.DESC, null, true, true, true, false, true); assertNotNull(projects); assertTrue(projects.size() >= 2); int matchCount = 0; for (Project project : projects) { if (TEST_PROJECT_NAME_1.equals(project.getName())) matchCount++; else if (TEST_PROJECT_NAME_2.equals(project.getName())) matchCount++; } assertEquals(2, matchCount); projects = gitLabApi.getProjectApi().getProjects(TEST_PROJECT_NAME_1); assertNotNull(projects); assertEquals(2, projects.size()); assertEquals(TEST_PROJECT_NAME_2, projects.get(0).getName()); assertEquals(TEST_PROJECT_NAME_1, projects.get(1).getName()); } @Test public void testListProjectsWithStatistics() throws GitLabApiException { List<Project> projects = gitLabApi.getProjectApi().getProjects(false, null, Constants.ProjectOrderBy.NAME, Constants.SortOrder.DESC, null, false, false, false, false, true); assertNotNull(projects); assertTrue(projects.size() >= 2); assertNotNull(projects.get(0).getStatistics()); assertNotNull(projects.get(0).getStatistics().getLfsObjectsSize()); assertNotNull(projects.get(0).getStatistics().getCommitCount()); assertNotNull(projects.get(0).getStatistics().getJobArtifactsSize()); assertNotNull(projects.get(0).getStatistics().getStorageSize()); assertNotNull(projects.get(0).getStatistics().getRepositorySize()); assertNotNull(projects.get(0).getStatistics().getWikiSize()); }
351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
@Test public void testListProjectsWithParamsViaPager() throws GitLabApiException { Pager<Project> pager = gitLabApi.getProjectApi().getProjects(false, Visibility.PUBLIC, Constants.ProjectOrderBy.NAME, Constants.SortOrder.DESC, null, true, true, true, false, true, 10); assertNotNull(pager); assertTrue(pager.getTotalItems() >= 2); List<Project> projects = pager.next(); int matchCount = 0; for (Project project : projects) { if (TEST_PROJECT_NAME_1.equals(project.getName())) matchCount++; else if (TEST_PROJECT_NAME_2.equals(project.getName())) matchCount++; } assertEquals(2, matchCount); projects = gitLabApi.getProjectApi().getProjects(TEST_PROJECT_NAME_1); assertNotNull(projects); assertEquals(2, projects.size()); assertEquals(TEST_PROJECT_NAME_2, projects.get(0).getName()); assertEquals(TEST_PROJECT_NAME_1, projects.get(1).getName()); } @Test public void testListProjectsWithParamByPage() throws GitLabApiException { List<Project> projects = gitLabApi.getProjectApi().getProjects(false, Visibility.PUBLIC, Constants.ProjectOrderBy.NAME, Constants.SortOrder.DESC, null, true, true, true, false, true, 1, 10); assertNotNull(projects); assertTrue(projects.size() >= 2); int matchCount = 0; for (Project project : projects) { if (TEST_PROJECT_NAME_1.equals(project.getName())) matchCount++; else if (TEST_PROJECT_NAME_2.equals(project.getName())) matchCount++; } assertEquals(2, matchCount); projects = gitLabApi.getProjectApi().getProjects(TEST_PROJECT_NAME_1); assertNotNull(projects); assertEquals(2, projects.size()); assertEquals(TEST_PROJECT_NAME_2, projects.get(0).getName()); assertEquals(TEST_PROJECT_NAME_1, projects.get(1).getName()); } @Test public void testListStarredProjects() throws GitLabApiException { assumeTrue(testProject != null); try { gitLabApi.getProjectApi().starProject(testProject); } catch (Exception ignore) { } List<Project> projects = gitLabApi.getProjectApi().getStarredProjects(); try { gitLabApi.getProjectApi().unstarProject(testProject); } catch (Exception ignore) { } assertNotNull(projects);
421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
assertNotNull(projects); assertEquals(1, projects.size()); assertEquals(TEST_PROJECT_NAME, projects.get(0).getName()); } @Test public void testListStarredProjectsWithParams() throws GitLabApiException { assumeTrue(testProject != null); try { gitLabApi.getProjectApi().starProject(testProject); } catch (Exception ignore) { } List<Project> projects = gitLabApi.getProjectApi().getProjects(false, Visibility.PUBLIC, Constants.ProjectOrderBy.NAME, Constants.SortOrder.DESC, TEST_PROJECT_NAME, true, true, true, true, true); try { gitLabApi.getProjectApi().unstarProject(testProject); } catch (Exception ignore) { } assertNotNull(projects); assertEquals(1, projects.size()); assertEquals(TEST_PROJECT_NAME, projects.get(0).getName()); } @Test public void testRemoveByDelete() throws GitLabApiException { Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME_1); gitLabApi.getProjectApi().deleteProject(project); } @Test public void testCreateParameterBased() throws GitLabApiException { Project newProject = gitLabApi.getProjectApi().createProject( TEST_PROJECT_NAME_2, null, "GitLab4J test project.", true, true, true, true, Visibility.PUBLIC, null, null); assertNotNull(newProject); assertEquals(TEST_PROJECT_NAME_2, newProject.getName()); assertEquals("GitLab4J test project.", newProject.getDescription()); assertEquals(true, newProject.getIssuesEnabled()); assertEquals(true, newProject.getMergeRequestsEnabled()); assertEquals(true, newProject.getWikiEnabled()); assertEquals(true, newProject.getSnippetsEnabled()); assertTrue(Visibility.PUBLIC == newProject.getVisibility() || Boolean.TRUE == newProject.getPublic()); } @Test public void testProjects() throws GitLabApiException { List<Project> projects = gitLabApi.getProjectApi().getProjects(); assertTrue(projects != null); } @Test public void testProjectsWithFilter() throws GitLabApiException { ProjectFilter filter = new ProjectFilter().withOwned(true).withStatistics(false); List<Project> projects = gitLabApi.getProjectApi().getProjects(filter); assertTrue(projects != null); assertTrue(projects.size() > 0); assertNull(projects.get(0).getStatistics()); } @Test public void testProjectsWithFilterAndStatistics() throws GitLabApiException { ProjectFilter filter = new ProjectFilter().withOwned(true).withStatistics(true); List<Project> projects = gitLabApi.getProjectApi().getProjects(filter); assertTrue(projects != null); assertTrue(projects.size() > 0);
491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
assertNotNull(projects.get(0).getStatistics()); } @Test public void testProjectsWithAccessLevelFilter() throws GitLabApiException { ProjectFilter filter = new ProjectFilter().withMinAccessLevel(AccessLevel.GUEST); List<Project> guestProjects = gitLabApi.getProjectApi().getProjects(filter); assertTrue(guestProjects != null); assertTrue(guestProjects.size() > 0); // Use sudo to impersonate a non-admin user try { gitLabApi.sudo(TEST_SUDO_AS_USERNAME); filter = new ProjectFilter().withMinAccessLevel(AccessLevel.OWNER); List<Project> ownedProjects = gitLabApi.getProjectApi().getProjects(filter); assertTrue(ownedProjects != null); assertTrue(guestProjects.size() > ownedProjects.size()); } finally { gitLabApi.unsudo(); } } @Test public void testProjectPerPage() throws GitLabApiException { List<Project> projects = gitLabApi.getProjectApi().getProjects(1, 10); assertNotNull(projects); assertTrue(projects.size() > 0); } @Test public void testOwnedProjects() throws GitLabApiException { List<Project> projects = gitLabApi.getProjectApi().getOwnedProjects(); assertTrue(projects != null); } @Test public void testOwnedProjectsPerPage() throws GitLabApiException { List<Project> projects = gitLabApi.getProjectApi().getOwnedProjects(1, 10); assertTrue(projects != null); assertTrue(projects.size() > 0); } @Test public void testMemberProjects() throws GitLabApiException { List<Project> projects = gitLabApi.getProjectApi().getMemberProjects(); assertTrue(projects != null); } @Test public void testMemberProjectsPerPage() throws GitLabApiException { List<Project> projects = gitLabApi.getProjectApi().getMemberProjects(1, 10); assertTrue(projects != null); assertTrue(projects.size() > 0); } @Test public void testProjectLanguages() throws GitLabApiException { assumeTrue(TEST_GROUP != null && TEST_GROUP_PROJECT != null); assumeTrue(TEST_GROUP.trim().length() > 0 && TEST_GROUP_PROJECT.trim().length() > 0); Project project = gitLabApi.getProjectApi().getProject(TEST_GROUP, TEST_GROUP_PROJECT); assertNotNull(project); Map<String, Float> projectLanguages = gitLabApi.getProjectApi().getProjectLanguages(project.getId()); assertNotNull(projectLanguages); }
561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
@Test public void testCreateProjectInNamespace() throws GitLabApiException { assumeTrue(currentUser != null); Project namespaceProject = null; try { namespaceProject = gitLabApi.getProjectApi().createProject(currentUser.getId(), TEST_NAMESPACE_PROJECT_NAME); assertNotNull(namespaceProject); } finally { if (namespaceProject != null) { try { gitLabApi.getProjectApi().deleteProject(namespaceProject); } catch (Exception ignore) {} } } } @Test public void testForkProject() throws GitLabApiException { assumeTrue(TEST_GROUP != null && TEST_GROUP_PROJECT != null); assumeTrue(TEST_GROUP.trim().length() > 0 && TEST_GROUP_PROJECT.trim().length() > 0); Project project = gitLabApi.getProjectApi().getProject(TEST_GROUP, TEST_GROUP_PROJECT); assertNotNull(project); Project forkedProject = null; try { forkedProject = gitLabApi.getProjectApi().forkProject(project.getId(), TEST_NAMESPACE); assertNotNull(forkedProject); } finally { if (forkedProject != null) { try { gitLabApi.getProjectApi().deleteProject(forkedProject); } catch (Exception ignore) {} } } } @Test public void testShareProject() throws GitLabApiException { assumeTrue(TEST_GROUP != null && TEST_GROUP_PROJECT != null); assumeTrue(TEST_GROUP.trim().length() > 0 && TEST_GROUP_PROJECT.trim().length() > 0); assumeTrue(testProject != null); List<Group> groups = gitLabApi.getGroupApi().getGroups(TEST_GROUP); assertNotNull(groups); Group shareGroup = groups.get(0); gitLabApi.getProjectApi().shareProject(testProject, shareGroup.getId(), AccessLevel.DEVELOPER, null); gitLabApi.getProjectApi().unshareProject(testProject, shareGroup.getId()); } @Test public void testArchiveProject() throws GitLabApiException { assertNotNull(testProject); assertEquals(true, gitLabApi.getProjectApi().archiveProject(testProject.getId()).getArchived()); assertEquals(false, gitLabApi.getProjectApi().unarchiveProject(testProject).getArchived()); } @Test public void testGetOptionalProject() throws GitLabApiException { Optional<Project> optional = gitLabApi.getProjectApi().getOptionalProject(TEST_NAMESPACE, TEST_PROJECT_NAME); assertNotNull(optional); assertTrue(optional.isPresent()); assertEquals(TEST_PROJECT_NAME, optional.get().getName());
631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
Long projectId = optional.get().getId(); optional = gitLabApi.getProjectApi().getOptionalProject(projectId); assertNotNull(optional); assertTrue(optional.isPresent()); assertEquals(projectId, optional.get().getId()); optional = gitLabApi.getProjectApi().getOptionalProject(TEST_NAMESPACE, "this-project-does-not-exist"); assertNotNull(optional); assertFalse(optional.isPresent()); assertEquals(Response.Status.NOT_FOUND.getStatusCode(), GitLabApi.getOptionalException(optional).getHttpStatus()); optional = gitLabApi.getProjectApi().getOptionalProject(1234567L); assertNotNull(optional); assertFalse(optional.isPresent()); assertEquals(Response.Status.NOT_FOUND.getStatusCode(), GitLabApi.getOptionalException(optional).getHttpStatus()); } @Test public void testStarAndUnstarProject() throws GitLabApiException { assumeTrue(testProject != null); try { gitLabApi.getProjectApi().unstarProject(testProject); } catch (Exception ignore) { } Project starredProject = gitLabApi.getProjectApi().starProject(testProject); assertNotNull(starredProject); assertEquals(1, (int)starredProject.getStarCount()); Project unstarredProject = gitLabApi.getProjectApi().unstarProject(testProject); assertNotNull(unstarredProject); assertEquals(0, (int)unstarredProject.getStarCount()); } @Test public void testTransferProject() throws GitLabApiException { assumeTrue(TEST_XFER_NAMESPACE != null && TEST_XFER_NAMESPACE.trim().length() > 0); Project project = new Project() .withName(TEST_XFER_PROJECT_NAME) .withDescription("GitLab4J test project - transfer.") .withVisibility(Visibility.PUBLIC); Project newProject = gitLabApi.getProjectApi().createProject(project); assertNotNull(newProject); Project projectToTransfer = gitLabApi.getProjectApi().getProject(newProject); assertNotNull(projectToTransfer); Project transferedProject = gitLabApi.getProjectApi().transferProject(projectToTransfer, TEST_XFER_NAMESPACE); assertNotNull(transferedProject); } @Test public void testVariables() throws GitLabApiException { assumeTrue(testProject != null); String key = TEST_VARIABLE_KEY_PREFIX + HelperUtils.getRandomInt() + "_" + HelperUtils.getRandomInt(); String value = "ABCDEFG12345678" + HelperUtils.getRandomInt(); Variable variable = gitLabApi.getProjectApi().createVariable(testProject, key, value, null, null, true); assertNotNull(variable); assertEquals(key, variable.getKey()); assertEquals(value, variable.getValue()); Stream<Variable> variables = gitLabApi.getProjectApi().getVariablesStream(testProject);
701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
assertNotNull(variables); Variable matchingVariable = variables.filter(v -> v.getKey().equals(key)).findAny().orElse(null); assertNotNull(matchingVariable); assertEquals(key, matchingVariable.getKey()); assertEquals(value, matchingVariable.getValue()); assertFalse(matchingVariable.getProtected()); String scope = matchingVariable.getEnvironmentScope(); assertTrue(scope == null || "*".equals(scope)); gitLabApi.getProjectApi().updateVariable(testProject, key, "NO_VALUE", true, "DEV"); variable = gitLabApi.getProjectApi().getVariable(testProject, key); assertNotNull(variable); assertEquals(key, variable.getKey()); assertEquals("NO_VALUE", variable.getValue()); assertTrue(variable.getProtected()); gitLabApi.getProjectApi().updateVariable(testProject, key, value, Variable.Type.ENV_VAR, false, true, "DEV"); variable = gitLabApi.getProjectApi().getVariable(testProject, key); assertNotNull(variable); assertEquals(key, variable.getKey()); assertEquals(value, variable.getValue()); assertEquals(Variable.Type.ENV_VAR, variable.getVariableType()); assertFalse(variable.getProtected()); gitLabApi.getProjectApi().deleteVariable(testProject, key); variables = gitLabApi.getProjectApi().getVariablesStream(testProject); assertNotNull(variables); matchingVariable = variables.filter(v -> v.getKey().equals(key)).findAny().orElse(null); assertNull(matchingVariable); } @Test public void testFileVariable() throws GitLabApiException { assumeTrue(testProject != null); String key = TEST_VARIABLE_KEY_PREFIX + HelperUtils.getRandomInt() + "_" + HelperUtils.getRandomInt(); String value = "/tmp/test.txt"; Variable variable = gitLabApi.getProjectApi().createVariable(testProject, key, value, Variable.Type.FILE, null, false); assertNotNull(variable); assertEquals(key, variable.getKey()); assertEquals(value, variable.getValue()); assertEquals(Variable.Type.FILE, variable.getVariableType()); gitLabApi.getProjectApi().deleteVariable(testProject, key); Stream<Variable> variables = gitLabApi.getProjectApi().getVariablesStream(testProject); assertNotNull(variables); Variable matchingVariable = variables.filter(v -> v.getKey().equals(key)).findAny().orElse(null); assertNull(matchingVariable); } @Test public void testGetMembers() throws GitLabApiException { assumeTrue(testProject != null); // Act List<Member> members = gitLabApi.getProjectApi().getMembers(testProject); // Assert assertNotNull(members); }
771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
@Test public void testAllMemberOperations() throws GitLabApiException { assumeTrue(testProject != null); // Act List<Member> members = gitLabApi.getProjectApi().getAllMembers(testProject); // Assert assertNotNull(members); } @Test public void testRequestAccess() throws GitLabApiException { assumeTrue(TEST_REQUEST_ACCESS_USERNAME != null && TEST_REQUEST_ACCESS_USERNAME.length() > 0); gitLabApi.sudo(TEST_REQUEST_ACCESS_USERNAME); User user = gitLabApi.getUserApi().getCurrentUser(); assertNotNull(user); final Long userId = user.getId(); try { try { AccessRequest accessRequest = gitLabApi.getProjectApi().requestAccess(testProject); assertNotNull(accessRequest); assertEquals(userId, accessRequest.getId()); } finally { gitLabApi.unsudo(); } Stream<AccessRequest> requests = gitLabApi.getProjectApi().getAccessRequestsStream(testProject); assertTrue(requests.anyMatch(r -> r.getId() == userId)); AccessRequest accessRequest = gitLabApi.getProjectApi().approveAccessRequest(testProject, user.getId(), AccessLevel.DEVELOPER); assertNotNull(accessRequest); assertEquals(user.getId(), accessRequest.getId()); assertEquals(AccessLevel.DEVELOPER, accessRequest.getAccessLevel()); user = null; requests = gitLabApi.getProjectApi().getAccessRequestsStream(testProject); assertFalse(requests.anyMatch(r -> r.getId() == userId)); } finally { try { if (user == null) { gitLabApi.getProjectApi().removeMember(testProject, userId); } else { gitLabApi.getProjectApi().denyAccessRequest(testProject, userId); } } catch (Exception ignore) {} } } @Test public void testDenyRequestAccess() throws GitLabApiException { assumeTrue(TEST_REQUEST_ACCESS_USERNAME != null && TEST_REQUEST_ACCESS_USERNAME.length() > 0); gitLabApi.sudo(TEST_REQUEST_ACCESS_USERNAME); User user = gitLabApi.getUserApi().getCurrentUser(); assertNotNull(user); final Long userId = user.getId(); try { try {
841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
AccessRequest accessRequest = gitLabApi.getProjectApi().requestAccess(testProject); assertNotNull(accessRequest); assertEquals(userId, accessRequest.getId()); } finally { gitLabApi.unsudo(); } List<AccessRequest> requests = gitLabApi.getProjectApi().getAccessRequests(testProject); assertTrue(requests.stream().anyMatch(r -> r.getId() == userId)); gitLabApi.getProjectApi().denyAccessRequest(testProject, userId); requests = gitLabApi.getProjectApi().getAccessRequests(testProject); assertFalse(requests.stream().anyMatch(r -> r.getId() == userId)); user = null; } finally { try { if (user != null) { gitLabApi.getProjectApi().denyAccessRequest(testProject, userId); } } catch (Exception ignore) { } } } @Test public void testGetProjectStatistics() throws GitLabApiException { assertNotNull(testProject); Optional<ProjectFetches> statistics = gitLabApi.getProjectApi().getOptionalProjectStatistics(testProject); assertTrue(statistics.isPresent()); } @Test public void testTriggerHousekeeping() throws GitLabApiException { assertNotNull(testProject); try { gitLabApi.getProjectApi().triggerHousekeeping(testProject); } catch (GitLabApiException glae) { if (!glae.getMessage().contains("already triggered")) { throw glae; } } } }