github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/api/cloudcontroller/ccv2/service_test.go (about) 1 package ccv2_test 2 3 import ( 4 "net/http" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 7 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/ghttp" 12 ) 13 14 var _ = Describe("Service", func() { 15 var client *Client 16 17 BeforeEach(func() { 18 client = NewTestClient() 19 }) 20 21 Describe("GetService", func() { 22 When("the service exists", func() { 23 When("the value of the 'extra' json key is non-empty", func() { 24 BeforeEach(func() { 25 response := `{ 26 "metadata": { 27 "guid": "some-service-guid" 28 }, 29 "entity": { 30 "label": "some-service", 31 "description": "some-description", 32 "extra": "{\"provider\":{\"name\":\"The name\"},\"listing\":{\"imageUrl\":\"http://catgifpage.com/cat.gif\",\"blurb\":\"fake broker that is fake\",\"longDescription\":\"A long time ago, in a galaxy far far away...\"},\"displayName\":\"The Fake Broker\",\"shareable\":true}" 33 } 34 }` 35 server.AppendHandlers( 36 CombineHandlers( 37 VerifyRequest(http.MethodGet, "/v2/services/some-service-guid"), 38 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 39 ), 40 ) 41 }) 42 43 It("returns the service and warnings", func() { 44 service, warnings, err := client.GetService("some-service-guid") 45 Expect(err).NotTo(HaveOccurred()) 46 47 Expect(service).To(Equal(Service{ 48 GUID: "some-service-guid", 49 Label: "some-service", 50 Description: "some-description", 51 Extra: ServiceExtra{ 52 Shareable: true, 53 }, 54 })) 55 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 56 }) 57 }) 58 59 When("the value of the 'extra' json key is null", func() { 60 BeforeEach(func() { 61 response := `{ 62 "metadata": { 63 "guid": "some-service-guid" 64 }, 65 "entity": { 66 "extra": null 67 } 68 }` 69 server.AppendHandlers( 70 CombineHandlers( 71 VerifyRequest(http.MethodGet, "/v2/services/some-service-guid"), 72 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 73 ), 74 ) 75 }) 76 77 It("returns extra.shareable == 'false'", func() { 78 service, _, err := client.GetService("some-service-guid") 79 Expect(err).NotTo(HaveOccurred()) 80 81 Expect(service).To(Equal(Service{ 82 GUID: "some-service-guid", 83 Extra: ServiceExtra{Shareable: false}, 84 })) 85 }) 86 }) 87 88 When("the value of the 'extra' json key is the empty string", func() { 89 BeforeEach(func() { 90 response := `{ 91 "metadata": { 92 "guid": "some-service-guid" 93 }, 94 "entity": { 95 "extra": "" 96 } 97 }` 98 server.AppendHandlers( 99 CombineHandlers( 100 VerifyRequest(http.MethodGet, "/v2/services/some-service-guid"), 101 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 102 ), 103 ) 104 }) 105 106 It("returns extra.shareable == 'false'", func() { 107 service, _, err := client.GetService("some-service-guid") 108 Expect(err).NotTo(HaveOccurred()) 109 110 Expect(service).To(Equal(Service{ 111 GUID: "some-service-guid", 112 Extra: ServiceExtra{Shareable: false}, 113 })) 114 }) 115 }) 116 117 When("the key 'extra' is not in the json response", func() { 118 BeforeEach(func() { 119 response := `{ 120 "metadata": { 121 "guid": "some-service-guid" 122 } 123 }` 124 server.AppendHandlers( 125 CombineHandlers( 126 VerifyRequest(http.MethodGet, "/v2/services/some-service-guid"), 127 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 128 ), 129 ) 130 }) 131 132 It("returns extra.shareable == 'false'", func() { 133 service, _, err := client.GetService("some-service-guid") 134 Expect(err).NotTo(HaveOccurred()) 135 136 Expect(service).To(Equal(Service{ 137 GUID: "some-service-guid", 138 Extra: ServiceExtra{Shareable: false}, 139 })) 140 }) 141 }) 142 143 When("the documentation url is set", func() { 144 Context("in the entity structure", func() { 145 BeforeEach(func() { 146 response := `{ 147 "metadata": { 148 "guid": "some-service-guid" 149 }, 150 "entity": { 151 "documentation_url": "some-url" 152 } 153 }` 154 server.AppendHandlers( 155 CombineHandlers( 156 VerifyRequest(http.MethodGet, "/v2/services/some-service-guid"), 157 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 158 ), 159 ) 160 }) 161 162 It("returns the documentation url correctly", func() { 163 service, _, err := client.GetService("some-service-guid") 164 Expect(err).NotTo(HaveOccurred()) 165 166 Expect(service).To(Equal(Service{ 167 GUID: "some-service-guid", 168 DocumentationURL: "some-url", 169 })) 170 }) 171 }) 172 173 Context("in the extra structure", func() { 174 BeforeEach(func() { 175 response := `{ 176 "metadata": { 177 "guid": "some-service-guid" 178 }, 179 "entity": { 180 "extra": "{\"documentationUrl\":\"some-url\"}" 181 } 182 }` 183 server.AppendHandlers( 184 CombineHandlers( 185 VerifyRequest(http.MethodGet, "/v2/services/some-service-guid"), 186 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 187 ), 188 ) 189 }) 190 191 It("returns the documentation url correctly", func() { 192 service, _, err := client.GetService("some-service-guid") 193 Expect(err).NotTo(HaveOccurred()) 194 195 Expect(service).To(Equal(Service{ 196 GUID: "some-service-guid", 197 DocumentationURL: "some-url", 198 })) 199 }) 200 }) 201 202 Context("in both the entity and extra structures", func() { 203 BeforeEach(func() { 204 response := `{ 205 "metadata": { 206 "guid": "some-service-guid" 207 }, 208 "entity": { 209 "documentation_url": "entity-url", 210 "extra": "{\"documentationUrl\":\"some-url\"}" 211 } 212 }` 213 server.AppendHandlers( 214 CombineHandlers( 215 VerifyRequest(http.MethodGet, "/v2/services/some-service-guid"), 216 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 217 ), 218 ) 219 }) 220 221 It("prioritises the entity structure", func() { 222 service, _, err := client.GetService("some-service-guid") 223 Expect(err).NotTo(HaveOccurred()) 224 225 Expect(service).To(Equal(Service{ 226 GUID: "some-service-guid", 227 DocumentationURL: "entity-url", 228 })) 229 }) 230 }) 231 }) 232 }) 233 234 When("the service does not exist (testing general error case)", func() { 235 BeforeEach(func() { 236 response := `{ 237 "description": "The service could not be found: non-existant-service-guid", 238 "error_code": "CF-ServiceNotFound", 239 "code": 120003 240 }` 241 242 server.AppendHandlers( 243 CombineHandlers( 244 VerifyRequest(http.MethodGet, "/v2/services/non-existant-service-guid"), 245 RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 246 )) 247 }) 248 249 It("returns an error and warnings", func() { 250 _, warnings, err := client.GetService("non-existant-service-guid") 251 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{Message: "The service could not be found: non-existant-service-guid"})) 252 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 253 }) 254 }) 255 }) 256 Describe("GetServices", func() { 257 var ( 258 services []Service 259 warnings Warnings 260 executeErr error 261 ) 262 263 JustBeforeEach(func() { 264 services, warnings, executeErr = client.GetServices(Filter{ 265 Type: constant.LabelFilter, 266 Operator: constant.EqualOperator, 267 Values: []string{"some-label"}, 268 }) 269 }) 270 271 When("the cc returns back services", func() { 272 BeforeEach(func() { 273 response1 := `{ 274 "next_url": "/v2/services?q=label:some-label&page=2", 275 "resources": [ 276 { 277 "metadata": { 278 "guid": "some-service-guid-1" 279 }, 280 "entity": { 281 "label": "some-service" 282 } 283 }, 284 { 285 "metadata": { 286 "guid": "some-service-guid-2" 287 }, 288 "entity": { 289 "label": "other-service" 290 } 291 } 292 ] 293 }` 294 295 response2 := `{ 296 "next_url": null, 297 "resources": [ 298 { 299 "metadata": { 300 "guid": "some-service-guid-3" 301 }, 302 "entity": { 303 "label": "some-service" 304 } 305 }, 306 { 307 "metadata": { 308 "guid": "some-service-guid-4" 309 }, 310 "entity": { 311 "label": "other-service" 312 } 313 } 314 ] 315 }` 316 317 server.AppendHandlers( 318 CombineHandlers( 319 VerifyRequest(http.MethodGet, "/v2/services", "q=label:some-label"), 320 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 321 ), 322 ) 323 server.AppendHandlers( 324 CombineHandlers( 325 VerifyRequest(http.MethodGet, "/v2/services", "q=label:some-label&page=2"), 326 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 327 ), 328 ) 329 }) 330 331 It("returns all the queried services", func() { 332 Expect(executeErr).NotTo(HaveOccurred()) 333 Expect(services).To(ConsistOf([]Service{ 334 {GUID: "some-service-guid-1", Label: "some-service"}, 335 {GUID: "some-service-guid-2", Label: "other-service"}, 336 {GUID: "some-service-guid-3", Label: "some-service"}, 337 {GUID: "some-service-guid-4", Label: "other-service"}, 338 })) 339 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 340 }) 341 }) 342 343 When("the cc returns an error", func() { 344 BeforeEach(func() { 345 response := `{ 346 "description": "Some description.", 347 "error_code": "CF-Error", 348 "code": 90003 349 }` 350 server.AppendHandlers( 351 CombineHandlers( 352 VerifyRequest(http.MethodGet, "/v2/services"), 353 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 354 ), 355 ) 356 }) 357 358 It("returns an error and warnings", func() { 359 Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{ 360 V2ErrorResponse: ccerror.V2ErrorResponse{ 361 Code: 90003, 362 Description: "Some description.", 363 ErrorCode: "CF-Error", 364 }, 365 ResponseCode: http.StatusTeapot, 366 })) 367 Expect(warnings).To(ConsistOf("this is a warning")) 368 }) 369 }) 370 }) 371 Describe("GetSpaceServices", func() { 372 var ( 373 services []Service 374 warnings Warnings 375 executeErr error 376 ) 377 378 JustBeforeEach(func() { 379 services, warnings, executeErr = client.GetSpaceServices("some-space-guid", Filter{ 380 Type: constant.LabelFilter, 381 Operator: constant.EqualOperator, 382 Values: []string{"some-label"}, 383 }) 384 }) 385 386 When("the cc returns back services", func() { 387 BeforeEach(func() { 388 response1 := `{ 389 "next_url": "/v2/spaces/some-space-guid/services?q=label:some-label&page=2", 390 "resources": [ 391 { 392 "metadata": { 393 "guid": "some-service-guid-1" 394 }, 395 "entity": { 396 "label": "some-service" 397 } 398 }, 399 { 400 "metadata": { 401 "guid": "some-service-guid-2" 402 }, 403 "entity": { 404 "label": "other-service" 405 } 406 } 407 ] 408 }` 409 410 response2 := `{ 411 "next_url": null, 412 "resources": [ 413 { 414 "metadata": { 415 "guid": "some-service-guid-3" 416 }, 417 "entity": { 418 "label": "some-service" 419 } 420 }, 421 { 422 "metadata": { 423 "guid": "some-service-guid-4" 424 }, 425 "entity": { 426 "label": "other-service" 427 } 428 } 429 ] 430 }` 431 432 server.AppendHandlers( 433 CombineHandlers( 434 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/services", "q=label:some-label"), 435 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 436 ), 437 ) 438 server.AppendHandlers( 439 CombineHandlers( 440 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/services", "q=label:some-label&page=2"), 441 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 442 ), 443 ) 444 }) 445 446 It("returns all the queried services", func() { 447 Expect(executeErr).NotTo(HaveOccurred()) 448 Expect(services).To(ConsistOf([]Service{ 449 {GUID: "some-service-guid-1", Label: "some-service"}, 450 {GUID: "some-service-guid-2", Label: "other-service"}, 451 {GUID: "some-service-guid-3", Label: "some-service"}, 452 {GUID: "some-service-guid-4", Label: "other-service"}, 453 })) 454 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 455 }) 456 }) 457 458 When("the cc returns an error", func() { 459 BeforeEach(func() { 460 response := `{ 461 "description": "Some description.", 462 "error_code": "CF-Error", 463 "code": 90003 464 }` 465 server.AppendHandlers( 466 CombineHandlers( 467 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/services"), 468 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 469 ), 470 ) 471 }) 472 473 It("returns an error and warnings", func() { 474 Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{ 475 V2ErrorResponse: ccerror.V2ErrorResponse{ 476 Code: 90003, 477 Description: "Some description.", 478 ErrorCode: "CF-Error", 479 }, 480 ResponseCode: http.StatusTeapot, 481 })) 482 Expect(warnings).To(ConsistOf("this is a warning")) 483 }) 484 }) 485 }) 486 })