Add tests for API models

This commit is contained in:
Naoki Takezoe
2018-12-24 22:03:27 +09:00
parent 468cab6982
commit 114de52434
3 changed files with 243 additions and 10 deletions

View File

@@ -9,7 +9,7 @@ import gitbucket.core.util.RepositoryName
case class ApiBranch(name: String, commit: ApiBranchCommit, protection: ApiBranchProtection)(
repositoryName: RepositoryName
) extends FieldSerializable {
def _links =
val _links =
Map(
"self" -> ApiPath(s"/api/v3/repos/${repositoryName.fullName}/branches/${name}"),
"html" -> ApiPath(s"/${repositoryName.fullName}/tree/${name}")

View File

@@ -1,8 +1,11 @@
package gitbucket.core.api
import java.util.{Calendar, TimeZone, Date}
import java.util.{Base64, Calendar, Date, TimeZone}
import gitbucket.core.model.Account
import gitbucket.core.util.JGitUtil.{CommitInfo, DiffInfo}
import gitbucket.core.util.RepositoryName
import org.eclipse.jgit.diff.DiffEntry.ChangeType
object ApiSpecModels {
@@ -20,16 +23,26 @@ object ApiSpecModels {
f.parse(date)
}
val account = Account(
userName = "octocat",
fullName = "octocat",
mailAddress = "octocat@example.com",
password = "1234",
isAdmin = false,
url = None,
registeredDate = date1,
updatedDate = date1,
lastLoginDate = Some(date1),
image = None,
isGroupAccount = false,
isRemoved = false,
description = None
)
val sha1 = "6dcb09b5b57875f334f61aebed695e2e4193db5e"
val repo1Name = RepositoryName("octocat/Hello-World")
val apiUser = ApiUser(
login = "octocat",
email = "octocat@example.com",
`type` = "User",
site_admin = false,
created_at = date1
)
val apiUser = ApiUser(account)
val repository = ApiRepository(
name = repo1Name.name,
@@ -167,4 +180,90 @@ object ApiSpecModels {
Some(ApiBranchProtection.Status(ApiBranchProtection.Everyone, Seq("continuous-integration/travis-ci")))
)
val apiBranch = ApiBranch(
name = "master",
commit = ApiBranchCommit("468cab6982b37db5eb167568210ec188673fb653"),
protection = apiBranchProtection
)(
repositoryName = repo1Name
)
val apiBranchForList = ApiBranchForList("master", ApiBranchCommit("468cab6982b37db5eb167568210ec188673fb653"))
val apiPusher = ApiPusher(account)
val apiEndPoint = ApiEndPoint()
// TODO use factory method defined in companion object?
val apiPlugin = ApiPlugin(
id = "gist",
name = "Gist Plugin",
version = "4.16.0",
description = "Provides Gist feature on GitBucket.",
jarFileName = "gitbucket-gist-plugin-gitbucket_4.30.0-SNAPSHOT-4.17.0.jar"
)
val apiError = ApiError(
message = "A repository with this name already exists on this account",
documentation_url = Some("https://developer.github.com/v3/repos/#create")
)
// TODO use factory method defined in companion object?
val apiGroup = ApiGroup("octocats", Some("Admin group"), date1)
val apiRef = ApiRef(
ref = "refs/heads/featureA",
`object` = ApiObject("aa218f56b14c9653891f9e74264a383fa43fefbd")
)
// TODO use factory method defined in companion object?
val apiContents = ApiContents(
`type` = "file",
name = "README.md",
path = "README.md",
sha = "3d21ec53a331a6f037a91c368710b99387d012c1",
content = Some(Base64.getEncoder.encodeToString("README".getBytes("UTF-8"))),
encoding = Some("base64")
)(repo1Name)
val apiCommits = ApiCommits(
repositoryName = repo1Name,
commitInfo = CommitInfo(
id = "3d21ec53a331a6f037a91c368710b99387d012c1",
shortMessage = "short message",
fullMessage = "full message",
parents = List("1da452aa92d7db1bc093d266c80a69857718c406"),
authorTime = date1,
authorName = "octocat",
authorEmailAddress = "octocat@example.com",
commitTime = date1,
committerName = "octocat",
committerEmailAddress = "octocat@example.com"
),
diffs = Seq(
DiffInfo(
changeType = ChangeType.MODIFY,
oldPath = "README.md",
newPath = "README.md",
oldContent = None,
newContent = None,
oldIsImage = false,
newIsImage = false,
oldObjectId = None,
newObjectId = Some("6dcb09b5b57875f334f61aebed695e2e4193db5e"),
oldMode = "old_mode",
newMode = "new_mode",
tooLarge = false,
patch = Some("""@@ -1 +1,2 @@
|-body1
|\ No newline at end of file
|+body1
|+body2
|\ No newline at end of file""".stripMargin)
)
),
author = account,
committer = account,
commentCount = 1
)
}

View File

@@ -257,6 +257,112 @@ class JsonFormatSpec extends FunSuite {
}
}"""
val apiBranchJson = s"""{
"name": "master",
"commit": {"sha": "468cab6982b37db5eb167568210ec188673fb653"},
"protection": $apiBranchProtectionJson,
"_links": {
"self": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/branches/master",
"html": "http://gitbucket.exmple.com/octocat/Hello-World/tree/master"
}
}"""
val apiBranchForListJson = """{"name": "master", "commit": {"sha": "468cab6982b37db5eb167568210ec188673fb653"}}"""
val apiPluginJson = """{
"id": "gist",
"name": "Gist Plugin",
"version": "4.16.0",
"description": "Provides Gist feature on GitBucket.",
"jarFileName": "gitbucket-gist-plugin-gitbucket_4.30.0-SNAPSHOT-4.17.0.jar"
}"""
val apiPusherJson = """{"name":"octocat","email":"octocat@example.com"}"""
val apiEndPointJson = """{"rate_limit_url":"http://gitbucket.exmple.com/api/v3/rate_limit"}"""
val apiErrorJson = """{
"message": "A repository with this name already exists on this account",
"documentation_url": "https://developer.github.com/v3/repos/#create"
}"""
val apiGroupJson = """{
"login": "octocats",
"description": "Admin group",
"created_at": "2011-04-14T16:00:49Z",
"id": 0,
"url": "http://gitbucket.exmple.com/api/v3/orgs/octocats",
"html_url": "http://gitbucket.exmple.com/octocats",
"avatar_url": "http://gitbucket.exmple.com/octocats/_avatar"
}""".stripMargin
val apiRefJson = """{
"ref": "refs/heads/featureA",
"object": {"sha":"aa218f56b14c9653891f9e74264a383fa43fefbd"}
}""".stripMargin
val apiContentsJson =
"""{
"type": "file",
"name": "README.md",
"path": "README.md",
"sha": "3d21ec53a331a6f037a91c368710b99387d012c1",
"content": "UkVBRE1F",
"encoding": "base64",
"download_url": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/raw/3d21ec53a331a6f037a91c368710b99387d012c1/README.md"
}"""
val apiCommitsJson = s"""{
"url": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/3d21ec53a331a6f037a91c368710b99387d012c1",
"sha": "3d21ec53a331a6f037a91c368710b99387d012c1",
"html_url": "http://gitbucket.exmple.comoctocat/Hello-World/commit/3d21ec53a331a6f037a91c368710b99387d012c1",
"comment_url": "http://gitbucket.exmple.com",
"commit": {
"url": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/3d21ec53a331a6f037a91c368710b99387d012c1",
"author": {
"name": "octocat",
"email": "octocat@example.com",
"date": "2011-04-14T16:00:49Z"
},
"committer": {
"name": "octocat",
"email": "octocat@example.com",
"date": "2011-04-14T16:00:49Z"
},
"message": "short message",
"comment_count": 1,
"tree": {
"url": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/tree/3d21ec53a331a6f037a91c368710b99387d012c1",
"sha": "3d21ec53a331a6f037a91c368710b99387d012c1"
}
},
"author": $apiUserJson,
"committer": $apiUserJson,
"parents": [
{
"url": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/tree/1da452aa92d7db1bc093d266c80a69857718c406",
"sha": "1da452aa92d7db1bc093d266c80a69857718c406"
}
],
"stats": {
"additions": 2,
"deletions": 1,
"total": 3
},
"files": [
{
"filename": "README.md",
"additions": 2,
"deletions": 1,
"changes": 3,
"status": "modified",
"raw_url": "http://gitbucket.exmple.com/octocat/Hello-World/raw/3d21ec53a331a6f037a91c368710b99387d012c1/README.md",
"blob_url": "http://gitbucket.exmple.com/octocat/Hello-World/blob/3d21ec53a331a6f037a91c368710b99387d012c1/README.md",
"patch": "@@ -1 +1,2 @@\\n-body1\\n\\\\ No newline at end of file\\n+body1\\n+body2\\n\\\\ No newline at end of file"
}
]
}"""
def assertJson(resultJson: String, expectJson: String) = {
import java.util.regex.Pattern
val json2 = Pattern.compile("""^\s*//.*$""", Pattern.MULTILINE).matcher(expectJson).replaceAll("")
@@ -311,4 +417,32 @@ class JsonFormatSpec extends FunSuite {
test("apiBranchProtection") {
assertJson(JsonFormat(apiBranchProtection), apiBranchProtectionJson)
}
test("apiBranch") {
assertJson(JsonFormat(apiBranch), apiBranchJson)
assertJson(JsonFormat(apiBranchForList), apiBranchForListJson)
}
test("apiCommits") {
assertJson(JsonFormat(apiCommits), apiCommitsJson)
}
test("apiContents") {
assertJson(JsonFormat(apiContents), apiContentsJson)
}
test("apiEndPoint") {
assertJson(JsonFormat(apiEndPoint), apiEndPointJson)
}
test("apiError") {
assertJson(JsonFormat(apiError), apiErrorJson)
}
test("apiGroup") {
assertJson(JsonFormat(apiGroup), apiGroupJson)
}
test("apiPlugin") {
assertJson(JsonFormat(apiPlugin), apiPluginJson)
}
test("apiPusher") {
assertJson(JsonFormat(apiPusher), apiPusherJson)
}
test("apiRef") {
assertJson(JsonFormat(apiRef), apiRefJson)
}
}