github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/v7action/service_plan_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/resources"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Service Plan Actions", func() {
    16  	var (
    17  		actor                     *Actor
    18  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
    23  		actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil)
    24  	})
    25  
    26  	Describe("GetServicePlanByNameOfferingAndBroker", func() {
    27  		const servicePlanName = "myServicePlan"
    28  
    29  		var (
    30  			servicePlan         resources.ServicePlan
    31  			warnings            Warnings
    32  			executionError      error
    33  			serviceBrokerName   string
    34  			serviceOfferingName string
    35  		)
    36  
    37  		BeforeEach(func() {
    38  			serviceBrokerName = ""
    39  			serviceOfferingName = ""
    40  		})
    41  
    42  		JustBeforeEach(func() {
    43  			servicePlan, warnings, executionError = actor.GetServicePlanByNameOfferingAndBroker(
    44  				servicePlanName,
    45  				serviceOfferingName,
    46  				serviceBrokerName,
    47  			)
    48  		})
    49  
    50  		When("the cloud controller request is successful", func() {
    51  			When("the cloud controller returns one service plan", func() {
    52  				BeforeEach(func() {
    53  					fakeCloudControllerClient.GetServicePlansReturns([]resources.ServicePlan{
    54  						{
    55  							Name: "some-service-plan",
    56  							GUID: "some-service-plan-guid",
    57  						},
    58  					}, ccv3.Warnings{"some-service-plan-warning"}, nil)
    59  				})
    60  
    61  				It("returns a service plan and warnings", func() {
    62  					Expect(executionError).NotTo(HaveOccurred())
    63  
    64  					Expect(servicePlan).To(Equal(resources.ServicePlan{Name: "some-service-plan", GUID: "some-service-plan-guid"}))
    65  					Expect(warnings).To(ConsistOf("some-service-plan-warning"))
    66  					Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
    67  					Expect(fakeCloudControllerClient.GetServicePlansArgsForCall(0)).To(ConsistOf(
    68  						ccv3.Query{Key: ccv3.NameFilter, Values: []string{servicePlanName}},
    69  					))
    70  				})
    71  			})
    72  
    73  			When("the cloud controller returns no service plans", func() {
    74  				BeforeEach(func() {
    75  					fakeCloudControllerClient.GetServicePlansReturns(
    76  						nil,
    77  						ccv3.Warnings{"some-service-plan-warning"},
    78  						nil)
    79  				})
    80  
    81  				It("returns an error and warnings", func() {
    82  					Expect(executionError).To(MatchError(actionerror.ServicePlanNotFoundError{
    83  						PlanName:     servicePlanName,
    84  						OfferingName: serviceOfferingName,
    85  					}))
    86  					Expect(warnings).To(ConsistOf("some-service-plan-warning"))
    87  				})
    88  			})
    89  
    90  			When("the cloud controller returns more than one service plan", func() {
    91  				BeforeEach(func() {
    92  					fakeCloudControllerClient.GetServicePlansReturns([]resources.ServicePlan{
    93  						{
    94  							Name: "some-service-plan-1",
    95  							GUID: "some-service-plan-guid-1",
    96  						},
    97  						{
    98  							Name: "some-service-plan-2",
    99  							GUID: "some-service-plan-guid-2",
   100  						},
   101  					}, ccv3.Warnings{"some-service-plan-warning"}, nil)
   102  				})
   103  
   104  				It("returns an error and warnings", func() {
   105  					Expect(executionError).To(MatchError(actionerror.DuplicateServicePlanError{Name: servicePlanName}))
   106  					Expect(warnings).To(ConsistOf("some-service-plan-warning"))
   107  				})
   108  			})
   109  		})
   110  
   111  		When("the cloud controller returns an error", func() {
   112  			BeforeEach(func() {
   113  				fakeCloudControllerClient.GetServicePlansReturns(
   114  					nil,
   115  					ccv3.Warnings{"some-service-plan-warning"},
   116  					errors.New("random error"),
   117  				)
   118  			})
   119  
   120  			It("returns an error and warnings", func() {
   121  				Expect(executionError).To(MatchError("random error"))
   122  				Expect(warnings).To(ConsistOf("some-service-plan-warning"))
   123  			})
   124  		})
   125  
   126  		When("the offering name is provided", func() {
   127  			BeforeEach(func() {
   128  				serviceOfferingName = "some-offering-name"
   129  				fakeCloudControllerClient.GetServicePlansReturns([]resources.ServicePlan{
   130  					{
   131  						Name: "some-service-plan",
   132  						GUID: "some-service-plan-guid",
   133  					},
   134  					{
   135  						Name: "some-service-plan",
   136  						GUID: "some-other-service-plan-guid",
   137  					},
   138  				}, ccv3.Warnings{"some-service-plan-warning"}, nil)
   139  			})
   140  
   141  			It("queries on the service plan and offering name", func() {
   142  				Expect(warnings).To(ConsistOf("some-service-plan-warning"))
   143  				Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
   144  				Expect(fakeCloudControllerClient.GetServicePlansArgsForCall(0)).To(ConsistOf(
   145  					ccv3.Query{Key: ccv3.NameFilter, Values: []string{servicePlanName}},
   146  					ccv3.Query{Key: ccv3.ServiceOfferingNamesFilter, Values: []string{serviceOfferingName}},
   147  				))
   148  			})
   149  
   150  			When("it returns multiple results", func() {
   151  				It("returns an error with the right hint", func() {
   152  					Expect(executionError).To(MatchError(actionerror.DuplicateServicePlanError{
   153  						Name:                servicePlanName,
   154  						ServiceOfferingName: serviceOfferingName,
   155  					}))
   156  				})
   157  			})
   158  		})
   159  
   160  		When("the broker name is provided", func() {
   161  			BeforeEach(func() {
   162  				serviceBrokerName = "some-broker-name"
   163  				fakeCloudControllerClient.GetServicePlansReturns([]resources.ServicePlan{
   164  					{
   165  						Name: "some-service-plan",
   166  						GUID: "some-service-plan-guid",
   167  					},
   168  					{
   169  						Name: "some-service-plan",
   170  						GUID: "other-some-service-plan-guid",
   171  					},
   172  				}, ccv3.Warnings{"some-service-plan-warning"}, nil)
   173  			})
   174  
   175  			It("queries on the service plan name and the broker name", func() {
   176  				Expect(warnings).To(ConsistOf("some-service-plan-warning"))
   177  				Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
   178  				Expect(fakeCloudControllerClient.GetServicePlansArgsForCall(0)).To(ConsistOf(
   179  					ccv3.Query{Key: ccv3.NameFilter, Values: []string{servicePlanName}},
   180  					ccv3.Query{Key: ccv3.ServiceBrokerNamesFilter, Values: []string{serviceBrokerName}},
   181  				))
   182  			})
   183  
   184  			When("it returns multiple results", func() {
   185  				It("returns an error with the right hint", func() {
   186  					Expect(executionError).To(MatchError(actionerror.DuplicateServicePlanError{
   187  						Name:              servicePlanName,
   188  						ServiceBrokerName: serviceBrokerName,
   189  					}))
   190  				})
   191  			})
   192  		})
   193  
   194  		When("the broker and offering name are provided", func() {
   195  			BeforeEach(func() {
   196  				serviceOfferingName = "some-offering-name"
   197  				serviceBrokerName = "some-broker-name"
   198  				fakeCloudControllerClient.GetServicePlansReturns([]resources.ServicePlan{
   199  					{
   200  						Name: "some-service-plan",
   201  						GUID: "some-service-plan-guid",
   202  					},
   203  					{
   204  						Name: "some-service-plan",
   205  						GUID: "other-some-service-plan-guid",
   206  					},
   207  				}, ccv3.Warnings{"some-service-plan-warning"}, nil)
   208  			})
   209  
   210  			It("queries on the service plan, offering and broker names", func() {
   211  				Expect(warnings).To(ConsistOf("some-service-plan-warning"))
   212  				Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
   213  				Expect(fakeCloudControllerClient.GetServicePlansArgsForCall(0)).To(ConsistOf(
   214  					ccv3.Query{Key: ccv3.NameFilter, Values: []string{servicePlanName}},
   215  					ccv3.Query{Key: ccv3.ServiceOfferingNamesFilter, Values: []string{serviceOfferingName}},
   216  					ccv3.Query{Key: ccv3.ServiceBrokerNamesFilter, Values: []string{serviceBrokerName}},
   217  				))
   218  			})
   219  
   220  			When("it returns multiple results", func() {
   221  				It("returns an error with the right hint", func() {
   222  					Expect(executionError).To(MatchError(actionerror.DuplicateServicePlanError{
   223  						Name:                servicePlanName,
   224  						ServiceOfferingName: serviceOfferingName,
   225  						ServiceBrokerName:   serviceBrokerName,
   226  					}))
   227  				})
   228  			})
   229  		})
   230  	})
   231  })