From 4655708ceeccdf3814601ed8c51e9ff414b6bbc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fede=20Fern=C3=A1ndez?= Date: Wed, 3 May 2017 17:30:14 +0200 Subject: [PATCH 1/5] Moves decoder and encoder tests to shared --- .../scala/github4s/utils/FakeResponses.scala | 375 ------------------ .../test/scala/github4s/utils/TestUtils.scala | 81 +--- .../scala/github4s/unit/DecodersSpec.scala | 67 ---- .../scala/github4s/unit/EncodersSpec.scala | 107 ----- .../test/scala/github4s/utils/TestUtils.scala | 243 +----------- .../scala/github4s/unit/DecodersSpec.scala | 1 - .../scala/github4s/unit/EncodersSpec.scala | 6 +- .../github4s/utils/DummyGithubUrls.scala | 0 .../scala/github4s/utils/FakeResponses.scala | 0 .../test/scala/github4s/utils/TestData.scala | 263 ++++++++++++ 10 files changed, 271 insertions(+), 872 deletions(-) delete mode 100644 github4s/js/src/test/scala/github4s/utils/FakeResponses.scala delete mode 100644 github4s/jvm/src/test/scala/github4s/unit/DecodersSpec.scala delete mode 100644 github4s/jvm/src/test/scala/github4s/unit/EncodersSpec.scala rename github4s/{js => shared}/src/test/scala/github4s/unit/DecodersSpec.scala (98%) rename github4s/{js => shared}/src/test/scala/github4s/unit/EncodersSpec.scala (95%) rename github4s/{jvm => shared}/src/test/scala/github4s/utils/DummyGithubUrls.scala (100%) rename github4s/{jvm => shared}/src/test/scala/github4s/utils/FakeResponses.scala (100%) create mode 100644 github4s/shared/src/test/scala/github4s/utils/TestData.scala diff --git a/github4s/js/src/test/scala/github4s/utils/FakeResponses.scala b/github4s/js/src/test/scala/github4s/utils/FakeResponses.scala deleted file mode 100644 index 25362b205..000000000 --- a/github4s/js/src/test/scala/github4s/utils/FakeResponses.scala +++ /dev/null @@ -1,375 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.utils - -trait FakeResponses { - - val emptyListResponse = - """ - |[] - """.stripMargin - - val listCommitsValidResponse = - """ - |[ - | { - | "sha": "5f919723674b46a8ade67e6a62348953d2bf4350", - | "commit": { - | "author": { - | "name": "Rafa Paradela", - | "email": "rafa.p@47deg.com", - | "date": "2016-05-23T14:00:25Z" - | }, - | "committer": { - | "name": "Rafa Paradela", - | "email": "rafa.p@47deg.com", - | "date": "2016-05-23T14:00:25Z" - | }, - | "message": "Moved/Renamed package to omit organization prefix", - | "tree": { - | "sha": "373377d6041f110396e5650c2a84b24745d677d0", - | "url": "https://api.github.com/repos/47deg/github4s/git/trees/373377d6041f110396e5650c2a84b24745d677d0" - | }, - | "url": "https://api.github.com/repos/47deg/github4s/git/commits/5f919723674b46a8ade67e6a62348953d2bf4350", - | "comment_count": 0 - | }, - | "url": "https://api.github.com/repos/47deg/github4s/commits/5f919723674b46a8ade67e6a62348953d2bf4350", - | "html_url": "https://github.com/47deg/github4s/commit/5f919723674b46a8ade67e6a62348953d2bf4350", - | "comments_url": "https://api.github.com/repos/47deg/github4s/commits/5f919723674b46a8ade67e6a62348953d2bf4350/comments", - | "author": { - | "login": "rafaparadela", - | "id": 315070, - | "avatar_url": "https://avatars.githubusercontent.com/u/315070?v=3", - | "gravatar_id": "", - | "url": "https://api.github.com/users/rafaparadela", - | "html_url": "https://github.com/rafaparadela", - | "followers_url": "https://api.github.com/users/rafaparadela/followers", - | "following_url": "https://api.github.com/users/rafaparadela/following{/other_user}", - | "gists_url": "https://api.github.com/users/rafaparadela/gists{/gist_id}", - | "starred_url": "https://api.github.com/users/rafaparadela/starred{/owner}{/repo}", - | "subscriptions_url": "https://api.github.com/users/rafaparadela/subscriptions", - | "organizations_url": "https://api.github.com/users/rafaparadela/orgs", - | "repos_url": "https://api.github.com/users/rafaparadela/repos", - | "events_url": "https://api.github.com/users/rafaparadela/events{/privacy}", - | "received_events_url": "https://api.github.com/users/rafaparadela/received_events", - | "type": "User", - | "site_admin": false - | }, - | "committer": { - | "login": "rafaparadela", - | "id": 315070, - | "avatar_url": "https://avatars.githubusercontent.com/u/315070?v=3", - | "gravatar_id": "", - | "url": "https://api.github.com/users/rafaparadela", - | "html_url": "https://github.com/rafaparadela", - | "followers_url": "https://api.github.com/users/rafaparadela/followers", - | "following_url": "https://api.github.com/users/rafaparadela/following{/other_user}", - | "gists_url": "https://api.github.com/users/rafaparadela/gists{/gist_id}", - | "starred_url": "https://api.github.com/users/rafaparadela/starred{/owner}{/repo}", - | "subscriptions_url": "https://api.github.com/users/rafaparadela/subscriptions", - | "organizations_url": "https://api.github.com/users/rafaparadela/orgs", - | "repos_url": "https://api.github.com/users/rafaparadela/repos", - | "events_url": "https://api.github.com/users/rafaparadela/events{/privacy}", - | "received_events_url": "https://api.github.com/users/rafaparadela/received_events", - | "type": "User", - | "site_admin": false - | }, - | "parents": [ - | { - | "sha": "e23072c0637e7ecc2ef6d9a6d090288dc63f6beb", - | "url": "https://api.github.com/repos/47deg/github4s/commits/e23072c0637e7ecc2ef6d9a6d090288dc63f6beb", - | "html_url": "https://github.com/47deg/github4s/commit/e23072c0637e7ecc2ef6d9a6d090288dc63f6beb" - | } - | ] - | }, - | { - | "sha": "5f919723674b46a8ade67e6a62348953d2bf4350", - | "commit": { - | "author": { - | "name": "Rafa Paradela", - | "email": "rafa.p@47deg.com", - | "date": "2016-05-23T14:00:25Z" - | }, - | "committer": { - | "name": "Rafa Paradela", - | "email": "rafa.p@47deg.com", - | "date": "2016-05-23T14:00:25Z" - | }, - | "message": "Moved/Renamed package to omit organization prefix", - | "tree": { - | "sha": "373377d6041f110396e5650c2a84b24745d677d0", - | "url": "https://api.github.com/repos/47deg/github4s/git/trees/373377d6041f110396e5650c2a84b24745d677d0" - | }, - | "url": "https://api.github.com/repos/47deg/github4s/git/commits/5f919723674b46a8ade67e6a62348953d2bf4350", - | "comment_count": 0 - | }, - | "url": "https://api.github.com/repos/47deg/github4s/commits/5f919723674b46a8ade67e6a62348953d2bf4350", - | "html_url": "https://github.com/47deg/github4s/commit/5f919723674b46a8ade67e6a62348953d2bf4350", - | "comments_url": "https://api.github.com/repos/47deg/github4s/commits/5f919723674b46a8ade67e6a62348953d2bf4350/comments", - | "author": null, - | "committer": { - | "login": "rafaparadela", - | "id": 315070, - | "avatar_url": "https://avatars.githubusercontent.com/u/315070?v=3", - | "gravatar_id": "", - | "url": "https://api.github.com/users/rafaparadela", - | "html_url": "https://github.com/rafaparadela", - | "followers_url": "https://api.github.com/users/rafaparadela/followers", - | "following_url": "https://api.github.com/users/rafaparadela/following{/other_user}", - | "gists_url": "https://api.github.com/users/rafaparadela/gists{/gist_id}", - | "starred_url": "https://api.github.com/users/rafaparadela/starred{/owner}{/repo}", - | "subscriptions_url": "https://api.github.com/users/rafaparadela/subscriptions", - | "organizations_url": "https://api.github.com/users/rafaparadela/orgs", - | "repos_url": "https://api.github.com/users/rafaparadela/repos", - | "events_url": "https://api.github.com/users/rafaparadela/events{/privacy}", - | "received_events_url": "https://api.github.com/users/rafaparadela/received_events", - | "type": "User", - | "site_admin": false - | }, - | "parents": [ - | { - | "sha": "e23072c0637e7ecc2ef6d9a6d090288dc63f6beb", - | "url": "https://api.github.com/repos/47deg/github4s/commits/e23072c0637e7ecc2ef6d9a6d090288dc63f6beb", - | "html_url": "https://github.com/47deg/github4s/commit/e23072c0637e7ecc2ef6d9a6d090288dc63f6beb" - | } - | ] - | } - |] - """.stripMargin - - val getRepoResponse = - """ - |{ - | "id": 53343599, - | "name": "github4s", - | "full_name": "47deg/github4s", - | "owner": { - | "login": "47deg", - | "id": 479857, - | "avatar_url": "https://avatars.githubusercontent.com/u/479857?v=3", - | "gravatar_id": "", - | "url": "https://api.github.com/users/47deg", - | "html_url": "https://github.com/47deg", - | "followers_url": "https://api.github.com/users/47deg/followers", - | "following_url": "https://api.github.com/users/47deg/following{/other_user}", - | "gists_url": "https://api.github.com/users/47deg/gists{/gist_id}", - | "starred_url": "https://api.github.com/users/47deg/starred{/owner}{/repo}", - | "subscriptions_url": "https://api.github.com/users/47deg/subscriptions", - | "organizations_url": "https://api.github.com/users/47deg/orgs", - | "repos_url": "https://api.github.com/users/47deg/repos", - | "events_url": "https://api.github.com/users/47deg/events{/privacy}", - | "received_events_url": "https://api.github.com/users/47deg/received_events", - | "type": "Organization", - | "site_admin": false - | }, - | "private": false, - | "html_url": "https://github.com/47deg/github4s", - | "description": "A GitHub API wrapper written in Scala", - | "fork": false, - | "url": "https://api.github.com/repos/47deg/github4s", - | "forks_url": "https://api.github.com/repos/47deg/github4s/forks", - | "keys_url": "https://api.github.com/repos/47deg/github4s/keys{/key_id}", - | "collaborators_url": "https://api.github.com/repos/47deg/github4s/collaborators{/collaborator}", - | "teams_url": "https://api.github.com/repos/47deg/github4s/teams", - | "hooks_url": "https://api.github.com/repos/47deg/github4s/hooks", - | "issue_events_url": "https://api.github.com/repos/47deg/github4s/issues/events{/number}", - | "events_url": "https://api.github.com/repos/47deg/github4s/events", - | "assignees_url": "https://api.github.com/repos/47deg/github4s/assignees{/user}", - | "branches_url": "https://api.github.com/repos/47deg/github4s/branches{/branch}", - | "tags_url": "https://api.github.com/repos/47deg/github4s/tags", - | "blobs_url": "https://api.github.com/repos/47deg/github4s/git/blobs{/sha}", - | "git_tags_url": "https://api.github.com/repos/47deg/github4s/git/tags{/sha}", - | "git_refs_url": "https://api.github.com/repos/47deg/github4s/git/refs{/sha}", - | "trees_url": "https://api.github.com/repos/47deg/github4s/git/trees{/sha}", - | "statuses_url": "https://api.github.com/repos/47deg/github4s/statuses/{sha}", - | "languages_url": "https://api.github.com/repos/47deg/github4s/languages", - | "stargazers_url": "https://api.github.com/repos/47deg/github4s/stargazers", - | "contributors_url": "https://api.github.com/repos/47deg/github4s/contributors", - | "subscribers_url": "https://api.github.com/repos/47deg/github4s/subscribers", - | "subscription_url": "https://api.github.com/repos/47deg/github4s/subscription", - | "commits_url": "https://api.github.com/repos/47deg/github4s/commits{/sha}", - | "git_commits_url": "https://api.github.com/repos/47deg/github4s/git/commits{/sha}", - | "comments_url": "https://api.github.com/repos/47deg/github4s/comments{/number}", - | "issue_comment_url": "https://api.github.com/repos/47deg/github4s/issues/comments{/number}", - | "contents_url": "https://api.github.com/repos/47deg/github4s/contents/{+path}", - | "compare_url": "https://api.github.com/repos/47deg/github4s/compare/{base}...{head}", - | "merges_url": "https://api.github.com/repos/47deg/github4s/merges", - | "archive_url": "https://api.github.com/repos/47deg/github4s/{archive_format}{/ref}", - | "downloads_url": "https://api.github.com/repos/47deg/github4s/downloads", - | "issues_url": "https://api.github.com/repos/47deg/github4s/issues{/number}", - | "pulls_url": "https://api.github.com/repos/47deg/github4s/pulls{/number}", - | "milestones_url": "https://api.github.com/repos/47deg/github4s/milestones{/number}", - | "notifications_url": "https://api.github.com/repos/47deg/github4s/notifications{?since,all,participating}", - | "labels_url": "https://api.github.com/repos/47deg/github4s/labels{/name}", - | "releases_url": "https://api.github.com/repos/47deg/github4s/releases{/id}", - | "deployments_url": "https://api.github.com/repos/47deg/github4s/deployments", - | "created_at": "2016-03-07T17:10:37Z", - | "updated_at": "2016-05-20T10:46:21Z", - | "pushed_at": "2016-05-24T14:45:01Z", - | "git_url": "git://github.com/47deg/github4s.git", - | "ssh_url": "git@github.com:47deg/github4s.git", - | "clone_url": "https://github.com/47deg/github4s.git", - | "svn_url": "https://github.com/47deg/github4s", - | "homepage": "http://47deg.github.io/github4s", - | "size": 458, - | "stargazers_count": 0, - | "watchers_count": 0, - | "language": "Scala", - | "has_issues": true, - | "has_downloads": true, - | "has_wiki": true, - | "has_pages": true, - | "forks_count": 1, - | "mirror_url": null, - | "open_issues_count": 5, - | "forks": 1, - | "open_issues": 5, - | "watchers": 0, - | "default_branch": "master", - | "organization": { - | "login": "47deg", - | "id": 479857, - | "avatar_url": "https://avatars.githubusercontent.com/u/479857?v=3", - | "gravatar_id": "", - | "url": "https://api.github.com/users/47deg", - | "html_url": "https://github.com/47deg", - | "followers_url": "https://api.github.com/users/47deg/followers", - | "following_url": "https://api.github.com/users/47deg/following{/other_user}", - | "gists_url": "https://api.github.com/users/47deg/gists{/gist_id}", - | "starred_url": "https://api.github.com/users/47deg/starred{/owner}{/repo}", - | "subscriptions_url": "https://api.github.com/users/47deg/subscriptions", - | "organizations_url": "https://api.github.com/users/47deg/orgs", - | "repos_url": "https://api.github.com/users/47deg/repos", - | "events_url": "https://api.github.com/users/47deg/events{/privacy}", - | "received_events_url": "https://api.github.com/users/47deg/received_events", - | "type": "Organization", - | "site_admin": false - | }, - | "network_count": 1, - | "subscribers_count": 8 - |} - """.stripMargin - - val getCombinedStatusValidResponse = - """ - { - "state": "success", - "statuses": [ - { - "url": "https://api.github.com/repos/47deg/github4s/statuses/e20eab90fef0fc02abc96868713a57ac8e5eaf50", - "id": 1142923124, - "state": "success", - "description": "The Travis CI build passed", - "target_url": "https://travis-ci.org/47deg/github4s/builds/220706044", - "context": "continuous-integration/travis-ci/push", - "created_at": "2017-04-10T20:28:21Z", - "updated_at": "2017-04-10T20:28:21Z" - }, - { - "url": "https://api.github.com/repos/47deg/github4s/statuses/e20eab90fef0fc02abc96868713a57ac8e5eaf50", - "id": 1142928020, - "state": "success", - "description": "80.24% remains the same compared to a6f4c12", - "target_url": "https://codecov.io/gh/47deg/github4s/commit/e20eab90fef0fc02abc96868713a57ac8e5eaf50", - "context": "codecov/project", - "created_at": "2017-04-10T20:29:58Z", - "updated_at": "2017-04-10T20:29:58Z" - }, - { - "url": "https://api.github.com/repos/47deg/github4s/statuses/e20eab90fef0fc02abc96868713a57ac8e5eaf50", - "id": 1142928022, - "state": "success", - "description": "Coverage not affected when comparing a6f4c12...e20eab9", - "target_url": "https://codecov.io/gh/47deg/github4s/commit/e20eab90fef0fc02abc96868713a57ac8e5eaf50", - "context": "codecov/patch", - "created_at": "2017-04-10T20:29:58Z", - "updated_at": "2017-04-10T20:29:58Z" - } - ], - "sha": "e20eab90fef0fc02abc96868713a57ac8e5eaf50", - "total_count": 3, - "repository": { - "id": 53343599, - "name": "github4s", - "full_name": "47deg/github4s", - "owner": { - "login": "47deg", - "id": 479857, - "avatar_url": "https://avatars0.githubusercontent.com/u/479857?v=3", - "gravatar_id": "", - "url": "https://api.github.com/users/47deg", - "html_url": "https://github.com/47deg", - "followers_url": "https://api.github.com/users/47deg/followers", - "following_url": "https://api.github.com/users/47deg/following{/other_user}", - "gists_url": "https://api.github.com/users/47deg/gists{/gist_id}", - "starred_url": "https://api.github.com/users/47deg/starred{/owner}{/repo}", - "subscriptions_url": "https://api.github.com/users/47deg/subscriptions", - "organizations_url": "https://api.github.com/users/47deg/orgs", - "repos_url": "https://api.github.com/users/47deg/repos", - "events_url": "https://api.github.com/users/47deg/events{/privacy}", - "received_events_url": "https://api.github.com/users/47deg/received_events", - "type": "Organization", - "site_admin": false - }, - "private": false, - "html_url": "https://github.com/47deg/github4s", - "description": "A GitHub API wrapper written in Scala", - "fork": false, - "url": "https://api.github.com/repos/47deg/github4s", - "forks_url": "https://api.github.com/repos/47deg/github4s/forks", - "keys_url": "https://api.github.com/repos/47deg/github4s/keys{/key_id}", - "collaborators_url": "https://api.github.com/repos/47deg/github4s/collaborators{/collaborator}", - "teams_url": "https://api.github.com/repos/47deg/github4s/teams", - "hooks_url": "https://api.github.com/repos/47deg/github4s/hooks", - "issue_events_url": "https://api.github.com/repos/47deg/github4s/issues/events{/number}", - "events_url": "https://api.github.com/repos/47deg/github4s/events", - "assignees_url": "https://api.github.com/repos/47deg/github4s/assignees{/user}", - "branches_url": "https://api.github.com/repos/47deg/github4s/branches{/branch}", - "tags_url": "https://api.github.com/repos/47deg/github4s/tags", - "blobs_url": "https://api.github.com/repos/47deg/github4s/git/blobs{/sha}", - "git_tags_url": "https://api.github.com/repos/47deg/github4s/git/tags{/sha}", - "git_refs_url": "https://api.github.com/repos/47deg/github4s/git/refs{/sha}", - "trees_url": "https://api.github.com/repos/47deg/github4s/git/trees{/sha}", - "statuses_url": "https://api.github.com/repos/47deg/github4s/statuses/{sha}", - "languages_url": "https://api.github.com/repos/47deg/github4s/languages", - "stargazers_url": "https://api.github.com/repos/47deg/github4s/stargazers", - "contributors_url": "https://api.github.com/repos/47deg/github4s/contributors", - "subscribers_url": "https://api.github.com/repos/47deg/github4s/subscribers", - "subscription_url": "https://api.github.com/repos/47deg/github4s/subscription", - "commits_url": "https://api.github.com/repos/47deg/github4s/commits{/sha}", - "git_commits_url": "https://api.github.com/repos/47deg/github4s/git/commits{/sha}", - "comments_url": "https://api.github.com/repos/47deg/github4s/comments{/number}", - "issue_comment_url": "https://api.github.com/repos/47deg/github4s/issues/comments{/number}", - "contents_url": "https://api.github.com/repos/47deg/github4s/contents/{+path}", - "compare_url": "https://api.github.com/repos/47deg/github4s/compare/{base}...{head}", - "merges_url": "https://api.github.com/repos/47deg/github4s/merges", - "archive_url": "https://api.github.com/repos/47deg/github4s/{archive_format}{/ref}", - "downloads_url": "https://api.github.com/repos/47deg/github4s/downloads", - "issues_url": "https://api.github.com/repos/47deg/github4s/issues{/number}", - "pulls_url": "https://api.github.com/repos/47deg/github4s/pulls{/number}", - "milestones_url": "https://api.github.com/repos/47deg/github4s/milestones{/number}", - "notifications_url": "https://api.github.com/repos/47deg/github4s/notifications{?since,all,participating}", - "labels_url": "https://api.github.com/repos/47deg/github4s/labels{/name}", - "releases_url": "https://api.github.com/repos/47deg/github4s/releases{/id}", - "deployments_url": "https://api.github.com/repos/47deg/github4s/deployments" - }, - "commit_url": "https://api.github.com/repos/47deg/github4s/commits/e20eab90fef0fc02abc96868713a57ac8e5eaf50", - "url": "https://api.github.com/repos/47deg/github4s/commits/e20eab90fef0fc02abc96868713a57ac8e5eaf50/status" - } - """ - -} diff --git a/github4s/js/src/test/scala/github4s/utils/TestUtils.scala b/github4s/js/src/test/scala/github4s/utils/TestUtils.scala index d64a4213e..b0eec0a14 100644 --- a/github4s/js/src/test/scala/github4s/utils/TestUtils.scala +++ b/github4s/js/src/test/scala/github4s/utils/TestUtils.scala @@ -16,7 +16,6 @@ package github4s.utils -import com.github.marklister.base64.Base64._ import org.scalatest.{Assertion, Matchers} import cats.implicits._ import github4s.GithubResponses.{GHResponse, GHResult} @@ -24,7 +23,8 @@ import github4s.free.domain._ import scala.concurrent.Future -trait TestUtils extends Matchers { +trait TestUtils extends Matchers with TestData { + def testFutureIsLeft[A](response: Future[GHResponse[A]])( implicit ec: scala.concurrent.ExecutionContext): Future[Assertion] = { response map { r => @@ -47,80 +47,7 @@ trait TestUtils extends Matchers { } } - val accessToken: Option[String] = Option(github4s.BuildInfo.token) - def tokenHeader: String = "token " + accessToken.getOrElse("") - val headerUserAgent: Map[String, String] = Map("user-agent" -> "github4s") - - val validUsername = "rafaparadela" - val invalidUsername = "GHInvalidaUserName" - val invalidPassword = "invalidPassword" - - def validBasicAuth = s"Basic ${s"$validUsername:".getBytes.toBase64}" - def invalidBasicAuth = - s"Basic ${s"$validUsername:$invalidPassword".getBytes.toBase64}" - - val validScopes = List("public_repo") - val validNote = "New access token" - val validClientId = "e8e39175648c9db8c280" - val invalidClientSecret = "1234567890" - val validCode = "code" - val invalidCode = "invalid-code" - - val validRepoOwner = "47deg" - val validRepoName = "github4s" - val validFilePath = "README.md" - val invalidFilePath = "NON_EXISTENT_FILE_IN_REPOSITORY" - val invalidRepoName = "GHInvalidRepoName" - val validRedirectUri = "http://localhost:9000/_oauth-callback" - val validPage = 1 - val invalidPage = 999 - val validPerPage = 100 - - val validSinceInt = 100 - val invalidSinceInt = 999999999 - - val okStatusCode = 200 - val createdStatusCode = 201 - val unauthorizedStatusCode = 401 - val notFoundStatusCode = 404 - - val validAnonParameter = "true" - val invalidAnonParameter = "X" - - val validGistDescription = "A Gist" - val validGistPublic = false - val validGistFileContent = "val meaningOfLife = 42" - val validGistFilename = "test.scala" - - val validSearchQuery = "Scala 2.12" - val nonExistentSearchQuery = "nonExistentSearchQueryString" - val validSearchParams = List( - OwnerParamInRepository(s"$validRepoOwner/$validRepoName"), - IssueTypeIssue, - SearchIn(Set(SearchInTitle)) - ) - - val validIssue = 48 - val validIssueTitle = "Sample Title" - val validIssueBody = "Sample Body" - val validIssueState = "closed" - val validIssueLabel = List("bug", "code review") - val validAssignees = List(validUsername) - - val validRefSingle = "heads/master" - val invalidRef = "heads/feature-branch-that-no-longer-exists" - - val validCommitSha = "d3b048c1f500ee5450e5d7b3d1921ed3e7645891" - val invalidCommitSha = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - val validCommitMsg = "Add SBT project settings" - - val validStatusState = "success" - val validMode = "100644" - val validBlobType = "blob" - - val validHead = "test-pr-issue" - val validBase = "master" - - val validPullRequestNumber = 1 + val accessToken: Option[String] = Option(github4s.BuildInfo.token) + def tokenHeader: String = "token " + accessToken.getOrElse("") } diff --git a/github4s/jvm/src/test/scala/github4s/unit/DecodersSpec.scala b/github4s/jvm/src/test/scala/github4s/unit/DecodersSpec.scala deleted file mode 100644 index 1fb3a2d76..000000000 --- a/github4s/jvm/src/test/scala/github4s/unit/DecodersSpec.scala +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.unit - -import cats.data.NonEmptyList -import cats.syntax.either._ -import github4s.free.domain.{CombinedStatus, Commit, Repository} -import github4s.utils.FakeResponses -import github4s.Decoders._ -import io.circe.generic.auto._ -import io.circe.parser._ -import org.scalatest._ - -class DecodersSpec extends FlatSpec with Matchers with FakeResponses { - - "Commit decoder" should "return a list of commits when the JSON is valid" in { - decode[List[Commit]](listCommitsValidResponse) should be('right) - } - - it should "return an empty list for an empty JSON" in { - decode[List[Commit]](emptyListResponse).toOption map (_.isEmpty shouldBe true) - } - - "Repository decoder" should "return a valid repo for a valid JSON" in { - decode[Repository](getRepoResponse) should be('right) - } - - it should "return an error for an empty JSON" in { - decode[Repository](emptyListResponse) should be('left) - } - - "StatusRepository decoder" should "return a valid repo for a valid JSON" in { - decode[CombinedStatus](getCombinedStatusValidResponse) should be('right) - } - - it should "return an error for an empty JSON" in { - decode[CombinedStatus](emptyListResponse) should be('left) - } - - "NonEmptyList Decoder" should "return a valid NonEmptyList for a valid JSON list" in { - decode[NonEmptyList[Int]]("[1,2,3]") should be(Right(NonEmptyList.of(1, 2, 3))) - } - - case class Foo(a: Int) - it should "return a valid NonEmtpyList for a valid JSON" in { - decode[NonEmptyList[Foo]]("""{"a": 1}""") should be(Right(NonEmptyList(Foo(1), Nil))) - } - - it should "return an error for an empty list" in { - decode[NonEmptyList[Int]](emptyListResponse) should be('left) - } - -} diff --git a/github4s/jvm/src/test/scala/github4s/unit/EncodersSpec.scala b/github4s/jvm/src/test/scala/github4s/unit/EncodersSpec.scala deleted file mode 100644 index 9d941819a..000000000 --- a/github4s/jvm/src/test/scala/github4s/unit/EncodersSpec.scala +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.unit - -import github4s.Encoders._ -import github4s.free.domain._ -import github4s.utils.TestUtils -import io.circe.parser._ -import io.circe.syntax._ -import org.scalatest._ - -class EncodersSpec extends FlatSpec with Matchers with TestUtils { - - "TreeData encoder" should "encode the TreeDataSha" in { - val treeData: TreeData = TreeDataSha(validFilePath, validMode, validBlobType, validCommitSha) - - val expectedJsonString = - s""" - | { - | "path": "$validFilePath", - | "mode": "$validMode", - | "type": "$validBlobType", - | "sha": "$validCommitSha" - | } - """.stripMargin - - val expectedJson = parse(expectedJsonString).right.get - val actualJson = treeData.asJson - - actualJson shouldBe expectedJson - } - - it should "encode the TreeDataBlob" in { - val treeData: TreeData = TreeDataBlob(validFilePath, validMode, validBlobType, validCommitMsg) - - val expectedJsonString = - s""" - | { - | "path": "$validFilePath", - | "mode": "$validMode", - | "type": "$validBlobType", - | "content": "$validCommitMsg" - | } - """.stripMargin - - val expectedJson = parse(expectedJsonString).right.get - val actualJson = treeData.asJson - - actualJson shouldBe expectedJson - } - - "CreatePullRequest encoder" should "encode the CreatePullRequestData" in { - val createPullRequest: CreatePullRequest = - CreatePullRequestData(validIssueTitle, validHead, validBase, validCommitMsg, Some(false)) - - val expectedJsonString = - s""" - | { - | "title": "$validIssueTitle", - | "head": "$validHead", - | "base": "$validBase", - | "body": "$validCommitMsg", - | "maintainer_can_modify": false - | } - """.stripMargin - - val expectedJson = parse(expectedJsonString).right.get - val actualJson = createPullRequest.asJson - - actualJson shouldBe expectedJson - } - - it should "encode the CreatePullRequestIssue" in { - val createPullRequest: CreatePullRequest = - CreatePullRequestIssue(validIssue, validHead, validBase, Some(false)) - - val expectedJsonString = - s""" - | { - | "issue": $validIssue, - | "head": "$validHead", - | "base": "$validBase", - | "maintainer_can_modify": false - | } - """.stripMargin - - val expectedJson = parse(expectedJsonString).right.get - val actualJson = createPullRequest.asJson - - actualJson shouldBe expectedJson - } - -} diff --git a/github4s/jvm/src/test/scala/github4s/utils/TestUtils.scala b/github4s/jvm/src/test/scala/github4s/utils/TestUtils.scala index 24c23732e..201c2c5f7 100644 --- a/github4s/jvm/src/test/scala/github4s/utils/TestUtils.scala +++ b/github4s/jvm/src/test/scala/github4s/utils/TestUtils.scala @@ -16,250 +16,9 @@ package github4s.utils -import com.github.marklister.base64.Base64.Encoder -import github4s.free.domain._ -import io.circe.parser.parse -import org.mockito.ArgumentMatcher - -trait TestUtils { - - case class JsonArgMatcher(json: String) extends ArgumentMatcher[String] { - override def matches(argument: String): Boolean = parse(json) == parse(argument) - } +trait TestUtils extends TestData { val accessToken: Option[String] = sys.env.get("GITHUB4S_ACCESS_TOKEN") def tokenHeader: String = "token " + accessToken.getOrElse("") - val headerUserAgent = Map("user-agent" -> "github4s") - - val validUsername = "rafaparadela" - val invalidUsername = "GHInvalidaUserName" - val invalidPassword = "invalidPassword" - - def validBasicAuth = s"Basic ${s"$validUsername:".getBytes.toBase64}" - def invalidBasicAuth = - s"Basic ${s"$validUsername:$invalidPassword".getBytes.toBase64}" - - val validScopes = List("public_repo") - val validNote = "New access token" - val validClientId = "e8e39175648c9db8c280" - val invalidClientSecret = "1234567890" - val validCode = "code" - val invalidCode = "invalid-code" - - val validRepoOwner = "47deg" - val validRepoName = "github4s" - val invalidRepoName = "GHInvalidRepoName" - val validRedirectUri = "http://localhost:9000/_oauth-callback" - val validPage = 1 - val invalidPage = 999 - val validPerPage = 100 - val validFilePath = "README.md" - val invalidFilePath = "NON_EXISTENT_FILE_IN_REPOSITORY" - val validDirPath = "lib" - val validSymlinkPath = "bin/some-symlink" - val validSubmodulePath = "test/qunit" - - val validSinceInt = 100 - val invalidSinceInt = 999999999 - - val okStatusCode = 200 - val createdStatusCode = 201 - val unauthorizedStatusCode = 401 - val notFoundStatusCode = 404 - - val validAnonParameter = "true" - val invalidAnonParameter = "X" - - val validGistDescription = "A Gist" - val validGistPublic = true - val validGistFileContent = "val meaningOfLife = 42" - val validGistFilename = "test.scala" - - val validSearchQuery = "Scala 2.12" - val nonExistentSearchQuery = "nonExistentSearchQueryString" - val validSearchParams = List( - OwnerParamInRepository(s"$validRepoOwner/$validRepoName"), - IssueTypeIssue, - SearchIn(Set(SearchInTitle)) - ) - - val validIssue = 48 - val validIssueTitle = "Sample Title" - val validIssueBody = "Sample Body" - val validIssueState = "closed" - val validIssueLabel = List("bug", "code review") - val validAssignees = List(validUsername) - - val githubApiUrl = "http://api.github.com" - val encoding = Some("utf-8") - - val validRefSingle = "heads/master" - val validRefMultiple = "heads/feature" - val invalidRef = "heads/feature-branch-that-no-longer-exists" - - val validCommitSha = "d3b048c1f500ee5450e5d7b3d1921ed3e7645891" - val validCommitMsg = "Add SBT project settings" - val commitType = "commit" - val invalidCommitSha = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - - val validTreeSha = "827efc6d56897b048c772eb4087f854f46256132" - val invalidTreeSha = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - - val validTagTitle = "v0.1.1" - val validTagSha = "c3d0be41ecbe669545ee3e94d31ed9a4bc91ee3c" - - val validPullRequestFileSha = "f80f79cafbe3f2ba71311b82e1171e73bd37a470" - val validPullRequestNumber = 1 - - val validHead = "test-pr-issue" - val invalidHead = "" - - val validBase = "master" - val invalidBase = "" - - val validNewPullRequestData = NewPullRequestData("Amazing new feature", "Please pull this in!") - val invalidNewPullRequestData = NewPullRequestData("", "") - - val validNewPullRequestIssue = NewPullRequestIssue(31) - val invalidNewPullRequestIssue = NewPullRequestIssue(5) - - val validPath = "project/plugins.sbt" - - val validStatusState = "success" - val validMode = "100644" - val validBlobType = "blob" - - val treeDataList: List[TreeData] = List( - TreeDataSha(validPath, validMode, validBlobType, validTreeSha)) - val treeDataResult = List( - TreeDataResult( - path = validPath, - mode = validMode, - `type` = validBlobType, - size = Some(100), - sha = validTreeSha, - url = githubApiUrl)) - - val refObject = RefObject(commitType, validCommitSha, githubApiUrl) - val ref = Ref("XXXX", githubApiUrl, refObject) - - val refCommitAuthor = - RefAuthor("2014-11-07T22:01:45Z", validUsername, "developer@47deg.com") - val refInfo = new RefInfo(validTreeSha, githubApiUrl) - val refCommit = RefCommit( - sha = validCommitSha, - url = githubApiUrl, - author = refCommitAuthor, - committer = refCommitAuthor, - message = validNote, - tree = refInfo, - parents = List(refInfo)) - - val pullRequest = PullRequest( - id = 1, - number = validPullRequestNumber, - state = "open", - title = "Title", - body = "Body", - locked = false, - html_url = githubApiUrl, - created_at = "2011-04-10T20:09:31Z", - updated_at = None, - closed_at = None, - merged_at = None, - base = None, - head = None, - user = None, - assignee = None - ) - - val pullRequestFile = PullRequestFile( - sha = validPullRequestFileSha, - filename = validPath, - status = "modified", - additions = 3, - deletions = 1, - changes = 4, - blob_url = githubApiUrl, - raw_url = githubApiUrl, - contents_url = githubApiUrl, - patch = "", - previous_filename = None - ) - - val tag = Tag( - tag = validTagTitle, - sha = validTagSha, - url = githubApiUrl, - message = validNote, - tagger = refCommitAuthor, - `object` = RefObject(commitType, validCommitSha, githubApiUrl) - ) - - val release = Release( - id = 1, - tag_name = validTagTitle, - target_commitish = "master", - name = validTagTitle, - body = validNote, - draft = false, - prerelease = false, - created_at = "2011-04-10T20:09:31Z", - published_at = "2011-04-10T20:09:31Z", - author = User(1, validUsername, githubApiUrl, githubApiUrl), - url = githubApiUrl, - html_url = githubApiUrl, - assets_url = githubApiUrl, - upload_url = githubApiUrl, - tarball_url = githubApiUrl, - zipball_url = githubApiUrl - ) - - val content = Content( - `type` = "file", - encoding = Some("base64"), - target = None, - submodule_git_url = None, - size = validSinceInt, - name = validFilePath, - path = validFilePath, - content = Some(validGistFileContent.getBytes.toBase64), - sha = invalidCommitSha, - url = githubApiUrl, - git_url = githubApiUrl, - html_url = githubApiUrl, - download_url = Some(githubApiUrl) - ) - - val status = Status( - id = 1, - url = githubApiUrl, - state = validStatusState, - target_url = None, - description = None, - context = None, - creator = Some(User(1, validUsername, githubApiUrl, githubApiUrl)), - created_at = "2011-04-10T20:09:31Z", - updated_at = "2011-04-10T20:09:31Z" - ) - - val combinedStatus = CombinedStatus( - url = githubApiUrl, - state = validStatusState, - commit_url = githubApiUrl, - sha = validCommitSha, - total_count = 1, - statuses = List(status), - repository = StatusRepository( - id = 1, - name = validRepoName, - full_name = s"$validRepoOwner/$validRepoName", - owner = User(1, validUsername, githubApiUrl, githubApiUrl), - `private` = false, - description = None, - fork = false, - urls = Map() - ) - ) } diff --git a/github4s/js/src/test/scala/github4s/unit/DecodersSpec.scala b/github4s/shared/src/test/scala/github4s/unit/DecodersSpec.scala similarity index 98% rename from github4s/js/src/test/scala/github4s/unit/DecodersSpec.scala rename to github4s/shared/src/test/scala/github4s/unit/DecodersSpec.scala index 47b9e0a1d..0b7b245b6 100644 --- a/github4s/js/src/test/scala/github4s/unit/DecodersSpec.scala +++ b/github4s/shared/src/test/scala/github4s/unit/DecodersSpec.scala @@ -17,7 +17,6 @@ package github4s.unit import cats.data.NonEmptyList -import cats.syntax.either._ import github4s.Decoders._ import github4s.free.domain.{CombinedStatus, Commit, Repository} import github4s.utils.FakeResponses diff --git a/github4s/js/src/test/scala/github4s/unit/EncodersSpec.scala b/github4s/shared/src/test/scala/github4s/unit/EncodersSpec.scala similarity index 95% rename from github4s/js/src/test/scala/github4s/unit/EncodersSpec.scala rename to github4s/shared/src/test/scala/github4s/unit/EncodersSpec.scala index 4d401ed01..cce07222e 100644 --- a/github4s/js/src/test/scala/github4s/unit/EncodersSpec.scala +++ b/github4s/shared/src/test/scala/github4s/unit/EncodersSpec.scala @@ -18,12 +18,12 @@ package github4s.unit import github4s.Encoders._ import github4s.free.domain._ -import github4s.utils.TestUtils +import github4s.utils.TestData import io.circe.parser._ import io.circe.syntax._ -import org.scalatest._ +import org.scalatest.{FlatSpec, Matchers} -class EncodersSpec extends FlatSpec with Matchers with TestUtils { +class EncodersSpec extends FlatSpec with Matchers with TestData { "TreeData encoder" should "encode the TreeDataSha" in { val treeData: TreeData = TreeDataSha(validFilePath, validMode, validBlobType, validCommitSha) diff --git a/github4s/jvm/src/test/scala/github4s/utils/DummyGithubUrls.scala b/github4s/shared/src/test/scala/github4s/utils/DummyGithubUrls.scala similarity index 100% rename from github4s/jvm/src/test/scala/github4s/utils/DummyGithubUrls.scala rename to github4s/shared/src/test/scala/github4s/utils/DummyGithubUrls.scala diff --git a/github4s/jvm/src/test/scala/github4s/utils/FakeResponses.scala b/github4s/shared/src/test/scala/github4s/utils/FakeResponses.scala similarity index 100% rename from github4s/jvm/src/test/scala/github4s/utils/FakeResponses.scala rename to github4s/shared/src/test/scala/github4s/utils/FakeResponses.scala diff --git a/github4s/shared/src/test/scala/github4s/utils/TestData.scala b/github4s/shared/src/test/scala/github4s/utils/TestData.scala new file mode 100644 index 000000000..eb3b7976c --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/utils/TestData.scala @@ -0,0 +1,263 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package github4s.utils + +import com.github.marklister.base64.Base64._ +import github4s.free.domain._ +import io.circe.parser.parse +import org.mockito.ArgumentMatcher + +trait TestData { + + case class JsonArgMatcher(json: String) extends ArgumentMatcher[String] { + override def matches(argument: String): Boolean = parse(json) == parse(argument) + } + + val headerUserAgent = Map("user-agent" -> "github4s") + + val validUsername = "rafaparadela" + val invalidUsername = "GHInvalidaUserName" + val invalidPassword = "invalidPassword" + + def validBasicAuth = s"Basic ${s"$validUsername:".getBytes.toBase64}" + def invalidBasicAuth = + s"Basic ${s"$validUsername:$invalidPassword".getBytes.toBase64}" + + val validScopes = List("public_repo") + val validNote = "New access token" + val validClientId = "e8e39175648c9db8c280" + val invalidClientSecret = "1234567890" + val validCode = "code" + val invalidCode = "invalid-code" + + val validRepoOwner = "47deg" + val validRepoName = "github4s" + val invalidRepoName = "GHInvalidRepoName" + val validRedirectUri = "http://localhost:9000/_oauth-callback" + val validPage = 1 + val invalidPage = 999 + val validPerPage = 100 + val validFilePath = "README.md" + val invalidFilePath = "NON_EXISTENT_FILE_IN_REPOSITORY" + val validDirPath = "lib" + val validSymlinkPath = "bin/some-symlink" + val validSubmodulePath = "test/qunit" + + val validSinceInt = 100 + val invalidSinceInt = 999999999 + + val okStatusCode = 200 + val createdStatusCode = 201 + val unauthorizedStatusCode = 401 + val notFoundStatusCode = 404 + + val validAnonParameter = "true" + val invalidAnonParameter = "X" + + val validGistDescription = "A Gist" + val validGistPublic = true + val validGistFileContent = "val meaningOfLife = 42" + val validGistFilename = "test.scala" + + val validSearchQuery = "Scala 2.12" + val nonExistentSearchQuery = "nonExistentSearchQueryString" + val validSearchParams = List( + OwnerParamInRepository(s"$validRepoOwner/$validRepoName"), + IssueTypeIssue, + SearchIn(Set(SearchInTitle)) + ) + + val validIssue = 48 + val validIssueTitle = "Sample Title" + val validIssueBody = "Sample Body" + val validIssueState = "closed" + val validIssueLabel = List("bug", "code review") + val validAssignees = List(validUsername) + + val githubApiUrl = "http://api.github.com" + val encoding = Some("utf-8") + + val validRefSingle = "heads/master" + val validRefMultiple = "heads/feature" + val invalidRef = "heads/feature-branch-that-no-longer-exists" + + val validCommitSha = "d3b048c1f500ee5450e5d7b3d1921ed3e7645891" + val validCommitMsg = "Add SBT project settings" + val commitType = "commit" + val invalidCommitSha = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + + val validTreeSha = "827efc6d56897b048c772eb4087f854f46256132" + val invalidTreeSha = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + + val validTagTitle = "v0.1.1" + val validTagSha = "c3d0be41ecbe669545ee3e94d31ed9a4bc91ee3c" + + val validPullRequestFileSha = "f80f79cafbe3f2ba71311b82e1171e73bd37a470" + val validPullRequestNumber = 1 + + val validHead = "test-pr-issue" + val invalidHead = "" + + val validBase = "master" + val invalidBase = "" + + val validNewPullRequestData = NewPullRequestData("Amazing new feature", "Please pull this in!") + val invalidNewPullRequestData = NewPullRequestData("", "") + + val validNewPullRequestIssue = NewPullRequestIssue(31) + val invalidNewPullRequestIssue = NewPullRequestIssue(5) + + val validPath = "project/plugins.sbt" + + val validStatusState = "success" + val validMode = "100644" + val validBlobType = "blob" + + val treeDataList: List[TreeData] = List( + TreeDataSha(validPath, validMode, validBlobType, validTreeSha)) + val treeDataResult = List( + TreeDataResult( + path = validPath, + mode = validMode, + `type` = validBlobType, + size = Some(100), + sha = validTreeSha, + url = githubApiUrl)) + + val refObject = RefObject(commitType, validCommitSha, githubApiUrl) + val ref = Ref("XXXX", githubApiUrl, refObject) + + val refCommitAuthor = + RefAuthor("2014-11-07T22:01:45Z", validUsername, "developer@47deg.com") + val refInfo = new RefInfo(validTreeSha, githubApiUrl) + val refCommit = RefCommit( + sha = validCommitSha, + url = githubApiUrl, + author = refCommitAuthor, + committer = refCommitAuthor, + message = validNote, + tree = refInfo, + parents = List(refInfo)) + + val pullRequest = PullRequest( + id = 1, + number = validPullRequestNumber, + state = "open", + title = "Title", + body = "Body", + locked = false, + html_url = githubApiUrl, + created_at = "2011-04-10T20:09:31Z", + updated_at = None, + closed_at = None, + merged_at = None, + base = None, + head = None, + user = None, + assignee = None + ) + + val pullRequestFile = PullRequestFile( + sha = validPullRequestFileSha, + filename = validPath, + status = "modified", + additions = 3, + deletions = 1, + changes = 4, + blob_url = githubApiUrl, + raw_url = githubApiUrl, + contents_url = githubApiUrl, + patch = "", + previous_filename = None + ) + + val tag = Tag( + tag = validTagTitle, + sha = validTagSha, + url = githubApiUrl, + message = validNote, + tagger = refCommitAuthor, + `object` = RefObject(commitType, validCommitSha, githubApiUrl) + ) + + val release = Release( + id = 1, + tag_name = validTagTitle, + target_commitish = "master", + name = validTagTitle, + body = validNote, + draft = false, + prerelease = false, + created_at = "2011-04-10T20:09:31Z", + published_at = "2011-04-10T20:09:31Z", + author = User(1, validUsername, githubApiUrl, githubApiUrl), + url = githubApiUrl, + html_url = githubApiUrl, + assets_url = githubApiUrl, + upload_url = githubApiUrl, + tarball_url = githubApiUrl, + zipball_url = githubApiUrl + ) + + val content = Content( + `type` = "file", + encoding = Some("base64"), + target = None, + submodule_git_url = None, + size = validSinceInt, + name = validFilePath, + path = validFilePath, + content = Some(validGistFileContent.getBytes.toBase64), + sha = invalidCommitSha, + url = githubApiUrl, + git_url = githubApiUrl, + html_url = githubApiUrl, + download_url = Some(githubApiUrl) + ) + + val status = Status( + id = 1, + url = githubApiUrl, + state = validStatusState, + target_url = None, + description = None, + context = None, + creator = Some(User(1, validUsername, githubApiUrl, githubApiUrl)), + created_at = "2011-04-10T20:09:31Z", + updated_at = "2011-04-10T20:09:31Z" + ) + + val combinedStatus = CombinedStatus( + url = githubApiUrl, + state = validStatusState, + commit_url = githubApiUrl, + sha = validCommitSha, + total_count = 1, + statuses = List(status), + repository = StatusRepository( + id = 1, + name = validRepoName, + full_name = s"$validRepoOwner/$validRepoName", + owner = User(1, validUsername, githubApiUrl, githubApiUrl), + `private` = false, + description = None, + fork = false, + urls = Map() + ) + ) + +} From e12b165ab4d508f6e03f3edbe3764624e1021a38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fede=20Fern=C3=A1ndez?= Date: Wed, 3 May 2017 21:36:56 +0200 Subject: [PATCH 2/5] Uses scalamock instead of mockito and moves some tests to shared proj --- .../scala/github4s/unit/GHGitDataSpec.scala | 250 ------------ .../scala/github4s/unit/GitDataSpec.scala | 379 ------------------ .../github4s/unit/PullRequestsSpec.scala | 102 ----- .../test/scala/github4s/unit/ReposSpec.scala | 128 ------ .../scala/github4s/unit/StatusesSpec.scala | 132 ------ .../scala/github4s/unit/GHGitDataSpec.scala | 186 +++++++++ .../github4s/unit/GHPullRequestsSpec.scala | 114 ++---- .../scala/github4s/unit/GHReposSpec.scala | 71 +--- .../scala/github4s/unit/GHStatusesSpec.scala | 74 ++-- .../scala/github4s/unit/GitDataSpec.scala | 271 +++++++++++++ .../github4s/unit/PullRequestsSpec.scala | 64 +++ .../test/scala/github4s/unit/ReposSpec.scala | 90 +++++ .../scala/github4s/unit/StatusesSpec.scala | 80 ++++ .../test/scala/github4s/utils/BaseSpec.scala | 93 +++++ .../test/scala/github4s/utils/TestData.scala | 11 +- project/ProjectPlugin.scala | 6 +- 16 files changed, 874 insertions(+), 1177 deletions(-) delete mode 100644 github4s/jvm/src/test/scala/github4s/unit/GHGitDataSpec.scala delete mode 100644 github4s/jvm/src/test/scala/github4s/unit/GitDataSpec.scala delete mode 100644 github4s/jvm/src/test/scala/github4s/unit/PullRequestsSpec.scala delete mode 100644 github4s/jvm/src/test/scala/github4s/unit/ReposSpec.scala delete mode 100644 github4s/jvm/src/test/scala/github4s/unit/StatusesSpec.scala create mode 100644 github4s/shared/src/test/scala/github4s/unit/GHGitDataSpec.scala rename github4s/{jvm => shared}/src/test/scala/github4s/unit/GHPullRequestsSpec.scala (53%) rename github4s/{jvm => shared}/src/test/scala/github4s/unit/GHReposSpec.scala (54%) rename github4s/{jvm => shared}/src/test/scala/github4s/unit/GHStatusesSpec.scala (53%) create mode 100644 github4s/shared/src/test/scala/github4s/unit/GitDataSpec.scala create mode 100644 github4s/shared/src/test/scala/github4s/unit/PullRequestsSpec.scala create mode 100644 github4s/shared/src/test/scala/github4s/unit/ReposSpec.scala create mode 100644 github4s/shared/src/test/scala/github4s/unit/StatusesSpec.scala create mode 100644 github4s/shared/src/test/scala/github4s/utils/BaseSpec.scala diff --git a/github4s/jvm/src/test/scala/github4s/unit/GHGitDataSpec.scala b/github4s/jvm/src/test/scala/github4s/unit/GHGitDataSpec.scala deleted file mode 100644 index d416d73b8..000000000 --- a/github4s/jvm/src/test/scala/github4s/unit/GHGitDataSpec.scala +++ /dev/null @@ -1,250 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.unit - -import cats.data.NonEmptyList -import cats.free.Free -import github4s.GHGitData -import github4s.GithubResponses.{GHResponse, GHResult} -import github4s.app.GitHub4s -import github4s.free.algebra.GitDataOps -import github4s.free.domain._ -import github4s.utils.TestUtils -import org.scalatest.{FlatSpec, Matchers} -import org.scalatest.mockito.MockitoSugar.mock -import org.mockito.Mockito._ -import org.mockito.ArgumentMatchers.any - -class GHGitDataSpec extends FlatSpec with Matchers with TestUtils { - - "GHGitData.getReference" should "call to GitDataOps with the right parameters" in { - - val response: Free[GitHub4s, GHResponse[NonEmptyList[Ref]]] = - Free.pure(Right(GHResult(NonEmptyList(ref, Nil), okStatusCode, Map.empty))) - - val gitDataOps = mock[GitDataOps[GitHub4s]] - when(gitDataOps.getReference(any[String], any[String], any[String], any[Option[String]])) - .thenReturn(response) - - val token = Some("token") - val ghGitData = new GHGitData(token)(gitDataOps) - ghGitData.getReference(validRepoOwner, validRepoName, validRefSingle) - - verify(gitDataOps).getReference(validRepoOwner, validRepoName, validRefSingle, token) - } - - "GHGitData.createReference" should "call to GitDataOps with the right parameters" in { - - val response: Free[GitHub4s, GHResponse[Ref]] = - Free.pure(Right(GHResult(ref, okStatusCode, Map.empty))) - - val gitDataOps = mock[GitDataOps[GitHub4s]] - when( - gitDataOps - .createReference(any[String], any[String], any[String], any[String], any[Option[String]])) - .thenReturn(response) - - val token = Some("token") - val ghGitData = new GHGitData(token)(gitDataOps) - ghGitData.createReference(validRepoOwner, validRepoName, validRefSingle, validCommitSha) - - verify(gitDataOps).createReference( - validRepoOwner, - validRepoName, - validRefSingle, - validCommitSha, - token) - } - - "GHGitData.updateReference" should "call to GitDataOps with the right parameters" in { - - val response: Free[GitHub4s, GHResponse[Ref]] = - Free.pure(Right(GHResult(ref, okStatusCode, Map.empty))) - - val gitDataOps = mock[GitDataOps[GitHub4s]] - when( - gitDataOps.updateReference( - any[String], - any[String], - any[String], - any[String], - any[Option[Boolean]], - any[Option[String]])) - .thenReturn(response) - - val token = Some("token") - val ghGitData = new GHGitData(token)(gitDataOps) - ghGitData.updateReference( - validRepoOwner, - validRepoName, - validRefSingle, - validCommitSha, - Some(false)) - - verify(gitDataOps).updateReference( - validRepoOwner, - validRepoName, - validRefSingle, - validCommitSha, - Some(false), - token) - } - - "GHGitData.getCommit" should "call to GitDataOps with the right parameters" in { - - val response: Free[GitHub4s, GHResponse[RefCommit]] = - Free.pure(Right(GHResult(refCommit, okStatusCode, Map.empty))) - - val gitDataOps = mock[GitDataOps[GitHub4s]] - when(gitDataOps.getCommit(any[String], any[String], any[String], any[Option[String]])) - .thenReturn(response) - - val token = Some("token") - val ghGitData = new GHGitData(token)(gitDataOps) - ghGitData.getCommit(validRepoOwner, validRepoName, validCommitSha) - - verify(gitDataOps).getCommit(validRepoOwner, validRepoName, validCommitSha, token) - } - - "GHGitData.createCommit" should "call to GitDataOps with the right parameters" in { - - val response: Free[GitHub4s, GHResponse[RefCommit]] = - Free.pure(Right(GHResult(refCommit, okStatusCode, Map.empty))) - - val gitDataOps = mock[GitDataOps[GitHub4s]] - when( - gitDataOps.createCommit( - any[String], - any[String], - any[String], - any[String], - any[List[String]], - any[Option[RefAuthor]], - any[Option[String]])) - .thenReturn(response) - - val token = Some("token") - val ghGitData = new GHGitData(token)(gitDataOps) - ghGitData.createCommit( - validRepoOwner, - validRepoName, - validNote, - validTreeSha, - List(validCommitSha), - Some(refCommitAuthor)) - - verify(gitDataOps).createCommit( - validRepoOwner, - validRepoName, - validNote, - validTreeSha, - List(validCommitSha), - Some(refCommitAuthor), - token) - } - - "GHGitData.createBlob" should "call to GitDataOps with the right parameters" in { - - val response: Free[GitHub4s, GHResponse[RefInfo]] = - Free.pure( - Right(GHResult(new RefInfo(validCommitSha, githubApiUrl), okStatusCode, Map.empty))) - - val gitDataOps = mock[GitDataOps[GitHub4s]] - when(gitDataOps - .createBlob(any[String], any[String], any[String], any[Option[String]], any[Option[String]])) - .thenReturn(response) - - val token = Some("token") - val ghGitData = new GHGitData(token)(gitDataOps) - ghGitData.createBlob(validRepoOwner, validRepoName, validNote, encoding) - - verify(gitDataOps).createBlob(validRepoOwner, validRepoName, validNote, encoding, token) - } - - "GHGitData.createTree" should "call to GitDataOps with the right parameters" in { - - val response: Free[GitHub4s, GHResponse[TreeResult]] = - Free.pure( - Right( - GHResult( - TreeResult(validCommitSha, githubApiUrl, treeDataResult), - okStatusCode, - Map.empty))) - - val gitDataOps = mock[GitDataOps[GitHub4s]] - when( - gitDataOps.createTree( - any[String], - any[String], - any[Option[String]], - any[List[TreeData]], - any[Option[String]])) - .thenReturn(response) - - val token = Some("token") - val ghGitData = new GHGitData(token)(gitDataOps) - ghGitData.createTree(validRepoOwner, validRepoName, Some(validTreeSha), treeDataList) - - verify(gitDataOps).createTree( - validRepoOwner, - validRepoName, - Some(validTreeSha), - treeDataList, - token) - } - - "GHGitData.createTag" should "call to GitDataOps with the right parameters" in { - - val response: Free[GitHub4s, GHResponse[Tag]] = - Free.pure(Right(GHResult(tag, okStatusCode, Map.empty))) - - val gitDataOps = mock[GitDataOps[GitHub4s]] - when( - gitDataOps.createTag( - any[String], - any[String], - any[String], - any[String], - any[String], - any[String], - any[Option[RefAuthor]], - any[Option[String]])) - .thenReturn(response) - - val token = Some("token") - val ghGitData = new GHGitData(token)(gitDataOps) - ghGitData.createTag( - validRepoOwner, - validRepoName, - validTagTitle, - validNote, - validCommitSha, - commitType, - Some(refCommitAuthor)) - - verify(gitDataOps).createTag( - validRepoOwner, - validRepoName, - validTagTitle, - validNote, - validCommitSha, - commitType, - Some(refCommitAuthor), - token) - } - -} diff --git a/github4s/jvm/src/test/scala/github4s/unit/GitDataSpec.scala b/github4s/jvm/src/test/scala/github4s/unit/GitDataSpec.scala deleted file mode 100644 index a5949472f..000000000 --- a/github4s/jvm/src/test/scala/github4s/unit/GitDataSpec.scala +++ /dev/null @@ -1,379 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.unit - -import cats.Id -import cats.data.NonEmptyList -import github4s.{HttpClient, HttpRequestBuilderExtensionJVM, IdInstances} -import github4s.GithubResponses.{GHResponse, GHResult} -import github4s.api.GitData -import github4s.free.domain._ -import github4s.utils.{DummyGithubUrls, TestUtils} -import org.mockito.ArgumentMatchers.any -import org.mockito.ArgumentMatchers.argThat -import org.mockito.ArgumentMatchers.{eq => argEq} -import org.mockito.Mockito._ -import org.scalatest.mockito.MockitoSugar.mock -import org.scalatest.{FlatSpec, Matchers} -import io.circe.Decoder - -import scalaj.http.HttpResponse - -class GitDataSpec - extends FlatSpec - with Matchers - with TestUtils - with DummyGithubUrls - with IdInstances - with HttpRequestBuilderExtensionJVM { - - "GitData.reference" should "call to httpClient.get with the right parameters" in { - - val response: GHResponse[NonEmptyList[Ref]] = - Right(GHResult(NonEmptyList(ref, Nil), okStatusCode, Map.empty)) - - val httpClientMock = mock[HttpClient[HttpResponse[String], Id]] - when( - httpClientMock.get[NonEmptyList[Ref]]( - any[Option[String]], - any[String], - any[Map[String, String]], - any[Map[String, String]], - any[Option[Pagination]])(any[Decoder[NonEmptyList[Ref]]])) - .thenReturn(response) - - val token = Some("token") - val gitData = new GitData[HttpResponse[String], Id] { - override val httpClient: HttpClient[HttpResponse[String], Id] = httpClientMock - } - gitData.reference(token, headerUserAgent, validRepoOwner, validRepoName, validRefSingle) - - verify(httpClientMock).get[NonEmptyList[Ref]]( - argEq(token), - argEq(s"repos/$validRepoOwner/$validRepoName/git/refs/$validRefSingle"), - argEq(headerUserAgent), - any[Map[String, String]], - any[Option[Pagination]] - )(any[Decoder[NonEmptyList[Ref]]]) - } - - "GitData.createReference" should "call to httpClient.post with the right parameters" in { - - val response: GHResponse[Ref] = - Right(GHResult(ref, okStatusCode, Map.empty)) - - val httpClientMock = mock[HttpClient[HttpResponse[String], Id]] - when( - httpClientMock - .post[Ref](any[Option[String]], any[String], any[Map[String, String]], any[String])( - any[Decoder[Ref]])) - .thenReturn(response) - - val token = Some("token") - val gitData = new GitData[HttpResponse[String], Id] { - override val httpClient: HttpClient[HttpResponse[String], Id] = httpClientMock - } - gitData.createReference( - token, - headerUserAgent, - validRepoOwner, - validRepoName, - s"refs/$validRefSingle", - validCommitSha) - - val request = - s""" - |{ - | "ref": "refs/$validRefSingle", - | "sha": "$validCommitSha" - |} - """.stripMargin - - verify(httpClientMock).post[Ref]( - argEq(token), - argEq(s"repos/$validRepoOwner/$validRepoName/git/refs"), - argEq(headerUserAgent), - argThat(JsonArgMatcher(request)) - )(any[Decoder[Ref]]) - } - - "GitData.updateReference" should "call to httpClient.patch with the right parameters" in { - - val response: GHResponse[Ref] = - Right(GHResult(ref, okStatusCode, Map.empty)) - - val httpClientMock = mock[HttpClient[HttpResponse[String], Id]] - when( - httpClientMock - .patch[Ref](any[Option[String]], any[String], any[Map[String, String]], any[String])( - any[Decoder[Ref]])) - .thenReturn(response) - - val token = Some("token") - val force = Some(false) - val gitData = new GitData[HttpResponse[String], Id] { - override val httpClient: HttpClient[HttpResponse[String], Id] = httpClientMock - } - gitData.updateReference( - token, - headerUserAgent, - validRepoOwner, - validRepoName, - validRefSingle, - validCommitSha, - force) - - val request = - s""" - |{ - | "sha": "$validCommitSha" - | ${force.map(f => ",\"force\": " + f).getOrElse(" ")} - |} - """.stripMargin - - verify(httpClientMock).patch[Ref]( - argEq(token), - argEq(s"repos/$validRepoOwner/$validRepoName/git/refs/$validRefSingle"), - argEq(headerUserAgent), - argThat(JsonArgMatcher(request)) - )(any[Decoder[Ref]]) - } - - "GitData.getCommit" should "call to httpClient.get with the right parameters" in { - - val response: GHResponse[RefCommit] = - Right(GHResult(refCommit, okStatusCode, Map.empty)) - - val httpClientMock = mock[HttpClient[HttpResponse[String], Id]] - when( - httpClientMock.get[RefCommit]( - any[Option[String]], - any[String], - any[Map[String, String]], - any[Map[String, String]], - any[Option[Pagination]])(any[Decoder[RefCommit]])) - .thenReturn(response) - - val token = Some("token") - val gitData = new GitData[HttpResponse[String], Id] { - override val httpClient: HttpClient[HttpResponse[String], Id] = httpClientMock - } - - gitData.commit(token, headerUserAgent, validRepoOwner, validRepoName, validCommitSha) - - verify(httpClientMock).get[RefCommit]( - argEq(token), - argEq(s"repos/$validRepoOwner/$validRepoName/git/commits/$validCommitSha"), - argEq(headerUserAgent), - any[Map[String, String]], - any[Option[Pagination]] - )(any[Decoder[RefCommit]]) - } - - "GitData.createCommit" should "call to httpClient.post with the right parameters" in { - - val response: GHResponse[RefCommit] = - Right(GHResult(refCommit, okStatusCode, Map.empty)) - - val httpClientMock = mock[HttpClient[HttpResponse[String], Id]] - when( - httpClientMock - .post[RefCommit](any[Option[String]], any[String], any[Map[String, String]], any[String])( - any[Decoder[RefCommit]])) - .thenReturn(response) - - val token = Some("token") - val gitData = new GitData[HttpResponse[String], Id] { - override val httpClient: HttpClient[HttpResponse[String], Id] = httpClientMock - } - gitData.createCommit( - token, - headerUserAgent, - validRepoOwner, - validRepoName, - validNote, - validTreeSha, - List(validCommitSha), - Some(refCommitAuthor)) - - val request = - s""" - |{ - | "message": "$validNote", - | "tree": "$validTreeSha", - | "parents": ["$validCommitSha"], - | "author": { - | "name": "${refCommitAuthor.name}", - | "email": "${refCommitAuthor.email}", - | "date": "${refCommitAuthor.date}" - | } - |} - """.stripMargin - - verify(httpClientMock).post[RefCommit]( - argEq(token), - argEq(s"repos/$validRepoOwner/$validRepoName/git/commits"), - argEq(headerUserAgent), - argThat(JsonArgMatcher(request)) - )(any[Decoder[RefCommit]]) - } - - "GitData.createBlob" should "call to httpClient.post with the right parameters" in { - - val response: GHResponse[RefInfo] = - Right(GHResult(new RefInfo(validCommitSha, githubApiUrl), okStatusCode, Map.empty)) - - val httpClientMock = mock[HttpClient[HttpResponse[String], Id]] - when( - httpClientMock - .post[RefInfo](any[Option[String]], any[String], any[Map[String, String]], any[String])( - any[Decoder[RefInfo]])) - .thenReturn(response) - - val token = Some("token") - val gitData = new GitData[HttpResponse[String], Id] { - override val httpClient: HttpClient[HttpResponse[String], Id] = httpClientMock - } - gitData.createBlob(token, headerUserAgent, validRepoOwner, validRepoName, validNote, encoding) - - val request = - s""" - |{ - | "content": "$validNote" - | ${encoding.map(e => ",\"encoding\": \"" + e + "\"").getOrElse(" ")} - |} - """.stripMargin - - verify(httpClientMock).post[RefInfo]( - argEq(token), - argEq(s"repos/$validRepoOwner/$validRepoName/git/blobs"), - argEq(headerUserAgent), - argThat(JsonArgMatcher(request)) - )(any[Decoder[RefInfo]]) - } - - "GitData.createTree" should "call to httpClient.post with the right parameters" in { - - val response: GHResponse[TreeResult] = - Right( - GHResult( - TreeResult(validCommitSha, githubApiUrl, treeDataResult), - okStatusCode, - Map.empty)) - - val httpClientMock = mock[HttpClient[HttpResponse[String], Id]] - when( - httpClientMock - .post[TreeResult](any[Option[String]], any[String], any[Map[String, String]], any[String])( - any[Decoder[TreeResult]])) - .thenReturn(response) - - val token = Some("token") - val gitData = new GitData[HttpResponse[String], Id] { - override val httpClient: HttpClient[HttpResponse[String], Id] = httpClientMock - } - gitData.createTree( - token, - headerUserAgent, - validRepoOwner, - validRepoName, - Some(validTreeSha), - treeDataList) - - def treeDataJson(treeData: TreeData): String = { - - def attr: String = treeData match { - case d: TreeDataSha => "\"sha\": \"" + d.sha + "\"" - case d: TreeDataBlob => "\"content\": \"" + d.content + "\"" - } - - s""" - |{ - | "path": "${treeData.path}", - | "mode": "${treeData.mode}", - | "type": "${treeData.`type`}", - | $attr - |} - """.stripMargin - } - - val request = - s""" - |{ - | "base_tree": "$validTreeSha", - | "tree": [ ${treeDataList.map(treeDataJson).mkString(",")} ] - |} - """.stripMargin - - verify(httpClientMock).post[TreeResult]( - argEq(token), - argEq(s"repos/$validRepoOwner/$validRepoName/git/trees"), - argEq(headerUserAgent), - argThat(JsonArgMatcher(request)) - )(any[Decoder[TreeResult]]) - } - - "GitData.createTag" should "call to httpClient.post with the right parameters" in { - - val response: GHResponse[Tag] = Right(GHResult(tag, okStatusCode, Map.empty)) - - val httpClientMock = mock[HttpClient[HttpResponse[String], Id]] - when( - httpClientMock - .post[Tag](any[Option[String]], any[String], any[Map[String, String]], any[String])( - any[Decoder[Tag]])) - .thenReturn(response) - - val token = Some("token") - val gitData = new GitData[HttpResponse[String], Id] { - override val httpClient: HttpClient[HttpResponse[String], Id] = httpClientMock - } - gitData.createTag( - token, - headerUserAgent, - validRepoOwner, - validRepoName, - validTagTitle, - validNote, - validCommitSha, - commitType, - Some(refCommitAuthor)) - - val request = - s""" - |{ - | "tag": "$validTagTitle", - | "message": "$validNote", - | "object": "$validCommitSha", - | "type": "$commitType", - | "tagger": { - | "name": "${refCommitAuthor.name}", - | "email": "${refCommitAuthor.email}", - | "date": "${refCommitAuthor.date}" - | } - |} - """.stripMargin - - verify(httpClientMock).post[Tag]( - argEq(token), - argEq(s"repos/$validRepoOwner/$validRepoName/git/tags"), - argEq(headerUserAgent), - argThat(JsonArgMatcher(request)) - )(any[Decoder[Tag]]) - } - -} diff --git a/github4s/jvm/src/test/scala/github4s/unit/PullRequestsSpec.scala b/github4s/jvm/src/test/scala/github4s/unit/PullRequestsSpec.scala deleted file mode 100644 index 58ff7404d..000000000 --- a/github4s/jvm/src/test/scala/github4s/unit/PullRequestsSpec.scala +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.unit - -import cats.Id -import github4s.GithubResponses.{GHResponse, GHResult} -import github4s.api.PullRequests -import github4s.free.domain._ -import github4s.utils.{DummyGithubUrls, TestUtils} -import github4s.{HttpClient, HttpRequestBuilderExtensionJVM, IdInstances} -import io.circe.Decoder -import org.mockito.ArgumentMatchers.{any, eq => argEq} -import org.mockito.Mockito._ -import org.scalatest.mockito.MockitoSugar.mock -import org.scalatest.{FlatSpec, Matchers} - -import scalaj.http.HttpResponse - -class PullRequestsSpec - extends FlatSpec - with Matchers - with TestUtils - with DummyGithubUrls - with IdInstances - with HttpRequestBuilderExtensionJVM { - - "PullRequests.list" should "call to httpClient.get with the right parameters" in { - - val response: GHResponse[List[PullRequest]] = - Right(GHResult(List(pullRequest), okStatusCode, Map.empty)) - - val httpClientMock = mock[HttpClient[HttpResponse[String], Id]] - when( - httpClientMock.get[List[PullRequest]]( - any[Option[String]], - any[String], - any[Map[String, String]], - any[Map[String, String]], - any[Option[Pagination]])(any[Decoder[List[PullRequest]]])) - .thenReturn(response) - - val token = Some("token") - val pullRequests = new PullRequests[HttpResponse[String], Id] { - override val httpClient: HttpClient[HttpResponse[String], Id] = httpClientMock - } - pullRequests.list(token, headerUserAgent, validRepoOwner, validRepoName, Nil) - - verify(httpClientMock).get[List[PullRequest]]( - argEq(token), - argEq(s"repos/$validRepoOwner/$validRepoName/pulls"), - argEq(headerUserAgent), - any[Map[String, String]], - any[Option[Pagination]] - )(any[Decoder[List[PullRequest]]]) - } - - "PullRequests.listFiles" should "call to httpClient.get with the right parameters" in { - - val response: GHResponse[List[PullRequestFile]] = - Right(GHResult(List(pullRequestFile), okStatusCode, Map.empty)) - - val httpClientMock = mock[HttpClient[HttpResponse[String], Id]] - when( - httpClientMock.get[List[PullRequestFile]]( - any[Option[String]], - any[String], - any[Map[String, String]], - any[Map[String, String]], - any[Option[Pagination]])(any[Decoder[List[PullRequestFile]]])) - .thenReturn(response) - - val token = Some("token") - val pullRequests = new PullRequests[HttpResponse[String], Id] { - override val httpClient: HttpClient[HttpResponse[String], Id] = httpClientMock - } - pullRequests - .listFiles(token, headerUserAgent, validRepoOwner, validRepoName, validPullRequestNumber) - - verify(httpClientMock).get[List[PullRequestFile]]( - argEq(token), - argEq(s"repos/$validRepoOwner/$validRepoName/pulls/$validPullRequestNumber/files"), - argEq(headerUserAgent), - any[Map[String, String]], - any[Option[Pagination]] - )(any[Decoder[List[PullRequestFile]]]) - } - -} diff --git a/github4s/jvm/src/test/scala/github4s/unit/ReposSpec.scala b/github4s/jvm/src/test/scala/github4s/unit/ReposSpec.scala deleted file mode 100644 index dfd965743..000000000 --- a/github4s/jvm/src/test/scala/github4s/unit/ReposSpec.scala +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.unit - -import cats.Id -import cats.data.NonEmptyList -import github4s.GithubResponses.{GHResponse, GHResult} -import github4s.api.Repos -import github4s.free.domain._ -import github4s.utils.{DummyGithubUrls, TestUtils} -import github4s.{HttpClient, HttpRequestBuilderExtensionJVM, IdInstances} -import io.circe.Decoder -import org.mockito.ArgumentMatchers.{any, argThat, eq => argEq} -import org.mockito.Mockito._ -import org.scalatest.mockito.MockitoSugar.mock -import org.scalatest.{FlatSpec, Matchers} - -import scalaj.http.HttpResponse - -class ReposSpec - extends FlatSpec - with Matchers - with TestUtils - with DummyGithubUrls - with IdInstances - with HttpRequestBuilderExtensionJVM { - - "Repos.getContents" should "call to httpClient.get with the right parameters" in { - - val response: GHResponse[NonEmptyList[Content]] = - Right(GHResult(NonEmptyList(content, Nil), okStatusCode, Map.empty)) - - val httpClientMock = mock[HttpClient[HttpResponse[String], Id]] - when( - httpClientMock - .get[NonEmptyList[Content]]( - any[Option[String]], - any[String], - any[Map[String, String]], - any[Map[String, String]], - any[Option[Pagination]])(any[Decoder[NonEmptyList[Content]]])) - .thenReturn(response) - - val token = Some("token") - val repos = new Repos[HttpResponse[String], Id] { - override val httpClient: HttpClient[HttpResponse[String], Id] = httpClientMock - } - repos.getContents( - accessToken = token, - headers = headerUserAgent, - owner = validRepoOwner, - repo = validRepoName, - path = validFilePath, - ref = Some("master") - ) - - verify(httpClientMock).get[NonEmptyList[Content]]( - argEq(token), - argEq(s"repos/$validRepoOwner/$validRepoName/contents/$validFilePath"), - argEq(headerUserAgent), - argEq(Map("ref" -> "master")), - any[Option[Pagination]] - )(any[Decoder[NonEmptyList[Content]]]) - } - - "Repos.createRelease" should "call to httpClient.post with the right parameters" in { - - val response: GHResponse[Release] = Right(GHResult(release, okStatusCode, Map.empty)) - - val httpClientMock = mock[HttpClient[HttpResponse[String], Id]] - when( - httpClientMock - .post[Release](any[Option[String]], any[String], any[Map[String, String]], any[String])( - any[Decoder[Release]])) - .thenReturn(response) - - val token = Some("token") - val repos = new Repos[HttpResponse[String], Id] { - override val httpClient: HttpClient[HttpResponse[String], Id] = httpClientMock - } - repos.createRelease( - accessToken = token, - headers = headerUserAgent, - owner = validRepoOwner, - repo = validRepoName, - tagName = validTagTitle, - name = validTagTitle, - body = validNote, - targetCommitish = Some("master"), - draft = Some(false), - prerelease = Some(true) - ) - - val request = - s""" - |{ - | "tag_name": "$validTagTitle", - | "target_commitish": "master", - | "name": "$validTagTitle", - | "body": "$validNote", - | "draft": false, - | "prerelease": true - |} - """.stripMargin - - verify(httpClientMock).post[Release]( - argEq(token), - argEq(s"repos/$validRepoOwner/$validRepoName/releases"), - argEq(headerUserAgent), - argThat(JsonArgMatcher(request)) - )(any[Decoder[Release]]) - } - -} diff --git a/github4s/jvm/src/test/scala/github4s/unit/StatusesSpec.scala b/github4s/jvm/src/test/scala/github4s/unit/StatusesSpec.scala deleted file mode 100644 index 2979d2415..000000000 --- a/github4s/jvm/src/test/scala/github4s/unit/StatusesSpec.scala +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.unit - -import cats.Id -import github4s.GithubResponses.{GHResponse, GHResult} -import github4s.api.Statuses -import github4s.free.domain._ -import github4s.utils.{DummyGithubUrls, TestUtils} -import github4s.{HttpClient, HttpRequestBuilderExtensionJVM, IdInstances} -import io.circe.Decoder -import org.mockito.ArgumentMatchers.{any, argThat, eq => argEq} -import org.mockito.Mockito._ -import org.scalatest.{FlatSpec, Matchers} -import org.scalatest.mockito.MockitoSugar.mock - -import scalaj.http.HttpResponse - -class StatusesSpec - extends FlatSpec - with Matchers - with TestUtils - with DummyGithubUrls - with IdInstances - with HttpRequestBuilderExtensionJVM { - - "Statuses.get" should "call httpClient.get with the right parameters" in { - val response: GHResponse[CombinedStatus] = - Right(GHResult(combinedStatus, okStatusCode, Map.empty)) - - val httpClientMock = mock[HttpClient[HttpResponse[String], Id]] - when( - httpClientMock.get[CombinedStatus]( - any[Option[String]], - any[String], - any[Map[String, String]], - any[Map[String, String]], - any[Option[Pagination]])(any[Decoder[CombinedStatus]])) - .thenReturn(response) - - val token = Some("token") - val statuses = new Statuses[HttpResponse[String], Id] { - override val httpClient: HttpClient[HttpResponse[String], Id] = httpClientMock - } - statuses.get(token, headerUserAgent, validRepoOwner, validRepoName, validRefSingle) - - verify(httpClientMock).get[CombinedStatus]( - argEq(token), - argEq(s"repos/$validRepoOwner/$validRepoName/commits/$validRefSingle/status"), - argEq(headerUserAgent), - any[Map[String, String]], - any[Option[Pagination]] - )(any[Decoder[CombinedStatus]]) - } - - "Statuses.list" should "call htppClient.get with the right parameters" in { - val response: GHResponse[List[Status]] = Right(GHResult(List(status), okStatusCode, Map.empty)) - - val httpClientMock = mock[HttpClient[HttpResponse[String], Id]] - when( - httpClientMock.get[List[Status]]( - any[Option[String]], - any[String], - any[Map[String, String]], - any[Map[String, String]], - any[Option[Pagination]])(any[Decoder[List[Status]]])) - .thenReturn(response) - - val token = Some("token") - val statuses = new Statuses[HttpResponse[String], Id] { - override val httpClient: HttpClient[HttpResponse[String], Id] = httpClientMock - } - statuses.list(token, headerUserAgent, validRepoOwner, validRepoName, validRefSingle) - - verify(httpClientMock).get[List[Status]]( - argEq(token), - argEq(s"repos/$validRepoOwner/$validRepoName/commits/$validRefSingle/statuses"), - argEq(headerUserAgent), - any[Map[String, String]], - any[Option[Pagination]] - )(any[Decoder[List[Status]]]) - } - - "Statuses.create" should "call httpClient.post with the right parameters" in { - val response: GHResponse[Status] = Right(GHResult(status, createdStatusCode, Map.empty)) - - val httpClientMock = mock[HttpClient[HttpResponse[String], Id]] - when( - httpClientMock - .post[Status](any[Option[String]], any[String], any[Map[String, String]], any[String])( - any[Decoder[Status]])) - .thenReturn(response) - - val token = Some("token") - val statuses = new Statuses[HttpResponse[String], Id] { - override val httpClient: HttpClient[HttpResponse[String], Id] = httpClientMock - } - statuses.create( - token, - headerUserAgent, - validRepoOwner, - validRepoName, - validCommitSha, - validStatusState, - None, - None, - None) - - val request = s"""{"state":"$validStatusState"}""" - - verify(httpClientMock).post[Status]( - argEq(token), - argEq(s"repos/$validRepoOwner/$validRepoName/statuses/$validCommitSha"), - argEq(headerUserAgent), - argThat(JsonArgMatcher(request)) - )(any[Decoder[Status]]) - } -} diff --git a/github4s/shared/src/test/scala/github4s/unit/GHGitDataSpec.scala b/github4s/shared/src/test/scala/github4s/unit/GHGitDataSpec.scala new file mode 100644 index 000000000..536034502 --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/unit/GHGitDataSpec.scala @@ -0,0 +1,186 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package github4s.unit + +import cats.data.NonEmptyList +import cats.free.Free +import github4s.GHGitData +import github4s.GithubResponses.{GHResponse, GHResult} +import github4s.app.GitHub4s +import github4s.free.domain._ +import github4s.utils.BaseSpec + +class GHGitDataSpec extends BaseSpec { + + "GHGitData.getReference" should "call to GitDataOps with the right parameters" in { + + val response: Free[GitHub4s, GHResponse[NonEmptyList[Ref]]] = + Free.pure(Right(GHResult(NonEmptyList(ref, Nil), okStatusCode, Map.empty))) + + val gitDataOps = mock[GitDataOpsTest] + (gitDataOps.getReference _) + .expects(validRepoOwner, validRepoName, validRefSingle, sampleToken) + .returns(response) + + val ghGitData = new GHGitData(sampleToken)(gitDataOps) + ghGitData.getReference(validRepoOwner, validRepoName, validRefSingle) + } + + "GHGitData.createReference" should "call to GitDataOps with the right parameters" in { + + val response: Free[GitHub4s, GHResponse[Ref]] = + Free.pure(Right(GHResult(ref, okStatusCode, Map.empty))) + + val gitDataOps = mock[GitDataOpsTest] + (gitDataOps.createReference _) + .expects(validRepoOwner, validRepoName, validRefSingle, validCommitSha, sampleToken) + .returns(response) + + val ghGitData = new GHGitData(sampleToken)(gitDataOps) + ghGitData.createReference(validRepoOwner, validRepoName, validRefSingle, validCommitSha) + } + + "GHGitData.updateReference" should "call to GitDataOps with the right parameters" in { + + val response: Free[GitHub4s, GHResponse[Ref]] = + Free.pure(Right(GHResult(ref, okStatusCode, Map.empty))) + + val gitDataOps = mock[GitDataOpsTest] + (gitDataOps.updateReference _) + .expects( + validRepoOwner, + validRepoName, + validRefSingle, + validCommitSha, + Some(false), + sampleToken) + .returns(response) + + val ghGitData = new GHGitData(sampleToken)(gitDataOps) + ghGitData.updateReference( + validRepoOwner, + validRepoName, + validRefSingle, + validCommitSha, + Some(false)) + } + + "GHGitData.getCommit" should "call to GitDataOps with the right parameters" in { + + val response: Free[GitHub4s, GHResponse[RefCommit]] = + Free.pure(Right(GHResult(refCommit, okStatusCode, Map.empty))) + + val gitDataOps = mock[GitDataOpsTest] + (gitDataOps.getCommit _) + .expects(validRepoOwner, validRepoName, validCommitSha, sampleToken) + .returns(response) + + val ghGitData = new GHGitData(sampleToken)(gitDataOps) + ghGitData.getCommit(validRepoOwner, validRepoName, validCommitSha) + } + + "GHGitData.createCommit" should "call to GitDataOps with the right parameters" in { + + val response: Free[GitHub4s, GHResponse[RefCommit]] = + Free.pure(Right(GHResult(refCommit, okStatusCode, Map.empty))) + + val gitDataOps = mock[GitDataOpsTest] + (gitDataOps.createCommit _) + .expects( + validRepoOwner, + validRepoName, + validNote, + validTreeSha, + List(validCommitSha), + Some(refCommitAuthor), + sampleToken) + .returns(response) + + val ghGitData = new GHGitData(sampleToken)(gitDataOps) + ghGitData.createCommit( + validRepoOwner, + validRepoName, + validNote, + validTreeSha, + List(validCommitSha), + Some(refCommitAuthor)) + } + + "GHGitData.createBlob" should "call to GitDataOps with the right parameters" in { + + val response: Free[GitHub4s, GHResponse[RefInfo]] = + Free.pure( + Right(GHResult(new RefInfo(validCommitSha, githubApiUrl), okStatusCode, Map.empty))) + + val gitDataOps = mock[GitDataOpsTest] + (gitDataOps.createBlob _) + .expects(validRepoOwner, validRepoName, validNote, encoding, sampleToken) + .returns(response) + + val ghGitData = new GHGitData(sampleToken)(gitDataOps) + ghGitData.createBlob(validRepoOwner, validRepoName, validNote, encoding) + } + + "GHGitData.createTree" should "call to GitDataOps with the right parameters" in { + + val response: Free[GitHub4s, GHResponse[TreeResult]] = + Free.pure( + Right( + GHResult( + TreeResult(validCommitSha, githubApiUrl, treeDataResult), + okStatusCode, + Map.empty))) + + val gitDataOps = mock[GitDataOpsTest] + (gitDataOps.createTree _) + .expects(validRepoOwner, validRepoName, Some(validTreeSha), treeDataList, sampleToken) + .returns(response) + + val ghGitData = new GHGitData(sampleToken)(gitDataOps) + ghGitData.createTree(validRepoOwner, validRepoName, Some(validTreeSha), treeDataList) + } + + "GHGitData.createTag" should "call to GitDataOps with the right parameters" in { + + val response: Free[GitHub4s, GHResponse[Tag]] = + Free.pure(Right(GHResult(tag, okStatusCode, Map.empty))) + + val gitDataOps = mock[GitDataOpsTest] + (gitDataOps.createTag _) + .expects( + validRepoOwner, + validRepoName, + validTagTitle, + validNote, + validCommitSha, + commitType, + Some(refCommitAuthor), + sampleToken) + .returns(response) + + val ghGitData = new GHGitData(sampleToken)(gitDataOps) + ghGitData.createTag( + validRepoOwner, + validRepoName, + validTagTitle, + validNote, + validCommitSha, + commitType, + Some(refCommitAuthor)) + } + +} diff --git a/github4s/jvm/src/test/scala/github4s/unit/GHPullRequestsSpec.scala b/github4s/shared/src/test/scala/github4s/unit/GHPullRequestsSpec.scala similarity index 53% rename from github4s/jvm/src/test/scala/github4s/unit/GHPullRequestsSpec.scala rename to github4s/shared/src/test/scala/github4s/unit/GHPullRequestsSpec.scala index 9efe91a39..6dde838c3 100644 --- a/github4s/jvm/src/test/scala/github4s/unit/GHPullRequestsSpec.scala +++ b/github4s/shared/src/test/scala/github4s/unit/GHPullRequestsSpec.scala @@ -20,32 +20,23 @@ import cats.free.Free import github4s.GHPullRequests import github4s.GithubResponses.{GHResponse, GHResult} import github4s.app.GitHub4s -import github4s.free.algebra.PullRequestOps import github4s.free.domain._ -import github4s.utils.TestUtils -import org.mockito.ArgumentMatchers.any -import org.mockito.Mockito._ -import org.scalatest.mockito.MockitoSugar.mock -import org.scalatest.{FlatSpec, Matchers} +import github4s.utils.BaseSpec -class GHPullRequestsSpec extends FlatSpec with Matchers with TestUtils { +class GHPullRequestsSpec extends BaseSpec { "GHPullRequests.list" should "call to PullRequestOps with the right parameters" in { val response: Free[GitHub4s, GHResponse[List[PullRequest]]] = Free.pure(Right(GHResult(List(pullRequest), okStatusCode, Map.empty))) - val pullRequestOps = mock[PullRequestOps[GitHub4s]] - when( - pullRequestOps - .listPullRequests(any[String], any[String], any[List[PRFilter]], any[Option[String]])) - .thenReturn(response) + val pullRequestOps = mock[PullRequestOpsTest] + (pullRequestOps.listPullRequests _) + .expects(validRepoOwner, validRepoName, Nil, sampleToken) + .returns(response) - val token = Some("token") - val ghPullRequests = new GHPullRequests(token)(pullRequestOps) + val ghPullRequests = new GHPullRequests(sampleToken)(pullRequestOps) ghPullRequests.list(validRepoOwner, validRepoName) - - verify(pullRequestOps).listPullRequests(validRepoOwner, validRepoName, Nil, token) } "GHPullRequests.listFiles" should "call to PullRequestOps with the right parameters" in { @@ -53,18 +44,13 @@ class GHPullRequestsSpec extends FlatSpec with Matchers with TestUtils { val response: Free[GitHub4s, GHResponse[List[PullRequestFile]]] = Free.pure(Right(GHResult(List(pullRequestFile), okStatusCode, Map.empty))) - val pullRequestOps = mock[PullRequestOps[GitHub4s]] - when( - pullRequestOps - .listPullRequestFiles(any[String], any[String], any[Int], any[Option[String]])) - .thenReturn(response) + val pullRequestOps = mock[PullRequestOpsTest] + (pullRequestOps.listPullRequestFiles _) + .expects(validRepoOwner, validRepoName, validPullRequestNumber, sampleToken) + .returns(response) - val token = Some("token") - val ghPullRequests = new GHPullRequests(token)(pullRequestOps) + val ghPullRequests = new GHPullRequests(sampleToken)(pullRequestOps) ghPullRequests.listFiles(validRepoOwner, validRepoName, validPullRequestNumber) - - verify(pullRequestOps) - .listPullRequestFiles(validRepoOwner, validRepoName, validPullRequestNumber, token) } "GHPullRequests.createPullRequestData" should "call to PullRequestOps with the right parameters" in { @@ -72,21 +58,19 @@ class GHPullRequestsSpec extends FlatSpec with Matchers with TestUtils { val response: Free[GitHub4s, GHResponse[PullRequest]] = Free.pure(Right(GHResult(pullRequest, okStatusCode, Map.empty))) - val pullRequestOps = mock[PullRequestOps[GitHub4s]] - when( - pullRequestOps - .createPullRequest( - any[String], - any[String], - any[NewPullRequest], - any[String], - any[String], - any[Option[Boolean]], - any[Option[String]])) - .thenReturn(response) - - val token = Some("token") - val ghPullRequests = new GHPullRequests(token)(pullRequestOps) + val pullRequestOps = mock[PullRequestOpsTest] + (pullRequestOps.createPullRequest _) + .expects( + validRepoOwner, + validRepoName, + validNewPullRequestData, + validHead, + validBase, + Some(true), + sampleToken) + .returns(response) + + val ghPullRequests = new GHPullRequests(sampleToken)(pullRequestOps) ghPullRequests.create( validRepoOwner, validRepoName, @@ -94,16 +78,6 @@ class GHPullRequestsSpec extends FlatSpec with Matchers with TestUtils { validHead, validBase, Some(true)) - - verify(pullRequestOps) - .createPullRequest( - validRepoOwner, - validRepoName, - validNewPullRequestData, - validHead, - validBase, - Some(true), - token) } "GHPullRequests.createPullRequestIssue" should "call to PullRequestOps with the right parameters" in { @@ -111,21 +85,19 @@ class GHPullRequestsSpec extends FlatSpec with Matchers with TestUtils { val response: Free[GitHub4s, GHResponse[PullRequest]] = Free.pure(Right(GHResult(pullRequest, okStatusCode, Map.empty))) - val pullRequestOps = mock[PullRequestOps[GitHub4s]] - when( - pullRequestOps - .createPullRequest( - any[String], - any[String], - any[NewPullRequest], - any[String], - any[String], - any[Option[Boolean]], - any[Option[String]])) - .thenReturn(response) - - val token = Some("token") - val ghPullRequests = new GHPullRequests(token)(pullRequestOps) + val pullRequestOps = mock[PullRequestOpsTest] + (pullRequestOps.createPullRequest _) + .expects( + validRepoOwner, + validRepoName, + validNewPullRequestIssue, + validHead, + validBase, + Some(true), + sampleToken) + .returns(response) + + val ghPullRequests = new GHPullRequests(sampleToken)(pullRequestOps) ghPullRequests.create( validRepoOwner, validRepoName, @@ -133,15 +105,5 @@ class GHPullRequestsSpec extends FlatSpec with Matchers with TestUtils { validHead, validBase, Some(true)) - - verify(pullRequestOps) - .createPullRequest( - validRepoOwner, - validRepoName, - validNewPullRequestIssue, - validHead, - validBase, - Some(true), - token) } } diff --git a/github4s/jvm/src/test/scala/github4s/unit/GHReposSpec.scala b/github4s/shared/src/test/scala/github4s/unit/GHReposSpec.scala similarity index 54% rename from github4s/jvm/src/test/scala/github4s/unit/GHReposSpec.scala rename to github4s/shared/src/test/scala/github4s/unit/GHReposSpec.scala index c46623739..353e7df6e 100644 --- a/github4s/jvm/src/test/scala/github4s/unit/GHReposSpec.scala +++ b/github4s/shared/src/test/scala/github4s/unit/GHReposSpec.scala @@ -23,39 +23,22 @@ import github4s.GithubResponses.{GHResponse, GHResult} import github4s.app.GitHub4s import github4s.free.algebra.RepositoryOps import github4s.free.domain._ -import github4s.utils.TestUtils -import org.mockito.ArgumentMatchers.any -import org.mockito.Mockito._ -import org.scalatest.mockito.MockitoSugar.mock -import org.scalatest.{FlatSpec, Matchers} +import github4s.utils.BaseSpec -class GHReposSpec extends FlatSpec with Matchers with TestUtils { +class GHReposSpec extends BaseSpec { "GHRepos.contents" should "call to RepositoryOps with the right parameters" in { val response: Free[GitHub4s, GHResponse[NonEmptyList[Content]]] = Free.pure(Right(GHResult(NonEmptyList(content, Nil), okStatusCode, Map.empty))) - val repoOps = mock[RepositoryOps[GitHub4s]] - when( - repoOps.getContents( - any[String], - any[String], - any[String], - any[Option[String]], - any[Option[String]])) - .thenReturn(response) + val repoOps = mock[RepositoryOpsTest] + (repoOps.getContents _) + .expects(validRepoOwner, validRepoName, validFilePath, Some("master"), sampleToken) + .returns(response) - val token = Some("token") - val ghReposData = new GHRepos(token)(repoOps) + val ghReposData = new GHRepos(sampleToken)(repoOps) ghReposData.getContents(validRepoOwner, validRepoName, validFilePath, Some("master")) - - verify(repoOps).getContents( - validRepoOwner, - validRepoName, - validFilePath, - Some("master"), - token) } "GHRepos.createRelease" should "call to RepositoryOps with the right parameters" in { @@ -63,22 +46,21 @@ class GHReposSpec extends FlatSpec with Matchers with TestUtils { val response: Free[GitHub4s, GHResponse[Release]] = Free.pure(Right(GHResult(release, createdStatusCode, Map.empty))) - val repoOps = mock[RepositoryOps[GitHub4s]] - when( - repoOps.createRelease( - any[String], - any[String], - any[String], - any[String], - any[String], - any[Option[String]], - any[Option[Boolean]], - any[Option[Boolean]], - any[Option[String]])) - .thenReturn(response) + val repoOps = mock[RepositoryOpsTest] + (repoOps.createRelease _) + .expects( + validRepoOwner, + validRepoName, + validTagTitle, + validTagTitle, + validNote, + Some("master"), + Some(false), + Some(false), + sampleToken) + .returns(response) - val token = Some("token") - val ghReposData = new GHRepos(token)(repoOps) + val ghReposData = new GHRepos(sampleToken)(repoOps) ghReposData.createRelease( validRepoOwner, validRepoName, @@ -88,17 +70,6 @@ class GHReposSpec extends FlatSpec with Matchers with TestUtils { Some("master"), Some(false), Some(false)) - - verify(repoOps).createRelease( - validRepoOwner, - validRepoName, - validTagTitle, - validTagTitle, - validNote, - Some("master"), - Some(false), - Some(false), - token) } } diff --git a/github4s/jvm/src/test/scala/github4s/unit/GHStatusesSpec.scala b/github4s/shared/src/test/scala/github4s/unit/GHStatusesSpec.scala similarity index 53% rename from github4s/jvm/src/test/scala/github4s/unit/GHStatusesSpec.scala rename to github4s/shared/src/test/scala/github4s/unit/GHStatusesSpec.scala index cf619e021..95efae7a3 100644 --- a/github4s/jvm/src/test/scala/github4s/unit/GHStatusesSpec.scala +++ b/github4s/shared/src/test/scala/github4s/unit/GHStatusesSpec.scala @@ -20,64 +20,52 @@ import cats.free.Free import github4s.GHStatuses import github4s.GithubResponses.{GHResponse, GHResult} import github4s.app.GitHub4s -import github4s.free.algebra.StatusOps import github4s.free.domain.{CombinedStatus, Status} -import github4s.utils.TestUtils -import org.mockito.ArgumentMatchers.any -import org.mockito.Mockito._ -import org.scalatest.{FlatSpec, Matchers} -import org.scalatest.mockito.MockitoSugar.mock +import github4s.utils.BaseSpec + +class GHStatusesSpec extends BaseSpec { -class GHStatusesSpec extends FlatSpec with Matchers with TestUtils { "GHStatuses.get" should "call to StatusesOps with the right parameters" in { val response: Free[GitHub4s, GHResponse[CombinedStatus]] = Free.pure(Right(GHResult(combinedStatus, okStatusCode, Map.empty))) - val statusesOps = mock[StatusOps[GitHub4s]] - when(statusesOps.getCombinedStatus(any[String], any[String], any[String], any[Option[String]])) - .thenReturn(response) - - val token = Some("token") - val ghStatuses = new GHStatuses(token)(statusesOps) + val statusesOps = mock[StatusOpsTest] + (statusesOps.getCombinedStatus _) + .expects(validRepoOwner, validRepoName, validRefSingle, sampleToken) + .returns(response) + val ghStatuses = new GHStatuses(sampleToken)(statusesOps) ghStatuses.getCombinedStatus(validRepoOwner, validRepoName, validRefSingle) - - verify(statusesOps).getCombinedStatus(validRepoOwner, validRepoName, validRefSingle, token) } "GHStatuses.list" should "call to StatusesOps with the right parameters" in { val response: Free[GitHub4s, GHResponse[List[Status]]] = Free.pure(Right(GHResult(List(status), okStatusCode, Map.empty))) - val statusesOps = mock[StatusOps[GitHub4s]] - when(statusesOps.listStatuses(any[String], any[String], any[String], any[Option[String]])) - .thenReturn(response) - - val token = Some("token") - val ghStatuses = new GHStatuses(token)(statusesOps) + val statusesOps = mock[StatusOpsTest] + (statusesOps.listStatuses _) + .expects(validRepoOwner, validRepoName, validRefSingle, sampleToken) + .returns(response) + val ghStatuses = new GHStatuses(sampleToken)(statusesOps) ghStatuses.listStatuses(validRepoOwner, validRepoName, validRefSingle) - - verify(statusesOps).listStatuses(validRepoOwner, validRepoName, validRefSingle, token) } "GHStatuses.create" should "call to StatusesOps with the right parameters" in { val response: Free[GitHub4s, GHResponse[Status]] = Free.pure(Right(GHResult(status, createdStatusCode, Map.empty))) - val statusesOps = mock[StatusOps[GitHub4s]] - when( - statusesOps.createStatus( - any[String], - any[String], - any[String], - any[String], - any[Option[String]], - any[Option[String]], - any[Option[String]], - any[Option[String]])) - .thenReturn(response) - - val token = Some("token") - val ghStatuses = new GHStatuses(token)(statusesOps) + val statusesOps = mock[StatusOpsTest] + (statusesOps.createStatus _) + .expects( + validRepoOwner, + validRepoName, + validCommitSha, + validStatusState, + None, + None, + None, + sampleToken) + .returns(response) + val ghStatuses = new GHStatuses(sampleToken)(statusesOps) ghStatuses .createStatus( validRepoOwner, @@ -87,15 +75,5 @@ class GHStatusesSpec extends FlatSpec with Matchers with TestUtils { None, None, None) - - verify(statusesOps).createStatus( - validRepoOwner, - validRepoName, - validCommitSha, - validStatusState, - None, - None, - None, - token) } } diff --git a/github4s/shared/src/test/scala/github4s/unit/GitDataSpec.scala b/github4s/shared/src/test/scala/github4s/unit/GitDataSpec.scala new file mode 100644 index 000000000..a02d97620 --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/unit/GitDataSpec.scala @@ -0,0 +1,271 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package github4s.unit + +import cats.Id +import cats.data.NonEmptyList +import github4s.GithubResponses.{GHResponse, GHResult} +import github4s.HttpClient +import github4s.api.GitData +import github4s.free.domain._ +import github4s.utils.BaseSpec + +class GitDataSpec extends BaseSpec { + + "GitData.reference" should "call to httpClient.get with the right parameters" in { + + val response: GHResponse[NonEmptyList[Ref]] = + Right(GHResult(NonEmptyList(ref, Nil), okStatusCode, Map.empty)) + + val httpClientMock = httpClientMockGet[NonEmptyList[Ref]]( + url = s"repos/$validRepoOwner/$validRepoName/git/refs/$validRefSingle", + response = response + ) + + val gitData = new GitData[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + gitData.reference(sampleToken, headerUserAgent, validRepoOwner, validRepoName, validRefSingle) + } + + "GitData.createReference" should "call to httpClient.post with the right parameters" in { + + val response: GHResponse[Ref] = + Right(GHResult(ref, okStatusCode, Map.empty)) + val request = + s""" + |{ + | "ref": "refs/$validRefSingle", + | "sha": "$validCommitSha" + |} + """.stripMargin + val httpClientMock = httpClientMockPost[Ref]( + url = s"repos/$validRepoOwner/$validRepoName/git/refs", + json = request, + response = response + ) + + val gitData = new GitData[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + gitData.createReference( + sampleToken, + headerUserAgent, + validRepoOwner, + validRepoName, + s"refs/$validRefSingle", + validCommitSha) + } + + "GitData.updateReference" should "call to httpClient.patch with the right parameters" in { + + val response: GHResponse[Ref] = + Right(GHResult(ref, okStatusCode, Map.empty)) + val force = Some(false) + val request = + s""" + |{ + | "sha": "$validCommitSha" + | ${force.map(f => ",\"force\": " + f).getOrElse(" ")} + |} + """.stripMargin + + val httpClientMock = httpClientMockPatch[Ref]( + url = s"repos/$validRepoOwner/$validRepoName/git/refs/$validRefSingle", + json = request, + response = response + ) + + val gitData = new GitData[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + gitData.updateReference( + sampleToken, + headerUserAgent, + validRepoOwner, + validRepoName, + validRefSingle, + validCommitSha, + force) + } + + "GitData.getCommit" should "call to httpClient.get with the right parameters" in { + + val response: GHResponse[RefCommit] = + Right(GHResult(refCommit, okStatusCode, Map.empty)) + val httpClientMock = httpClientMockGet[RefCommit]( + url = s"repos/$validRepoOwner/$validRepoName/git/commits/$validCommitSha", + response = response + ) + val gitData = new GitData[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + + gitData.commit(sampleToken, headerUserAgent, validRepoOwner, validRepoName, validCommitSha) + } + + "GitData.createCommit" should "call to httpClient.post with the right parameters" in { + + val response: GHResponse[RefCommit] = + Right(GHResult(refCommit, okStatusCode, Map.empty)) + val request = + s""" + |{ + | "message": "$validNote", + | "tree": "$validTreeSha", + | "parents": ["$validCommitSha"], + | "author": { + | "name": "${refCommitAuthor.name}", + | "email": "${refCommitAuthor.email}", + | "date": "${refCommitAuthor.date}" + | } + |} + """.stripMargin + val httpClientMock = httpClientMockPost[RefCommit]( + url = s"repos/$validRepoOwner/$validRepoName/git/commits", + json = request, + response = response + ) + val gitData = new GitData[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + gitData.createCommit( + sampleToken, + headerUserAgent, + validRepoOwner, + validRepoName, + validNote, + validTreeSha, + List(validCommitSha), + Some(refCommitAuthor)) + } + + "GitData.createBlob" should "call to httpClient.post with the right parameters" in { + + val response: GHResponse[RefInfo] = + Right(GHResult(new RefInfo(validCommitSha, githubApiUrl), okStatusCode, Map.empty)) + val request = + s""" + |{ + | "content": "$validNote" + | ${encoding.map(e => ",\"encoding\": \"" + e + "\"").getOrElse(" ")} + |} + """.stripMargin + val httpClientMock = httpClientMockPost[RefInfo]( + url = s"repos/$validRepoOwner/$validRepoName/git/blobs", + json = request, + response = response + ) + val gitData = new GitData[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + gitData.createBlob( + sampleToken, + headerUserAgent, + validRepoOwner, + validRepoName, + validNote, + encoding) + } + + "GitData.createTree" should "call to httpClient.post with the right parameters" in { + + val response: GHResponse[TreeResult] = + Right( + GHResult( + TreeResult(validCommitSha, githubApiUrl, treeDataResult), + okStatusCode, + Map.empty)) + def treeDataJson(treeData: TreeData): String = { + + def attr: String = treeData match { + case d: TreeDataSha => "\"sha\": \"" + d.sha + "\"" + case d: TreeDataBlob => "\"content\": \"" + d.content + "\"" + } + + s""" + |{ + | "path": "${treeData.path}", + | "mode": "${treeData.mode}", + | "type": "${treeData.`type`}", + | $attr + |} + """.stripMargin + } + + val request = + s""" + |{ + | "base_tree": "$validTreeSha", + | "tree": [ ${treeDataList.map(treeDataJson).mkString(",")} ] + |} + """.stripMargin + val httpClientMock = httpClientMockPost[TreeResult]( + url = s"repos/$validRepoOwner/$validRepoName/git/trees", + json = request, + response = response + ) + val gitData = new GitData[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + gitData.createTree( + sampleToken, + headerUserAgent, + validRepoOwner, + validRepoName, + Some(validTreeSha), + treeDataList) + } + + "GitData.createTag" should "call to httpClient.post with the right parameters" in { + + val response: GHResponse[Tag] = Right(GHResult(tag, okStatusCode, Map.empty)) + val request = + s""" + |{ + | "tag": "$validTagTitle", + | "message": "$validNote", + | "object": "$validCommitSha", + | "type": "$commitType", + | "tagger": { + | "name": "${refCommitAuthor.name}", + | "email": "${refCommitAuthor.email}", + | "date": "${refCommitAuthor.date}" + | } + |} + """.stripMargin + val httpClientMock = httpClientMockPost[Tag]( + url = s"repos/$validRepoOwner/$validRepoName/git/tags", + json = request, + response = response + ) + val gitData = new GitData[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + gitData.createTag( + sampleToken, + headerUserAgent, + validRepoOwner, + validRepoName, + validTagTitle, + validNote, + validCommitSha, + commitType, + Some(refCommitAuthor)) + } + +} diff --git a/github4s/shared/src/test/scala/github4s/unit/PullRequestsSpec.scala b/github4s/shared/src/test/scala/github4s/unit/PullRequestsSpec.scala new file mode 100644 index 000000000..0a0f7ba4a --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/unit/PullRequestsSpec.scala @@ -0,0 +1,64 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package github4s.unit + +import cats.Id +import github4s.GithubResponses.{GHResponse, GHResult} +import github4s.HttpClient +import github4s.api.PullRequests +import github4s.free.domain._ +import github4s.utils.BaseSpec + +class PullRequestsSpec extends BaseSpec { + + "PullRequests.list" should "call to httpClient.get with the right parameters" in { + + val response: GHResponse[List[PullRequest]] = + Right(GHResult(List(pullRequest), okStatusCode, Map.empty)) + + val httpClientMock = httpClientMockGet[List[PullRequest]]( + url = s"repos/$validRepoOwner/$validRepoName/pulls", + response = response + ) + val pullRequests = new PullRequests[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + pullRequests.list(sampleToken, headerUserAgent, validRepoOwner, validRepoName, Nil) + } + + "PullRequests.listFiles" should "call to httpClient.get with the right parameters" in { + + val response: GHResponse[List[PullRequestFile]] = + Right(GHResult(List(pullRequestFile), okStatusCode, Map.empty)) + + val httpClientMock = httpClientMockGet[List[PullRequestFile]]( + url = s"repos/$validRepoOwner/$validRepoName/pulls/$validPullRequestNumber/files", + response = response + ) + val pullRequests = new PullRequests[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + pullRequests + .listFiles( + sampleToken, + headerUserAgent, + validRepoOwner, + validRepoName, + validPullRequestNumber) + } + +} diff --git a/github4s/shared/src/test/scala/github4s/unit/ReposSpec.scala b/github4s/shared/src/test/scala/github4s/unit/ReposSpec.scala new file mode 100644 index 000000000..a4e823f49 --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/unit/ReposSpec.scala @@ -0,0 +1,90 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package github4s.unit + +import cats.Id +import cats.data.NonEmptyList +import github4s.GithubResponses.{GHResponse, GHResult} +import github4s.HttpClient +import github4s.api.Repos +import github4s.free.domain._ +import github4s.utils.BaseSpec + +class ReposSpec extends BaseSpec { + + "Repos.getContents" should "call to httpClient.get with the right parameters" in { + + val response: GHResponse[NonEmptyList[Content]] = + Right(GHResult(NonEmptyList(content, Nil), okStatusCode, Map.empty)) + + val httpClientMock = httpClientMockGet[NonEmptyList[Content]]( + url = s"repos/$validRepoOwner/$validRepoName/contents/$validFilePath", + params = Map("ref" -> "master"), + response = response + ) + + val repos = new Repos[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + repos.getContents( + accessToken = sampleToken, + headers = headerUserAgent, + owner = validRepoOwner, + repo = validRepoName, + path = validFilePath, + ref = Some("master") + ) + } + + "Repos.createRelease" should "call to httpClient.post with the right parameters" in { + + val response: GHResponse[Release] = Right(GHResult(release, okStatusCode, Map.empty)) + val request = + s""" + |{ + | "tag_name": "$validTagTitle", + | "target_commitish": "master", + | "name": "$validTagTitle", + | "body": "$validNote", + | "draft": false, + | "prerelease": true + |} + """.stripMargin + val httpClientMock = httpClientMockPost[Release]( + url = s"repos/$validRepoOwner/$validRepoName/releases", + json = request, + response = response + ) + + val repos = new Repos[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + repos.createRelease( + accessToken = sampleToken, + headers = headerUserAgent, + owner = validRepoOwner, + repo = validRepoName, + tagName = validTagTitle, + name = validTagTitle, + body = validNote, + targetCommitish = Some("master"), + draft = Some(false), + prerelease = Some(true) + ) + } + +} diff --git a/github4s/shared/src/test/scala/github4s/unit/StatusesSpec.scala b/github4s/shared/src/test/scala/github4s/unit/StatusesSpec.scala new file mode 100644 index 000000000..d488c165f --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/unit/StatusesSpec.scala @@ -0,0 +1,80 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package github4s.unit + +import cats.Id +import github4s.GithubResponses.{GHResponse, GHResult} +import github4s.HttpClient +import github4s.api.Statuses +import github4s.free.domain._ +import github4s.utils.BaseSpec + +class StatusesSpec extends BaseSpec { + + "Statuses.get" should "call httpClient.get with the right parameters" in { + val response: GHResponse[CombinedStatus] = + Right(GHResult(combinedStatus, okStatusCode, Map.empty)) + + val httpClientMock = httpClientMockGet[CombinedStatus]( + url = s"repos/$validRepoOwner/$validRepoName/commits/$validRefSingle/status", + response = response + ) + + val statuses = new Statuses[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + statuses.get(sampleToken, headerUserAgent, validRepoOwner, validRepoName, validRefSingle) + } + + "Statuses.list" should "call htppClient.get with the right parameters" in { + val response: GHResponse[List[Status]] = Right(GHResult(List(status), okStatusCode, Map.empty)) + + val httpClientMock = httpClientMockGet[List[Status]]( + url = s"repos/$validRepoOwner/$validRepoName/commits/$validRefSingle/statuses", + response = response + ) + + val statuses = new Statuses[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + statuses.list(sampleToken, headerUserAgent, validRepoOwner, validRepoName, validRefSingle) + } + + "Statuses.create" should "call httpClient.post with the right parameters" in { + val response: GHResponse[Status] = Right(GHResult(status, createdStatusCode, Map.empty)) + + val httpClientMock = httpClientMockPost[Status]( + url = s"repos/$validRepoOwner/$validRepoName/statuses/$validCommitSha", + json = s"""{"state":"$validStatusState"}""", + response = response + ) + + val statuses = new Statuses[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + statuses.create( + sampleToken, + headerUserAgent, + validRepoOwner, + validRepoName, + validCommitSha, + validStatusState, + None, + None, + None) + } +} diff --git a/github4s/shared/src/test/scala/github4s/utils/BaseSpec.scala b/github4s/shared/src/test/scala/github4s/utils/BaseSpec.scala new file mode 100644 index 000000000..2b807115f --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/utils/BaseSpec.scala @@ -0,0 +1,93 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package github4s.utils + +import cats.Id +import github4s.GithubResponses.{GHResponse, UnexpectedException} +import github4s.app.GitHub4s +import github4s.free.algebra.{GitDataOps, PullRequestOps, RepositoryOps, StatusOps} +import github4s.free.domain.Pagination +import github4s.{HttpClient, HttpRequestBuilder, HttpRequestBuilderExtension, IdInstances} +import io.circe +import io.circe.Decoder +import io.circe.parser.parse +import org.scalamock.matchers.MockParameter +import org.scalamock.scalatest.MockFactory +import org.scalatest.{FlatSpec, Matchers} + +trait BaseSpec extends FlatSpec with Matchers with TestData with IdInstances with MockFactory { + + case class JsonMockParameter(json: String) extends MockParameter[String](json) { + override def equals(argument: Any): Boolean = parse(json) == parse(argument.toString) + } + + class HttpClientTest extends HttpClient[String, Id] + + implicit def httpRequestBuilderExtension: HttpRequestBuilderExtension[String, Id] = + new HttpRequestBuilderExtension[String, Id] { + override def run[A](rb: HttpRequestBuilder[String, Id])( + implicit D: circe.Decoder[A]): Id[GHResponse[A]] = + Left(UnexpectedException("Stub!")) + } + + def httpClientMockGet[T]( + url: String, + params: Map[String, String] = Map.empty, + response: GHResponse[T]): HttpClient[String, Id] = { + val httpClientMock = mock[HttpClientTest] + (httpClientMock + .get[T]( + _: Option[String], + _: String, + _: Map[String, String], + _: Map[String, String], + _: Option[Pagination])(_: Decoder[T])) + .expects(sampleToken, url, headerUserAgent, params, *, *) + .returns(response) + httpClientMock + } + + def httpClientMockPost[T]( + url: String, + json: String, + response: GHResponse[T]): HttpClient[String, Id] = { + val httpClientMock = mock[HttpClientTest] + (httpClientMock + .post[T](_: Option[String], _: String, _: Map[String, String], _: String)(_: Decoder[T])) + .expects(sampleToken, url, headerUserAgent, JsonMockParameter(json), *) + .returns(response) + httpClientMock + } + + def httpClientMockPatch[T]( + url: String, + json: String, + response: GHResponse[T]): HttpClient[String, Id] = { + val httpClientMock = mock[HttpClientTest] + (httpClientMock + .patch[T](_: Option[String], _: String, _: Map[String, String], _: String)(_: Decoder[T])) + .expects(sampleToken, url, headerUserAgent, JsonMockParameter(json), *) + .returns(response) + httpClientMock + } + + class GitDataOpsTest extends GitDataOps[GitHub4s] + class PullRequestOpsTest extends PullRequestOps[GitHub4s] + class RepositoryOpsTest extends RepositoryOps[GitHub4s] + class StatusOpsTest extends StatusOps[GitHub4s] + +} diff --git a/github4s/shared/src/test/scala/github4s/utils/TestData.scala b/github4s/shared/src/test/scala/github4s/utils/TestData.scala index eb3b7976c..f08150800 100644 --- a/github4s/shared/src/test/scala/github4s/utils/TestData.scala +++ b/github4s/shared/src/test/scala/github4s/utils/TestData.scala @@ -18,16 +18,11 @@ package github4s.utils import com.github.marklister.base64.Base64._ import github4s.free.domain._ -import io.circe.parser.parse -import org.mockito.ArgumentMatcher -trait TestData { +trait TestData extends DummyGithubUrls { - case class JsonArgMatcher(json: String) extends ArgumentMatcher[String] { - override def matches(argument: String): Boolean = parse(json) == parse(argument) - } - - val headerUserAgent = Map("user-agent" -> "github4s") + val sampleToken: Option[String] = Some("token") + val headerUserAgent: Map[String, String] = Map("user-agent" -> "github4s") val validUsername = "rafaparadela" val invalidUsername = "GHInvalidaUserName" diff --git a/project/ProjectPlugin.scala b/project/ProjectPlugin.scala index ba4607dc7..9952711dd 100644 --- a/project/ProjectPlugin.scala +++ b/project/ProjectPlugin.scala @@ -41,14 +41,12 @@ object ProjectPlugin extends AutoPlugin { %%("circe-generic"), %%("circe-parser"), %%("base64"), + "org.scalamock" %% "scalamock-scalatest-support" % "3.5.0" % Test, %%("scalatest") % "test" ) lazy val standardCommonDeps = Seq( - libraryDependencies ++= Seq( - %("mockito-core") % "test", - compilerPlugin(%%("paradise") cross CrossVersion.full) - ) + libraryDependencies += compilerPlugin(%%("paradise") cross CrossVersion.full) ) lazy val jvmDeps = Seq( From a01ca2ae5191219a3c0289a2e7dac88396cd0819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fede=20Fern=C3=A1ndez?= Date: Thu, 4 May 2017 09:50:39 +0200 Subject: [PATCH 3/5] Moves integration tests to shared proj --- .../integration/IntegrationSpec.scala | 46 +++++++ .../github4s/integration/GHAuthSpec.scala | 66 ---------- .../github4s/integration/GHGistsSpec.scala | 45 ------- .../github4s/integration/GHGitDataSpec.scala | 66 ---------- .../github4s/integration/GHIssuesSpec.scala | 90 -------------- .../integration/GHPullRequestsSpec.scala | 91 -------------- .../github4s/integration/GHReposSpec.scala | 114 ------------------ .../github4s/integration/GHStatusesSpec.scala | 104 ---------------- .../github4s/integration/GHUsersSpec.scala | 83 ------------- .../integration/IntegrationSpec.scala | 47 ++++++++ .../github4s/integration/GHAuthSpec.scala | 20 ++- .../github4s/integration/GHGistsSpec.scala | 16 +-- .../github4s/integration/GHGitDataSpec.scala | 20 ++- .../github4s/integration/GHIssuesSpec.scala | 18 ++- .../integration/GHPullRequestsSpec.scala | 22 ++-- .../github4s/integration/GHReposSpec.scala | 30 ++--- .../github4s/integration/GHStatusesSpec.scala | 24 ++-- .../github4s/integration/GHUsersSpec.scala | 25 ++-- .../github4s/utils/BaseIntegrationSpec.scala | 53 ++++++++ 19 files changed, 210 insertions(+), 770 deletions(-) create mode 100644 github4s/js/src/test/scala/github4s/integration/IntegrationSpec.scala delete mode 100644 github4s/jvm/src/test/scala/github4s/integration/GHAuthSpec.scala delete mode 100644 github4s/jvm/src/test/scala/github4s/integration/GHGistsSpec.scala delete mode 100644 github4s/jvm/src/test/scala/github4s/integration/GHGitDataSpec.scala delete mode 100644 github4s/jvm/src/test/scala/github4s/integration/GHIssuesSpec.scala delete mode 100644 github4s/jvm/src/test/scala/github4s/integration/GHPullRequestsSpec.scala delete mode 100644 github4s/jvm/src/test/scala/github4s/integration/GHReposSpec.scala delete mode 100644 github4s/jvm/src/test/scala/github4s/integration/GHStatusesSpec.scala delete mode 100644 github4s/jvm/src/test/scala/github4s/integration/GHUsersSpec.scala create mode 100644 github4s/jvm/src/test/scala/github4s/integration/IntegrationSpec.scala rename github4s/{js => shared}/src/test/scala/github4s/integration/GHAuthSpec.scala (79%) rename github4s/{js => shared}/src/test/scala/github4s/integration/GHGistsSpec.scala (76%) rename github4s/{js => shared}/src/test/scala/github4s/integration/GHGitDataSpec.scala (79%) rename github4s/{js => shared}/src/test/scala/github4s/integration/GHIssuesSpec.scala (85%) rename github4s/{js => shared}/src/test/scala/github4s/integration/GHPullRequestsSpec.scala (81%) rename github4s/{js => shared}/src/test/scala/github4s/integration/GHReposSpec.scala (82%) rename github4s/{js => shared}/src/test/scala/github4s/integration/GHStatusesSpec.scala (83%) rename github4s/{js => shared}/src/test/scala/github4s/integration/GHUsersSpec.scala (74%) create mode 100644 github4s/shared/src/test/scala/github4s/utils/BaseIntegrationSpec.scala diff --git a/github4s/js/src/test/scala/github4s/integration/IntegrationSpec.scala b/github4s/js/src/test/scala/github4s/integration/IntegrationSpec.scala new file mode 100644 index 000000000..a074e437f --- /dev/null +++ b/github4s/js/src/test/scala/github4s/integration/IntegrationSpec.scala @@ -0,0 +1,46 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package github4s.integration + +import fr.hmil.roshttp.response.SimpleHttpResponse +import github4s.HttpRequestBuilderExtension +import github4s.free.interpreters.{Capture, Interpreters} +import github4s.js.ImplicitsJS +import github4s.utils.{BaseIntegrationSpec, TestUtils} + +import scala.concurrent.Future + +class IntegrationSpec + extends BaseIntegrationSpec[SimpleHttpResponse] + with GHAuthSpec[SimpleHttpResponse] + with GHGistsSpec[SimpleHttpResponse] + with GHGitDataSpec[SimpleHttpResponse] + with GHIssuesSpec[SimpleHttpResponse] + with GHPullRequestsSpec[SimpleHttpResponse] + with GHReposSpec[SimpleHttpResponse] + with GHStatusesSpec[SimpleHttpResponse] + with GHUsersSpec[SimpleHttpResponse] + with ImplicitsJS + with TestUtils { + + override implicit def extension( + implicit capture: Capture[Future]): HttpRequestBuilderExtension[SimpleHttpResponse, Future] = + extensionJS + + override implicit def futureInterpreters: Interpreters[Future, SimpleHttpResponse] = + intInstanceFutureRosHttp +} diff --git a/github4s/jvm/src/test/scala/github4s/integration/GHAuthSpec.scala b/github4s/jvm/src/test/scala/github4s/integration/GHAuthSpec.scala deleted file mode 100644 index e5b79fce1..000000000 --- a/github4s/jvm/src/test/scala/github4s/integration/GHAuthSpec.scala +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.integration - -import cats.Id -import cats.implicits._ -import github4s.Github._ -import github4s.Github -import github4s.utils.TestUtils -import org.scalatest._ -import github4s.jvm.Implicits._ -import scalaj.http._ - -class GHAuthSpec extends FlatSpec with Matchers with TestUtils { - - "Auth >> NewAuth" should "return error on Left when invalid credential is provided" in { - val response = Github().auth - .newAuth( - validUsername, - invalidPassword, - validScopes, - validNote, - validClientId, - invalidClientSecret) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('left) - } - - "Auth >> AuthorizeUrl" should "return the expected URL for valid username" in { - val response = - Github().auth - .authorizeUrl(validClientId, validRedirectUri, validScopes) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - - response.toOption map { r ⇒ - r.result.url.contains(validRedirectUri) shouldBe true - r.statusCode shouldBe okStatusCode - } - - } - - "Auth >> GetAccessToken" should "return error on Left for invalid code value" in { - val response = Github().auth - .getAccessToken(validClientId, invalidClientSecret, "", validRedirectUri, "") - .exec[Id, HttpResponse[String]](headerUserAgent) - response should be('left) - } - -} diff --git a/github4s/jvm/src/test/scala/github4s/integration/GHGistsSpec.scala b/github4s/jvm/src/test/scala/github4s/integration/GHGistsSpec.scala deleted file mode 100644 index 2b18daefe..000000000 --- a/github4s/jvm/src/test/scala/github4s/integration/GHGistsSpec.scala +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.integration - -import cats.Id -import cats.implicits._ -import github4s.Github._ -import github4s.free.domain.GistFile -import github4s.Github -import github4s.utils.TestUtils -import github4s.jvm.Implicits._ -import org.scalatest._ - -import scalaj.http.HttpResponse - -class GHGistsSpec extends FlatSpec with Matchers with TestUtils { - "Gists >> Post" should "return the provided gist" in { - val response = Github(accessToken).gists - .newGist( - validGistDescription, - validGistPublic, - Map(validGistFilename -> GistFile(validGistFileContent))) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - response.toOption map { r ⇒ - r.result.description shouldBe validGistDescription - r.statusCode shouldBe createdStatusCode - } - } -} diff --git a/github4s/jvm/src/test/scala/github4s/integration/GHGitDataSpec.scala b/github4s/jvm/src/test/scala/github4s/integration/GHGitDataSpec.scala deleted file mode 100644 index fc2d4355d..000000000 --- a/github4s/jvm/src/test/scala/github4s/integration/GHGitDataSpec.scala +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.integration - -import cats.Id -import cats.implicits._ -import github4s.Github -import github4s.Github._ -import github4s.jvm.Implicits._ -import github4s.utils.TestUtils -import org.scalatest._ - -import scalaj.http.HttpResponse - -class GHGitDataSpec extends FlatSpec with Matchers with TestUtils { - - "GitData >> GetReference" should "return a list of references" in { - val response = Github(accessToken).gitData - .getReference(validRepoOwner, validRepoName, "heads") - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - response.toOption map { r ⇒ - r.result.tail.nonEmpty shouldBe true - r.statusCode shouldBe okStatusCode - } - } - - it should "return at least one reference" in { - val response = Github(accessToken).gitData - .getReference(validRepoOwner, validRepoName, validRefSingle) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - response.toOption map { r ⇒ - r.result.head.ref.contains(validRefSingle) shouldBe true - r.statusCode shouldBe okStatusCode - } - } - - "GitData >> GetCommit" should "return one commit" in { - val response = Github(accessToken).gitData - .getCommit(validRepoOwner, validRepoName, validCommitSha) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - response.toOption map { r ⇒ - r.result.message shouldBe validCommitMsg - r.statusCode shouldBe okStatusCode - } - } -} diff --git a/github4s/jvm/src/test/scala/github4s/integration/GHIssuesSpec.scala b/github4s/jvm/src/test/scala/github4s/integration/GHIssuesSpec.scala deleted file mode 100644 index 26d010def..000000000 --- a/github4s/jvm/src/test/scala/github4s/integration/GHIssuesSpec.scala +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.integration - -import cats.Id -import cats.implicits._ -import github4s.Github -import github4s.Github._ -import github4s.jvm.Implicits._ -import github4s.utils.TestUtils -import org.scalatest._ - -import scalaj.http.HttpResponse - -class GHIssuesSpec extends FlatSpec with Matchers with TestUtils { - - "Issues >> List" should "return a list of issues" in { - val response = Github(accessToken).issues - .listIssues(validRepoOwner, validRepoName) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - response.toOption map { r ⇒ - r.result.nonEmpty shouldBe true - r.statusCode shouldBe okStatusCode - } - } - - "Issues >> Search" should "return at least one issue for a valid query" in { - val response = Github(accessToken).issues - .searchIssues(validSearchQuery, validSearchParams) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - response.toOption map { r ⇒ - r.result.total_count > 0 shouldBe true - r.result.items.nonEmpty shouldBe true - r.statusCode shouldBe okStatusCode - } - } - - it should "return an empty result for a non existent query string" in { - val response = Github(accessToken).issues - .searchIssues(nonExistentSearchQuery, validSearchParams) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - response.toOption map { r ⇒ - r.result.total_count shouldBe 0 - r.result.items.nonEmpty shouldBe false - r.statusCode shouldBe okStatusCode - } - } - - "Issues >> Edit" should "edit the specified issue" in { - val response = Github(accessToken).issues - .editIssue( - validRepoOwner, - validRepoName, - validIssue, - validIssueState, - validIssueTitle, - validIssueBody, - None, - validIssueLabel, - validAssignees) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - response.toOption map { r ⇒ - r.result.state shouldBe validIssueState - r.result.title shouldBe validIssueTitle - r.statusCode shouldBe okStatusCode - } - } -} diff --git a/github4s/jvm/src/test/scala/github4s/integration/GHPullRequestsSpec.scala b/github4s/jvm/src/test/scala/github4s/integration/GHPullRequestsSpec.scala deleted file mode 100644 index d62f771e3..000000000 --- a/github4s/jvm/src/test/scala/github4s/integration/GHPullRequestsSpec.scala +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.integration - -import cats.Id -import cats.implicits._ -import github4s.Github -import github4s.Github._ -import github4s.free.domain.{PRFilterAll, PRFilterBase, PRFilterOrderAsc, PRFilterSortCreated} -import github4s.jvm.Implicits._ -import github4s.utils.TestUtils -import org.scalatest.{FlatSpec, Matchers} - -import scalaj.http.HttpResponse - -class GHPullRequestsSpec extends FlatSpec with Matchers with TestUtils { - - "PullRequests >> List" should "return a right response when a valid repo is provided" in { - val response = - Github(accessToken).pullRequests - .list(validRepoOwner, validRepoName) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - response.toOption map { r ⇒ - r.statusCode shouldBe okStatusCode - } - } - - it should "return a non empty list when a valid repo and some filters are provided" in { - val response = - Github(accessToken).pullRequests - .list( - validRepoOwner, - validRepoName, - List(PRFilterAll, PRFilterSortCreated, PRFilterOrderAsc, PRFilterBase("master"))) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - response.toOption map { r ⇒ - r.result.nonEmpty shouldBe true - r.statusCode shouldBe okStatusCode - } - } - - it should "return error when an invalid repo name is passed" in { - val response = - Github(accessToken).pullRequests - .list(validRepoOwner, invalidRepoName) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('left) - } - - "PullRequests >> ListFiles" should "return a right response when a valid repo is provided" in { - val response = - Github(accessToken).pullRequests - .listFiles(validRepoOwner, validRepoName, validPullRequestNumber) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - response.toOption map { r ⇒ - r.result.nonEmpty shouldBe true - r.statusCode shouldBe okStatusCode - } - } - - it should "return error when an invalid repo name is passed" in { - val response = - Github(accessToken).pullRequests - .listFiles(validRepoOwner, invalidRepoName, validPullRequestNumber) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('left) - } - -} diff --git a/github4s/jvm/src/test/scala/github4s/integration/GHReposSpec.scala b/github4s/jvm/src/test/scala/github4s/integration/GHReposSpec.scala deleted file mode 100644 index 8eda0ee0e..000000000 --- a/github4s/jvm/src/test/scala/github4s/integration/GHReposSpec.scala +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.integration - -import cats.Id -import cats.implicits._ -import github4s.Github._ -import github4s.Github -import github4s.jvm.Implicits._ -import github4s.utils.TestUtils -import org.scalatest.{FlatSpec, Matchers} - -import scalaj.http.HttpResponse - -class GHReposSpec extends FlatSpec with Matchers with TestUtils { - - "Repos >> Get" should "return the expected name when a valid repo is provided" in { - - val response = - Github(accessToken).repos - .get(validRepoOwner, validRepoName) - .exec[Id, HttpResponse[String]](headerUserAgent) - response should be('right) - response.toOption map { r ⇒ - r.result.name shouldBe validRepoName - r.statusCode shouldBe okStatusCode - } - } - - it should "return error when an invalid repo name is passed" in { - val response = - Github(accessToken).repos - .get(validRepoOwner, invalidRepoName) - .exec[Id, HttpResponse[String]](headerUserAgent) - response should be('left) - } - - "Repos >> GetContents" should "return the expected contents when valid path is provided" in { - - val response = - Github(accessToken).repos - .getContents(validRepoOwner, validRepoName, validFilePath) - .exec[Id, HttpResponse[String]](headerUserAgent) - response should be('right) - response.toOption map { r ⇒ - r.result.head.path shouldBe validFilePath - r.statusCode shouldBe okStatusCode - } - } - - it should "return error when an invalid path is passed" in { - val response = - Github(accessToken).repos - .getContents(validRepoOwner, validRepoName, invalidFilePath) - .exec[Id, HttpResponse[String]](headerUserAgent) - response should be('left) - } - - "Repos >> ListCommits" should "return the expected list of commits for valid data" in { - val response = Github(accessToken).repos - .listCommits(validRepoOwner, validRepoName) - .exec[Id, HttpResponse[String]](headerUserAgent) - response should be('right) - - response.toOption map { r ⇒ - r.result.nonEmpty shouldBe true - r.statusCode shouldBe okStatusCode - } - } - - it should "return error for invalid repo name" in { - val response = Github(accessToken).repos - .listCommits(invalidRepoName, validRepoName) - .exec[Id, HttpResponse[String]](headerUserAgent) - response should be('left) - } - - "Repos >> ListContributors" should "return the expected list of contributors for valid data" in { - val response = - Github(accessToken).repos - .listContributors(validRepoOwner, validRepoName) - .exec[Id, HttpResponse[String]](headerUserAgent) - response should be('right) - - response.toOption map { r ⇒ - r.result shouldNot be(empty) - r.statusCode shouldBe okStatusCode - } - - } - - it should "return error for invalid repo name" in { - val response = - Github(accessToken).repos - .listContributors(invalidRepoName, validRepoName) - .exec[Id, HttpResponse[String]](headerUserAgent) - response should be('left) - } - -} diff --git a/github4s/jvm/src/test/scala/github4s/integration/GHStatusesSpec.scala b/github4s/jvm/src/test/scala/github4s/integration/GHStatusesSpec.scala deleted file mode 100644 index 196013a9b..000000000 --- a/github4s/jvm/src/test/scala/github4s/integration/GHStatusesSpec.scala +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.integration - -import cats.Id -import cats.implicits._ -import github4s.Github -import github4s.Github._ -import github4s.jvm.Implicits._ -import github4s.utils.TestUtils -import org.scalatest._ - -import scalaj.http.HttpResponse - -class GHStatusesSpec extends FlatSpec with Matchers with TestUtils { - "Statuses >> Get" should "return a combined status" in { - val response = Github(accessToken).statuses - .getCombinedStatus(validRepoOwner, validRepoName, validRefSingle) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - response.toOption map { r => - r.result.repository.full_name shouldBe s"$validRepoOwner/$validRepoName" - r.statusCode shouldBe okStatusCode - } - } - - it should "return an error when an invalid ref is passed" in { - val response = Github(accessToken).statuses - .getCombinedStatus(validRepoOwner, validRepoName, invalidRef) - .exec[Id, HttpResponse[String]](headerUserAgent) - response should be('left) - } - - "Statuses >> List" should "return a list of statuses" in { - val response = Github(accessToken).statuses - .listStatuses(validRepoOwner, validRepoName, validCommitSha) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - response.toOption map { r => - r.result.nonEmpty shouldBe true - r.statusCode shouldBe okStatusCode - } - } - - it should "return an empty list when an invalid ref is passed" in { - val response = Github(accessToken).statuses - .listStatuses(validRepoOwner, validRepoName, invalidRef) - .exec[Id, HttpResponse[String]](headerUserAgent) - response should be('right) - response.toOption map { r => - r.result.isEmpty shouldBe true - r.statusCode shouldBe okStatusCode - } - } - - "Statuses >> Create" should "create a status" in { - val response = Github(accessToken).statuses - .createStatus( - validRepoOwner, - validRepoName, - validCommitSha, - validStatusState, - None, - None, - None) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - response.toOption map { r ⇒ - r.result.state shouldBe validStatusState - r.statusCode shouldBe createdStatusCode - } - } - - it should "return an error when an invalid sha is passed" in { - val response = Github(accessToken).statuses - .createStatus( - validRepoOwner, - validRepoName, - invalidCommitSha, - validStatusState, - None, - None, - None) - .exec[Id, HttpResponse[String]](headerUserAgent) - response should be('left) - } -} diff --git a/github4s/jvm/src/test/scala/github4s/integration/GHUsersSpec.scala b/github4s/jvm/src/test/scala/github4s/integration/GHUsersSpec.scala deleted file mode 100644 index 6cbfaa11b..000000000 --- a/github4s/jvm/src/test/scala/github4s/integration/GHUsersSpec.scala +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright 2016-2017 47 Degrees, LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github4s.integration - -import cats.Id -import cats.implicits._ -import github4s.Github._ -import github4s.Github -import github4s.jvm.Implicits._ -import github4s.utils.TestUtils -import org.scalatest._ - -import scalaj.http.HttpResponse - -class GHUsersSpec extends FlatSpec with Matchers with TestUtils { - - "Users >> Get" should "return the expected login for a valid username" in { - val response = - Github(accessToken).users - .get(validUsername) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('right) - response.toOption map { r ⇒ - r.result.login shouldBe validUsername - r.statusCode shouldBe okStatusCode - } - } - - it should "return error on Left for invalid username" in { - val response = Github(accessToken).users - .get(invalidUsername) - .exec[Id, HttpResponse[String]](headerUserAgent) - - response should be('left) - } - - "Users >> GetAuth" should "return error on Left when no accessToken is provided" in { - val response = - Github().users.getAuth.exec[Id, HttpResponse[String]](headerUserAgent) - response should be('left) - } - - "Users >> GetUsers" should "return users for a valid since value" in { - val response = Github(accessToken).users - .getUsers(validSinceInt) - .exec[Id, HttpResponse[String]](headerUserAgent) - response should be('right) - - response.toOption map { r ⇒ - r.result.nonEmpty shouldBe true - r.statusCode shouldBe okStatusCode - } - } - - it should "return an empty list when a invalid since value is provided" in { - val response = - Github(accessToken).users - .getUsers(invalidSinceInt) - .exec[Id, HttpResponse[String]](headerUserAgent) - response should be('right) - - response.toOption map { r ⇒ - r.result.isEmpty shouldBe true - r.statusCode shouldBe okStatusCode - } - } - -} diff --git a/github4s/jvm/src/test/scala/github4s/integration/IntegrationSpec.scala b/github4s/jvm/src/test/scala/github4s/integration/IntegrationSpec.scala new file mode 100644 index 000000000..c3ca7652a --- /dev/null +++ b/github4s/jvm/src/test/scala/github4s/integration/IntegrationSpec.scala @@ -0,0 +1,47 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package github4s.integration + +import github4s.HttpRequestBuilderExtension +import github4s.free.interpreters.{Capture, Interpreters} +import github4s.jvm.ImplicitsJVM +import github4s.utils.{BaseIntegrationSpec, TestUtils} + +import scala.concurrent.Future +import scalaj.http.HttpResponse + +class IntegrationSpec + extends BaseIntegrationSpec[HttpResponse[String]] + with GHAuthSpec[HttpResponse[String]] + with GHGistsSpec[HttpResponse[String]] + with GHGitDataSpec[HttpResponse[String]] + with GHIssuesSpec[HttpResponse[String]] + with GHPullRequestsSpec[HttpResponse[String]] + with GHReposSpec[HttpResponse[String]] + with GHStatusesSpec[HttpResponse[String]] + with GHUsersSpec[HttpResponse[String]] + with ImplicitsJVM + with TestUtils { + + override implicit def extension(implicit capture: Capture[Future]): HttpRequestBuilderExtension[ + HttpResponse[String], + Future] = + extensionJVM[Future] + + override implicit def futureInterpreters: Interpreters[Future, HttpResponse[String]] = + intInstanceFutureScalaJ +} diff --git a/github4s/js/src/test/scala/github4s/integration/GHAuthSpec.scala b/github4s/shared/src/test/scala/github4s/integration/GHAuthSpec.scala similarity index 79% rename from github4s/js/src/test/scala/github4s/integration/GHAuthSpec.scala rename to github4s/shared/src/test/scala/github4s/integration/GHAuthSpec.scala index c25a4098b..d6e42cb66 100644 --- a/github4s/js/src/test/scala/github4s/integration/GHAuthSpec.scala +++ b/github4s/shared/src/test/scala/github4s/integration/GHAuthSpec.scala @@ -16,19 +16,13 @@ package github4s.integration -import github4s.Github._ import github4s.Github -import github4s.utils.TestUtils -import org.scalatest._ +import github4s.Github._ import github4s.free.domain.Authorize -import github4s.js.Implicits._ - -import scala.concurrent.ExecutionContext - -class GHAuthSpec extends AsyncFlatSpec with Matchers with TestUtils { +import github4s.implicits._ +import github4s.utils.BaseIntegrationSpec - override implicit val executionContext: ExecutionContext = - scala.concurrent.ExecutionContext.Implicits.global +trait GHAuthSpec[T] extends BaseIntegrationSpec[T] { "Auth >> NewAuth" should "return error on Left when invalid credential is provided" in { @@ -40,7 +34,7 @@ class GHAuthSpec extends AsyncFlatSpec with Matchers with TestUtils { validNote, validClientId, invalidClientSecret) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsLeft(response) } @@ -49,7 +43,7 @@ class GHAuthSpec extends AsyncFlatSpec with Matchers with TestUtils { val response = Github().auth .authorizeUrl(validClientId, validRedirectUri, validScopes) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[Authorize](response, { r => r.result.url.contains(validRedirectUri) shouldBe true @@ -60,7 +54,7 @@ class GHAuthSpec extends AsyncFlatSpec with Matchers with TestUtils { "Auth >> GetAccessToken" should "return error on Left for invalid code value" in { val response = Github().auth .getAccessToken(validClientId, invalidClientSecret, "", validRedirectUri, "") - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsLeft(response) } diff --git a/github4s/js/src/test/scala/github4s/integration/GHGistsSpec.scala b/github4s/shared/src/test/scala/github4s/integration/GHGistsSpec.scala similarity index 76% rename from github4s/js/src/test/scala/github4s/integration/GHGistsSpec.scala rename to github4s/shared/src/test/scala/github4s/integration/GHGistsSpec.scala index d8e1da74f..d75ff2e7f 100644 --- a/github4s/js/src/test/scala/github4s/integration/GHGistsSpec.scala +++ b/github4s/shared/src/test/scala/github4s/integration/GHGistsSpec.scala @@ -16,19 +16,13 @@ package github4s.integration -import github4s.Github._ import github4s.Github -import github4s.utils.TestUtils -import org.scalatest._ +import github4s.Github._ import github4s.free.domain.{Gist, GistFile} -import github4s.js.Implicits._ - -import scala.concurrent.ExecutionContext - -class GHGistsSpec extends AsyncFlatSpec with Matchers with TestUtils { +import github4s.implicits._ +import github4s.utils.BaseIntegrationSpec - override implicit val executionContext: ExecutionContext = - scala.concurrent.ExecutionContext.Implicits.global +trait GHGistsSpec[T] extends BaseIntegrationSpec[T] { "Gists >> Post" should "return the provided gist" in { val response = Github(accessToken).gists @@ -36,7 +30,7 @@ class GHGistsSpec extends AsyncFlatSpec with Matchers with TestUtils { validGistDescription, validGistPublic, Map(validGistFilename -> GistFile(validGistFileContent))) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[Gist](response, { r => r.result.description shouldBe validGistDescription diff --git a/github4s/js/src/test/scala/github4s/integration/GHGitDataSpec.scala b/github4s/shared/src/test/scala/github4s/integration/GHGitDataSpec.scala similarity index 79% rename from github4s/js/src/test/scala/github4s/integration/GHGitDataSpec.scala rename to github4s/shared/src/test/scala/github4s/integration/GHGitDataSpec.scala index 6ca47f6d2..c69ff5799 100644 --- a/github4s/js/src/test/scala/github4s/integration/GHGitDataSpec.scala +++ b/github4s/shared/src/test/scala/github4s/integration/GHGitDataSpec.scala @@ -14,27 +14,21 @@ * limitations under the License. */ -package github4s.utils.integration +package github4s.integration import cats.data.NonEmptyList import github4s.Github import github4s.Github._ import github4s.free.domain.{Ref, RefCommit} -import github4s.js.Implicits._ -import github4s.utils.TestUtils -import org.scalatest._ +import github4s.implicits._ +import github4s.utils.BaseIntegrationSpec -import scala.concurrent.ExecutionContext - -class GHGitDataSpec extends AsyncFlatSpec with Matchers with TestUtils { - - override implicit val executionContext: ExecutionContext = - scala.concurrent.ExecutionContext.Implicits.global +trait GHGitDataSpec[T] extends BaseIntegrationSpec[T] { "GitData >> GetReference" should "return a list of references" in { val response = Github(accessToken).gitData .getReference(validRepoOwner, validRepoName, "heads") - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[NonEmptyList[Ref]](response, { r => r.result.tail.nonEmpty shouldBe true @@ -45,7 +39,7 @@ class GHGitDataSpec extends AsyncFlatSpec with Matchers with TestUtils { it should "return at least one reference" in { val response = Github(accessToken).gitData .getReference(validRepoOwner, validRepoName, validRefSingle) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[NonEmptyList[Ref]](response, { r => r.result.head.ref.contains(validRefSingle) shouldBe true @@ -56,7 +50,7 @@ class GHGitDataSpec extends AsyncFlatSpec with Matchers with TestUtils { "GitData >> GetCommit" should "return one commit" in { val response = Github(accessToken).gitData .getCommit(validRepoOwner, validRepoName, validCommitSha) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[RefCommit](response, { r => r.result.message shouldBe validCommitMsg diff --git a/github4s/js/src/test/scala/github4s/integration/GHIssuesSpec.scala b/github4s/shared/src/test/scala/github4s/integration/GHIssuesSpec.scala similarity index 85% rename from github4s/js/src/test/scala/github4s/integration/GHIssuesSpec.scala rename to github4s/shared/src/test/scala/github4s/integration/GHIssuesSpec.scala index b3c5ec1d8..acdac078b 100644 --- a/github4s/js/src/test/scala/github4s/integration/GHIssuesSpec.scala +++ b/github4s/shared/src/test/scala/github4s/integration/GHIssuesSpec.scala @@ -19,21 +19,17 @@ package github4s.integration import github4s.Github import github4s.Github._ import github4s.free.domain.{Issue, SearchIssuesResult} -import github4s.js.Implicits._ -import github4s.utils.TestUtils -import org.scalatest._ +import github4s.implicits._ +import github4s.utils.BaseIntegrationSpec import scala.concurrent.ExecutionContext -class GHIssuesSpec extends AsyncFlatSpec with Matchers with TestUtils { - - override implicit val executionContext: ExecutionContext = - scala.concurrent.ExecutionContext.Implicits.global +trait GHIssuesSpec[T] extends BaseIntegrationSpec[T] { "Issues >> List" should "return a list of issues" in { val response = Github(accessToken).issues .listIssues(validRepoOwner, validRepoName) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[List[Issue]](response, { r => r.result.nonEmpty shouldBe true @@ -44,7 +40,7 @@ class GHIssuesSpec extends AsyncFlatSpec with Matchers with TestUtils { "Issues >> Search" should "return at least one issue for a valid query" in { val response = Github(accessToken).issues .searchIssues(validSearchQuery, validSearchParams) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[SearchIssuesResult](response, { r => r.result.total_count > 0 shouldBe true @@ -56,7 +52,7 @@ class GHIssuesSpec extends AsyncFlatSpec with Matchers with TestUtils { it should "return an empty result for a non existent query string" in { val response = Github(accessToken).issues .searchIssues(nonExistentSearchQuery, validSearchParams) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[SearchIssuesResult](response, { r => r.result.total_count shouldBe 0 @@ -77,7 +73,7 @@ class GHIssuesSpec extends AsyncFlatSpec with Matchers with TestUtils { None, validIssueLabel, validAssignees) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[Issue](response, { r => r.result.state shouldBe validIssueState diff --git a/github4s/js/src/test/scala/github4s/integration/GHPullRequestsSpec.scala b/github4s/shared/src/test/scala/github4s/integration/GHPullRequestsSpec.scala similarity index 81% rename from github4s/js/src/test/scala/github4s/integration/GHPullRequestsSpec.scala rename to github4s/shared/src/test/scala/github4s/integration/GHPullRequestsSpec.scala index 773341b6b..4d81d1374 100644 --- a/github4s/js/src/test/scala/github4s/integration/GHPullRequestsSpec.scala +++ b/github4s/shared/src/test/scala/github4s/integration/GHPullRequestsSpec.scala @@ -19,22 +19,16 @@ package github4s.integration import github4s.Github import github4s.Github._ import github4s.free.domain._ -import github4s.js.Implicits._ -import github4s.utils.TestUtils -import org.scalatest.{AsyncFlatSpec, Matchers} +import github4s.implicits._ +import github4s.utils.BaseIntegrationSpec -import scala.concurrent.ExecutionContext - -class GHPullRequestsSpec extends AsyncFlatSpec with Matchers with TestUtils { - - override implicit val executionContext: ExecutionContext = - scala.concurrent.ExecutionContext.Implicits.global +trait GHPullRequestsSpec[T] extends BaseIntegrationSpec[T] { "PullRequests >> List" should "return a right response when valid repo is provided" in { val response = Github(accessToken).pullRequests .list(validRepoOwner, validRepoName) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[List[PullRequest]](response, { r => r.statusCode shouldBe okStatusCode @@ -48,7 +42,7 @@ class GHPullRequestsSpec extends AsyncFlatSpec with Matchers with TestUtils { validRepoOwner, validRepoName, List(PRFilterAll, PRFilterSortCreated, PRFilterOrderAsc)) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[List[PullRequest]](response, { r => r.result.nonEmpty shouldBe true @@ -60,7 +54,7 @@ class GHPullRequestsSpec extends AsyncFlatSpec with Matchers with TestUtils { val response = Github(accessToken).pullRequests .list(validRepoOwner, invalidRepoName) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsLeft(response) } @@ -69,7 +63,7 @@ class GHPullRequestsSpec extends AsyncFlatSpec with Matchers with TestUtils { val response = Github(accessToken).pullRequests .listFiles(validRepoOwner, validRepoName, validPullRequestNumber) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[List[PullRequestFile]](response, { r => r.result.nonEmpty shouldBe true @@ -81,7 +75,7 @@ class GHPullRequestsSpec extends AsyncFlatSpec with Matchers with TestUtils { val response = Github(accessToken).pullRequests .listFiles(validRepoOwner, invalidRepoName, validPullRequestNumber) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsLeft(response) } diff --git a/github4s/js/src/test/scala/github4s/integration/GHReposSpec.scala b/github4s/shared/src/test/scala/github4s/integration/GHReposSpec.scala similarity index 82% rename from github4s/js/src/test/scala/github4s/integration/GHReposSpec.scala rename to github4s/shared/src/test/scala/github4s/integration/GHReposSpec.scala index 9af2ab704..10ec9a0c6 100644 --- a/github4s/js/src/test/scala/github4s/integration/GHReposSpec.scala +++ b/github4s/shared/src/test/scala/github4s/integration/GHReposSpec.scala @@ -17,26 +17,20 @@ package github4s.integration import cats.data.NonEmptyList -import github4s.Github._ import github4s.Github -import github4s.utils.TestUtils -import org.scalatest.{AsyncFlatSpec, Matchers} +import github4s.Github._ import github4s.free.domain.{Commit, Content, Repository, User} -import github4s.js.Implicits._ - -import scala.concurrent.ExecutionContext - -class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils { +import github4s.implicits._ +import github4s.utils.BaseIntegrationSpec - override implicit val executionContext: ExecutionContext = - scala.concurrent.ExecutionContext.Implicits.global +trait GHReposSpec[T] extends BaseIntegrationSpec[T] { "Repos >> Get" should "return the expected name when a valid repo is provided" in { val response = Github(accessToken).repos .get(validRepoOwner, validRepoName) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[Repository](response, { r => r.result.name shouldBe validRepoName @@ -48,7 +42,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils { val response = Github(accessToken).repos .get(validRepoOwner, invalidRepoName) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsLeft(response) } @@ -58,7 +52,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils { val response = Github(accessToken).repos .getContents(validRepoOwner, validRepoName, validFilePath) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[NonEmptyList[Content]](response, { r => r.result.head.path shouldBe validFilePath @@ -70,7 +64,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils { val response = Github(accessToken).repos .getContents(validRepoOwner, validRepoName, invalidFilePath) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsLeft(response) } @@ -78,7 +72,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils { val response = Github(accessToken).repos .listCommits(validRepoOwner, validRepoName) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[List[Commit]](response, { r => r.result.nonEmpty shouldBe true @@ -90,7 +84,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils { val response = Github(accessToken).repos .listCommits(invalidRepoName, validRepoName) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsLeft(response) } @@ -99,7 +93,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils { val response = Github(accessToken).repos .listContributors(validRepoOwner, validRepoName) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[List[User]](response, { r => r.result shouldNot be(empty) @@ -111,7 +105,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils { val response = Github(accessToken).repos .listContributors(invalidRepoName, validRepoName) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsLeft(response) } diff --git a/github4s/js/src/test/scala/github4s/integration/GHStatusesSpec.scala b/github4s/shared/src/test/scala/github4s/integration/GHStatusesSpec.scala similarity index 83% rename from github4s/js/src/test/scala/github4s/integration/GHStatusesSpec.scala rename to github4s/shared/src/test/scala/github4s/integration/GHStatusesSpec.scala index 80be1546c..beeb54b62 100644 --- a/github4s/js/src/test/scala/github4s/integration/GHStatusesSpec.scala +++ b/github4s/shared/src/test/scala/github4s/integration/GHStatusesSpec.scala @@ -19,21 +19,15 @@ package github4s.integration import github4s.Github import github4s.Github._ import github4s.free.domain.{CombinedStatus, Status} -import github4s.js.Implicits._ -import github4s.utils.TestUtils -import org.scalatest.{AsyncFlatSpec, Matchers} +import github4s.implicits._ +import github4s.utils.BaseIntegrationSpec -import scala.concurrent.ExecutionContext - -class GHStatusesSpec extends AsyncFlatSpec with Matchers with TestUtils { - - override implicit val executionContext: ExecutionContext = - scala.concurrent.ExecutionContext.Implicits.global +trait GHStatusesSpec[T] extends BaseIntegrationSpec[T] { "Statuses >> Get" should "return a combined status" in { val response = Github(accessToken).statuses .getCombinedStatus(validRepoOwner, validRepoName, validRefSingle) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[CombinedStatus](response, { r => r.result.repository.full_name shouldBe s"$validRepoOwner/$validRepoName" @@ -44,14 +38,14 @@ class GHStatusesSpec extends AsyncFlatSpec with Matchers with TestUtils { it should "return an error when an invalid ref is passed" in { val response = Github(accessToken).statuses .getCombinedStatus(validRepoOwner, validRepoName, invalidRef) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsLeft(response) } "Statuses >> List" should "return a non empty list when a valid ref is provided" in { val response = Github(accessToken).statuses .listStatuses(validRepoOwner, validRepoName, validCommitSha) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[List[Status]](response, { r => r.result.nonEmpty shouldBe true @@ -62,7 +56,7 @@ class GHStatusesSpec extends AsyncFlatSpec with Matchers with TestUtils { it should "return an empty list when an invalid ref is provided" in { val response = Github(accessToken).statuses .listStatuses(validRepoOwner, validRepoName, invalidRef) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[List[Status]](response, { r => r.result.isEmpty shouldBe true @@ -80,7 +74,7 @@ class GHStatusesSpec extends AsyncFlatSpec with Matchers with TestUtils { None, None, None) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[Status](response, { r => r.result.state shouldBe validStatusState @@ -98,7 +92,7 @@ class GHStatusesSpec extends AsyncFlatSpec with Matchers with TestUtils { None, None, None) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsLeft(response) } diff --git a/github4s/js/src/test/scala/github4s/integration/GHUsersSpec.scala b/github4s/shared/src/test/scala/github4s/integration/GHUsersSpec.scala similarity index 74% rename from github4s/js/src/test/scala/github4s/integration/GHUsersSpec.scala rename to github4s/shared/src/test/scala/github4s/integration/GHUsersSpec.scala index 9c189e885..262fab902 100644 --- a/github4s/js/src/test/scala/github4s/integration/GHUsersSpec.scala +++ b/github4s/shared/src/test/scala/github4s/integration/GHUsersSpec.scala @@ -16,24 +16,17 @@ package github4s.integration -import github4s.Github._ import github4s.Github -import github4s.utils.TestUtils -import org.scalatest._ -import fr.hmil.roshttp.response.SimpleHttpResponse - -import scala.concurrent.{ExecutionContext, Future} +import github4s.Github._ import github4s.free.domain.User -import github4s.js.Implicits._ - -class GHUsersSpec extends AsyncFlatSpec with Matchers with TestUtils { +import github4s.implicits._ +import github4s.utils.BaseIntegrationSpec - override implicit val executionContext: ExecutionContext = - scala.concurrent.ExecutionContext.Implicits.global +trait GHUsersSpec[T] extends BaseIntegrationSpec[T] { "Users >> Get" should "return the expected login for a valid username" in { val response = - Github(accessToken).users.get(validUsername).execFuture(headerUserAgent) + Github(accessToken).users.get(validUsername).execFuture[T](headerUserAgent) testFutureIsRight[User](response, { r => r.result.login shouldBe validUsername @@ -44,14 +37,14 @@ class GHUsersSpec extends AsyncFlatSpec with Matchers with TestUtils { it should "return error on Left for invalid username" in { val response = Github(accessToken).users .get(invalidUsername) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsLeft(response) } "Users >> GetAuth" should "return error on Left when no accessToken is provided" in { val response = - Github().users.getAuth.exec[Future, SimpleHttpResponse](headerUserAgent) + Github().users.getAuth.execFuture[T](headerUserAgent) testFutureIsLeft(response) } @@ -60,7 +53,7 @@ class GHUsersSpec extends AsyncFlatSpec with Matchers with TestUtils { val response = Github(accessToken).users .getUsers(validSinceInt) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[List[User]](response, { r => r.result.nonEmpty shouldBe true @@ -72,7 +65,7 @@ class GHUsersSpec extends AsyncFlatSpec with Matchers with TestUtils { val response = Github(accessToken).users .getUsers(invalidSinceInt) - .execFuture(headerUserAgent) + .execFuture[T](headerUserAgent) testFutureIsRight[List[User]](response, { r => r.result.isEmpty shouldBe true diff --git a/github4s/shared/src/test/scala/github4s/utils/BaseIntegrationSpec.scala b/github4s/shared/src/test/scala/github4s/utils/BaseIntegrationSpec.scala new file mode 100644 index 000000000..e6d8dad11 --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/utils/BaseIntegrationSpec.scala @@ -0,0 +1,53 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package github4s.utils + +import github4s.GithubResponses.{GHResponse, GHResult} +import github4s.free.interpreters.{Capture, Interpreters} +import github4s.HttpRequestBuilderExtension +import org.scalatest.{Assertion, AsyncFlatSpec, Matchers} + +import scala.concurrent.{ExecutionContext, Future} + +abstract class BaseIntegrationSpec[T] extends AsyncFlatSpec with Matchers with TestData { + + override implicit val executionContext: ExecutionContext = + scala.concurrent.ExecutionContext.Implicits.global + + implicit def futureInterpreters: Interpreters[Future, T] + implicit def extension(implicit capture: Capture[Future]): HttpRequestBuilderExtension[T, Future] + + def accessToken: Option[String] + + def testFutureIsLeft[A](response: Future[GHResponse[A]]): Future[Assertion] = { + response map { r => + r.isLeft shouldBe true + } + } + + def testFutureIsRight[A]( + response: Future[GHResponse[A]], + f: (GHResult[A]) => Assertion): Future[Assertion] = { + response map { r ⇒ + r.isRight shouldBe true + r.toOption map (f(_)) match { + case _ => succeed + } + } + } + +} From 2f5576cfe0a15e8a2aef86d03f8bd15becf255f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fede=20Fern=C3=A1ndez?= Date: Thu, 4 May 2017 10:02:10 +0200 Subject: [PATCH 4/5] Fixes missing import --- github4s/shared/src/test/scala/github4s/unit/DecodersSpec.scala | 1 + .../src/test/scala/github4s/utils/BaseIntegrationSpec.scala | 1 + 2 files changed, 2 insertions(+) diff --git a/github4s/shared/src/test/scala/github4s/unit/DecodersSpec.scala b/github4s/shared/src/test/scala/github4s/unit/DecodersSpec.scala index 0b7b245b6..47b9e0a1d 100644 --- a/github4s/shared/src/test/scala/github4s/unit/DecodersSpec.scala +++ b/github4s/shared/src/test/scala/github4s/unit/DecodersSpec.scala @@ -17,6 +17,7 @@ package github4s.unit import cats.data.NonEmptyList +import cats.syntax.either._ import github4s.Decoders._ import github4s.free.domain.{CombinedStatus, Commit, Repository} import github4s.utils.FakeResponses diff --git a/github4s/shared/src/test/scala/github4s/utils/BaseIntegrationSpec.scala b/github4s/shared/src/test/scala/github4s/utils/BaseIntegrationSpec.scala index e6d8dad11..d11eabec7 100644 --- a/github4s/shared/src/test/scala/github4s/utils/BaseIntegrationSpec.scala +++ b/github4s/shared/src/test/scala/github4s/utils/BaseIntegrationSpec.scala @@ -16,6 +16,7 @@ package github4s.utils +import cats.syntax.either._ import github4s.GithubResponses.{GHResponse, GHResult} import github4s.free.interpreters.{Capture, Interpreters} import github4s.HttpRequestBuilderExtension From c18216ed48ef5c399cb3710116f7f940e25ffa8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fede=20Fern=C3=A1ndez?= Date: Thu, 4 May 2017 10:55:50 +0200 Subject: [PATCH 5/5] Adds JS and JVM suffix to TestUtils --- .../integration/IntegrationSpec.scala | 4 +-- .../{TestUtils.scala => TestUtilsJS.scala} | 31 ++----------------- .../integration/IntegrationSpec.scala | 4 +-- .../test/scala/github4s/unit/ApiSpec.scala | 4 +-- .../github4s/utils/MockGithubApiServer.scala | 2 +- .../{TestUtils.scala => TestUtilsJVM.scala} | 2 +- 6 files changed, 10 insertions(+), 37 deletions(-) rename github4s/js/src/test/scala/github4s/utils/{TestUtils.scala => TestUtilsJS.scala} (50%) rename github4s/jvm/src/test/scala/github4s/utils/{TestUtils.scala => TestUtilsJVM.scala} (95%) diff --git a/github4s/js/src/test/scala/github4s/integration/IntegrationSpec.scala b/github4s/js/src/test/scala/github4s/integration/IntegrationSpec.scala index a074e437f..8b56a70be 100644 --- a/github4s/js/src/test/scala/github4s/integration/IntegrationSpec.scala +++ b/github4s/js/src/test/scala/github4s/integration/IntegrationSpec.scala @@ -20,7 +20,7 @@ import fr.hmil.roshttp.response.SimpleHttpResponse import github4s.HttpRequestBuilderExtension import github4s.free.interpreters.{Capture, Interpreters} import github4s.js.ImplicitsJS -import github4s.utils.{BaseIntegrationSpec, TestUtils} +import github4s.utils.{BaseIntegrationSpec, TestUtilsJS} import scala.concurrent.Future @@ -35,7 +35,7 @@ class IntegrationSpec with GHStatusesSpec[SimpleHttpResponse] with GHUsersSpec[SimpleHttpResponse] with ImplicitsJS - with TestUtils { + with TestUtilsJS { override implicit def extension( implicit capture: Capture[Future]): HttpRequestBuilderExtension[SimpleHttpResponse, Future] = diff --git a/github4s/js/src/test/scala/github4s/utils/TestUtils.scala b/github4s/js/src/test/scala/github4s/utils/TestUtilsJS.scala similarity index 50% rename from github4s/js/src/test/scala/github4s/utils/TestUtils.scala rename to github4s/js/src/test/scala/github4s/utils/TestUtilsJS.scala index b0eec0a14..78a298de8 100644 --- a/github4s/js/src/test/scala/github4s/utils/TestUtils.scala +++ b/github4s/js/src/test/scala/github4s/utils/TestUtilsJS.scala @@ -16,36 +16,9 @@ package github4s.utils -import org.scalatest.{Assertion, Matchers} -import cats.implicits._ -import github4s.GithubResponses.{GHResponse, GHResult} -import github4s.free.domain._ +import org.scalatest.Matchers -import scala.concurrent.Future - -trait TestUtils extends Matchers with TestData { - - def testFutureIsLeft[A](response: Future[GHResponse[A]])( - implicit ec: scala.concurrent.ExecutionContext): Future[Assertion] = { - response map { r => - r.isLeft should be(true) - } - } - - def testFutureIsRight[A](response: Future[GHResponse[A]], f: (GHResult[A]) => Assertion)( - implicit ec: scala.concurrent.ExecutionContext): Future[Assertion] = { - response map { r ⇒ - { - r.isRight should be(true) - - r.toOption map { rr => - f(rr) - } match { - case _ => succeed - } - } - } - } +trait TestUtilsJS extends Matchers with TestData { val accessToken: Option[String] = Option(github4s.BuildInfo.token) def tokenHeader: String = "token " + accessToken.getOrElse("") diff --git a/github4s/jvm/src/test/scala/github4s/integration/IntegrationSpec.scala b/github4s/jvm/src/test/scala/github4s/integration/IntegrationSpec.scala index c3ca7652a..7d0849fb3 100644 --- a/github4s/jvm/src/test/scala/github4s/integration/IntegrationSpec.scala +++ b/github4s/jvm/src/test/scala/github4s/integration/IntegrationSpec.scala @@ -19,7 +19,7 @@ package github4s.integration import github4s.HttpRequestBuilderExtension import github4s.free.interpreters.{Capture, Interpreters} import github4s.jvm.ImplicitsJVM -import github4s.utils.{BaseIntegrationSpec, TestUtils} +import github4s.utils.{BaseIntegrationSpec, TestUtilsJVM} import scala.concurrent.Future import scalaj.http.HttpResponse @@ -35,7 +35,7 @@ class IntegrationSpec with GHStatusesSpec[HttpResponse[String]] with GHUsersSpec[HttpResponse[String]] with ImplicitsJVM - with TestUtils { + with TestUtilsJVM { override implicit def extension(implicit capture: Capture[Future]): HttpRequestBuilderExtension[ HttpResponse[String], diff --git a/github4s/jvm/src/test/scala/github4s/unit/ApiSpec.scala b/github4s/jvm/src/test/scala/github4s/unit/ApiSpec.scala index ba12f3aa9..c9560617c 100644 --- a/github4s/jvm/src/test/scala/github4s/unit/ApiSpec.scala +++ b/github4s/jvm/src/test/scala/github4s/unit/ApiSpec.scala @@ -18,7 +18,7 @@ package github4s.unit import github4s.api._ import github4s.free.domain.{GistFile, Pagination} -import github4s.utils.{DummyGithubUrls, MockGithubApiServer, TestUtils} +import github4s.utils.{DummyGithubUrls, MockGithubApiServer, TestUtilsJVM} import org.scalatest._ import cats.implicits._ @@ -29,7 +29,7 @@ import github4s.jvm.ImplicitsJVM class ApiSpec extends FlatSpec with Matchers - with TestUtils + with TestUtilsJVM with MockGithubApiServer with DummyGithubUrls with ImplicitsJVM { diff --git a/github4s/jvm/src/test/scala/github4s/utils/MockGithubApiServer.scala b/github4s/jvm/src/test/scala/github4s/utils/MockGithubApiServer.scala index 57d5cc6da..3c221c106 100644 --- a/github4s/jvm/src/test/scala/github4s/utils/MockGithubApiServer.scala +++ b/github4s/jvm/src/test/scala/github4s/utils/MockGithubApiServer.scala @@ -21,7 +21,7 @@ import org.mockserver.model.HttpResponse._ import org.mockserver.model.JsonBody._ import org.mockserver.model.NottableString._ -trait MockGithubApiServer extends MockServerService with FakeResponses with TestUtils { +trait MockGithubApiServer extends MockServerService with FakeResponses with TestUtilsJVM { //Users >> get mockServer diff --git a/github4s/jvm/src/test/scala/github4s/utils/TestUtils.scala b/github4s/jvm/src/test/scala/github4s/utils/TestUtilsJVM.scala similarity index 95% rename from github4s/jvm/src/test/scala/github4s/utils/TestUtils.scala rename to github4s/jvm/src/test/scala/github4s/utils/TestUtilsJVM.scala index 201c2c5f7..db576996a 100644 --- a/github4s/jvm/src/test/scala/github4s/utils/TestUtils.scala +++ b/github4s/jvm/src/test/scala/github4s/utils/TestUtilsJVM.scala @@ -16,7 +16,7 @@ package github4s.utils -trait TestUtils extends TestData { +trait TestUtilsJVM extends TestData { val accessToken: Option[String] = sys.env.get("GITHUB4S_ACCESS_TOKEN") def tokenHeader: String = "token " + accessToken.getOrElse("")