github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/v7action/marketplace_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "code.cloudfoundry.org/cli/actor/v7action"
     7  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     9  	"code.cloudfoundry.org/cli/resources"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("marketplace", func() {
    15  	var (
    16  		actor                     *Actor
    17  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
    22  		actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil)
    23  	})
    24  
    25  	Describe("Marketplace", func() {
    26  		BeforeEach(func() {
    27  			fakeCloudControllerClient.GetServicePlansWithOfferingsReturns(
    28  				[]ccv3.ServiceOfferingWithPlans{
    29  					{
    30  						GUID:              "offering-guid-1",
    31  						Name:              "offering-1",
    32  						Description:       "about offering 1",
    33  						ServiceBrokerName: "service-broker-1",
    34  						Plans: []resources.ServicePlan{
    35  							{
    36  								GUID: "plan-guid-1",
    37  								Name: "plan-1",
    38  							},
    39  						},
    40  					},
    41  					{
    42  						GUID:              "offering-guid-2",
    43  						Name:              "offering-2",
    44  						Description:       "about offering 2",
    45  						ServiceBrokerName: "service-broker-2",
    46  						Plans: []resources.ServicePlan{
    47  							{
    48  								GUID: "plan-guid-2",
    49  								Name: "plan-2",
    50  							},
    51  							{
    52  								GUID: "plan-guid-3",
    53  								Name: "plan-3",
    54  							},
    55  						},
    56  					},
    57  				},
    58  				ccv3.Warnings{"foo", "bar"},
    59  				nil,
    60  			)
    61  		})
    62  
    63  		It("calls the client", func() {
    64  			_, _, _ = actor.Marketplace(MarketplaceFilter{})
    65  
    66  			Expect(fakeCloudControllerClient.GetServicePlansWithOfferingsCallCount()).To(Equal(1))
    67  			queries := fakeCloudControllerClient.GetServicePlansWithOfferingsArgsForCall(0)
    68  			Expect(queries).To(ConsistOf(
    69  				ccv3.Query{Key: ccv3.AvailableFilter, Values: []string{"true"}},
    70  				ccv3.Query{Key: ccv3.PerPage, Values: []string{ccv3.MaxPerPage}},
    71  			))
    72  		})
    73  
    74  		It("returns a list of service offerings and plans", func() {
    75  			offerings, warnings, err := actor.Marketplace(MarketplaceFilter{})
    76  			Expect(err).NotTo(HaveOccurred())
    77  			Expect(warnings).To(ConsistOf("foo", "bar"))
    78  			Expect(offerings).To(Equal([]ServiceOfferingWithPlans{
    79  				{
    80  					GUID:              "offering-guid-1",
    81  					Name:              "offering-1",
    82  					Description:       "about offering 1",
    83  					ServiceBrokerName: "service-broker-1",
    84  					Plans: []resources.ServicePlan{
    85  						{
    86  							GUID: "plan-guid-1",
    87  							Name: "plan-1",
    88  						},
    89  					},
    90  				},
    91  				{
    92  					GUID:              "offering-guid-2",
    93  					Name:              "offering-2",
    94  					Description:       "about offering 2",
    95  					ServiceBrokerName: "service-broker-2",
    96  					Plans: []resources.ServicePlan{
    97  						{
    98  							GUID: "plan-guid-2",
    99  							Name: "plan-2",
   100  						},
   101  						{
   102  							GUID: "plan-guid-3",
   103  							Name: "plan-3",
   104  						},
   105  					},
   106  				},
   107  			}))
   108  		})
   109  
   110  		When("a space GUID is specified", func() {
   111  			It("adds the GUID to the query", func() {
   112  				_, _, _ = actor.Marketplace(MarketplaceFilter{SpaceGUID: "space-guid"})
   113  
   114  				Expect(fakeCloudControllerClient.GetServicePlansWithOfferingsCallCount()).To(Equal(1))
   115  				queries := fakeCloudControllerClient.GetServicePlansWithOfferingsArgsForCall(0)
   116  				Expect(queries).To(ContainElement(ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{"space-guid"}}))
   117  			})
   118  		})
   119  
   120  		When("a service offering name is specified", func() {
   121  			It("adds the service offering name to the query", func() {
   122  				_, _, _ = actor.Marketplace(MarketplaceFilter{ServiceOfferingName: "my-service-offering"})
   123  
   124  				Expect(fakeCloudControllerClient.GetServicePlansWithOfferingsCallCount()).To(Equal(1))
   125  				queries := fakeCloudControllerClient.GetServicePlansWithOfferingsArgsForCall(0)
   126  				Expect(queries).To(ContainElement(ccv3.Query{Key: ccv3.ServiceOfferingNamesFilter, Values: []string{"my-service-offering"}}))
   127  			})
   128  		})
   129  
   130  		When("a service broker name is specified", func() {
   131  			It("adds the service broker name to the query", func() {
   132  				_, _, _ = actor.Marketplace(MarketplaceFilter{ServiceBrokerName: "my-service-broker"})
   133  
   134  				Expect(fakeCloudControllerClient.GetServicePlansWithOfferingsCallCount()).To(Equal(1))
   135  				queries := fakeCloudControllerClient.GetServicePlansWithOfferingsArgsForCall(0)
   136  				Expect(queries).To(ContainElement(ccv3.Query{Key: ccv3.ServiceBrokerNamesFilter, Values: []string{"my-service-broker"}}))
   137  			})
   138  		})
   139  
   140  		When("the show unavailable filter is specified", func() {
   141  			It("does not add the `available` query parameter", func() {
   142  				_, _, _ = actor.Marketplace(MarketplaceFilter{ShowUnavailable: true})
   143  
   144  				Expect(fakeCloudControllerClient.GetServicePlansWithOfferingsCallCount()).To(Equal(1))
   145  				queries := fakeCloudControllerClient.GetServicePlansWithOfferingsArgsForCall(0)
   146  				Expect(queries).NotTo(ContainElement(ccv3.Query{Key: ccv3.AvailableFilter, Values: []string{"true"}}))
   147  			})
   148  		})
   149  
   150  		When("all filters are specified", func() {
   151  			It("adds all the filters to the query", func() {
   152  				_, _, _ = actor.Marketplace(MarketplaceFilter{
   153  					SpaceGUID:           "space-guid",
   154  					ServiceBrokerName:   "my-service-broker",
   155  					ServiceOfferingName: "my-service-offering",
   156  				})
   157  
   158  				Expect(fakeCloudControllerClient.GetServicePlansWithOfferingsCallCount()).To(Equal(1))
   159  				queries := fakeCloudControllerClient.GetServicePlansWithOfferingsArgsForCall(0)
   160  				Expect(queries).To(ConsistOf(
   161  					ccv3.Query{Key: ccv3.ServiceBrokerNamesFilter, Values: []string{"my-service-broker"}},
   162  					ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{"space-guid"}},
   163  					ccv3.Query{Key: ccv3.ServiceOfferingNamesFilter, Values: []string{"my-service-offering"}},
   164  					ccv3.Query{Key: ccv3.AvailableFilter, Values: []string{"true"}},
   165  					ccv3.Query{Key: ccv3.PerPage, Values: []string{ccv3.MaxPerPage}},
   166  				))
   167  			})
   168  		})
   169  
   170  		When("the client returns an error", func() {
   171  			BeforeEach(func() {
   172  				fakeCloudControllerClient.GetServicePlansWithOfferingsReturns(
   173  					[]ccv3.ServiceOfferingWithPlans{{}},
   174  					ccv3.Warnings{"foo", "bar"},
   175  					errors.New("bang"),
   176  				)
   177  			})
   178  
   179  			It("fails", func() {
   180  				offerings, warnings, err := actor.Marketplace(MarketplaceFilter{})
   181  				Expect(err).To(MatchError("bang"))
   182  				Expect(warnings).To(ConsistOf("foo", "bar"))
   183  				Expect(offerings).To(BeNil())
   184  			})
   185  		})
   186  	})
   187  })