An error occurred while loading the file. Please try again.
An error occurred while loading the file. Please try again.
An error occurred while loading the file. Please try again.
-
Greg Messner authored6753e62f
/*
* 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.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.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.Visibility;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
/**
* 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.
*
* NOTE: &FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that the tests are in the correct order
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestProjectApi {
// The following needs to be set to your test repository
private static final String TEST_NAMESPACE;
private static final String TEST_PROJECT_NAME;
private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN;
private static final String TEST_GROUP;
private static final String TEST_GROUP_PROJECT;
static {
TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME");
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
TEST_GROUP = TestUtils.getProperty("TEST_GROUP");
TEST_GROUP_PROJECT = TestUtils.getProperty("TEST_GROUP_PROJECT");
}
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_PROJECT_NAME_UPDATE = "test-gitlab4j-create-project-update";
private static GitLabApi gitLabApi;
public TestProjectApi() {
super();
}
@BeforeClass
public static void setup() {
String problems = "";
if (TEST_NAMESPACE == null || TEST_NAMESPACE.trim().length() == 0) {
problems += "TEST_NAMESPACE cannot be empty\n";
}
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) {
problems += "TEST_HOST_URL cannot be empty\n";
}
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().length() == 0) {
problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
}
if (problems.isEmpty()) {
gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
} else {
System.err.print(problems);
}
deleteAllTestProjects();
}
@AfterClass
public static void teardown() throws GitLabApiException {
deleteAllTestProjects();
}
private static void deleteAllTestProjects() {
if (gitLabApi != null) {
try {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME_1);
gitLabApi.getProjectApi().deleteProject(project);
} catch (GitLabApiException ignore) {}
try {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME_2);
gitLabApi.getProjectApi().deleteProject(project);
} catch (GitLabApiException ignore) {}
try {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME_UPDATE);
gitLabApi.getProjectApi().deleteProject(project);
} catch (GitLabApiException ignore) {}
if (TEST_GROUP != null && TEST_PROJECT_NAME != null) {
try {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
List<Group> groups = gitLabApi.getGroupApi().getGroups(TEST_GROUP);
gitLabApi.getProjectApi().unshareProject(project.getId(), groups.get(0).getId());
} catch (GitLabApiException ignore) {
}
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
}
if (TEST_GROUP != null && TEST_GROUP_PROJECT != null) {
try {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_GROUP_PROJECT);
gitLabApi.getProjectApi().deleteProject(project);
} catch (GitLabApiException ignore) {
}
}
}
}
@Before
public void beforeMethod() {
assumeTrue(gitLabApi != null);
}
@Test
public void testCreate() throws GitLabApiException {
Project project = new Project()
.withName(TEST_PROJECT_NAME_1)
.withDescription("GitLab4J test project.")
.withIssuesEnabled(true)
.withMergeRequestsEnabled(true)
.withWikiEnabled(true)
.withSnippetsEnabled(true)
.withVisibility(Visibility.PUBLIC);
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());
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);
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());
assertTrue(Visibility.PUBLIC == newProject.getVisibility() || Boolean.TRUE == newProject.getPublic());
project = new Project()
.withId(newProject.getId())
.withName(newProject.getName())
.withDescription("GitLab4J test updateProject()")
.withIssuesEnabled(false)
.withMergeRequestsEnabled(false)
.withWikiEnabled(false)
.withSnippetsEnabled(false)
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
.withVisibility(Visibility.PRIVATE);
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());
}
@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 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);
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
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 {
List<Project> projects = gitLabApi.getProjectApi().getStarredProjects();
assertNotNull(projects);
assertNotNull(projects);
assertEquals(1, projects.size());
assertEquals(TEST_PROJECT_NAME, projects.get(0).getName());
}
@Test
public void testListStarredProjectsWithParams() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getProjects(false, Visibility.PUBLIC,
Constants.ProjectOrderBy.NAME, Constants.SortOrder.DESC, TEST_PROJECT_NAME, true, true, true, true, true);
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
351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
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 testRemoveByDeleteParameterBased() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME_2);
gitLabApi.getProjectApi().deleteProject(project);
}
@Test
public void testProjects() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getProjects();
assertTrue(projects != null);
}
@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 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 = gitLabApi.getProjectApi().forkProject(project.getId(), TEST_NAMESPACE);
assertNotNull(forkedProject);
}
421422423424425426427428429430431432433434435436437438439
@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);
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
List<Group> groups = gitLabApi.getGroupApi().getGroups(TEST_GROUP);
assertNotNull(groups);
Group shareGroup = groups.get(0);
gitLabApi.getProjectApi().shareProject(project.getId(), shareGroup.getId(), AccessLevel.DEVELOPER, null);
gitLabApi.getProjectApi().unshareProject(project.getId(), shareGroup.getId());
}
}