Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
08e77fd2
Commit
08e77fd2
authored
Jun 24, 2017
by
Greg Messner
Browse files
Improved test coverage and added test-gitlab4j.properties support.
parent
2596bedd
Changes
6
Hide whitespace changes
Inline
Side-by-side
src/test/java/org/gitlab4j/api/TestGitLabApi.java
View file @
08e77fd2
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
;
...
...
@@ -24,18 +25,14 @@ import org.junit.Test;
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_PROJECT_NAME
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
*
* If any of the above are NULL, all tests in this class will be skipped. If running from mvn simply
* 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
* 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 testCreate() is executed first.
*/
...
...
@@ -48,10 +45,10 @@ public class TestGitLabApi {
private
static
final
String
TEST_HOST_URL
;
private
static
final
String
TEST_PRIVATE_TOKEN
;
static
{
TEST_NAMESPACE
=
System
.
getProperty
(
"TEST_NAMESPACE"
);
TEST_PROJECT_NAME
=
System
.
getProperty
(
"TEST_PROJECT_NAME"
);
TEST_HOST_URL
=
System
.
getProperty
(
"TEST_HOST_URL"
);
TEST_PRIVATE_TOKEN
=
System
.
getProperty
(
"TEST_PRIVATE_TOKEN"
);
TEST_NAMESPACE
=
TestUtils
.
getProperty
(
"TEST_NAMESPACE"
);
TEST_PROJECT_NAME
=
TestUtils
.
getProperty
(
"TEST_PROJECT_NAME"
);
TEST_HOST_URL
=
TestUtils
.
getProperty
(
"TEST_HOST_URL"
);
TEST_PRIVATE_TOKEN
=
TestUtils
.
getProperty
(
"TEST_PRIVATE_TOKEN"
);
}
private
static
final
String
TEST_BRANCH_NAME
=
"feature/test_branch"
;
...
...
@@ -120,12 +117,39 @@ public class TestGitLabApi {
assertTrue
(
projects
!=
null
);
}
@Test
public
void
testProjectPerPage
()
throws
GitLabApiException
{
List
<
Project
>
projects
=
gitLabApi
.
getProjectApi
().
getProjects
(
1
,
10
);
assertNotNull
(
projects
);
assertEquals
(
10
,
projects
.
size
());
}
@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
testCreateBranch
()
throws
GitLabApiException
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME
);
...
...
@@ -133,6 +157,11 @@ public class TestGitLabApi {
Branch
branch
=
gitLabApi
.
getRepositoryApi
().
createBranch
(
project
.
getId
(),
TEST_BRANCH_NAME
,
"master"
);
assertNotNull
(
branch
);
Branch
fetchedBranch
=
gitLabApi
.
getRepositoryApi
().
getBranch
(
project
.
getId
(),
TEST_BRANCH_NAME
);
assertNotNull
(
fetchedBranch
);
assertEquals
(
branch
.
getName
(),
fetchedBranch
.
getName
());
}
@Test
...
...
@@ -172,4 +201,34 @@ public class TestGitLabApi {
assertTrue
(
file
.
length
()
>
0
);
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
);
}
}
src/test/java/org/gitlab4j/api/TestGitLabSession.java
View file @
08e77fd2
...
...
@@ -10,18 +10,14 @@ import org.junit.BeforeClass;
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_USERNAME
* TEST_PASSWORD
* TEST_PRIVATE_TOKEN
*
* If any of the above are NULL, all tests in this class will be skipped. If running from mvn simply
* 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
* If any of the above are NULL, all tests in this class will be skipped.
*/
public
class
TestGitLabSession
{
...
...
@@ -31,10 +27,10 @@ public class TestGitLabSession {
private
static
final
String
TEST_HOST_URL
;
private
static
final
String
TEST_PRIVATE_TOKEN
;
static
{
TEST_USERNAME
=
System
.
getProperty
(
"TEST_USERNAME"
);
TEST_PASSWORD
=
System
.
getProperty
(
"TEST_PASSWORD"
);
TEST_HOST_URL
=
System
.
getProperty
(
"TEST_HOST_URL"
);
TEST_PRIVATE_TOKEN
=
System
.
getProperty
(
"TEST_PRIVATE_TOKEN"
);
TEST_USERNAME
=
TestUtils
.
getProperty
(
"TEST_USERNAME"
);
TEST_PASSWORD
=
TestUtils
.
getProperty
(
"TEST_PASSWORD"
);
TEST_HOST_URL
=
TestUtils
.
getProperty
(
"TEST_HOST_URL"
);
TEST_PRIVATE_TOKEN
=
TestUtils
.
getProperty
(
"TEST_PRIVATE_TOKEN"
);
}
private
static
String
problems
=
""
;
...
...
src/test/java/org/gitlab4j/api/TestNamespaceApi.java
View file @
08e77fd2
...
...
@@ -13,17 +13,13 @@ import org.junit.BeforeClass;
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_HOST_URL
* TEST_PRIVATE_TOKEN
*
* If any of the above are NULL, all tests in this class will be skipped. If running from mvn simply
* use a command line similar to:
*
* mvn test -DTEST_PRIVATE_TOKEN=your_private_token -DTEST_HOST_URL=https://gitlab.com \
* -DTEST_NAMESPACE=your_namespace
* If any of the above are NULL, all tests in this class will be skipped.
*
*/
public
class
TestNamespaceApi
{
...
...
@@ -34,9 +30,9 @@ public class TestNamespaceApi {
private
static
final
String
TEST_HOST_URL
;
private
static
final
String
TEST_PRIVATE_TOKEN
;
static
{
TEST_NAMESPACE
=
System
.
getProperty
(
"TEST_NAMESPACE"
);
TEST_HOST_URL
=
System
.
getProperty
(
"TEST_HOST_URL"
);
TEST_PRIVATE_TOKEN
=
System
.
getProperty
(
"TEST_PRIVATE_TOKEN"
);
TEST_NAMESPACE
=
TestUtils
.
getProperty
(
"TEST_NAMESPACE"
);
TEST_HOST_URL
=
TestUtils
.
getProperty
(
"TEST_HOST_URL"
);
TEST_PRIVATE_TOKEN
=
TestUtils
.
getProperty
(
"TEST_PRIVATE_TOKEN"
);
}
private
static
GitLabApi
gitLabApi
;
...
...
src/test/java/org/gitlab4j/api/TestPager.java
View file @
08e77fd2
...
...
@@ -8,6 +8,8 @@ import static org.junit.Assume.assumeTrue;
import
java.util.List
;
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.junit.Before
;
import
org.junit.BeforeClass
;
...
...
@@ -16,17 +18,14 @@ import org.junit.Test;
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_PROJECT_NAME
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
*
* If any of the above are NULL, all tests in this class will be skipped. If running from mvn simply
* use a command line similar to:
*
* mvn test -DTEST_PRIVATE_TOKEN=your_private_token -DTEST_HOST_URL=https://gitlab.com \
* -DTEST_NAMESPACE=your_namespace
* 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
*/
...
...
@@ -34,14 +33,16 @@ import org.junit.runners.MethodSorters;
public
class
TestPager
{
// 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
;
static
{
TEST_NAMESPACE
=
System
.
getProperty
(
"TEST_NAMESPACE"
);
TEST_HOST_URL
=
System
.
getProperty
(
"TEST_HOST_URL"
);
TEST_PRIVATE_TOKEN
=
System
.
getProperty
(
"TEST_PRIVATE_TOKEN"
);
TEST_NAMESPACE
=
TestUtils
.
getProperty
(
"TEST_NAMESPACE"
);
TEST_PROJECT_NAME
=
TestUtils
.
getProperty
(
"TEST_PROJECT_NAME"
);
TEST_HOST_URL
=
TestUtils
.
getProperty
(
"TEST_HOST_URL"
);
TEST_PRIVATE_TOKEN
=
TestUtils
.
getProperty
(
"TEST_PRIVATE_TOKEN"
);
}
private
static
GitLabApi
gitLabApi
;
...
...
@@ -58,6 +59,10 @@ public class TestPager {
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
)
{
problems
+=
"TEST_HOST_URL cannot be empty\n"
;
}
...
...
@@ -83,54 +88,116 @@ public class TestPager {
Pager
<
Project
>
pager
=
gitLabApi
.
getProjectApi
().
getProjects
(
10
);
assertNotNull
(
pager
);
assertEquals
(
pager
.
getItemsPerPage
()
,
10
);
assertEquals
(
10
,
pager
.
getItemsPerPage
());
assertTrue
(
0
<
pager
.
getTotalPages
());
assertTrue
(
0
<
pager
.
getTotalItems
());
int
itemNumber
=
0
;
int
pageIndex
=
0
;
while
(
pager
.
hasNext
()
&&
pageIndex
<
4
)
{
List
<
Project
>
projects
=
pager
.
next
();
pageIndex
++;
assertEquals
(
pageIndex
,
pager
.
getCurrentPage
());
if
(
pageIndex
<
pager
.
getTotalPages
())
assertEquals
(
10
,
projects
.
size
());
for
(
Project
project
:
projects
)
{
itemNumber
++;
System
.
out
.
format
(
"page=%d, item=%d, projectId=%d, projectName=%s%n"
,
pageIndex
,
itemNumber
,
project
.
getId
(),
project
.
getName
());
}
}
}
@Test
public
void
testMemberProjectPager
()
throws
GitLabApiException
{
Pager
<
Project
>
pager
=
gitLabApi
.
getProjectApi
().
getMemberProjects
(
2
);
assertNotNull
(
pager
);
assertEquals
(
pager
.
getItemsPerPage
()
,
2
);
assertEquals
(
2
,
pager
.
getItemsPerPage
());
assertTrue
(
0
<
pager
.
getTotalPages
());
assertTrue
(
0
<
pager
.
getTotalItems
());
int
itemNumber
=
0
;
int
pageIndex
=
0
;
while
(
pager
.
hasNext
()
&&
pageIndex
<
10
)
{
List
<
Project
>
projects
=
pager
.
next
();
pageIndex
++;
assertEquals
(
pageIndex
,
pager
.
getCurrentPage
());
if
(
pageIndex
<
pager
.
getTotalPages
())
assertEquals
(
2
,
projects
.
size
());
for
(
Project
project
:
projects
)
{
itemNumber
++;
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
());
}
}
}
}
src/test/java/org/gitlab4j/api/TestProjectApi.java
View file @
08e77fd2
...
...
@@ -18,17 +18,13 @@ import org.junit.Test;
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_HOST_URL
* TEST_PRIVATE_TOKEN
*
* If any of the above are NULL, all tests in this class will be skipped. If running from mvn simply
* use a command line similar to:
*
* mvn test -DTEST_PRIVATE_TOKEN=your_private_token -DTEST_HOST_URL=https://gitlab.com \
* -DTEST_NAMESPACE=your_namespace
* 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
*/
...
...
@@ -41,9 +37,9 @@ public class TestProjectApi {
private
static
final
String
TEST_HOST_URL
;
private
static
final
String
TEST_PRIVATE_TOKEN
;
static
{
TEST_NAMESPACE
=
System
.
getProperty
(
"TEST_NAMESPACE"
);
TEST_HOST_URL
=
System
.
getProperty
(
"TEST_HOST_URL"
);
TEST_PRIVATE_TOKEN
=
System
.
getProperty
(
"TEST_PRIVATE_TOKEN"
);
TEST_NAMESPACE
=
TestUtils
.
getProperty
(
"TEST_NAMESPACE"
);
TEST_HOST_URL
=
TestUtils
.
getProperty
(
"TEST_HOST_URL"
);
TEST_PRIVATE_TOKEN
=
TestUtils
.
getProperty
(
"TEST_PRIVATE_TOKEN"
);
}
private
static
final
String
TEST_PROJECT_NAME
=
"test-gitlab4j-create-project"
;
...
...
src/test/java/org/gitlab4j/api/TestUtils.java
View file @
08e77fd2
package
org.gitlab4j.api
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.Reader
;
import
java.util.Properties
;
public
class
TestUtils
{
...
...
@@ -23,4 +26,24 @@ public class TestUtils {
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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment