Add unit test

This commit is contained in:
shimamoto
2019-01-08 21:37:10 +09:00
parent aef8e32da3
commit 8aa6e83673
3 changed files with 281 additions and 364 deletions

View File

@@ -3,6 +3,7 @@ package gitbucket.core.api
import java.util.{Base64, Calendar, Date, TimeZone} import java.util.{Base64, Calendar, Date, TimeZone}
import gitbucket.core.model._ import gitbucket.core.model._
import gitbucket.core.service.ProtectedBranchService.ProtectedBranchInfo
import gitbucket.core.service.RepositoryService.RepositoryInfo import gitbucket.core.service.RepositoryService.RepositoryInfo
import gitbucket.core.util.JGitUtil.{CommitInfo, DiffInfo, TagInfo} import gitbucket.core.util.JGitUtil.{CommitInfo, DiffInfo, TagInfo}
import gitbucket.core.util.RepositoryName import gitbucket.core.util.RepositoryName
@@ -24,6 +25,8 @@ object ApiSpecModels {
f.parse(date) f.parse(date)
} }
// Models
val account = Account( val account = Account(
userName = "octocat", userName = "octocat",
fullName = "octocat", fullName = "octocat",
@@ -155,6 +158,8 @@ object ApiSpecModels {
originalNewLine = None originalNewLine = None
) )
// APIs
val apiUser = ApiUser(account) val apiUser = ApiUser(account)
val apiRepository = ApiRepository( val apiRepository = ApiRepository(
@@ -219,6 +224,207 @@ object ApiSpecModels {
issueId = commitComment.issueId.get issueId = commitComment.issueId.get
) )
val commitInfo = (id: String) =>
CommitInfo(
id = id,
shortMessage = "short message",
fullMessage = "full message",
parents = List("1da452aa92d7db1bc093d266c80a69857718c406"),
authorTime = date1,
authorName = account.userName,
authorEmailAddress = account.mailAddress,
commitTime = date1,
committerName = account.userName,
committerEmailAddress = account.mailAddress
)
val apiCommitListItem = ApiCommitListItem(
commit = commitInfo(sha1),
repositoryName = repo1Name
)
val apiBranchProtection = ApiBranchProtection(
info = ProtectedBranchInfo(
owner = repo1Name.owner,
repository = repo1Name.name,
enabled = true,
contexts = Seq("continuous-integration/travis-ci"),
includeAdministrators = true
)
)
val apiBranch = ApiBranch(
name = "master",
commit = ApiBranchCommit(sha1),
protection = apiBranchProtection
)(
repositoryName = repo1Name
)
val apiBranchForList = ApiBranchForList(
name = "master",
commit = ApiBranchCommit(sha1)
)
// JSON String for APIs
val jsonUser = """{
|"login":"octocat",
|"email":"octocat@example.com",
|"type":"User",
|"site_admin":false,
|"created_at":"2011-04-14T16:00:49Z",
|"id":0,
|"url":"http://gitbucket.exmple.com/api/v3/users/octocat",
|"html_url":"http://gitbucket.exmple.com/octocat",
|"avatar_url":"http://gitbucket.exmple.com/octocat/_avatar"
|}""".stripMargin
val jsonRepository = s"""{
|"name":"Hello-World",
|"full_name":"octocat/Hello-World",
|"description":"This your first repo!",
|"watchers":0,
|"forks":1,
|"private":false,
|"default_branch":"master",
|"owner":$jsonUser,
|"id":0,
|"forks_count":1,
|"watchers_count":0,
|"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World",
|"http_url":"http://gitbucket.exmple.com/git/octocat/Hello-World.git",
|"clone_url":"http://gitbucket.exmple.com/git/octocat/Hello-World.git",
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World"
|}""".stripMargin
val jsonLabel =
"""{"name":"bug","color":"f29513","url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/labels/bug"}"""
val jsonIssue = s"""{
|"number":1347,
|"title":"Found a bug",
|"user":$jsonUser,
|"labels":[$jsonLabel],
|"state":"open",
|"created_at":"2011-04-14T16:00:49Z",
|"updated_at":"2011-04-14T16:00:49Z",
|"body":"I'm having a problem with this.",
|"id":0,
|"comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/issues/1347/comments",
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/issues/1347"
|}""".stripMargin
// TODO comments_url is correct?
val jsonIssuePR = s"""{
|"number":1347,
|"title":"new-feature",
|"user":$jsonUser,
|"labels":[$jsonLabel],
|"state":"closed",
|"created_at":"2011-04-14T16:00:49Z",
|"updated_at":"2011-04-14T16:00:49Z",
|"body":"Please pull these awesome changes",
|"id":0,
|"comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/issues/1347/comments",
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347",
|"pull_request":{
|"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347",
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347"}
|}""".stripMargin
// TODO comments_url is correct?
val jsonPullRequest = s"""{
|"number":1347,
|"state":"closed",
|"updated_at":"2011-04-14T16:00:49Z",
|"created_at":"2011-04-14T16:00:49Z",
|"head":{"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e","ref":"new-topic","repo":$jsonRepository,"label":"new-topic","user":$jsonUser},
|"base":{"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e","ref":"master","repo":$jsonRepository,"label":"master","user":$jsonUser},
|"merged":true,
|"merged_at":"2011-04-14T16:00:49Z",
|"merged_by":$jsonUser,
|"title":"new-feature",
|"body":"Please pull these awesome changes",
|"user":$jsonUser,
|"labels":[$jsonLabel],
|"assignee":$jsonUser,
|"id":0,
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347",
|"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347",
|"commits_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347/commits",
|"review_comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347/comments",
|"review_comment_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/comments/{number}",
|"comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/issues/1347/comments",
|"statuses_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e"
|}""".stripMargin
val jsonPullRequestReviewComment = s"""{
|"id":29724692,
|"path":"README.md",
|"commit_id":"0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|"user":$jsonUser,
|"body":"Maybe you should use more emoji on this line.",
|"created_at":"2015-05-05T23:40:27Z",
|"updated_at":"2015-05-05T23:40:27Z",
|"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/comments/29724692",
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347#discussion_r29724692",
|"pull_request_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347",
|"_links":{
|"self":{"href":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/comments/29724692"},
|"html":{"href":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347#discussion_r29724692"},
|"pull_request":{"href":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347"}}
|}""".stripMargin
val jsonComment = s"""{
|"id":1,
|"user":$jsonUser,
|"body":"Me too",
|"created_at":"2011-04-14T16:00:49Z",
|"updated_at":"2011-04-14T16:00:49Z",
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/issues/1347#comment-1"
|}""".stripMargin
val jsonCommentPR = s"""{
|"id":1,
|"user":$jsonUser,
|"body":"Me too",
|"created_at":"2011-04-14T16:00:49Z",
|"updated_at":"2011-04-14T16:00:49Z",
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347#comment-1"
|}""".stripMargin
val jsonCommitListItem = s"""{
|"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e",
|"commit":{
|"message":"full message",
|"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"},
|"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e"
|},
|"parents":[{
|"sha":"1da452aa92d7db1bc093d266c80a69857718c406",
|"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/1da452aa92d7db1bc093d266c80a69857718c406"}],
|"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e"
|}""".stripMargin
val jsonBranchProtection =
"""{
|"enabled":true,
|"required_status_checks":{"enforcement_level":"everyone","contexts":["continuous-integration/travis-ci"]}
|}""".stripMargin
val jsonBranch = s"""{
|"name":"master",
|"commit":{"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e"},
|"protection":$jsonBranchProtection,
|"_links":{
|"self":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/branches/master",
|"html":"http://gitbucket.exmple.com/octocat/Hello-World/tree/master"}
|}""".stripMargin
val jsonBranchForList = """{"name":"master","commit":{"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e"}}"""
// TODO ------------ // TODO ------------
val apiCommitStatus = ApiCommitStatus( val apiCommitStatus = ApiCommitStatus(
@@ -245,18 +451,6 @@ object ApiSpecModels {
val apiPersonIdent = ApiPersonIdent("Monalisa Octocat", "support@example.com", date1) val apiPersonIdent = ApiPersonIdent("Monalisa Octocat", "support@example.com", date1)
val apiCommitListItem = ApiCommitListItem(
sha = sha1,
commit = ApiCommitListItem.Commit(
message = "Fix all the bugs",
author = apiPersonIdent,
committer = apiPersonIdent
)(sha1, repo1Name),
author = Some(apiUser),
committer = Some(apiUser),
parents = Seq(ApiCommitListItem.Parent("6dcb09b5b57875f334f61aebed695e2e4193db5e")(repo1Name))
)(repo1Name)
val apiCombinedCommitStatus = ApiCombinedCommitStatus( val apiCombinedCommitStatus = ApiCombinedCommitStatus(
state = "success", state = "success",
sha = sha1, sha = sha1,
@@ -265,26 +459,10 @@ object ApiSpecModels {
repository = apiRepository repository = apiRepository
) )
val apiBranchProtection = ApiBranchProtection(
true,
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 apiPusher = ApiPusher(account)
val apiEndPoint = ApiEndPoint() val apiEndPoint = ApiEndPoint()
// TODO use factory method defined in companion object?
val apiPlugin = ApiPlugin( val apiPlugin = ApiPlugin(
id = "gist", id = "gist",
name = "Gist Plugin", name = "Gist Plugin",
@@ -298,7 +476,6 @@ object ApiSpecModels {
documentation_url = Some("https://developer.github.com/v3/repos/#create") 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 apiGroup = ApiGroup("octocats", Some("Admin group"), date1)
val apiRef = ApiRef( val apiRef = ApiRef(
@@ -306,7 +483,6 @@ object ApiSpecModels {
`object` = ApiObject("aa218f56b14c9653891f9e74264a383fa43fefbd") `object` = ApiObject("aa218f56b14c9653891f9e74264a383fa43fefbd")
) )
// TODO use factory method defined in companion object?
val apiContents = ApiContents( val apiContents = ApiContents(
`type` = "file", `type` = "file",
name = "README.md", name = "README.md",

View File

@@ -7,35 +7,8 @@ import org.scalatest.FunSuite
class JsonFormatSpec extends FunSuite { class JsonFormatSpec extends FunSuite {
import ApiSpecModels._ import ApiSpecModels._
val apiUserJson = """{ val apiUserJson = """"""
"login":"octocat", val repositoryJson = s""""""
"email":"octocat@example.com",
"type":"User",
"site_admin":false,
"id": 0,
"created_at":"2011-04-14T16:00:49Z",
"url":"http://gitbucket.exmple.com/api/v3/users/octocat",
"html_url":"http://gitbucket.exmple.com/octocat",
"avatar_url":"http://gitbucket.exmple.com/octocat/_avatar"
}"""
val repositoryJson = s"""{
"name" : "Hello-World",
"full_name" : "octocat/Hello-World",
"description" : "This your first repo!",
"id": 0,
"watchers" : 0,
"forks" : 0,
"private" : false,
"default_branch" : "master",
"owner" : $apiUserJson,
"forks_count" : 0,
"watchers_count" : 0,
"url" : "${context.baseUrl}/api/v3/repos/octocat/Hello-World",
"http_url" : "${context.baseUrl}/git/octocat/Hello-World.git",
"clone_url" : "${context.baseUrl}/git/octocat/Hello-World.git",
"html_url" : "${context.baseUrl}/octocat/Hello-World"
}"""
val apiCommitStatusJson = s"""{ val apiCommitStatusJson = s"""{
"created_at":"2011-04-14T16:00:49Z", "created_at":"2011-04-14T16:00:49Z",
@@ -78,49 +51,6 @@ class JsonFormatSpec extends FunSuite {
] ]
}""" }"""
val apiCommentJson = s"""{
"id": 1,
"body": "Me too",
"user": $apiUserJson,
"html_url" : "${context.baseUrl}/octocat/Hello-World/issues/100#comment-1",
"created_at": "2011-04-14T16:00:49Z",
"updated_at": "2011-04-14T16:00:49Z"
}"""
val apiCommentPRJson = s"""{
"id": 1,
"body": "Me too",
"user": $apiUserJson,
"html_url" : "${context.baseUrl}/octocat/Hello-World/pull/100#comment-1",
"created_at": "2011-04-14T16:00:49Z",
"updated_at": "2011-04-14T16:00:49Z"
}"""
val apiPersonIdentJson = """ {
"name": "Monalisa Octocat",
"email": "support@example.com",
"date": "2011-04-14T16:00:49Z"
}"""
val apiCommitListItemJson = s"""{
"url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
"commit": {
"url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
"author": $apiPersonIdentJson,
"committer": $apiPersonIdentJson,
"message": "Fix all the bugs"
},
"author": $apiUserJson,
"committer": $apiUserJson,
"parents": [
{
"url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e"
}
]
}"""
val apiCombinedCommitStatusJson = s"""{ val apiCombinedCommitStatusJson = s"""{
"state": "success", "state": "success",
"sha": "$sha1", "sha": "$sha1",
@@ -130,144 +60,7 @@ class JsonFormatSpec extends FunSuite {
"url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/commits/$sha1/status" "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/commits/$sha1/status"
}""" }"""
val apiLabelJson = s"""{ val apiLabelJson = s""""""
"name": "bug",
"color": "f29513",
"url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/labels/bug"
}"""
val apiIssueJson = s"""{
"number": 1347,
"state": "open",
"title": "Found a bug",
"body": "I'm having a problem with this.",
"user": $apiUserJson,
"id": 0,
"labels": [$apiLabelJson],
"comments_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/issues/1347/comments",
"html_url": "${context.baseUrl}/octocat/Hello-World/issues/1347",
"created_at": "2011-04-14T16:00:49Z",
"updated_at": "2011-04-14T16:00:49Z"
}"""
val apiIssuePRJson = s"""{
"number": 1347,
"state": "open",
"title": "Found a bug",
"body": "I'm having a problem with this.",
"user": $apiUserJson,
"id": 0,
"labels": [$apiLabelJson],
"comments_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/issues/1347/comments",
"html_url": "${context.baseUrl}/octocat/Hello-World/pull/1347",
"pull_request": {
"url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/1347",
"html_url": "${context.baseUrl}/octocat/Hello-World/pull/1347"
// "diff_url": "${context.baseUrl}/octocat/Hello-World/pull/1347.diff",
// "patch_url": "${context.baseUrl}/octocat/Hello-World/pull/1347.patch"
},
"created_at": "2011-04-14T16:00:49Z",
"updated_at": "2011-04-14T16:00:49Z"
}"""
val apiPullRequestJson = s"""{
"number": 1347,
"state" : "open",
"id": 0,
"updated_at": "2011-04-14T16:00:49Z",
"created_at": "2011-04-14T16:00:49Z",
// "closed_at": "2011-04-14T16:00:49Z",
"head": {
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
"ref": "new-topic",
"repo": $repositoryJson,
"label": "new-topic",
"user": $apiUserJson
},
"base": {
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
"ref": "master",
"repo": $repositoryJson,
"label": "master",
"user": $apiUserJson
},
// "merge_commit_sha": "e5bd3914e2e596debea16f433f57875b5b90bcd6",
// "mergeable": true,
"merged": false,
"merged_at": "2011-04-14T16:00:49Z",
"merged_by": $apiUserJson,
"title": "new-feature",
"body": "Please pull these awesome changes",
"user": $apiUserJson,
"assignee": $apiUserJson,
"labels": [$apiLabelJson],
"html_url": "${context.baseUrl}/octocat/Hello-World/pull/1347",
"url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/1347",
"commits_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/1347/commits",
"review_comments_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/1347/comments",
"review_comment_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/comments/{number}",
"comments_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/issues/1347/comments",
"statuses_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e"
// "diff_url": "${context.baseUrl}/octocat/Hello-World/pull/1347.diff",
// "patch_url": "${context.baseUrl}/octocat/Hello-World/pull/1347.patch",
// "issue_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/issues/1347",
// "state": "open",
// "comments": 10,
// "commits": 3,
// "additions": 100,
// "deletions": 3,
// "changed_files": 5
}"""
val apiPullRequestReviewCommentJson = s"""{
"url": "http://gitbucket.exmple.com/api/v3/repos/baxterthehacker/public-repo/pulls/comments/29724692",
"id": 29724692,
// "diff_hunk": "@@ -1 +1 @@\\n-# public-repo",
"path": "README.md",
// "position": 1,
// "original_position": 1,
"commit_id": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
// "original_commit_id": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
"user": $apiUserJson,
"body": "Maybe you should use more emoji on this line.",
"created_at": "2015-05-05T23:40:27Z",
"updated_at": "2015-05-05T23:40:27Z",
"html_url": "http://gitbucket.exmple.com/baxterthehacker/public-repo/pull/1#discussion_r29724692",
"pull_request_url": "http://gitbucket.exmple.com/api/v3/repos/baxterthehacker/public-repo/pulls/1",
"_links": {
"self": {
"href": "http://gitbucket.exmple.com/api/v3/repos/baxterthehacker/public-repo/pulls/comments/29724692"
},
"html": {
"href": "http://gitbucket.exmple.com/baxterthehacker/public-repo/pull/1#discussion_r29724692"
},
"pull_request": {
"href": "http://gitbucket.exmple.com/api/v3/repos/baxterthehacker/public-repo/pulls/1"
}
}
}"""
val apiBranchProtectionJson = """{
"enabled": true,
"required_status_checks": {
"enforcement_level": "everyone",
"contexts": [
"continuous-integration/travis-ci"
]
}
}"""
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 = """{ val apiPluginJson = """{
"id": "gist", "id": "gist",
@@ -363,37 +156,28 @@ class JsonFormatSpec extends FunSuite {
] ]
}""" }"""
@deprecated
def assertJson(resultJson: String, expectJson: String) = { def assertJson(resultJson: String, expectJson: String) = {
import java.util.regex.Pattern fail("TODO")
val json2 = Pattern.compile("""^\s*//.*$""", Pattern.MULTILINE).matcher(expectJson).replaceAll("")
val js2 = try {
parse(json2)
} catch {
case e: com.fasterxml.jackson.core.JsonParseException => {
val p = java.lang.Math.max(e.getLocation.getCharOffset() - 10, 0).toInt
val message = json2.substring(p, java.lang.Math.min(p + 100, json2.length))
throw new com.fasterxml.jackson.core.JsonParseException(e.getProcessor, message + e.getMessage)
}
}
val js1 = parse(resultJson)
assert(js1 === js2)
} }
private def expected(json: String) = json.replaceAll("\n", "")
test("apiUser") { test("apiUser") {
assertJson(JsonFormat(apiUser), apiUserJson) assert(JsonFormat(apiUser) == expected(jsonUser))
} }
test("repository") { test("apiRepository") {
assertJson(JsonFormat(repository), repositoryJson) assert(JsonFormat(apiRepository) == expected(jsonRepository))
} }
test("apiPushCommit") { test("apiPushCommit") {
assertJson(JsonFormat(apiCommit), apiPushCommitJson) assertJson(JsonFormat(apiCommit), apiPushCommitJson)
} }
test("apiComment") { test("apiComment") {
assertJson(JsonFormat(apiComment), apiCommentJson) assert(JsonFormat(apiComment) == expected(jsonComment))
assertJson(JsonFormat(apiCommentPR), apiCommentPRJson) assert(JsonFormat(apiCommentPR) == expected(jsonCommentPR))
} }
test("apiCommitListItem") { test("apiCommitListItem") {
assertJson(JsonFormat(apiCommitListItem), apiCommitListItemJson) assert(JsonFormat(apiCommitListItem) == expected(jsonCommitListItem))
} }
test("apiCommitStatus") { test("apiCommitStatus") {
assertJson(JsonFormat(apiCommitStatus), apiCommitStatusJson) assertJson(JsonFormat(apiCommitStatus), apiCommitStatusJson)
@@ -402,24 +186,24 @@ class JsonFormatSpec extends FunSuite {
assertJson(JsonFormat(apiCombinedCommitStatus), apiCombinedCommitStatusJson) assertJson(JsonFormat(apiCombinedCommitStatus), apiCombinedCommitStatusJson)
} }
test("apiLabel") { test("apiLabel") {
assertJson(JsonFormat(apiLabel), apiLabelJson) assert(JsonFormat(apiLabel) == expected(jsonLabel))
} }
test("apiIssue") { test("apiIssue") {
assertJson(JsonFormat(apiIssue), apiIssueJson) assert(JsonFormat(apiIssue) == expected(jsonIssue))
assertJson(JsonFormat(apiIssuePR), apiIssuePRJson) assert(JsonFormat(apiIssuePR) == expected(jsonIssuePR))
} }
test("apiPullRequest") { test("apiPullRequest") {
assertJson(JsonFormat(apiPullRequest), apiPullRequestJson) assert(JsonFormat(apiPullRequest) == expected(jsonPullRequest))
} }
test("apiPullRequestReviewComment") { test("apiPullRequestReviewComment") {
assertJson(JsonFormat(apiPullRequestReviewComment), apiPullRequestReviewCommentJson) assert(JsonFormat(apiPullRequestReviewComment) == expected(jsonPullRequestReviewComment))
} }
test("apiBranchProtection") { test("apiBranchProtection") {
assertJson(JsonFormat(apiBranchProtection), apiBranchProtectionJson) assert(JsonFormat(apiBranchProtection) == expected(jsonBranchProtection))
} }
test("apiBranch") { test("apiBranch") {
assertJson(JsonFormat(apiBranch), apiBranchJson) assert(JsonFormat(apiBranch) == expected(jsonBranch))
assertJson(JsonFormat(apiBranchForList), apiBranchForListJson) assert(JsonFormat(apiBranchForList) == expected(jsonBranchForList))
} }
test("apiCommits") { test("apiCommits") {
assertJson(JsonFormat(apiCommits), apiCommitsJson) assertJson(JsonFormat(apiCommits), apiCommitsJson)

View File

@@ -2,83 +2,11 @@ package gitbucket.core.service
import gitbucket.core.api.JsonFormat import gitbucket.core.api.JsonFormat
import gitbucket.core.service.WebHookService._ import gitbucket.core.service.WebHookService._
import org.json4s.jackson.JsonMethods.parse
import org.json4s._
import org.scalatest.{Assertion, FunSuite} import org.scalatest.{Assertion, FunSuite}
class WebHookJsonFormatSpec extends FunSuite { class WebHookJsonFormatSpec extends FunSuite {
import gitbucket.core.api.ApiSpecModels._ import gitbucket.core.api.ApiSpecModels._
// TODO move to ApiSpecModels?
val jsonUser = """{
|"login":"octocat",
|"email":"octocat@example.com",
|"type":"User",
|"site_admin":false,
|"created_at":"2011-04-14T16:00:49Z",
|"id":0,
|"url":"http://gitbucket.exmple.com/api/v3/users/octocat",
|"html_url":"http://gitbucket.exmple.com/octocat",
|"avatar_url":"http://gitbucket.exmple.com/octocat/_avatar"
|}""".stripMargin
val jsonRepository = s"""{
|"name":"Hello-World",
|"full_name":"octocat/Hello-World",
|"description":"This your first repo!",
|"watchers":0,
|"forks":1,
|"private":false,
|"default_branch":"master",
|"owner":$jsonUser,
|"id":0,
|"forks_count":1,
|"watchers_count":0,
|"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World",
|"http_url":"http://gitbucket.exmple.com/git/octocat/Hello-World.git",
|"clone_url":"http://gitbucket.exmple.com/git/octocat/Hello-World.git",
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World"
|}""".stripMargin
val jsonIssue = s"""{
|"number":1347,
|"title":"Found a bug",
|"user":$jsonUser,
|"labels":[{"name":"bug","color":"f29513","url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/labels/bug"}],
|"state":"open",
|"created_at":"2011-04-14T16:00:49Z",
|"updated_at":"2011-04-14T16:00:49Z",
|"body":"I'm having a problem with this.",
|"id":0,
|"comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/issues/1347/comments",
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/issues/1347"
|}""".stripMargin
val jsonPullRequest = s"""{
|"number":1347,
|"state":"closed",
|"updated_at":"2011-04-14T16:00:49Z",
|"created_at":"2011-04-14T16:00:49Z",
|"head":{"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e","ref":"new-topic","repo":$jsonRepository,"label":"new-topic","user":$jsonUser},
|"base":{"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e","ref":"master","repo":$jsonRepository,"label":"master","user":$jsonUser},
|"merged":true,
|"merged_at":"2011-04-14T16:00:49Z",
|"merged_by":$jsonUser,
|"title":"new-feature",
|"body":"Please pull these awesome changes",
|"user":$jsonUser,
|"labels":[{"name":"bug","color":"f29513","url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/labels/bug"}],
|"assignee":$jsonUser,
|"id":0,
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347",
|"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347",
|"commits_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347/commits",
|"review_comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347/comments",
|"review_comment_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/comments/{number}",
|"comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/issues/1347/comments",
|"statuses_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e"
|}""".stripMargin
private def assert(payload: WebHookPayload, expected: String): Assertion = { private def assert(payload: WebHookPayload, expected: String): Assertion = {
val json = JsonFormat(payload) val json = JsonFormat(payload)
assert(json == expected.replaceAll("\n", "")) assert(json == expected.replaceAll("\n", ""))
@@ -105,7 +33,58 @@ class WebHookJsonFormatSpec extends FunSuite {
} }
test("WebHookPushPayload") { test("WebHookPushPayload") {
fail("TODO") import gitbucket.core.util.GitSpecUtil._
import org.eclipse.jgit.lib.{Constants, ObjectId}
withTestRepository { git =>
createFile(git, Constants.HEAD, "README.md", "body1", message = "initial")
createFile(git, Constants.HEAD, "README.md", "body1\nbody2", message = "modified")
val branchId = git.getRepository.resolve("master")
val payload = WebHookPushPayload(
git = git,
sender = account,
refName = "refs/heads/master",
repositoryInfo = repositoryInfo,
commits = List(commitInfo(branchId.name)),
repositoryOwner = account,
newId = ObjectId.fromString(sha1),
oldId = ObjectId.fromString(sha1)
)
// TODO url is correct?
val expected = s"""{
|"pusher":{"name":"octocat","email":"octocat@example.com"},
|"sender":$jsonUser,
|"ref":"refs/heads/master",
|"before":"6dcb09b5b57875f334f61aebed695e2e4193db5e",
|"after":"6dcb09b5b57875f334f61aebed695e2e4193db5e",
|"commits":[{
|"id":"${branchId.name}",
|"message":"full message",
|"timestamp":"2011-04-14T16:00:49Z",
|"added":[],
|"removed":[],
|"modified":["README.md"],
|"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"},
|"url":"http://gitbucket.exmple.com/octocat/Hello-World/commit/${branchId.name}"
|}],
|"repository":$jsonRepository,
|"compare":"http://gitbucket.exmple.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e",
|"head_commit":{
|"id":"${branchId.name}",
|"message":"full message",
|"timestamp":"2011-04-14T16:00:49Z",
|"added":[],
|"removed":[],
|"modified":["README.md"],
|"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"},
|"url":"http://gitbucket.exmple.com/octocat/Hello-World/commit/${branchId.name}"}
|}""".stripMargin
assert(payload, expected)
}
} }
test("WebHookIssuesPayload") { test("WebHookIssuesPayload") {
@@ -166,13 +145,7 @@ class WebHookJsonFormatSpec extends FunSuite {
|"action":"created", |"action":"created",
|"repository":$jsonRepository, |"repository":$jsonRepository,
|"issue":$jsonIssue, |"issue":$jsonIssue,
|"comment":{ |"comment":$jsonComment,
|"id":1,
|"user":$jsonUser,
|"body":"Me too",
|"created_at":"2011-04-14T16:00:49Z",
|"updated_at":"2011-04-14T16:00:49Z",
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/issues/1347#comment-1"},
|"sender":$jsonUser |"sender":$jsonUser
|}""".stripMargin |}""".stripMargin
assert(payload, expected) assert(payload, expected)
@@ -196,23 +169,7 @@ class WebHookJsonFormatSpec extends FunSuite {
) )
val expected = s"""{ val expected = s"""{
|"action":"create", |"action":"create",
|"comment":{ |"comment":$jsonPullRequestReviewComment,
|"id":29724692,
|"path":"README.md",
|"commit_id":"0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|"user":$jsonUser,
|"body":"Maybe you should use more emoji on this line.",
|"created_at":"2015-05-05T23:40:27Z",
|"updated_at":"2015-05-05T23:40:27Z",
|"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/comments/29724692",
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347#discussion_r29724692",
|"pull_request_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347",
|"_links":{
|"self":{"href":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/comments/29724692"},
|"html":{"href":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347#discussion_r29724692"},
|"pull_request":{"href":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347"}
|}
|},
|"pull_request":$jsonPullRequest, |"pull_request":$jsonPullRequest,
|"repository":$jsonRepository, |"repository":$jsonRepository,
|"sender":$jsonUser |"sender":$jsonUser