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