github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/route_binding_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/ccv3fakes"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
    12  	"code.cloudfoundry.org/cli/resources"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("RouteBinding", func() {
    18  	var (
    19  		requester *ccv3fakes.FakeRequester
    20  		client    *Client
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		requester = new(ccv3fakes.FakeRequester)
    25  		client, _ = NewFakeRequesterTestClient(requester)
    26  	})
    27  
    28  	Describe("CreateRouteBinding", func() {
    29  		When("the request succeeds", func() {
    30  			It("returns warnings and no errors", func() {
    31  				requester.MakeRequestReturns("fake-job-url", Warnings{"fake-warning"}, nil)
    32  
    33  				binding := resources.RouteBinding{
    34  					ServiceInstanceGUID: "fake-service-instance-guid",
    35  					RouteGUID:           "fake-route-guid",
    36  				}
    37  
    38  				jobURL, warnings, err := client.CreateRouteBinding(binding)
    39  
    40  				Expect(jobURL).To(Equal(JobURL("fake-job-url")))
    41  				Expect(warnings).To(ConsistOf("fake-warning"))
    42  				Expect(err).NotTo(HaveOccurred())
    43  
    44  				Expect(requester.MakeRequestCallCount()).To(Equal(1))
    45  				Expect(requester.MakeRequestArgsForCall(0)).To(Equal(RequestParams{
    46  					RequestName: internal.PostRouteBindingRequest,
    47  					RequestBody: binding,
    48  				}))
    49  			})
    50  		})
    51  
    52  		When("the request fails", func() {
    53  			It("returns errors and warnings", func() {
    54  				requester.MakeRequestReturns("", Warnings{"fake-warning"}, errors.New("bang"))
    55  
    56  				jobURL, warnings, err := client.CreateRouteBinding(resources.RouteBinding{})
    57  
    58  				Expect(jobURL).To(BeEmpty())
    59  				Expect(warnings).To(ConsistOf("fake-warning"))
    60  				Expect(err).To(MatchError("bang"))
    61  			})
    62  		})
    63  	})
    64  
    65  	Describe("GetRouteBindings", func() {
    66  		var (
    67  			query      []Query
    68  			bindings   []resources.RouteBinding
    69  			included   IncludedResources
    70  			warnings   Warnings
    71  			executeErr error
    72  		)
    73  
    74  		BeforeEach(func() {
    75  			requester.MakeListRequestCalls(func(requestParams RequestParams) (IncludedResources, Warnings, error) {
    76  				for i := 1; i <= 3; i++ {
    77  					Expect(requestParams.AppendToList(resources.RouteBinding{
    78  						GUID:                fmt.Sprintf("route-binding-%d-guid", i),
    79  						ServiceInstanceGUID: fmt.Sprintf("si-%d-guid", i),
    80  					})).NotTo(HaveOccurred())
    81  				}
    82  				return IncludedResources{}, Warnings{"warning-1", "warning-2"}, nil
    83  			})
    84  
    85  			query = []Query{
    86  				{Key: ServiceInstanceGUIDFilter, Values: []string{"si-1-guid", "si-2-guid", "si-3-guid", "si-4-guid"}},
    87  			}
    88  		})
    89  
    90  		JustBeforeEach(func() {
    91  			bindings, included, warnings, executeErr = client.GetRouteBindings(query...)
    92  		})
    93  
    94  		It("makes the correct call", func() {
    95  			Expect(requester.MakeListRequestCallCount()).To(Equal(1))
    96  			actualParams := requester.MakeListRequestArgsForCall(0)
    97  			Expect(actualParams.RequestName).To(Equal(internal.GetRouteBindingsRequest))
    98  			Expect(actualParams.Query).To(ConsistOf(Query{Key: ServiceInstanceGUIDFilter, Values: []string{"si-1-guid", "si-2-guid", "si-3-guid", "si-4-guid"}}))
    99  			Expect(actualParams.ResponseBody).To(BeAssignableToTypeOf(resources.RouteBinding{}))
   100  		})
   101  
   102  		It("returns a list of route bindings", func() {
   103  			Expect(executeErr).ToNot(HaveOccurred())
   104  			Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   105  
   106  			Expect(bindings).To(ConsistOf(
   107  				resources.RouteBinding{
   108  					GUID:                "route-binding-1-guid",
   109  					ServiceInstanceGUID: "si-1-guid",
   110  				},
   111  				resources.RouteBinding{
   112  					GUID:                "route-binding-2-guid",
   113  					ServiceInstanceGUID: "si-2-guid",
   114  				},
   115  				resources.RouteBinding{
   116  					GUID:                "route-binding-3-guid",
   117  					ServiceInstanceGUID: "si-3-guid",
   118  				},
   119  			))
   120  		})
   121  
   122  		When("there are included resources", func() {
   123  			BeforeEach(func() {
   124  				requester.MakeListRequestReturns(
   125  					IncludedResources{
   126  						ServiceInstances: []resources.ServiceInstance{
   127  							{Name: "foo", GUID: "foo-guid"},
   128  							{Name: "bar", GUID: "bar-guid"},
   129  						},
   130  					},
   131  					nil,
   132  					nil,
   133  				)
   134  			})
   135  
   136  			It("returns the included resources", func() {
   137  				Expect(included).To(Equal(IncludedResources{
   138  					ServiceInstances: []resources.ServiceInstance{
   139  						{Name: "foo", GUID: "foo-guid"},
   140  						{Name: "bar", GUID: "bar-guid"},
   141  					},
   142  				}))
   143  			})
   144  		})
   145  
   146  		When("the cloud controller returns errors and warnings", func() {
   147  			BeforeEach(func() {
   148  				errors := []ccerror.V3Error{
   149  					{
   150  						Code:   42424,
   151  						Detail: "Some detailed error message",
   152  						Title:  "CF-SomeErrorTitle",
   153  					},
   154  					{
   155  						Code:   11111,
   156  						Detail: "Some other detailed error message",
   157  						Title:  "CF-SomeOtherErrorTitle",
   158  					},
   159  				}
   160  
   161  				requester.MakeListRequestReturns(
   162  					IncludedResources{},
   163  					Warnings{"this is a warning"},
   164  					ccerror.MultiError{ResponseCode: http.StatusTeapot, Errors: errors},
   165  				)
   166  			})
   167  
   168  			It("returns the error and all warnings", func() {
   169  				Expect(executeErr).To(MatchError(ccerror.MultiError{
   170  					ResponseCode: http.StatusTeapot,
   171  					Errors: []ccerror.V3Error{
   172  						{
   173  							Code:   42424,
   174  							Detail: "Some detailed error message",
   175  							Title:  "CF-SomeErrorTitle",
   176  						},
   177  						{
   178  							Code:   11111,
   179  							Detail: "Some other detailed error message",
   180  							Title:  "CF-SomeOtherErrorTitle",
   181  						},
   182  					},
   183  				}))
   184  				Expect(warnings).To(ConsistOf("this is a warning"))
   185  			})
   186  		})
   187  	})
   188  
   189  	Describe("DeleteRouteBinding", func() {
   190  		const (
   191  			guid   = "fake-route-binding-guid"
   192  			jobURL = JobURL("fake-job-url")
   193  		)
   194  
   195  		It("makes the right request", func() {
   196  			client.DeleteRouteBinding(guid)
   197  
   198  			Expect(requester.MakeRequestCallCount()).To(Equal(1))
   199  			Expect(requester.MakeRequestArgsForCall(0)).To(Equal(RequestParams{
   200  				RequestName: internal.DeleteRouteBindingRequest,
   201  				URIParams:   internal.Params{"route_binding_guid": guid},
   202  			}))
   203  		})
   204  
   205  		When("the request succeeds", func() {
   206  			BeforeEach(func() {
   207  				requester.MakeRequestReturns(jobURL, Warnings{"fake-warning"}, nil)
   208  			})
   209  
   210  			It("returns warnings and no errors", func() {
   211  				job, warnings, err := client.DeleteRouteBinding(guid)
   212  
   213  				Expect(job).To(Equal(jobURL))
   214  				Expect(warnings).To(ConsistOf("fake-warning"))
   215  				Expect(err).NotTo(HaveOccurred())
   216  			})
   217  		})
   218  
   219  		When("the request fails", func() {
   220  			BeforeEach(func() {
   221  				requester.MakeRequestReturns("", Warnings{"fake-warning"}, errors.New("bang"))
   222  			})
   223  
   224  			It("returns errors and warnings", func() {
   225  				jobURL, warnings, err := client.DeleteRouteBinding(guid)
   226  
   227  				Expect(jobURL).To(BeEmpty())
   228  				Expect(warnings).To(ConsistOf("fake-warning"))
   229  				Expect(err).To(MatchError("bang"))
   230  			})
   231  		})
   232  	})
   233  })