github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/api/cloudcontroller/ccv2/service_binding_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  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/ghttp"
    11  )
    12  
    13  var _ = Describe("Service Binding", func() {
    14  	var client *Client
    15  
    16  	BeforeEach(func() {
    17  		client = NewTestClient()
    18  	})
    19  
    20  	Describe("CreateServiceBinding", func() {
    21  		Context("when the create is successful", func() {
    22  			BeforeEach(func() {
    23  				response := `
    24  						{
    25  							"metadata": {
    26  								"guid": "some-service-binding-guid"
    27  							}
    28  						}`
    29  				requestBody := map[string]interface{}{
    30  					"service_instance_guid": "some-service-instance-guid",
    31  					"app_guid":              "some-app-guid",
    32  					"parameters": map[string]interface{}{
    33  						"the-service-broker": "wants this object",
    34  					},
    35  				}
    36  				server.AppendHandlers(
    37  					CombineHandlers(
    38  						VerifyRequest(http.MethodPost, "/v2/service_bindings"),
    39  						VerifyJSONRepresenting(requestBody),
    40  						RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    41  					),
    42  				)
    43  			})
    44  
    45  			It("returns the created object and warnings", func() {
    46  				parameters := map[string]interface{}{
    47  					"the-service-broker": "wants this object",
    48  				}
    49  				serviceBinding, warnings, err := client.CreateServiceBinding("some-app-guid", "some-service-instance-guid", parameters)
    50  				Expect(err).NotTo(HaveOccurred())
    51  
    52  				Expect(serviceBinding).To(Equal(ServiceBinding{GUID: "some-service-binding-guid"}))
    53  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    54  			})
    55  		})
    56  
    57  		Context("when the create returns an error", func() {
    58  			BeforeEach(func() {
    59  				response := `
    60  				{
    61  					  "description": "The app space binding to service is taken: some-app-guid some-service-instance-guid",
    62  						  "error_code": "CF-ServiceBindingAppServiceTaken",
    63  							  "code": 90003
    64  							}
    65  			`
    66  				server.AppendHandlers(
    67  					CombineHandlers(
    68  						VerifyRequest(http.MethodPost, "/v2/service_bindings"),
    69  						RespondWith(http.StatusBadRequest, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    70  					),
    71  				)
    72  			})
    73  
    74  			It("returns the error and warnings", func() {
    75  				parameters := map[string]interface{}{
    76  					"the-service-broker": "wants this object",
    77  				}
    78  				_, warnings, err := client.CreateServiceBinding("some-app-guid", "some-service-instance-guid", parameters)
    79  				Expect(err).To(MatchError(ccerror.ServiceBindingTakenError{Message: "The app space binding to service is taken: some-app-guid some-service-instance-guid"}))
    80  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    81  			})
    82  		})
    83  	})
    84  
    85  	Describe("GetServiceBindings", func() {
    86  		BeforeEach(func() {
    87  			response1 := `{
    88  				"next_url": "/v2/service_bindings?q=app_guid:some-app-guid&page=2",
    89  				"resources": [
    90  					{
    91  						"metadata": {
    92  							"guid": "service-binding-guid-1"
    93  						}
    94  					},
    95  					{
    96  						"metadata": {
    97  							"guid": "service-binding-guid-2"
    98  						}
    99  					}
   100  				]
   101  			}`
   102  			response2 := `{
   103  				"next_url": null,
   104  				"resources": [
   105  					{
   106  						"metadata": {
   107  							"guid": "service-binding-guid-3"
   108  						}
   109  					},
   110  					{
   111  						"metadata": {
   112  							"guid": "service-binding-guid-4"
   113  						}
   114  					}
   115  				]
   116  			}`
   117  			server.AppendHandlers(
   118  				CombineHandlers(
   119  					VerifyRequest(http.MethodGet, "/v2/service_bindings", "q=app_guid:some-app-guid"),
   120  					RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   121  				),
   122  			)
   123  			server.AppendHandlers(
   124  				CombineHandlers(
   125  					VerifyRequest(http.MethodGet, "/v2/service_bindings", "q=app_guid:some-app-guid&page=2"),
   126  					RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
   127  				),
   128  			)
   129  		})
   130  
   131  		Context("when service bindings exist", func() {
   132  			It("returns all the queried service bindings", func() {
   133  				serviceBindings, warnings, err := client.GetServiceBindings([]Query{{
   134  					Filter:   AppGUIDFilter,
   135  					Operator: EqualOperator,
   136  					Value:    "some-app-guid",
   137  				}})
   138  				Expect(err).NotTo(HaveOccurred())
   139  				Expect(serviceBindings).To(ConsistOf([]ServiceBinding{
   140  					{GUID: "service-binding-guid-1"},
   141  					{GUID: "service-binding-guid-2"},
   142  					{GUID: "service-binding-guid-3"},
   143  					{GUID: "service-binding-guid-4"},
   144  				}))
   145  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"}))
   146  			})
   147  		})
   148  	})
   149  
   150  	Describe("DeleteServiceBinding", func() {
   151  		Context("when the service binding exist", func() {
   152  			BeforeEach(func() {
   153  				server.AppendHandlers(
   154  					CombineHandlers(
   155  						VerifyRequest(http.MethodDelete, "/v2/service_bindings/some-service-binding-guid"),
   156  						RespondWith(http.StatusNoContent, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   157  					),
   158  				)
   159  			})
   160  
   161  			It("deletes the service binding", func() {
   162  				warnings, err := client.DeleteServiceBinding("some-service-binding-guid")
   163  				Expect(err).NotTo(HaveOccurred())
   164  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   165  			})
   166  		})
   167  	})
   168  
   169  	Context("when the service binding does not exist", func() {
   170  		BeforeEach(func() {
   171  			response := `{
   172  				"code": 90004,
   173  				"description": "The service binding could not be found: some-service-binding-guid",
   174  				"error_code": "CF-ServiceBindingNotFound"
   175  			}`
   176  			server.AppendHandlers(
   177  				CombineHandlers(
   178  					VerifyRequest(http.MethodDelete, "/v2/service_bindings/some-service-binding-guid"),
   179  					RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   180  				),
   181  			)
   182  		})
   183  
   184  		It("returns a not found error", func() {
   185  			warnings, err := client.DeleteServiceBinding("some-service-binding-guid")
   186  			Expect(err).To(MatchError(ccerror.ResourceNotFoundError{
   187  				Message: "The service binding could not be found: some-service-binding-guid",
   188  			}))
   189  			Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   190  		})
   191  	})
   192  })