Commit 08e77fd2 authored by Greg Messner's avatar Greg Messner
Browse files

Improved test coverage and added test-gitlab4j.properties support.

parent 2596bedd
package org.gitlab4j.api; package org.gitlab4j.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;
...@@ -24,18 +25,14 @@ import org.junit.Test; ...@@ -24,18 +25,14 @@ import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
/** /**
* In order for these tests to run you must set the following systems properties: * In order for these tests to run you must set the following properties in test-gitlab4j.properties
* *
* TEST_NAMESPACE * TEST_NAMESPACE
* TEST_PROJECT_NAME * TEST_PROJECT_NAME
* TEST_HOST_URL * TEST_HOST_URL
* TEST_PRIVATE_TOKEN * TEST_PRIVATE_TOKEN
* *
* If any of the above are NULL, all tests in this class will be skipped. If running from mvn simply * If any of the above are NULL, all tests in this class will be skipped.
* use a command line similar to:
*
* mvn test -DTEST_PRIVATE_TOKEN=your_private_token -DTEST_HOST_URL=https://gitlab.com \
* -DTEST_NAMESPACE=your_namespace -DTEST_PROJECT_NAME=test-project
* *
* NOTE: &FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that testCreate() is executed first. * NOTE: &FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that testCreate() is executed first.
*/ */
...@@ -48,10 +45,10 @@ public class TestGitLabApi { ...@@ -48,10 +45,10 @@ public class TestGitLabApi {
private static final String TEST_HOST_URL; private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN; private static final String TEST_PRIVATE_TOKEN;
static { static {
TEST_NAMESPACE = System.getProperty("TEST_NAMESPACE"); TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
TEST_PROJECT_NAME = System.getProperty("TEST_PROJECT_NAME"); TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME");
TEST_HOST_URL = System.getProperty("TEST_HOST_URL"); TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = System.getProperty("TEST_PRIVATE_TOKEN"); TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
} }
private static final String TEST_BRANCH_NAME = "feature/test_branch"; private static final String TEST_BRANCH_NAME = "feature/test_branch";
...@@ -120,12 +117,39 @@ public class TestGitLabApi { ...@@ -120,12 +117,39 @@ public class TestGitLabApi {
assertTrue(projects != null); assertTrue(projects != null);
} }
@Test
public void testProjectPerPage() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getProjects(1, 10);
assertNotNull(projects);
assertEquals(10, projects.size());
}
@Test @Test
public void testOwnedProjects() throws GitLabApiException { public void testOwnedProjects() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getOwnedProjects(); List<Project> projects = gitLabApi.getProjectApi().getOwnedProjects();
assertTrue(projects != null); 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 @Test
public void testCreateBranch() throws GitLabApiException { public void testCreateBranch() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
...@@ -133,6 +157,11 @@ public class TestGitLabApi { ...@@ -133,6 +157,11 @@ public class TestGitLabApi {
Branch branch = gitLabApi.getRepositoryApi().createBranch(project.getId(), TEST_BRANCH_NAME, "master"); Branch branch = gitLabApi.getRepositoryApi().createBranch(project.getId(), TEST_BRANCH_NAME, "master");
assertNotNull(branch); assertNotNull(branch);
Branch fetchedBranch = gitLabApi.getRepositoryApi().getBranch(project.getId(), TEST_BRANCH_NAME);
assertNotNull(fetchedBranch);
assertEquals(branch.getName(), fetchedBranch.getName());
} }
@Test @Test
...@@ -172,4 +201,34 @@ public class TestGitLabApi { ...@@ -172,4 +201,34 @@ public class TestGitLabApi {
assertTrue(file.length() > 0); assertTrue(file.length() > 0);
file.delete(); file.delete();
} }
@Test
public void testRawFileViaFile() throws GitLabApiException, IOException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
File file = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README", null);
assertTrue(file.length() > 0);
file.delete();
file = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README", new File("."));
assertTrue(file.length() > 0);
file.delete();
}
@Test
public void testRepositoryFileViaInputStream() throws GitLabApiException, IOException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
InputStream in = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README");
Path target = Files.createTempFile(TEST_PROJECT_NAME + "-README", "");
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
assertTrue(target.toFile().length() > 0);
Files.delete(target);
}
} }
...@@ -10,18 +10,14 @@ import org.junit.BeforeClass; ...@@ -10,18 +10,14 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
/** /**
* In order for these tests to run you must set the following systems properties: * In order for these tests to run you must set the following properties in test-gitlab4j.properties
* *
* TEST_HOST_URL * TEST_HOST_URL
* TEST_USERNAME * TEST_USERNAME
* TEST_PASSWORD * TEST_PASSWORD
* TEST_PRIVATE_TOKEN * TEST_PRIVATE_TOKEN
* *
* If any of the above are NULL, all tests in this class will be skipped. If running from mvn simply * If any of the above are NULL, all tests in this class will be skipped.
* use a command line similar to:
*
* mvn test -DTEST_HOST_URL=https://gitlab.com -DTTEST_USERNAME=your_username \
* -DTEST_PASSWORD=your_strong_password -DTEST_PRIVATE_TOKEN=your_private_token
*/ */
public class TestGitLabSession { public class TestGitLabSession {
...@@ -31,10 +27,10 @@ public class TestGitLabSession { ...@@ -31,10 +27,10 @@ public class TestGitLabSession {
private static final String TEST_HOST_URL; private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN; private static final String TEST_PRIVATE_TOKEN;
static { static {
TEST_USERNAME = System.getProperty("TEST_USERNAME"); TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME");
TEST_PASSWORD = System.getProperty("TEST_PASSWORD"); TEST_PASSWORD = TestUtils.getProperty("TEST_PASSWORD");
TEST_HOST_URL = System.getProperty("TEST_HOST_URL"); TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = System.getProperty("TEST_PRIVATE_TOKEN"); TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
} }
private static String problems = ""; private static String problems = "";
......
...@@ -13,17 +13,13 @@ import org.junit.BeforeClass; ...@@ -13,17 +13,13 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
/** /**
* In order for these tests to run you must set the following systems properties: * In order for these tests to run you must set the following properties in test-gitlab4j.properties
* *
* TEST_NAMESPACE * TEST_NAMESPACE
* TEST_HOST_URL * TEST_HOST_URL
* TEST_PRIVATE_TOKEN * TEST_PRIVATE_TOKEN
* *
* If any of the above are NULL, all tests in this class will be skipped. If running from mvn simply * If any of the above are NULL, all tests in this class will be skipped.
* use a command line similar to:
*
* mvn test -DTEST_PRIVATE_TOKEN=your_private_token -DTEST_HOST_URL=https://gitlab.com \
* -DTEST_NAMESPACE=your_namespace
* *
*/ */
public class TestNamespaceApi { public class TestNamespaceApi {
...@@ -34,9 +30,9 @@ public class TestNamespaceApi { ...@@ -34,9 +30,9 @@ public class TestNamespaceApi {
private static final String TEST_HOST_URL; private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN; private static final String TEST_PRIVATE_TOKEN;
static { static {
TEST_NAMESPACE = System.getProperty("TEST_NAMESPACE"); TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
TEST_HOST_URL = System.getProperty("TEST_HOST_URL"); TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = System.getProperty("TEST_PRIVATE_TOKEN"); TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
} }
private static GitLabApi gitLabApi; private static GitLabApi gitLabApi;
......
...@@ -8,6 +8,8 @@ import static org.junit.Assume.assumeTrue; ...@@ -8,6 +8,8 @@ import static org.junit.Assume.assumeTrue;
import java.util.List; import java.util.List;
import org.gitlab4j.api.GitLabApi.ApiVersion; import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
...@@ -16,17 +18,14 @@ import org.junit.Test; ...@@ -16,17 +18,14 @@ import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
/** /**
* In order for these tests to run you must set the following systems properties: * In order for these tests to run you must set the following properties in test-gitlab4j.properties
* *
* TEST_NAMESPACE * TEST_NAMESPACE
* TEST_PROJECT_NAME
* TEST_HOST_URL * TEST_HOST_URL
* TEST_PRIVATE_TOKEN * TEST_PRIVATE_TOKEN
* *
* If any of the above are NULL, all tests in this class will be skipped. If running from mvn simply * If any of the above are NULL, all tests in this class will be skipped.
* use a command line similar to:
*
* mvn test -DTEST_PRIVATE_TOKEN=your_private_token -DTEST_HOST_URL=https://gitlab.com \
* -DTEST_NAMESPACE=your_namespace
* *
* NOTE: &amp;FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that the tests are in the correct order * NOTE: &amp;FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that the tests are in the correct order
*/ */
...@@ -34,14 +33,16 @@ import org.junit.runners.MethodSorters; ...@@ -34,14 +33,16 @@ import org.junit.runners.MethodSorters;
public class TestPager { public class TestPager {
// The following needs to be set to your test repository // The following needs to be set to your test repository
private static final String TEST_NAMESPACE; 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_HOST_URL;
private static final String TEST_PRIVATE_TOKEN; private static final String TEST_PRIVATE_TOKEN;
static { static {
TEST_NAMESPACE = System.getProperty("TEST_NAMESPACE"); TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
TEST_HOST_URL = System.getProperty("TEST_HOST_URL"); TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME");
TEST_PRIVATE_TOKEN = System.getProperty("TEST_PRIVATE_TOKEN"); TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
} }
private static GitLabApi gitLabApi; private static GitLabApi gitLabApi;
...@@ -58,6 +59,10 @@ public class TestPager { ...@@ -58,6 +59,10 @@ public class TestPager {
problems += "TEST_NAMESPACE cannot be empty\n"; problems += "TEST_NAMESPACE cannot be empty\n";
} }
if (TEST_PROJECT_NAME == null || TEST_PROJECT_NAME.trim().length() == 0) {
problems += "TEST_PROJECT_NAME cannot be empty\n";
}
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) { if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) {
problems += "TEST_HOST_URL cannot be empty\n"; problems += "TEST_HOST_URL cannot be empty\n";
} }
...@@ -83,54 +88,116 @@ public class TestPager { ...@@ -83,54 +88,116 @@ public class TestPager {
Pager<Project> pager = gitLabApi.getProjectApi().getProjects(10); Pager<Project> pager = gitLabApi.getProjectApi().getProjects(10);
assertNotNull(pager); assertNotNull(pager);
assertEquals(pager.getItemsPerPage(), 10); assertEquals(10, pager.getItemsPerPage());
assertTrue(0 < pager.getTotalPages()); assertTrue(0 < pager.getTotalPages());
assertTrue(0 < pager.getTotalItems()); assertTrue(0 < pager.getTotalItems());
int itemNumber = 0; int itemNumber = 0;
int pageIndex = 0; int pageIndex = 0;
while (pager.hasNext() && pageIndex < 4) { while (pager.hasNext() && pageIndex < 4) {
List<Project> projects = pager.next(); List<Project> projects = pager.next();
pageIndex++; pageIndex++;
assertEquals(pageIndex, pager.getCurrentPage()); assertEquals(pageIndex, pager.getCurrentPage());
if (pageIndex < pager.getTotalPages()) if (pageIndex < pager.getTotalPages())
assertEquals(10, projects.size()); assertEquals(10, projects.size());
for (Project project : projects) { for (Project project : projects) {
itemNumber++; itemNumber++;
System.out.format("page=%d, item=%d, projectId=%d, projectName=%s%n", pageIndex, itemNumber, project.getId(), project.getName()); System.out.format("page=%d, item=%d, projectId=%d, projectName=%s%n", pageIndex, itemNumber, project.getId(), project.getName());
} }
} }
} }
@Test @Test
public void testMemberProjectPager() throws GitLabApiException { public void testMemberProjectPager() throws GitLabApiException {
Pager<Project> pager = gitLabApi.getProjectApi().getMemberProjects(2); Pager<Project> pager = gitLabApi.getProjectApi().getMemberProjects(2);
assertNotNull(pager); assertNotNull(pager);
assertEquals(pager.getItemsPerPage(), 2); assertEquals(2, pager.getItemsPerPage());
assertTrue(0 < pager.getTotalPages()); assertTrue(0 < pager.getTotalPages());
assertTrue(0 < pager.getTotalItems()); assertTrue(0 < pager.getTotalItems());
int itemNumber = 0; int itemNumber = 0;
int pageIndex = 0; int pageIndex = 0;
while (pager.hasNext() && pageIndex < 10) { while (pager.hasNext() && pageIndex < 10) {
List<Project> projects = pager.next(); List<Project> projects = pager.next();
pageIndex++; pageIndex++;
assertEquals(pageIndex, pager.getCurrentPage()); assertEquals(pageIndex, pager.getCurrentPage());
if (pageIndex < pager.getTotalPages()) if (pageIndex < pager.getTotalPages())
assertEquals(2, projects.size()); assertEquals(2, projects.size());
for (Project project : projects) { for (Project project : projects) {
itemNumber++; itemNumber++;
System.out.format("page=%d, item=%d, projectId=%d, projectName=%s%n", pageIndex, itemNumber, project.getId(), project.getName()); System.out.format("page=%d, item=%d, projectId=%d, projectName=%s%n", pageIndex, itemNumber, project.getId(), project.getName());
} }
} }
} }
@Test
public void testBranchesPager() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
Pager<Branch> pager = gitLabApi.getRepositoryApi().getBranches(project.getId(), 2);
assertNotNull(pager);
assertEquals(2, pager.getItemsPerPage());
assertTrue(0 < pager.getTotalPages());
assertTrue(0 < pager.getTotalItems());
int itemNumber = 0;
int pageIndex = 0;
while (pager.hasNext() && pageIndex < 10) {
List<Branch> branches = pager.next();
pageIndex++;
assertEquals(pageIndex, pager.getCurrentPage());
if (pageIndex < pager.getTotalPages())
assertEquals(2, branches.size());
for (Branch branch : branches) {
itemNumber++;
System.out.format("page=%d, item=%d, branchName=%s, isMerged=%b%n", pageIndex, itemNumber, branch.getName(), branch.getMerged());
}
}
}
@Test
public void testMembersPager() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
Pager<Member> pager = gitLabApi.getProjectApi().getMembers(project.getId(), 2);
assertNotNull(pager);
assertEquals(2, pager.getItemsPerPage());
assertTrue(0 < pager.getTotalPages());
assertTrue(0 < pager.getTotalItems());
int itemNumber = 0;
int pageIndex = 0;
while (pager.hasNext() && pageIndex < 10) {
List<Member> members = pager.next();
pageIndex++;
assertEquals(pageIndex, pager.getCurrentPage());
if (pageIndex < pager.getTotalPages())
assertEquals(2, members.size());
for (Member member : members) {
itemNumber++;
System.out.format("page=%d, item=%d, name=%s, username=%s%n", pageIndex, itemNumber, member.getName(), member.getUsername());
}
}
}
} }
...@@ -18,17 +18,13 @@ import org.junit.Test; ...@@ -18,17 +18,13 @@ import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
/** /**
* In order for these tests to run you must set the following systems properties: * In order for these tests to run you must set the following properties in test-gitlab4j.properties
* *
* TEST_NAMESPACE * TEST_NAMESPACE
* TEST_HOST_URL * TEST_HOST_URL
* TEST_PRIVATE_TOKEN * TEST_PRIVATE_TOKEN
* *
* If any of the above are NULL, all tests in this class will be skipped. If running from mvn simply * If any of the above are NULL, all tests in this class will be skipped.
* use a command line similar to:
*
* mvn test -DTEST_PRIVATE_TOKEN=your_private_token -DTEST_HOST_URL=https://gitlab.com \
* -DTEST_NAMESPACE=your_namespace
* *
* NOTE: &amp;FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that the tests are in the correct order * NOTE: &amp;FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that the tests are in the correct order
*/ */
...@@ -41,9 +37,9 @@ public class TestProjectApi { ...@@ -41,9 +37,9 @@ public class TestProjectApi {
private static final String TEST_HOST_URL; private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN; private static final String TEST_PRIVATE_TOKEN;
static { static {
TEST_NAMESPACE = System.getProperty("TEST_NAMESPACE"); TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
TEST_HOST_URL = System.getProperty("TEST_HOST_URL"); TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = System.getProperty("TEST_PRIVATE_TOKEN"); TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
} }
private static final String TEST_PROJECT_NAME = "test-gitlab4j-create-project"; private static final String TEST_PROJECT_NAME = "test-gitlab4j-create-project";
......
package org.gitlab4j.api; package org.gitlab4j.api;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.util.Properties;
public class TestUtils { public class TestUtils {
...@@ -23,4 +26,24 @@ public class TestUtils { ...@@ -23,4 +26,24 @@ public class TestUtils {
return (out.toString()); return (out.toString());
} }
private static Properties testProperties;
static {
testProperties = new Properties();
try (InputStream input = new FileInputStream("test-gitlab4j.properties")) {
testProperties.load(input);
} catch (IOException ioe) {
}
}
/**
* Get a named property from the test-gitlab4j.properties file.
*
* @param key the key of the property to get
* @return the named property from the test-gitlab4j.properties file
*/
public static final String getProperty(String key) {
return (testProperties.getProperty(key));
}
} }
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment