github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/api/cloudcontroller/ccv2/service_plan_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 Plan", func() { 15 var client *Client 16 17 BeforeEach(func() { 18 client = NewTestClient() 19 }) 20 21 Describe("GetServicePlan", func() { 22 When("the service plan exists", func() { 23 BeforeEach(func() { 24 response := `{ 25 "metadata": { 26 "guid": "some-service-plan-guid" 27 }, 28 "entity": { 29 "name": "some-service-plan", 30 "public": true, 31 "service_guid": "some-service-guid" 32 } 33 }` 34 35 server.AppendHandlers( 36 CombineHandlers( 37 VerifyRequest(http.MethodGet, "/v2/service_plans/some-service-plan-guid"), 38 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 39 ), 40 ) 41 }) 42 43 It("returns the service plan and warnings", func() { 44 servicePlan, warnings, err := client.GetServicePlan("some-service-plan-guid") 45 Expect(err).NotTo(HaveOccurred()) 46 47 Expect(servicePlan).To(Equal(ServicePlan{ 48 GUID: "some-service-plan-guid", 49 Name: "some-service-plan", 50 Public: true, 51 ServiceGUID: "some-service-guid", 52 })) 53 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 54 }) 55 }) 56 57 When("the service plan does not exist (testing general error case)", func() { 58 BeforeEach(func() { 59 response := `{ 60 "description": "The service plan could not be found: non-existant-service-plan-guid", 61 "error_code": "CF-ServicePlanNotFound", 62 "code": 110003 63 }` 64 65 server.AppendHandlers( 66 CombineHandlers( 67 VerifyRequest(http.MethodGet, "/v2/service_plans/non-existant-service-plan-guid"), 68 RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 69 )) 70 }) 71 72 It("returns an error and warnings", func() { 73 _, warnings, err := client.GetServicePlan("non-existant-service-plan-guid") 74 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{Message: "The service plan could not be found: non-existant-service-plan-guid"})) 75 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 76 }) 77 }) 78 }) 79 Describe("GetServicePlans", func() { 80 var ( 81 services []ServicePlan 82 warnings Warnings 83 executeErr error 84 ) 85 86 JustBeforeEach(func() { 87 services, warnings, executeErr = client.GetServicePlans(Filter{ 88 Type: constant.ServiceGUIDFilter, 89 Operator: constant.EqualOperator, 90 Values: []string{"some-service-guid"}, 91 }) 92 }) 93 94 When("the cc returns back service plans", func() { 95 BeforeEach(func() { 96 response1 := `{ 97 "next_url": "/v2/service_plans?q=service_guid:some-service-guid&page=2", 98 "resources": [ 99 { 100 "metadata": { 101 "guid": "some-service-plan-guid-1" 102 }, 103 "entity": { 104 "name": "some-service-plan", 105 "service_guid": "some-service-guid" 106 } 107 }, 108 { 109 "metadata": { 110 "guid": "some-service-plan-guid-2" 111 }, 112 "entity": { 113 "name": "other-service-plan", 114 "service_guid": "some-service-guid" 115 } 116 } 117 ] 118 }` 119 120 response2 := `{ 121 "next_url": null, 122 "resources": [ 123 { 124 "metadata": { 125 "guid": "some-service-plan-guid-3" 126 }, 127 "entity": { 128 "name": "some-service-plan", 129 "service_guid": "some-service-guid" 130 } 131 }, 132 { 133 "metadata": { 134 "guid": "some-service-plan-guid-4" 135 }, 136 "entity": { 137 "name": "other-service-plan", 138 "service_guid": "some-service-guid" 139 } 140 } 141 ] 142 }` 143 144 server.AppendHandlers( 145 CombineHandlers( 146 VerifyRequest(http.MethodGet, "/v2/service_plans", "q=service_guid:some-service-guid"), 147 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 148 ), 149 ) 150 server.AppendHandlers( 151 CombineHandlers( 152 VerifyRequest(http.MethodGet, "/v2/service_plans", "q=service_guid:some-service-guid&page=2"), 153 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 154 ), 155 ) 156 }) 157 158 It("returns all the queried services", func() { 159 Expect(executeErr).NotTo(HaveOccurred()) 160 Expect(services).To(ConsistOf([]ServicePlan{ 161 {GUID: "some-service-plan-guid-1", ServiceGUID: "some-service-guid", Name: "some-service-plan"}, 162 {GUID: "some-service-plan-guid-2", ServiceGUID: "some-service-guid", Name: "other-service-plan"}, 163 {GUID: "some-service-plan-guid-3", ServiceGUID: "some-service-guid", Name: "some-service-plan"}, 164 {GUID: "some-service-plan-guid-4", ServiceGUID: "some-service-guid", Name: "other-service-plan"}, 165 })) 166 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 167 }) 168 }) 169 170 When("the cc returns an error", func() { 171 BeforeEach(func() { 172 response := `{ 173 "description": "Some description.", 174 "error_code": "CF-Error", 175 "code": 90003 176 }` 177 server.AppendHandlers( 178 CombineHandlers( 179 VerifyRequest(http.MethodGet, "/v2/service_plans"), 180 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 181 ), 182 ) 183 }) 184 185 It("returns an error and warnings", func() { 186 Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{ 187 V2ErrorResponse: ccerror.V2ErrorResponse{ 188 Code: 90003, 189 Description: "Some description.", 190 ErrorCode: "CF-Error", 191 }, 192 ResponseCode: http.StatusTeapot, 193 })) 194 Expect(warnings).To(ConsistOf("this is a warning")) 195 }) 196 }) 197 }) 198 })