github.com/arunkumar7540/cli@v6.45.0+incompatible/api/cloudcontroller/ccv3/service_broker_test.go (about) 1 package ccv3_test 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 8 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/ghttp" 12 ) 13 14 var _ = Describe("ServiceBroker", 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() 30 }) 31 32 When("service brokers exist", func() { 33 BeforeEach(func() { 34 response1 := fmt.Sprintf(` 35 { 36 "pagination": { 37 "next": { 38 "href": "%s/v3/service_brokers?page=2&per_page=2" 39 } 40 }, 41 "resources": [ 42 { 43 "name": "service-broker-name-1", 44 "guid": "service-broker-guid-1", 45 "url": "service-broker-url-1", 46 "relationships": {} 47 }, 48 { 49 "name": "service-broker-name-2", 50 "guid": "service-broker-guid-2", 51 "url": "service-broker-url-2", 52 "relationships": {} 53 } 54 ] 55 }`, server.URL()) 56 57 response2 := ` 58 { 59 "pagination": { 60 "next": null 61 }, 62 "resources": [ 63 { 64 "name": "service-broker-name-3", 65 "guid": "service-broker-guid-3", 66 "url": "service-broker-url-3", 67 "relationships": {} 68 } 69 ] 70 }` 71 72 server.AppendHandlers( 73 CombineHandlers( 74 VerifyRequest(http.MethodGet, "/v3/service_brokers"), 75 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 76 ), 77 ) 78 server.AppendHandlers( 79 CombineHandlers( 80 VerifyRequest(http.MethodGet, "/v3/service_brokers", "page=2&per_page=2"), 81 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 82 ), 83 ) 84 }) 85 86 It("returns the queried service-broker and all warnings", func() { 87 Expect(executeErr).NotTo(HaveOccurred()) 88 89 Expect(serviceBrokers).To(ConsistOf( 90 ServiceBroker{Name: "service-broker-name-1", GUID: "service-broker-guid-1", URL: "service-broker-url-1"}, 91 ServiceBroker{Name: "service-broker-name-2", GUID: "service-broker-guid-2", URL: "service-broker-url-2"}, 92 ServiceBroker{Name: "service-broker-name-3", GUID: "service-broker-guid-3", URL: "service-broker-url-3"}, 93 )) 94 Expect(warnings).To(ConsistOf("this is a warning", "this is another warning")) 95 }) 96 }) 97 98 When("the cloud controller returns errors and warnings", func() { 99 BeforeEach(func() { 100 response := `{ 101 "errors": [ 102 { 103 "code": 10008, 104 "detail": "The request is semantically invalid: command presence", 105 "title": "CF-UnprocessableEntity" 106 }, 107 { 108 "code": 10010, 109 "detail": "Isolation segment not found", 110 "title": "CF-ResourceNotFound" 111 } 112 ] 113 }` 114 server.AppendHandlers( 115 CombineHandlers( 116 VerifyRequest(http.MethodGet, "/v3/service_brokers"), 117 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 118 ), 119 ) 120 }) 121 122 It("returns the error and all warnings", func() { 123 Expect(executeErr).To(MatchError(ccerror.MultiError{ 124 ResponseCode: http.StatusTeapot, 125 Errors: []ccerror.V3Error{ 126 { 127 Code: 10008, 128 Detail: "The request is semantically invalid: command presence", 129 Title: "CF-UnprocessableEntity", 130 }, 131 { 132 Code: 10010, 133 Detail: "Isolation segment not found", 134 Title: "CF-ResourceNotFound", 135 }, 136 }, 137 })) 138 Expect(warnings).To(ConsistOf("this is a warning")) 139 }) 140 }) 141 }) 142 143 Describe("CreateServiceBroker", func() { 144 var ( 145 warnings Warnings 146 executeErr error 147 148 credentials = ServiceBrokerCredentials{ 149 Name: "name", 150 URL: "url", 151 Username: "username", 152 Password: "password", 153 } 154 155 expectedBody = map[string]interface{}{ 156 "name": "name", 157 "url": "url", 158 "username": "username", 159 "password": "password", 160 } 161 ) 162 163 JustBeforeEach(func() { 164 warnings, executeErr = client.CreateServiceBroker(credentials) 165 }) 166 167 When("the Cloud Controller successfully creates the broker", func() { 168 BeforeEach(func() { 169 server.AppendHandlers( 170 CombineHandlers( 171 VerifyRequest(http.MethodPost, "/v3/service_brokers"), 172 VerifyJSONRepresenting(expectedBody), 173 RespondWith(http.StatusOK, "", http.Header{"X-Cf-Warnings": {"this is a warning"}}), 174 ), 175 ) 176 }) 177 178 It("succeeds and returns warnings", func() { 179 Expect(executeErr).NotTo(HaveOccurred()) 180 Expect(warnings).To(ConsistOf("this is a warning")) 181 }) 182 }) 183 184 When("the broker is space scoped", func() { 185 BeforeEach(func() { 186 credentials.SpaceGUID = "space-guid" 187 expectedBody["space_guid"] = "space-guid" 188 server.AppendHandlers( 189 CombineHandlers( 190 VerifyRequest(http.MethodPost, "/v3/service_brokers"), 191 VerifyJSONRepresenting(expectedBody), 192 RespondWith(http.StatusOK, "", http.Header{"X-Cf-Warnings": {"this is a warning"}}), 193 ), 194 ) 195 }) 196 197 It("succeeds and returns warnings", func() { 198 Expect(executeErr).NotTo(HaveOccurred()) 199 Expect(warnings).To(ConsistOf("this is a warning")) 200 }) 201 }) 202 203 When("the Cloud Controller fails to create the broker", func() { 204 BeforeEach(func() { 205 response := `{ 206 "errors": [ 207 { 208 "code": 10008, 209 "detail": "The request is semantically invalid: command presence", 210 "title": "CF-UnprocessableEntity" 211 }, 212 { 213 "code": 10010, 214 "detail": "Isolation segment not found", 215 "title": "CF-ResourceNotFound" 216 } 217 ] 218 }` 219 220 server.AppendHandlers( 221 CombineHandlers( 222 VerifyRequest(http.MethodPost, "/v3/service_brokers"), 223 VerifyJSONRepresenting(expectedBody), 224 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 225 ), 226 ) 227 }) 228 229 It("returns parsed errors and warnings", func() { 230 Expect(executeErr).To(MatchError(ccerror.MultiError{ 231 ResponseCode: http.StatusTeapot, 232 Errors: []ccerror.V3Error{ 233 { 234 Code: 10008, 235 Detail: "The request is semantically invalid: command presence", 236 Title: "CF-UnprocessableEntity", 237 }, 238 { 239 Code: 10010, 240 Detail: "Isolation segment not found", 241 Title: "CF-ResourceNotFound", 242 }, 243 }, 244 })) 245 Expect(warnings).To(ConsistOf("this is a warning")) 246 }) 247 }) 248 }) 249 })