|
| 1 | +import org.scalatest._ |
| 2 | +import org.scalatest.Matchers |
| 3 | +import play.api.libs.json.Json |
| 4 | +import com.codacy.client.bitbucket.PullRequest |
| 5 | +import com.codacy.client.bitbucket.PullRequest._ |
| 6 | + |
| 7 | +class RemoteRepositorySpecs extends FlatSpec with Matchers { |
| 8 | + |
| 9 | + "RemoteRepository" should "successfully parse JSON with a commit" in { |
| 10 | + val input = |
| 11 | + """ |
| 12 | + |{ |
| 13 | + | "description": "", |
| 14 | + | "links": { |
| 15 | + | "decline": { |
| 16 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/decline" |
| 17 | + | }, |
| 18 | + | "commits": { |
| 19 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/commits" |
| 20 | + | }, |
| 21 | + | "self": { |
| 22 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368" |
| 23 | + | }, |
| 24 | + | "comments": { |
| 25 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/comments" |
| 26 | + | }, |
| 27 | + | "merge": { |
| 28 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/merge" |
| 29 | + | }, |
| 30 | + | "html": { |
| 31 | + | "href": "https://bitbucket.org//frontend./pull-requests/368" |
| 32 | + | }, |
| 33 | + | "activity": { |
| 34 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/activity" |
| 35 | + | }, |
| 36 | + | "diff": { |
| 37 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/diff" |
| 38 | + | }, |
| 39 | + | "approve": { |
| 40 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/approve" |
| 41 | + | }, |
| 42 | + | "statuses": { |
| 43 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/statuses" |
| 44 | + | } |
| 45 | + | }, |
| 46 | + | "title": "title", |
| 47 | + | "close_source_branch": true, |
| 48 | + | "merge_commit": null, |
| 49 | + | "destination": { |
| 50 | + | "commit": { |
| 51 | + | "hash" : "random_hash" |
| 52 | + | }, |
| 53 | + | "repository": { |
| 54 | + | "links": { |
| 55 | + | "self": { |
| 56 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend." |
| 57 | + | }, |
| 58 | + | "html": { |
| 59 | + | "href": "https://bitbucket.org//frontend." |
| 60 | + | }, |
| 61 | + | "avatar": { |
| 62 | + | "href": "https://bitbucket.org//frontend./avatar/32/" |
| 63 | + | } |
| 64 | + | }, |
| 65 | + | "type": "repository", |
| 66 | + | "name": "frontend.", |
| 67 | + | "full_name": "/frontend.", |
| 68 | + | "uuid": "{ d8e5fe0b-fa32-47f1-a98b-88973c00c1d8}" |
| 69 | + | }, |
| 70 | + | "branch": { |
| 71 | + | "name": "FASDREI-2035" |
| 72 | + | } |
| 73 | + | }, |
| 74 | + | "state": "OPEN", |
| 75 | + | "closed_by": null, |
| 76 | + | "source": { |
| 77 | + | "commit": { |
| 78 | + | "hash": "ae0ff1c530b7", |
| 79 | + | "links": { |
| 80 | + | "self": { |
| 81 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./commit/ae0ff1c530b7" |
| 82 | + | } |
| 83 | + | } |
| 84 | + | }, |
| 85 | + | "repository": { |
| 86 | + | "links": { |
| 87 | + | "self": { |
| 88 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend." |
| 89 | + | }, |
| 90 | + | "html": { |
| 91 | + | "href": "https://bitbucket.org//frontend." |
| 92 | + | }, |
| 93 | + | "avatar": { |
| 94 | + | "href": "https://bitbucket.org//frontend./avatar/32/" |
| 95 | + | } |
| 96 | + | }, |
| 97 | + | "type": "repository", |
| 98 | + | "name": "frontend.", |
| 99 | + | "full_name": "/frontend.", |
| 100 | + | "uuid": "{ d8e5fe0b-fa32-47f1-a98b-88973c00c1d8}" |
| 101 | + | }, |
| 102 | + | "branch": { |
| 103 | + | "name": "FASDREI-2033" |
| 104 | + | } |
| 105 | + | }, |
| 106 | + | "comment_count": 0, |
| 107 | + | "author": { |
| 108 | + | "username": "username", |
| 109 | + | "display_name": "user name", |
| 110 | + | "type": "user", |
| 111 | + | "uuid": "{ f6f653be-75ea-4dc9-84af-645a5ede93aa}", |
| 112 | + | "links": { |
| 113 | + | "self": { |
| 114 | + | "href": "https://bitbucket.org/!api/2.0/users/username" |
| 115 | + | }, |
| 116 | + | "html": { |
| 117 | + | "href": "https://bitbucket.org/username/" |
| 118 | + | }, |
| 119 | + | "avatar": { |
| 120 | + | "href": "https://bitbucket.org/account/username/avatar/32/" |
| 121 | + | } |
| 122 | + | } |
| 123 | + | }, |
| 124 | + | "created_on": "2016-06-16T16:08:51.006182+00:00", |
| 125 | + | "reason": "", |
| 126 | + | "updated_on": "2016-09-08T17:27:22.050009+00:00", |
| 127 | + | "type": "pullrequest", |
| 128 | + | "id": 368, |
| 129 | + | "task_count": 0 |
| 130 | + | } |
| 131 | + """.stripMargin |
| 132 | + val json = Json.parse(input) |
| 133 | + val value = json.validate[PullRequest] |
| 134 | + |
| 135 | + val parsed = value.fold(e => false, r => r.destCommit.isDefined) |
| 136 | + |
| 137 | + parsed shouldBe true |
| 138 | + } |
| 139 | + |
| 140 | + it should "successfully parse JSON with null commit" in { |
| 141 | + val input = |
| 142 | + """ |
| 143 | + |{ |
| 144 | + | "description": "", |
| 145 | + | "links": { |
| 146 | + | "decline": { |
| 147 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/decline" |
| 148 | + | }, |
| 149 | + | "commits": { |
| 150 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/commits" |
| 151 | + | }, |
| 152 | + | "self": { |
| 153 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368" |
| 154 | + | }, |
| 155 | + | "comments": { |
| 156 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/comments" |
| 157 | + | }, |
| 158 | + | "merge": { |
| 159 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/merge" |
| 160 | + | }, |
| 161 | + | "html": { |
| 162 | + | "href": "https://bitbucket.org//frontend./pull-requests/368" |
| 163 | + | }, |
| 164 | + | "activity": { |
| 165 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/activity" |
| 166 | + | }, |
| 167 | + | "diff": { |
| 168 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/diff" |
| 169 | + | }, |
| 170 | + | "approve": { |
| 171 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/approve" |
| 172 | + | }, |
| 173 | + | "statuses": { |
| 174 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/statuses" |
| 175 | + | } |
| 176 | + | }, |
| 177 | + | "title": "title", |
| 178 | + | "close_source_branch": true, |
| 179 | + | "merge_commit": null, |
| 180 | + | "destination": { |
| 181 | + | "commit": null, |
| 182 | + | "repository": { |
| 183 | + | "links": { |
| 184 | + | "self": { |
| 185 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend." |
| 186 | + | }, |
| 187 | + | "html": { |
| 188 | + | "href": "https://bitbucket.org//frontend." |
| 189 | + | }, |
| 190 | + | "avatar": { |
| 191 | + | "href": "https://bitbucket.org//frontend./avatar/32/" |
| 192 | + | } |
| 193 | + | }, |
| 194 | + | "type": "repository", |
| 195 | + | "name": "frontend.", |
| 196 | + | "full_name": "/frontend.", |
| 197 | + | "uuid": "{ d8e5fe0b-fa32-47f1-a98b-88973c00c1d8}" |
| 198 | + | }, |
| 199 | + | "branch": { |
| 200 | + | "name": "FASDREI-2035" |
| 201 | + | } |
| 202 | + | }, |
| 203 | + | "state": "OPEN", |
| 204 | + | "closed_by": null, |
| 205 | + | "source": { |
| 206 | + | "commit": { |
| 207 | + | "hash": "ae0ff1c530b7", |
| 208 | + | "links": { |
| 209 | + | "self": { |
| 210 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend./commit/ae0ff1c530b7" |
| 211 | + | } |
| 212 | + | } |
| 213 | + | }, |
| 214 | + | "repository": { |
| 215 | + | "links": { |
| 216 | + | "self": { |
| 217 | + | "href": "https://bitbucket.org/!api/2.0/repositories//frontend." |
| 218 | + | }, |
| 219 | + | "html": { |
| 220 | + | "href": "https://bitbucket.org//frontend." |
| 221 | + | }, |
| 222 | + | "avatar": { |
| 223 | + | "href": "https://bitbucket.org//frontend./avatar/32/" |
| 224 | + | } |
| 225 | + | }, |
| 226 | + | "type": "repository", |
| 227 | + | "name": "frontend.", |
| 228 | + | "full_name": "/frontend.", |
| 229 | + | "uuid": "{ d8e5fe0b-fa32-47f1-a98b-88973c00c1d8}" |
| 230 | + | }, |
| 231 | + | "branch": { |
| 232 | + | "name": "FASDREI-2033" |
| 233 | + | } |
| 234 | + | }, |
| 235 | + | "comment_count": 0, |
| 236 | + | "author": { |
| 237 | + | "username": "username", |
| 238 | + | "display_name": "user name", |
| 239 | + | "type": "user", |
| 240 | + | "uuid": "{ f6f653be-75ea-4dc9-84af-645a5ede93aa}", |
| 241 | + | "links": { |
| 242 | + | "self": { |
| 243 | + | "href": "https://bitbucket.org/!api/2.0/users/username" |
| 244 | + | }, |
| 245 | + | "html": { |
| 246 | + | "href": "https://bitbucket.org/username/" |
| 247 | + | }, |
| 248 | + | "avatar": { |
| 249 | + | "href": "https://bitbucket.org/account/username/avatar/32/" |
| 250 | + | } |
| 251 | + | } |
| 252 | + | }, |
| 253 | + | "created_on": "2016-06-16T16:08:51.006182+00:00", |
| 254 | + | "reason": "", |
| 255 | + | "updated_on": "2016-09-08T17:27:22.050009+00:00", |
| 256 | + | "type": "pullrequest", |
| 257 | + | "id": 368, |
| 258 | + | "task_count": 0 |
| 259 | + | } |
| 260 | + """.stripMargin |
| 261 | + val json = Json.parse(input) |
| 262 | + val value = json.validate[PullRequest] |
| 263 | + |
| 264 | + val parsed = value.fold(e =>false, r => r.destCommit.isEmpty) |
| 265 | + |
| 266 | + parsed shouldBe true |
| 267 | + } |
| 268 | +} |
0 commit comments