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
09d6df53
Commit
09d6df53
authored
Jul 25, 2018
by
Greg Messner
Browse files
Initial commit (#224).
parent
d23fba77
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/test/java/org/gitlab4j/api/TestRepositoryFileApi.java
0 → 100644
View file @
09d6df53
package
org.gitlab4j.api
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assume
.
assumeTrue
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.StandardCopyOption
;
import
java.util.Optional
;
import
org.gitlab4j.api.models.Branch
;
import
org.gitlab4j.api.models.Project
;
import
org.gitlab4j.api.models.RepositoryFile
;
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
*
* 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.
*/
@FixMethodOrder
(
MethodSorters
.
NAME_ASCENDING
)
public
class
TestRepositoryFileApi
{
// The following needs to be set to your test repository
private
static
final
String
TEST_PROJECT_NAME
;
private
static
final
String
TEST_NAMESPACE
;
private
static
final
String
TEST_HOST_URL
;
private
static
final
String
TEST_PRIVATE_TOKEN
;
static
{
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_for_files"
;
private
static
final
String
TEST_FILEPATH
=
"test-file.txt"
;
private
static
GitLabApi
gitLabApi
;
public
TestRepositoryFileApi
()
{
super
();
}
@BeforeClass
public
static
void
setup
()
{
String
problems
=
""
;
if
(
TEST_NAMESPACE
==
null
||
TEST_NAMESPACE
.
trim
().
isEmpty
())
{
problems
+=
"TEST_NAMESPACE cannot be empty\n"
;
}
if
(
TEST_PROJECT_NAME
==
null
||
TEST_PROJECT_NAME
.
trim
().
isEmpty
())
{
problems
+=
"TEST_PROJECT_NAME cannot be empty\n"
;
}
if
(
TEST_HOST_URL
==
null
||
TEST_HOST_URL
.
trim
().
isEmpty
())
{
problems
+=
"TEST_HOST_URL cannot be empty\n"
;
}
if
(
TEST_PRIVATE_TOKEN
==
null
||
TEST_PRIVATE_TOKEN
.
trim
().
isEmpty
())
{
problems
+=
"TEST_PRIVATE_TOKEN cannot be empty\n"
;
}
if
(
problems
.
isEmpty
())
{
gitLabApi
=
new
GitLabApi
(
TEST_HOST_URL
,
TEST_PRIVATE_TOKEN
);
teardown
();
}
else
{
System
.
err
.
print
(
problems
);
}
}
@AfterClass
public
static
void
teardown
()
{
if
(
gitLabApi
!=
null
)
{
try
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME
);
try
{
gitLabApi
.
getRepositoryFileApi
().
deleteFile
(
TEST_FILEPATH
,
project
.
getId
(),
TEST_BRANCH_NAME
,
"Cleanup test files."
);
}
catch
(
GitLabApiException
ignore
)
{
}
try
{
gitLabApi
.
getRepositoryApi
().
deleteBranch
(
project
.
getId
(),
TEST_BRANCH_NAME
);
}
catch
(
GitLabApiException
ignore
)
{
}
}
catch
(
GitLabApiException
ignore
)
{
}
}
}
@Before
public
void
beforeMethod
()
{
assumeTrue
(
gitLabApi
!=
null
);
}
@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
);
}
@Test
public
void
testRepositoryFileGetFile
()
throws
GitLabApiException
,
IOException
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME
);
assertNotNull
(
project
);
RepositoryFile
fileInfo
=
gitLabApi
.
getRepositoryFileApi
().
getFileInfo
(
project
.
getId
(),
"README"
,
"master"
);
assertNotNull
(
fileInfo
);
}
@Test
public
void
testRepositoryFileGetOptionalFile
()
throws
GitLabApiException
,
IOException
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME
);
assertNotNull
(
project
);
Optional
<
RepositoryFile
>
fileInfo
=
gitLabApi
.
getRepositoryFileApi
().
getOptionalFileInfo
(
project
.
getId
(),
"README"
,
"master"
);
assertNotNull
(
fileInfo
.
get
());
fileInfo
=
gitLabApi
.
getRepositoryFileApi
().
getOptionalFileInfo
(
project
.
getId
(),
"I-DONT-EXIST"
,
"master"
);
assertFalse
(
fileInfo
.
isPresent
());
GitLabApiException
e
=
GitLabApi
.
getOptionalException
(
fileInfo
);
assertNotNull
(
e
);
assertEquals
(
404
,
e
.
getHttpStatus
());
}
@Test
public
void
testCreateFileAndDeleteFile
()
throws
GitLabApiException
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME
);
assertNotNull
(
project
);
Branch
branch
=
gitLabApi
.
getRepositoryApi
().
createBranch
(
project
.
getId
(),
TEST_BRANCH_NAME
,
"master"
);
assertNotNull
(
branch
);
RepositoryFile
file
=
new
RepositoryFile
();
file
.
setFilePath
(
TEST_FILEPATH
);
file
.
setContent
(
"This is a test file."
);
RepositoryFile
createdFile
=
gitLabApi
.
getRepositoryFileApi
().
createFile
(
file
,
project
.
getId
(),
TEST_BRANCH_NAME
,
"Testing createFile()."
);
assertNotNull
(
createdFile
);
gitLabApi
.
getRepositoryFileApi
().
deleteFile
(
TEST_FILEPATH
,
project
.
getId
(),
TEST_BRANCH_NAME
,
"Testing deleteFile()."
);
gitLabApi
.
getRepositoryApi
().
deleteBranch
(
project
.
getId
(),
TEST_BRANCH_NAME
);
}
@Test
public
void
testCreateFileWithEmptyContent
()
throws
GitLabApiException
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME
);
assertNotNull
(
project
);
Branch
branch
=
gitLabApi
.
getRepositoryApi
().
createBranch
(
project
.
getId
(),
TEST_BRANCH_NAME
,
"master"
);
assertNotNull
(
branch
);
RepositoryFile
file
=
new
RepositoryFile
();
file
.
setFilePath
(
TEST_FILEPATH
);
file
.
setContent
(
""
);
RepositoryFile
createdFile
=
gitLabApi
.
getRepositoryFileApi
().
createFile
(
file
,
project
.
getId
(),
TEST_BRANCH_NAME
,
"Testing createFile()."
);
assertNotNull
(
createdFile
);
gitLabApi
.
getRepositoryFileApi
().
deleteFile
(
TEST_FILEPATH
,
project
.
getId
(),
TEST_BRANCH_NAME
,
"Testing deleteFile()."
);
gitLabApi
.
getRepositoryApi
().
deleteBranch
(
project
.
getId
(),
TEST_BRANCH_NAME
);
}
}
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