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