github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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/ccerror" 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("CreateServiceBinding", func() { 21 Context("when the create is successful", func() { 22 BeforeEach(func() { 23 response := ` 24 { 25 "metadata": { 26 "guid": "some-service-binding-guid" 27 } 28 }` 29 requestBody := map[string]interface{}{ 30 "service_instance_guid": "some-service-instance-guid", 31 "app_guid": "some-app-guid", 32 "parameters": map[string]interface{}{ 33 "the-service-broker": "wants this object", 34 }, 35 } 36 server.AppendHandlers( 37 CombineHandlers( 38 VerifyRequest(http.MethodPost, "/v2/service_bindings"), 39 VerifyJSONRepresenting(requestBody), 40 RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 41 ), 42 ) 43 }) 44 45 It("returns the created object and warnings", func() { 46 parameters := map[string]interface{}{ 47 "the-service-broker": "wants this object", 48 } 49 serviceBinding, warnings, err := client.CreateServiceBinding("some-app-guid", "some-service-instance-guid", parameters) 50 Expect(err).NotTo(HaveOccurred()) 51 52 Expect(serviceBinding).To(Equal(ServiceBinding{GUID: "some-service-binding-guid"})) 53 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 54 }) 55 }) 56 57 Context("when the create returns an error", func() { 58 BeforeEach(func() { 59 response := ` 60 { 61 "description": "The app space binding to service is taken: some-app-guid some-service-instance-guid", 62 "error_code": "CF-ServiceBindingAppServiceTaken", 63 "code": 90003 64 } 65 ` 66 server.AppendHandlers( 67 CombineHandlers( 68 VerifyRequest(http.MethodPost, "/v2/service_bindings"), 69 RespondWith(http.StatusBadRequest, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 70 ), 71 ) 72 }) 73 74 It("returns the error and warnings", func() { 75 parameters := map[string]interface{}{ 76 "the-service-broker": "wants this object", 77 } 78 _, warnings, err := client.CreateServiceBinding("some-app-guid", "some-service-instance-guid", parameters) 79 Expect(err).To(MatchError(ccerror.ServiceBindingTakenError{Message: "The app space binding to service is taken: some-app-guid some-service-instance-guid"})) 80 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 81 }) 82 }) 83 }) 84 85 Describe("GetServiceBindings", func() { 86 BeforeEach(func() { 87 response1 := `{ 88 "next_url": "/v2/service_bindings?q=app_guid:some-app-guid&page=2", 89 "resources": [ 90 { 91 "metadata": { 92 "guid": "service-binding-guid-1" 93 }, 94 "entity": { 95 "app_guid":"app-guid-1", 96 "service_instance_guid": "service-instance-guid-1" 97 } 98 }, 99 { 100 "metadata": { 101 "guid": "service-binding-guid-2" 102 }, 103 "entity": { 104 "app_guid":"app-guid-2", 105 "service_instance_guid": "service-instance-guid-2" 106 } 107 } 108 ] 109 }` 110 response2 := `{ 111 "next_url": null, 112 "resources": [ 113 { 114 "metadata": { 115 "guid": "service-binding-guid-3" 116 }, 117 "entity": { 118 "app_guid":"app-guid-3", 119 "service_instance_guid": "service-instance-guid-3" 120 } 121 }, 122 { 123 "metadata": { 124 "guid": "service-binding-guid-4" 125 }, 126 "entity": { 127 "app_guid":"app-guid-4", 128 "service_instance_guid": "service-instance-guid-4" 129 } 130 } 131 ] 132 }` 133 server.AppendHandlers( 134 CombineHandlers( 135 VerifyRequest(http.MethodGet, "/v2/service_bindings", "q=app_guid:some-app-guid"), 136 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 137 ), 138 ) 139 server.AppendHandlers( 140 CombineHandlers( 141 VerifyRequest(http.MethodGet, "/v2/service_bindings", "q=app_guid:some-app-guid&page=2"), 142 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 143 ), 144 ) 145 }) 146 147 Context("when service bindings exist", func() { 148 It("returns all the queried service bindings", func() { 149 serviceBindings, warnings, err := client.GetServiceBindings(Query{ 150 Filter: AppGUIDFilter, 151 Operator: EqualOperator, 152 Values: []string{"some-app-guid"}, 153 }) 154 Expect(err).NotTo(HaveOccurred()) 155 Expect(serviceBindings).To(ConsistOf([]ServiceBinding{ 156 {GUID: "service-binding-guid-1", AppGUID: "app-guid-1", ServiceInstanceGUID: "service-instance-guid-1"}, 157 {GUID: "service-binding-guid-2", AppGUID: "app-guid-2", ServiceInstanceGUID: "service-instance-guid-2"}, 158 {GUID: "service-binding-guid-3", AppGUID: "app-guid-3", ServiceInstanceGUID: "service-instance-guid-3"}, 159 {GUID: "service-binding-guid-4", AppGUID: "app-guid-4", ServiceInstanceGUID: "service-instance-guid-4"}, 160 })) 161 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 162 }) 163 }) 164 }) 165 166 Describe("DeleteServiceBinding", func() { 167 Context("when the service binding exist", func() { 168 BeforeEach(func() { 169 server.AppendHandlers( 170 CombineHandlers( 171 VerifyRequest(http.MethodDelete, "/v2/service_bindings/some-service-binding-guid"), 172 RespondWith(http.StatusNoContent, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}), 173 ), 174 ) 175 }) 176 177 It("deletes the service binding", func() { 178 warnings, err := client.DeleteServiceBinding("some-service-binding-guid") 179 Expect(err).NotTo(HaveOccurred()) 180 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 181 }) 182 }) 183 }) 184 185 Context("when the service binding does not exist", func() { 186 BeforeEach(func() { 187 response := `{ 188 "code": 90004, 189 "description": "The service binding could not be found: some-service-binding-guid", 190 "error_code": "CF-ServiceBindingNotFound" 191 }` 192 server.AppendHandlers( 193 CombineHandlers( 194 VerifyRequest(http.MethodDelete, "/v2/service_bindings/some-service-binding-guid"), 195 RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 196 ), 197 ) 198 }) 199 200 It("returns a not found error", func() { 201 warnings, err := client.DeleteServiceBinding("some-service-binding-guid") 202 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{ 203 Message: "The service binding could not be found: some-service-binding-guid", 204 })) 205 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 206 }) 207 }) 208 })