github.com/sleungcy-sap/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/service_broker_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 Broker", func() { 15 var client *Client 16 17 BeforeEach(func() { 18 client = NewTestClient() 19 }) 20 21 Describe("GetServiceBrokers", func() { 22 var ( 23 serviceBrokers []ServiceBroker 24 warnings Warnings 25 executeErr error 26 ) 27 28 JustBeforeEach(func() { 29 serviceBrokers, warnings, executeErr = client.GetServiceBrokers(Filter{ 30 Type: constant.NameFilter, 31 Operator: constant.EqualOperator, 32 Values: []string{"some-name"}, 33 }) 34 }) 35 36 When("the cc returns back service brokers", func() { 37 BeforeEach(func() { 38 response1 := `{ 39 "next_url": "/v2/service_brokers?q=name:some-name&page=2", 40 "resources": [ 41 { 42 "metadata": { 43 "guid": "service-broker-guid-1" 44 }, 45 "entity": { 46 "name":"some-broker-name" 47 } 48 }, 49 { 50 "metadata": { 51 "guid": "service-broker-guid-2" 52 }, 53 "entity": { 54 "name":"other-broker-name" 55 } 56 } 57 ] 58 }` 59 response2 := `{ 60 "next_url": null, 61 "resources": [ 62 { 63 "metadata": { 64 "guid": "service-broker-guid-3" 65 }, 66 "entity": { 67 "name":"some-broker-name" 68 } 69 }, 70 { 71 "metadata": { 72 "guid": "service-broker-guid-4" 73 }, 74 "entity": { 75 "name":"other-broker-name" 76 } 77 } 78 ] 79 }` 80 server.AppendHandlers( 81 CombineHandlers( 82 VerifyRequest(http.MethodGet, "/v2/service_brokers", "q=name:some-name"), 83 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 84 ), 85 ) 86 server.AppendHandlers( 87 CombineHandlers( 88 VerifyRequest(http.MethodGet, "/v2/service_brokers", "q=name:some-name&page=2"), 89 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 90 ), 91 ) 92 }) 93 94 It("returns all the queried service brokers", func() { 95 Expect(executeErr).NotTo(HaveOccurred()) 96 Expect(serviceBrokers).To(ConsistOf([]ServiceBroker{ 97 {GUID: "service-broker-guid-1", Name: "some-broker-name"}, 98 {GUID: "service-broker-guid-2", Name: "other-broker-name"}, 99 {GUID: "service-broker-guid-3", Name: "some-broker-name"}, 100 {GUID: "service-broker-guid-4", Name: "other-broker-name"}, 101 })) 102 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 103 }) 104 }) 105 106 When("the cc returns an error", func() { 107 BeforeEach(func() { 108 response := `{ 109 "description": "The broker is broken.", 110 "error_code": "CF-BrokenBroker", 111 "code": 90003 112 }` 113 server.AppendHandlers( 114 CombineHandlers( 115 VerifyRequest(http.MethodGet, "/v2/service_brokers"), 116 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 117 ), 118 ) 119 }) 120 121 It("returns an error and warnings", func() { 122 Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{ 123 V2ErrorResponse: ccerror.V2ErrorResponse{ 124 Code: 90003, 125 Description: "The broker is broken.", 126 ErrorCode: "CF-BrokenBroker", 127 }, 128 ResponseCode: http.StatusTeapot, 129 })) 130 Expect(warnings).To(ConsistOf("this is a warning")) 131 }) 132 }) 133 }) 134 135 Describe("CreateServiceBroker", func() { 136 var ( 137 serviceBroker ServiceBroker 138 warnings Warnings 139 executeErr error 140 spaceGUID string 141 ) 142 143 JustBeforeEach(func() { 144 serviceBroker, warnings, executeErr = client.CreateServiceBroker( 145 "broker-name", "username", "password", "https://broker.com", spaceGUID, 146 ) 147 }) 148 149 Context("with a spaceGUID", func() { 150 When("service broker creation is successful", func() { 151 BeforeEach(func() { 152 spaceGUID = "a-space-guid" 153 response := `{ 154 "metadata": { 155 "guid": "service-broker-guid", 156 "created_at": "2016-06-08T16:41:22Z", 157 "updated_at": "2016-06-08T16:41:26Z", 158 "url": "/v2/service_brokers/36931aaf-62a7-4019-a708-0e9abf7e7a8f" 159 }, 160 "entity": { 161 "name": "broker-name", 162 "broker_url": "https://broker.com", 163 "auth_username": "username", 164 "space_guid": "a-space-guid" 165 } 166 }` 167 168 requestBody := map[string]interface{}{ 169 "name": "broker-name", 170 "broker_url": "https://broker.com", 171 "auth_username": "username", 172 "auth_password": "password", 173 "space_guid": spaceGUID, 174 } 175 176 server.AppendHandlers( 177 CombineHandlers( 178 VerifyRequest(http.MethodPost, "/v2/service_brokers"), 179 VerifyJSONRepresenting(requestBody), 180 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"a-warning,another-warning"}}), 181 ), 182 ) 183 }) 184 185 It("returns the service broker and all warnings", func() { 186 Expect(executeErr).NotTo(HaveOccurred()) 187 Expect(warnings).To(ConsistOf(Warnings{"a-warning", "another-warning"})) 188 Expect(serviceBroker).To(Equal(ServiceBroker{ 189 GUID: "service-broker-guid", 190 Name: "broker-name", 191 BrokerURL: "https://broker.com", 192 AuthUsername: "username", 193 SpaceGUID: spaceGUID, 194 })) 195 }) 196 }) 197 }) 198 199 Context("without a spaceGUID", func() { 200 When("service broker creation is successful", func() { 201 BeforeEach(func() { 202 spaceGUID = "" 203 response := `{ 204 "metadata": { 205 "guid": "service-broker-guid", 206 "created_at": "2016-06-08T16:41:22Z", 207 "updated_at": "2016-06-08T16:41:26Z", 208 "url": "/v2/service_brokers/36931aaf-62a7-4019-a708-0e9abf7e7a8f" 209 }, 210 "entity": { 211 "name": "broker-name", 212 "broker_url": "https://broker.com", 213 "auth_username": "username", 214 "space_guid": "" 215 } 216 }` 217 218 requestBody := map[string]interface{}{ 219 "name": "broker-name", 220 "broker_url": "https://broker.com", 221 "auth_username": "username", 222 "auth_password": "password", 223 } 224 225 server.AppendHandlers( 226 CombineHandlers( 227 VerifyRequest(http.MethodPost, "/v2/service_brokers"), 228 VerifyJSONRepresenting(requestBody), 229 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"a-warning"}}), 230 ), 231 ) 232 }) 233 234 It("returns the service broker and all warnings", func() { 235 Expect(executeErr).NotTo(HaveOccurred()) 236 Expect(warnings).To(ConsistOf("a-warning")) 237 Expect(serviceBroker).To(Equal(ServiceBroker{ 238 GUID: "service-broker-guid", 239 Name: "broker-name", 240 BrokerURL: "https://broker.com", 241 AuthUsername: "username", 242 SpaceGUID: spaceGUID, 243 })) 244 }) 245 }) 246 }) 247 248 When("an error is returned", func() { 249 BeforeEach(func() { 250 response := `{ 251 "code": 10001, 252 "description": "I'm a teapot", 253 "error_code": "CF-TeapotError" 254 }` 255 256 server.AppendHandlers( 257 CombineHandlers( 258 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"a-warning, another-warning"}}), 259 ), 260 ) 261 }) 262 263 It("returns an empty service broker with errors and warnings", func() { 264 Expect(serviceBroker).To(Equal(ServiceBroker{})) 265 Expect(warnings).To(ConsistOf(Warnings{"a-warning", "another-warning"})) 266 Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{ 267 V2ErrorResponse: ccerror.V2ErrorResponse{ 268 Description: "I'm a teapot", 269 ErrorCode: "CF-TeapotError", 270 Code: 10001, 271 }, 272 RequestIDs: nil, 273 ResponseCode: http.StatusTeapot, 274 })) 275 }) 276 }) 277 }) 278 })