github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/api/cloudcontroller/ccv2/service_binding_test.go (about) 1 package ccv2_test 2 3 import ( 4 "net/http" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller" 7 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 . "github.com/onsi/gomega/ghttp" 11 ) 12 13 var _ = Describe("Service Binding", func() { 14 var client *Client 15 16 BeforeEach(func() { 17 client = NewTestClient() 18 }) 19 20 Describe("GetServiceBindings", func() { 21 BeforeEach(func() { 22 response1 := `{ 23 "next_url": "/v2/service_bindings?q=app_guid:some-app-guid&page=2", 24 "resources": [ 25 { 26 "metadata": { 27 "guid": "service-binding-guid-1" 28 } 29 }, 30 { 31 "metadata": { 32 "guid": "service-binding-guid-2" 33 } 34 } 35 ] 36 }` 37 response2 := `{ 38 "next_url": null, 39 "resources": [ 40 { 41 "metadata": { 42 "guid": "service-binding-guid-3" 43 } 44 }, 45 { 46 "metadata": { 47 "guid": "service-binding-guid-4" 48 } 49 } 50 ] 51 }` 52 server.AppendHandlers( 53 CombineHandlers( 54 VerifyRequest(http.MethodGet, "/v2/service_bindings", "q=app_guid:some-app-guid"), 55 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 56 ), 57 ) 58 server.AppendHandlers( 59 CombineHandlers( 60 VerifyRequest(http.MethodGet, "/v2/service_bindings", "q=app_guid:some-app-guid&page=2"), 61 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 62 ), 63 ) 64 }) 65 66 Context("when service bindings exist", func() { 67 It("returns all the queried service bindings", func() { 68 serviceBindings, warnings, err := client.GetServiceBindings([]Query{{ 69 Filter: AppGUIDFilter, 70 Operator: EqualOperator, 71 Value: "some-app-guid", 72 }}) 73 Expect(err).NotTo(HaveOccurred()) 74 Expect(serviceBindings).To(ConsistOf([]ServiceBinding{ 75 {GUID: "service-binding-guid-1"}, 76 {GUID: "service-binding-guid-2"}, 77 {GUID: "service-binding-guid-3"}, 78 {GUID: "service-binding-guid-4"}, 79 })) 80 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 81 }) 82 }) 83 }) 84 85 Describe("DeleteServiceBinding", func() { 86 Context("when the service binding exist", func() { 87 BeforeEach(func() { 88 server.AppendHandlers( 89 CombineHandlers( 90 VerifyRequest(http.MethodDelete, "/v2/service_bindings/some-service-binding-guid"), 91 RespondWith(http.StatusNoContent, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}), 92 ), 93 ) 94 }) 95 96 It("deletes the service binding", func() { 97 warnings, err := client.DeleteServiceBinding("some-service-binding-guid") 98 Expect(err).NotTo(HaveOccurred()) 99 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 100 }) 101 }) 102 }) 103 104 Context("when the service binding does not exist", func() { 105 BeforeEach(func() { 106 response := `{ 107 "code": 90004, 108 "description": "The service binding could not be found: some-service-binding-guid", 109 "error_code": "CF-ServiceBindingNotFound" 110 }` 111 server.AppendHandlers( 112 CombineHandlers( 113 VerifyRequest(http.MethodDelete, "/v2/service_bindings/some-service-binding-guid"), 114 RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 115 ), 116 ) 117 }) 118 119 It("returns a not found error", func() { 120 warnings, err := client.DeleteServiceBinding("some-service-binding-guid") 121 Expect(err).To(MatchError(cloudcontroller.ResourceNotFoundError{ 122 Message: "The service binding could not be found: some-service-binding-guid", 123 })) 124 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 125 }) 126 }) 127 })