code.cloudfoundry.org/cli@v7.1.0+incompatible/actor/v2action/service_plan_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "code.cloudfoundry.org/cli/actor/v2action"
     7  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("Service Plan Actions", func() {
    15  	var (
    16  		actor                     *Actor
    17  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    22  		actor = NewActor(fakeCloudControllerClient, nil, nil)
    23  
    24  	})
    25  
    26  	Describe("GetServicePlan", func() {
    27  		var (
    28  			servicePlan         ServicePlan
    29  			servicePlanWarnings Warnings
    30  			servicePlanErr      error
    31  		)
    32  
    33  		JustBeforeEach(func() {
    34  			servicePlan, servicePlanWarnings, servicePlanErr = actor.GetServicePlan("some-service-plan-guid")
    35  		})
    36  
    37  		When("the service plan exists", func() {
    38  			var returnedServicePlan ccv2.ServicePlan
    39  
    40  			BeforeEach(func() {
    41  				returnedServicePlan = ccv2.ServicePlan{
    42  					GUID: "some-service-plan-guid",
    43  					Name: "some-service-plan",
    44  				}
    45  				fakeCloudControllerClient.GetServicePlanReturns(
    46  					returnedServicePlan,
    47  					ccv2.Warnings{"get-service-plan-warning"},
    48  					nil)
    49  			})
    50  
    51  			It("returns the service plan and all warnings", func() {
    52  				Expect(servicePlanErr).ToNot(HaveOccurred())
    53  				Expect(servicePlan).To(Equal(ServicePlan(returnedServicePlan)))
    54  				Expect(servicePlanWarnings).To(ConsistOf("get-service-plan-warning"))
    55  
    56  				Expect(fakeCloudControllerClient.GetServicePlanCallCount()).To(Equal(1))
    57  				Expect(fakeCloudControllerClient.GetServicePlanArgsForCall(0)).To(Equal("some-service-plan-guid"))
    58  			})
    59  		})
    60  
    61  		When("an error is encountered getting the service plan", func() {
    62  			var expectedErr error
    63  
    64  			BeforeEach(func() {
    65  				expectedErr = errors.New("some-error")
    66  				fakeCloudControllerClient.GetServicePlanReturns(
    67  					ccv2.ServicePlan{},
    68  					ccv2.Warnings{"get-service-plan-warning"},
    69  					expectedErr)
    70  			})
    71  
    72  			It("returns the errors and all warnings", func() {
    73  				Expect(servicePlanErr).To(MatchError(expectedErr))
    74  				Expect(servicePlan).To(Equal(ServicePlan{}))
    75  				Expect(servicePlanWarnings).To(ConsistOf("get-service-plan-warning"))
    76  
    77  				Expect(fakeCloudControllerClient.GetServicePlanCallCount()).To(Equal(1))
    78  				Expect(fakeCloudControllerClient.GetServicePlanArgsForCall(0)).To(Equal("some-service-plan-guid"))
    79  			})
    80  		})
    81  	})
    82  
    83  	Describe("GetServicePlansForService", func() {
    84  		var (
    85  			servicePlans        []ServicePlan
    86  			servicePlanWarnings Warnings
    87  			servicePlanErr      error
    88  		)
    89  
    90  		BeforeEach(func() {
    91  			fakeCloudControllerClient.GetServiceBrokersReturns([]ccv2.ServiceBroker{{GUID: "broker-guid"}}, nil, nil)
    92  		})
    93  
    94  		JustBeforeEach(func() {
    95  			servicePlans, servicePlanWarnings, servicePlanErr = actor.GetServicePlansForService("some-service", "some-broker")
    96  		})
    97  
    98  		It("fetches a broker by name", func() {
    99  			Expect(fakeCloudControllerClient.GetServiceBrokersCallCount()).To(Equal(1))
   100  			Expect(fakeCloudControllerClient.GetServiceBrokersArgsForCall(0)).To(Equal([]ccv2.Filter{
   101  				{
   102  					Type:     constant.NameFilter,
   103  					Operator: constant.EqualOperator,
   104  					Values:   []string{"some-broker"},
   105  				},
   106  			}))
   107  		})
   108  
   109  		It("fetches services by label and the guid of the passed broker", func() {
   110  			Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
   111  			Expect(fakeCloudControllerClient.GetServicesArgsForCall(0)).To(ConsistOf(
   112  				ccv2.Filter{
   113  					Type:     constant.LabelFilter,
   114  					Operator: constant.EqualOperator,
   115  					Values:   []string{"some-service"},
   116  				},
   117  				ccv2.Filter{
   118  					Type:     constant.ServiceBrokerGUIDFilter,
   119  					Operator: constant.EqualOperator,
   120  					Values:   []string{"broker-guid"},
   121  				},
   122  			))
   123  		})
   124  
   125  		When("there is a service with plans", func() {
   126  			BeforeEach(func() {
   127  				services := []ccv2.Service{
   128  					{
   129  						GUID:        "some-service-guid",
   130  						Label:       "some-service",
   131  						Description: "service-description",
   132  					},
   133  				}
   134  
   135  				plans := []ccv2.ServicePlan{
   136  					{Name: "plan-a"},
   137  					{Name: "plan-b"},
   138  				}
   139  
   140  				fakeCloudControllerClient.GetServicesReturns(services, ccv2.Warnings{"get-services-warning"}, nil)
   141  				fakeCloudControllerClient.GetServicePlansReturns(plans, ccv2.Warnings{"get-plans-warning"}, nil)
   142  			})
   143  
   144  			It("returns all plans and warnings", func() {
   145  				Expect(servicePlanErr).NotTo(HaveOccurred())
   146  				Expect(servicePlans).To(Equal([]ServicePlan{
   147  					{
   148  						Name: "plan-a",
   149  					},
   150  					{
   151  						Name: "plan-b",
   152  					},
   153  				},
   154  				))
   155  				Expect(servicePlanWarnings).To(ConsistOf("get-services-warning", "get-plans-warning"))
   156  				Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
   157  				Expect(fakeCloudControllerClient.GetServicePlansArgsForCall(0)).To(Equal([]ccv2.Filter{{
   158  					Type:     constant.ServiceGUIDFilter,
   159  					Operator: constant.EqualOperator,
   160  					Values:   []string{"some-service-guid"},
   161  				}}))
   162  			})
   163  		})
   164  
   165  		When("GetServices returns an error and warnings", func() {
   166  			BeforeEach(func() {
   167  				fakeCloudControllerClient.GetServicesReturns(nil, ccv2.Warnings{"get-service-warning"}, errors.New("service-error"))
   168  			})
   169  
   170  			It("propagates the error and all warnings", func() {
   171  				Expect(servicePlanErr).To(MatchError(errors.New("service-error")))
   172  				Expect(servicePlanWarnings).To(ConsistOf("get-service-warning"))
   173  			})
   174  		})
   175  
   176  		When("GetServicePlans returns an error and warnings", func() {
   177  			BeforeEach(func() {
   178  				fakeCloudControllerClient.GetServicesReturns([]ccv2.Service{{}}, ccv2.Warnings{"get-service-warning"}, nil)
   179  				fakeCloudControllerClient.GetServicePlansReturns(nil, ccv2.Warnings{"get-plans-warning"}, errors.New("plans-error"))
   180  			})
   181  
   182  			It("propagates the error and all warnings", func() {
   183  				Expect(servicePlanErr).To(MatchError(errors.New("plans-error")))
   184  				Expect(servicePlanWarnings).To(ConsistOf("get-service-warning", "get-plans-warning"))
   185  			})
   186  		})
   187  	})
   188  })