github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/service_plan_visibility_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 Visibility", func() {
    15  	var client *Client
    16  
    17  	BeforeEach(func() {
    18  		client = NewTestClient()
    19  	})
    20  
    21  	Describe("GetServicePlanVisibilities", func() {
    22  		var (
    23  			visibilities []ServicePlanVisibility
    24  			warnings     Warnings
    25  			executeErr   error
    26  		)
    27  
    28  		JustBeforeEach(func() {
    29  			visibilities, warnings, executeErr = client.GetServicePlanVisibilities(Filter{
    30  				Type:     constant.OrganizationGUIDFilter,
    31  				Operator: constant.EqualOperator,
    32  				Values:   []string{"some-org-guid"},
    33  			})
    34  		})
    35  
    36  		When("the cc returns back service brokers", func() {
    37  			BeforeEach(func() {
    38  				response1 := `{
    39  				"next_url": "/v2/service_plan_visibilities?q=organization_guid:some-org-guid&page=2",
    40  				"resources": [
    41  					{
    42  						"metadata": {
    43  							"guid": "some-visibility-guid-1"
    44  						},
    45  						"entity": {
    46  							"service_plan_guid": "some-service-plan-guid-1",
    47  							"organization_guid": "some-org-guid"
    48  						}
    49  					},
    50  					{
    51  						"metadata": {
    52  							"guid": "some-visibility-guid-2"
    53  						},
    54  						"entity": {
    55  							"service_plan_guid": "some-service-plan-guid-2",
    56  							"organization_guid": "some-org-guid"
    57  						}
    58  					}
    59  				]
    60  			}`
    61  				response2 := `{
    62  				"next_url": null,
    63  				"resources": [
    64  					{
    65  						"metadata": {
    66  							"guid": "some-visibility-guid-3"
    67  						},
    68  						"entity": {
    69  							"service_plan_guid": "some-service-plan-guid-3",
    70  							"organization_guid": "some-org-guid"
    71  						}
    72  					},
    73  					{
    74  						"metadata": {
    75  							"guid": "some-visibility-guid-4"
    76  						},
    77  						"entity": {
    78  							"service_plan_guid": "some-service-plan-guid-4",
    79  							"organization_guid": "some-org-guid"
    80  						}
    81  					}
    82  				]
    83  			}`
    84  				server.AppendHandlers(
    85  					CombineHandlers(
    86  						VerifyRequest(http.MethodGet, "/v2/service_plan_visibilities", "q=organization_guid:some-org-guid"),
    87  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    88  					),
    89  				)
    90  				server.AppendHandlers(
    91  					CombineHandlers(
    92  						VerifyRequest(http.MethodGet, "/v2/service_plan_visibilities", "q=organization_guid:some-org-guid&page=2"),
    93  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
    94  					),
    95  				)
    96  			})
    97  
    98  			It("returns all the queried service brokers", func() {
    99  				Expect(executeErr).NotTo(HaveOccurred())
   100  				Expect(visibilities).To(ConsistOf([]ServicePlanVisibility{
   101  					{GUID: "some-visibility-guid-1", OrganizationGUID: "some-org-guid", ServicePlanGUID: "some-service-plan-guid-1"},
   102  					{GUID: "some-visibility-guid-2", OrganizationGUID: "some-org-guid", ServicePlanGUID: "some-service-plan-guid-2"},
   103  					{GUID: "some-visibility-guid-3", OrganizationGUID: "some-org-guid", ServicePlanGUID: "some-service-plan-guid-3"},
   104  					{GUID: "some-visibility-guid-4", OrganizationGUID: "some-org-guid", ServicePlanGUID: "some-service-plan-guid-4"},
   105  				}))
   106  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"}))
   107  			})
   108  		})
   109  
   110  		When("the cc returns an error", func() {
   111  			BeforeEach(func() {
   112  				response := `{
   113  					"description": "The broker is broken.",
   114  					"error_code": "CF-BrokenBroker",
   115  					"code": 90003
   116  				}`
   117  				server.AppendHandlers(
   118  					CombineHandlers(
   119  						VerifyRequest(http.MethodGet, "/v2/service_plan_visibilities"),
   120  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   121  					),
   122  				)
   123  			})
   124  
   125  			It("returns an error and warnings", func() {
   126  				Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{
   127  					V2ErrorResponse: ccerror.V2ErrorResponse{
   128  						Code:        90003,
   129  						Description: "The broker is broken.",
   130  						ErrorCode:   "CF-BrokenBroker",
   131  					},
   132  					ResponseCode: http.StatusTeapot,
   133  				}))
   134  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   135  			})
   136  		})
   137  	})
   138  
   139  	Describe("CreateServicePlanVisibility", func() {
   140  		var (
   141  			visibility ServicePlanVisibility
   142  			warnings   Warnings
   143  			executeErr error
   144  		)
   145  
   146  		JustBeforeEach(func() {
   147  			visibility, warnings, executeErr = client.CreateServicePlanVisibility("plan-guid-1", "org-guid-1")
   148  		})
   149  
   150  		When("the cc returns no error", func() {
   151  			BeforeEach(func() {
   152  				request := map[string]string{
   153  					"service_plan_guid": "plan-guid-1",
   154  					"organization_guid": "org-guid-1",
   155  				}
   156  
   157  				response := `{
   158  				"metadata": {
   159  					"guid": "plan-visibility-1",
   160  					"url": "/v2/service_plan_visibilities/f740b01a-4afe-4435-aedd-0a8308a7e7d6",
   161  					"created_at": "2016-06-08T16:41:31Z",
   162  					"updated_at": "2016-06-08T16:41:26Z"
   163  				},
   164  				"entity": {
   165  					"service_plan_guid": "plan-guid-1",
   166  					"organization_guid": "org-guid-1",
   167  					"service_plan_url": "/v2/service_plans/ab5780a9-ac8e-4412-9496-4512e865011a",
   168  					"organization_url": "/v2/organizations/55d0ff39-dac9-431f-ba6d-83f37381f1c3"
   169  				}
   170  			}`
   171  
   172  				server.AppendHandlers(
   173  					CombineHandlers(
   174  						VerifyRequest(http.MethodPost, "/v2/service_plan_visibilities"),
   175  						VerifyJSONRepresenting(request),
   176  						RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   177  					),
   178  				)
   179  			})
   180  
   181  			It("returns the created service plan visibility", func() {
   182  				Expect(executeErr).NotTo(HaveOccurred())
   183  				Expect(visibility.GUID).To(Equal("plan-visibility-1"))
   184  				Expect(visibility.ServicePlanGUID).To(Equal("plan-guid-1"))
   185  				Expect(visibility.OrganizationGUID).To(Equal("org-guid-1"))
   186  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   187  			})
   188  		})
   189  
   190  		When("the cc returns an error", func() {
   191  			BeforeEach(func() {
   192  				response := `{
   193  					"code": 10001,
   194  					"description": "Some Error",
   195  					"error_code": "CF-SomeError"
   196  				}`
   197  				server.AppendHandlers(
   198  					CombineHandlers(
   199  						VerifyRequest(http.MethodPost, "/v2/service_plan_visibilities"),
   200  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   201  					),
   202  				)
   203  			})
   204  
   205  			It("returns the error and warnings", func() {
   206  				Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{
   207  					ResponseCode: http.StatusTeapot,
   208  					V2ErrorResponse: ccerror.V2ErrorResponse{
   209  						Code:        10001,
   210  						Description: "Some Error",
   211  						ErrorCode:   "CF-SomeError",
   212  					},
   213  				}))
   214  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   215  			})
   216  		})
   217  	})
   218  
   219  	Describe("DeleteServicePlanVisibility", func() {
   220  		var (
   221  			warnings   Warnings
   222  			executeErr error
   223  		)
   224  
   225  		JustBeforeEach(func() {
   226  			warnings, executeErr = client.DeleteServicePlanVisibility("service-plan-visibility-guid")
   227  		})
   228  
   229  		When("the cc returns no error", func() {
   230  			BeforeEach(func() {
   231  				response := `{}`
   232  
   233  				server.AppendHandlers(
   234  					CombineHandlers(
   235  						VerifyRequest(http.MethodDelete, "/v2/service_plan_visibilities/service-plan-visibility-guid"),
   236  						RespondWith(http.StatusNoContent, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   237  					),
   238  				)
   239  			})
   240  
   241  			It("makes a successful request and returns all warnings", func() {
   242  				Expect(executeErr).NotTo(HaveOccurred())
   243  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   244  			})
   245  		})
   246  
   247  		When("the cc returns an error", func() {
   248  			BeforeEach(func() {
   249  				response := `{
   250  					"code": 10001,
   251  					"description": "Some Error",
   252  					"error_code": "CF-SomeError"
   253  				}`
   254  				server.AppendHandlers(
   255  					CombineHandlers(
   256  						VerifyRequest(http.MethodDelete, "/v2/service_plan_visibilities/service-plan-visibility-guid"),
   257  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   258  					),
   259  				)
   260  			})
   261  
   262  			It("returns the error and warnings", func() {
   263  				Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{
   264  					ResponseCode: http.StatusTeapot,
   265  					V2ErrorResponse: ccerror.V2ErrorResponse{
   266  						Code:        10001,
   267  						Description: "Some Error",
   268  						ErrorCode:   "CF-SomeError",
   269  					},
   270  				}))
   271  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   272  			})
   273  		})
   274  	})
   275  })