github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+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  	Describe("GetServicePlan", func() {
    26  		var (
    27  			servicePlan         ServicePlan
    28  			servicePlanWarnings Warnings
    29  			servicePlanErr      error
    30  		)
    31  
    32  		JustBeforeEach(func() {
    33  			servicePlan, servicePlanWarnings, servicePlanErr = actor.GetServicePlan("some-service-plan-guid")
    34  		})
    35  
    36  		When("the service plan exists", func() {
    37  			var returnedServicePlan ccv2.ServicePlan
    38  
    39  			BeforeEach(func() {
    40  				returnedServicePlan = ccv2.ServicePlan{
    41  					GUID: "some-service-plan-guid",
    42  					Name: "some-service-plan",
    43  				}
    44  				fakeCloudControllerClient.GetServicePlanReturns(
    45  					returnedServicePlan,
    46  					ccv2.Warnings{"get-service-plan-warning"},
    47  					nil)
    48  			})
    49  
    50  			It("returns the service plan and all warnings", func() {
    51  				Expect(servicePlanErr).ToNot(HaveOccurred())
    52  				Expect(servicePlan).To(Equal(ServicePlan(returnedServicePlan)))
    53  				Expect(servicePlanWarnings).To(ConsistOf("get-service-plan-warning"))
    54  
    55  				Expect(fakeCloudControllerClient.GetServicePlanCallCount()).To(Equal(1))
    56  				Expect(fakeCloudControllerClient.GetServicePlanArgsForCall(0)).To(Equal("some-service-plan-guid"))
    57  			})
    58  		})
    59  
    60  		When("an error is encountered getting the service plan", func() {
    61  			var expectedErr error
    62  
    63  			BeforeEach(func() {
    64  				expectedErr = errors.New("some-error")
    65  				fakeCloudControllerClient.GetServicePlanReturns(
    66  					ccv2.ServicePlan{},
    67  					ccv2.Warnings{"get-service-plan-warning"},
    68  					expectedErr)
    69  			})
    70  
    71  			It("returns the errors and all warnings", func() {
    72  				Expect(servicePlanErr).To(MatchError(expectedErr))
    73  				Expect(servicePlan).To(Equal(ServicePlan{}))
    74  				Expect(servicePlanWarnings).To(ConsistOf("get-service-plan-warning"))
    75  
    76  				Expect(fakeCloudControllerClient.GetServicePlanCallCount()).To(Equal(1))
    77  				Expect(fakeCloudControllerClient.GetServicePlanArgsForCall(0)).To(Equal("some-service-plan-guid"))
    78  			})
    79  		})
    80  	})
    81  
    82  	Describe("GetServicePlansForService", func() {
    83  		var (
    84  			servicePlans        []ServicePlan
    85  			servicePlanWarnings Warnings
    86  			servicePlanErr      error
    87  		)
    88  
    89  		JustBeforeEach(func() {
    90  			servicePlans, servicePlanWarnings, servicePlanErr = actor.GetServicePlansForService("some-service")
    91  		})
    92  
    93  		When("there is a service with plans", func() {
    94  			BeforeEach(func() {
    95  				services := []ccv2.Service{
    96  					{
    97  						GUID:        "some-service-guid",
    98  						Label:       "some-service",
    99  						Description: "service-description",
   100  					},
   101  				}
   102  
   103  				plans := []ccv2.ServicePlan{
   104  					{Name: "plan-a"},
   105  					{Name: "plan-b"},
   106  				}
   107  
   108  				fakeCloudControllerClient.GetServicesReturns(services, ccv2.Warnings{"get-services-warning"}, nil)
   109  				fakeCloudControllerClient.GetServicePlansReturns(plans, ccv2.Warnings{"get-plans-warning"}, nil)
   110  			})
   111  
   112  			It("returns all plans and warnings", func() {
   113  				Expect(servicePlanErr).NotTo(HaveOccurred())
   114  				Expect(servicePlans).To(Equal([]ServicePlan{
   115  					{
   116  						Name: "plan-a",
   117  					},
   118  					{
   119  						Name: "plan-b",
   120  					},
   121  				},
   122  				))
   123  				Expect(servicePlanWarnings).To(ConsistOf("get-services-warning", "get-plans-warning"))
   124  				Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
   125  				Expect(fakeCloudControllerClient.GetServicePlansArgsForCall(0)).To(Equal([]ccv2.Filter{{
   126  					Type:     constant.ServiceGUIDFilter,
   127  					Operator: constant.EqualOperator,
   128  					Values:   []string{"some-service-guid"},
   129  				}}))
   130  			})
   131  		})
   132  
   133  		When("GetServices returns an error and warnings", func() {
   134  			BeforeEach(func() {
   135  				fakeCloudControllerClient.GetServicesReturns(nil, ccv2.Warnings{"get-service-warning"}, errors.New("service-error"))
   136  			})
   137  
   138  			It("propagates the error and all warnings", func() {
   139  				Expect(servicePlanErr).To(MatchError(errors.New("service-error")))
   140  				Expect(servicePlanWarnings).To(ConsistOf("get-service-warning"))
   141  			})
   142  		})
   143  
   144  		When("GetServicePlans returns an error and warnings", func() {
   145  			BeforeEach(func() {
   146  				fakeCloudControllerClient.GetServicesReturns([]ccv2.Service{{}}, ccv2.Warnings{"get-service-warning"}, nil)
   147  				fakeCloudControllerClient.GetServicePlansReturns(nil, ccv2.Warnings{"get-plans-warning"}, errors.New("plans-error"))
   148  			})
   149  
   150  			It("propagates the error and all warnings", func() {
   151  				Expect(servicePlanErr).To(MatchError(errors.New("plans-error")))
   152  				Expect(servicePlanWarnings).To(ConsistOf("get-service-warning", "get-plans-warning"))
   153  			})
   154  		})
   155  	})
   156  })