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