github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+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 "documentation_url": "some-url", 33 "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}" 34 } 35 }` 36 server.AppendHandlers( 37 CombineHandlers( 38 VerifyRequest(http.MethodGet, "/v2/services/some-service-guid"), 39 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 40 ), 41 ) 42 }) 43 44 It("returns the service and warnings", func() { 45 service, warnings, err := client.GetService("some-service-guid") 46 Expect(err).NotTo(HaveOccurred()) 47 48 Expect(service).To(Equal(Service{ 49 GUID: "some-service-guid", 50 Label: "some-service", 51 Description: "some-description", 52 DocumentationURL: "some-url", 53 Extra: ServiceExtra{ 54 Shareable: true, 55 }, 56 })) 57 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 58 }) 59 }) 60 61 When("the value of the 'extra' json key is null", func() { 62 BeforeEach(func() { 63 response := `{ 64 "metadata": { 65 "guid": "some-service-guid" 66 }, 67 "entity": { 68 "extra": null 69 } 70 }` 71 server.AppendHandlers( 72 CombineHandlers( 73 VerifyRequest(http.MethodGet, "/v2/services/some-service-guid"), 74 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 75 ), 76 ) 77 }) 78 79 It("returns extra.shareable == 'false'", func() { 80 service, _, err := client.GetService("some-service-guid") 81 Expect(err).NotTo(HaveOccurred()) 82 83 Expect(service).To(Equal(Service{ 84 GUID: "some-service-guid", 85 Extra: ServiceExtra{Shareable: false}, 86 })) 87 }) 88 }) 89 90 When("the value of the 'extra' json key is the empty string", func() { 91 BeforeEach(func() { 92 response := `{ 93 "metadata": { 94 "guid": "some-service-guid" 95 }, 96 "entity": { 97 "extra": "" 98 } 99 }` 100 server.AppendHandlers( 101 CombineHandlers( 102 VerifyRequest(http.MethodGet, "/v2/services/some-service-guid"), 103 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 104 ), 105 ) 106 }) 107 108 It("returns extra.shareable == 'false'", func() { 109 service, _, err := client.GetService("some-service-guid") 110 Expect(err).NotTo(HaveOccurred()) 111 112 Expect(service).To(Equal(Service{ 113 GUID: "some-service-guid", 114 Extra: ServiceExtra{Shareable: false}, 115 })) 116 }) 117 }) 118 119 When("the key 'extra' is not in the json response", func() { 120 BeforeEach(func() { 121 response := `{ 122 "metadata": { 123 "guid": "some-service-guid" 124 } 125 }` 126 server.AppendHandlers( 127 CombineHandlers( 128 VerifyRequest(http.MethodGet, "/v2/services/some-service-guid"), 129 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 130 ), 131 ) 132 }) 133 134 It("returns extra.shareable == 'false'", func() { 135 service, _, err := client.GetService("some-service-guid") 136 Expect(err).NotTo(HaveOccurred()) 137 138 Expect(service).To(Equal(Service{ 139 GUID: "some-service-guid", 140 Extra: ServiceExtra{Shareable: false}, 141 })) 142 }) 143 }) 144 }) 145 146 When("the service does not exist (testing general error case)", func() { 147 BeforeEach(func() { 148 response := `{ 149 "description": "The service could not be found: non-existant-service-guid", 150 "error_code": "CF-ServiceNotFound", 151 "code": 120003 152 }` 153 154 server.AppendHandlers( 155 CombineHandlers( 156 VerifyRequest(http.MethodGet, "/v2/services/non-existant-service-guid"), 157 RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 158 )) 159 }) 160 161 It("returns an error and warnings", func() { 162 _, warnings, err := client.GetService("non-existant-service-guid") 163 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{Message: "The service could not be found: non-existant-service-guid"})) 164 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 165 }) 166 }) 167 }) 168 Describe("GetServices", func() { 169 var ( 170 services []Service 171 warnings Warnings 172 executeErr error 173 ) 174 175 JustBeforeEach(func() { 176 services, warnings, executeErr = client.GetServices(Filter{ 177 Type: constant.LabelFilter, 178 Operator: constant.EqualOperator, 179 Values: []string{"some-label"}, 180 }) 181 }) 182 183 When("the cc returns back services", func() { 184 BeforeEach(func() { 185 response1 := `{ 186 "next_url": "/v2/services?q=label:some-label&page=2", 187 "resources": [ 188 { 189 "metadata": { 190 "guid": "some-service-guid-1" 191 }, 192 "entity": { 193 "label": "some-service" 194 } 195 }, 196 { 197 "metadata": { 198 "guid": "some-service-guid-2" 199 }, 200 "entity": { 201 "label": "other-service" 202 } 203 } 204 ] 205 }` 206 207 response2 := `{ 208 "next_url": null, 209 "resources": [ 210 { 211 "metadata": { 212 "guid": "some-service-guid-3" 213 }, 214 "entity": { 215 "label": "some-service" 216 } 217 }, 218 { 219 "metadata": { 220 "guid": "some-service-guid-4" 221 }, 222 "entity": { 223 "label": "other-service" 224 } 225 } 226 ] 227 }` 228 229 server.AppendHandlers( 230 CombineHandlers( 231 VerifyRequest(http.MethodGet, "/v2/services", "q=label:some-label"), 232 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 233 ), 234 ) 235 server.AppendHandlers( 236 CombineHandlers( 237 VerifyRequest(http.MethodGet, "/v2/services", "q=label:some-label&page=2"), 238 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 239 ), 240 ) 241 }) 242 243 It("returns all the queried services", func() { 244 Expect(executeErr).NotTo(HaveOccurred()) 245 Expect(services).To(ConsistOf([]Service{ 246 {GUID: "some-service-guid-1", Label: "some-service"}, 247 {GUID: "some-service-guid-2", Label: "other-service"}, 248 {GUID: "some-service-guid-3", Label: "some-service"}, 249 {GUID: "some-service-guid-4", Label: "other-service"}, 250 })) 251 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 252 }) 253 }) 254 255 When("the cc returns an error", func() { 256 BeforeEach(func() { 257 response := `{ 258 "description": "Some description.", 259 "error_code": "CF-Error", 260 "code": 90003 261 }` 262 server.AppendHandlers( 263 CombineHandlers( 264 VerifyRequest(http.MethodGet, "/v2/services"), 265 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 266 ), 267 ) 268 }) 269 270 It("returns an error and warnings", func() { 271 Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{ 272 V2ErrorResponse: ccerror.V2ErrorResponse{ 273 Code: 90003, 274 Description: "Some description.", 275 ErrorCode: "CF-Error", 276 }, 277 ResponseCode: http.StatusTeapot, 278 })) 279 Expect(warnings).To(ConsistOf("this is a warning")) 280 }) 281 }) 282 }) 283 })