github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/actors/servicebuilder/service_builder_test.go (about)

     1  package servicebuilder_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/actors/planbuilder/planbuilderfakes"
     7  	"code.cloudfoundry.org/cli/cf/actors/servicebuilder"
     8  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     9  
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("Service Builder", func() {
    17  	var (
    18  		planBuilder     *planbuilderfakes.FakePlanBuilder
    19  		serviceBuilder  servicebuilder.ServiceBuilder
    20  		serviceRepo     *apifakes.FakeServiceRepository
    21  		service1        models.ServiceOffering
    22  		service2        models.ServiceOffering
    23  		v1Service       models.ServiceOffering
    24  		planWithoutOrgs models.ServicePlanFields
    25  		plan1           models.ServicePlanFields
    26  		plan2           models.ServicePlanFields
    27  		plan3           models.ServicePlanFields
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		serviceRepo = new(apifakes.FakeServiceRepository)
    32  		planBuilder = new(planbuilderfakes.FakePlanBuilder)
    33  
    34  		serviceBuilder = servicebuilder.NewBuilder(serviceRepo, planBuilder)
    35  		service1 = models.ServiceOffering{
    36  			ServiceOfferingFields: models.ServiceOfferingFields{
    37  				Label:      "my-service1",
    38  				GUID:       "service-guid1",
    39  				BrokerGUID: "my-service-broker-guid1",
    40  			},
    41  		}
    42  
    43  		service2 = models.ServiceOffering{
    44  			ServiceOfferingFields: models.ServiceOfferingFields{
    45  				Label:      "my-service2",
    46  				GUID:       "service-guid2",
    47  				BrokerGUID: "my-service-broker-guid2",
    48  			},
    49  		}
    50  
    51  		v1Service = models.ServiceOffering{
    52  			ServiceOfferingFields: models.ServiceOfferingFields{
    53  				Label:      "v1Service",
    54  				GUID:       "v1Service-guid",
    55  				BrokerGUID: "my-service-broker-guid1",
    56  				Provider:   "IAmV1",
    57  			},
    58  		}
    59  
    60  		serviceOfferings := models.ServiceOfferings([]models.ServiceOffering{service1, v1Service})
    61  		serviceRepo.FindServiceOfferingsByLabelReturns(serviceOfferings, nil)
    62  		serviceRepo.GetServiceOfferingByGUIDReturns(service1, nil)
    63  		serviceRepo.ListServicesFromBrokerReturns([]models.ServiceOffering{service1}, nil)
    64  		serviceRepo.ListServicesFromManyBrokersReturns([]models.ServiceOffering{service1, service2}, nil)
    65  
    66  		plan1 = models.ServicePlanFields{
    67  			Name:                "service-plan1",
    68  			GUID:                "service-plan1-guid",
    69  			ServiceOfferingGUID: "service-guid1",
    70  			OrgNames:            []string{"org1", "org2"},
    71  		}
    72  
    73  		plan2 = models.ServicePlanFields{
    74  			Name:                "service-plan2",
    75  			GUID:                "service-plan2-guid",
    76  			ServiceOfferingGUID: "service-guid1",
    77  		}
    78  
    79  		plan3 = models.ServicePlanFields{
    80  			Name:                "service-plan3",
    81  			GUID:                "service-plan3-guid",
    82  			ServiceOfferingGUID: "service-guid2",
    83  		}
    84  
    85  		planWithoutOrgs = models.ServicePlanFields{
    86  			Name:                "service-plan-without-orgs",
    87  			GUID:                "service-plan-without-orgs-guid",
    88  			ServiceOfferingGUID: "service-guid1",
    89  		}
    90  
    91  		planBuilder.GetPlansVisibleToOrgReturns([]models.ServicePlanFields{plan1, plan2}, nil)
    92  		planBuilder.GetPlansForServiceWithOrgsReturns([]models.ServicePlanFields{plan1, plan2}, nil)
    93  		planBuilder.GetPlansForManyServicesWithOrgsReturns([]models.ServicePlanFields{plan1, plan2, plan3}, nil)
    94  		planBuilder.GetPlansForServiceForOrgReturns([]models.ServicePlanFields{plan1, plan2}, nil)
    95  	})
    96  
    97  	Describe(".GetServicesForSpace", func() {
    98  		BeforeEach(func() {
    99  			serviceRepo.GetServiceOfferingsForSpaceReturns([]models.ServiceOffering{service1, service1}, nil)
   100  		})
   101  
   102  		It("returns the services for the space", func() {
   103  			services, err := serviceBuilder.GetServicesForSpace("spaceGUID")
   104  			Expect(err).NotTo(HaveOccurred())
   105  
   106  			Expect(len(services)).To(Equal(2))
   107  		})
   108  	})
   109  
   110  	Describe(".GetServicesForSpaceWithPlans", func() {
   111  		BeforeEach(func() {
   112  			serviceRepo.GetServiceOfferingsForSpaceReturns([]models.ServiceOffering{service1, service1}, nil)
   113  			planBuilder.GetPlansForServiceReturns([]models.ServicePlanFields{planWithoutOrgs}, nil)
   114  		})
   115  
   116  		It("returns the services for the space, populated with plans", func() {
   117  			services, err := serviceBuilder.GetServicesForSpaceWithPlans("spaceGUID")
   118  			Expect(err).NotTo(HaveOccurred())
   119  
   120  			Expect(len(services)).To(Equal(2))
   121  			Expect(services[0].Plans[0]).To(Equal(planWithoutOrgs))
   122  			Expect(services[1].Plans[0]).To(Equal(planWithoutOrgs))
   123  		})
   124  	})
   125  
   126  	Describe(".GetAllServices", func() {
   127  		BeforeEach(func() {
   128  			serviceRepo.GetAllServiceOfferingsReturns([]models.ServiceOffering{service1, service1}, nil)
   129  		})
   130  
   131  		It("returns the named service, populated with plans", func() {
   132  			services, err := serviceBuilder.GetAllServices()
   133  			Expect(err).NotTo(HaveOccurred())
   134  
   135  			Expect(len(services)).To(Equal(2))
   136  		})
   137  	})
   138  
   139  	Describe(".GetAllServicesWithPlans", func() {
   140  		BeforeEach(func() {
   141  			serviceRepo.GetAllServiceOfferingsReturns([]models.ServiceOffering{service1, service1}, nil)
   142  			planBuilder.GetPlansForServiceReturns([]models.ServicePlanFields{plan1}, nil)
   143  		})
   144  
   145  		It("returns the named service, populated with plans", func() {
   146  			services, err := serviceBuilder.GetAllServicesWithPlans()
   147  			Expect(err).NotTo(HaveOccurred())
   148  
   149  			Expect(len(services)).To(Equal(2))
   150  			Expect(services[0].Plans[0]).To(Equal(plan1))
   151  		})
   152  	})
   153  
   154  	Describe(".GetServiceByNameWithPlans", func() {
   155  		BeforeEach(func() {
   156  			planBuilder.GetPlansForServiceReturns([]models.ServicePlanFields{plan2}, nil)
   157  		})
   158  
   159  		It("returns the named service, populated with plans", func() {
   160  			service, err := serviceBuilder.GetServiceByNameWithPlans("my-service1")
   161  			Expect(err).NotTo(HaveOccurred())
   162  
   163  			Expect(len(service.Plans)).To(Equal(1))
   164  			Expect(service.Plans[0].Name).To(Equal("service-plan2"))
   165  			Expect(service.Plans[0].OrgNames).To(BeNil())
   166  		})
   167  	})
   168  
   169  	Describe(".GetServiceByNameWithPlansWithOrgNames", func() {
   170  		It("returns the named service, populated with plans", func() {
   171  			service, err := serviceBuilder.GetServiceByNameWithPlansWithOrgNames("my-service1")
   172  			Expect(err).NotTo(HaveOccurred())
   173  
   174  			Expect(len(service.Plans)).To(Equal(2))
   175  			Expect(service.Plans[0].Name).To(Equal("service-plan1"))
   176  			Expect(service.Plans[1].Name).To(Equal("service-plan2"))
   177  			Expect(service.Plans[0].OrgNames).To(Equal([]string{"org1", "org2"}))
   178  		})
   179  	})
   180  
   181  	Describe(".GetServiceByNameForSpace", func() {
   182  		Context("mixed v2 and v1 services", func() {
   183  			BeforeEach(func() {
   184  				service2 := models.ServiceOffering{
   185  					ServiceOfferingFields: models.ServiceOfferingFields{
   186  						Label: "service",
   187  						GUID:  "service-guid-v2",
   188  					},
   189  				}
   190  
   191  				service1 := models.ServiceOffering{
   192  					ServiceOfferingFields: models.ServiceOfferingFields{
   193  						Label:    "service",
   194  						GUID:     "service-guid",
   195  						Provider: "a provider",
   196  					},
   197  				}
   198  
   199  				serviceRepo.FindServiceOfferingsForSpaceByLabelReturns(
   200  					models.ServiceOfferings([]models.ServiceOffering{service1, service2}),
   201  					nil,
   202  				)
   203  			})
   204  
   205  			It("returns the nv2 service", func() {
   206  				service, err := serviceBuilder.GetServiceByNameForSpace("service", "spaceGUID")
   207  				Expect(err).NotTo(HaveOccurred())
   208  
   209  				Expect(len(service.Plans)).To(Equal(0))
   210  				Expect(service.GUID).To(Equal("service-guid-v2"))
   211  			})
   212  		})
   213  
   214  		Context("v2 services", func() {
   215  			BeforeEach(func() {
   216  				service := models.ServiceOffering{
   217  					ServiceOfferingFields: models.ServiceOfferingFields{
   218  						Label: "service",
   219  						GUID:  "service-guid",
   220  					},
   221  				}
   222  
   223  				serviceRepo.FindServiceOfferingsForSpaceByLabelReturns(
   224  					models.ServiceOfferings([]models.ServiceOffering{service}),
   225  					nil,
   226  				)
   227  			})
   228  
   229  			It("returns the named service", func() {
   230  				service, err := serviceBuilder.GetServiceByNameForSpace("service", "spaceGUID")
   231  				Expect(err).NotTo(HaveOccurred())
   232  
   233  				Expect(len(service.Plans)).To(Equal(0))
   234  				Expect(service.GUID).To(Equal("service-guid"))
   235  			})
   236  		})
   237  
   238  		Context("v1 services", func() {
   239  			BeforeEach(func() {
   240  				service := models.ServiceOffering{
   241  					ServiceOfferingFields: models.ServiceOfferingFields{
   242  						Label:    "service",
   243  						GUID:     "service-guid",
   244  						Provider: "a provider",
   245  					},
   246  				}
   247  
   248  				serviceRepo.FindServiceOfferingsForSpaceByLabelReturns(
   249  					models.ServiceOfferings([]models.ServiceOffering{service}),
   250  					nil,
   251  				)
   252  			})
   253  
   254  			It("returns the an error", func() {
   255  				service, err := serviceBuilder.GetServiceByNameForSpace("service", "spaceGUID")
   256  				Expect(service).To(Equal(models.ServiceOffering{}))
   257  				Expect(err).To(HaveOccurred())
   258  			})
   259  		})
   260  	})
   261  
   262  	Describe(".GetServiceByNameForSpaceWithPlans", func() {
   263  		BeforeEach(func() {
   264  			service := models.ServiceOffering{
   265  				ServiceOfferingFields: models.ServiceOfferingFields{
   266  					Label: "serviceWithPlans",
   267  				},
   268  			}
   269  
   270  			serviceRepo.FindServiceOfferingsForSpaceByLabelReturns(
   271  				models.ServiceOfferings([]models.ServiceOffering{service}),
   272  				nil,
   273  			)
   274  			planBuilder.GetPlansForServiceReturns([]models.ServicePlanFields{planWithoutOrgs}, nil)
   275  		})
   276  
   277  		It("returns the named service", func() {
   278  			service, err := serviceBuilder.GetServiceByNameForSpaceWithPlans("serviceWithPlans", "spaceGUID")
   279  			Expect(err).NotTo(HaveOccurred())
   280  
   281  			Expect(len(service.Plans)).To(Equal(1))
   282  			Expect(service.Plans[0].Name).To(Equal("service-plan-without-orgs"))
   283  			Expect(service.Plans[0].OrgNames).To(BeNil())
   284  		})
   285  	})
   286  
   287  	Describe(".GetServicesByNameForSpaceWithPlans", func() {
   288  		BeforeEach(func() {
   289  			serviceRepo.FindServiceOfferingsForSpaceByLabelReturns(
   290  				models.ServiceOfferings([]models.ServiceOffering{service1, v1Service}),
   291  				nil,
   292  			)
   293  
   294  			planBuilder.GetPlansForServiceReturns([]models.ServicePlanFields{planWithoutOrgs}, nil)
   295  		})
   296  
   297  		It("returns the named service", func() {
   298  			services, err := serviceBuilder.GetServicesByNameForSpaceWithPlans("serviceWithPlans", "spaceGUID")
   299  			Expect(err).NotTo(HaveOccurred())
   300  
   301  			Expect(len(services)).To(Equal(2))
   302  			Expect(services[0].Label).To(Equal("my-service1"))
   303  			Expect(services[0].Plans[0].Name).To(Equal("service-plan-without-orgs"))
   304  			Expect(services[0].Plans[0].OrgNames).To(BeNil())
   305  			Expect(services[1].Label).To(Equal("v1Service"))
   306  			Expect(services[1].Plans[0].Name).To(Equal("service-plan-without-orgs"))
   307  			Expect(services[1].Plans[0].OrgNames).To(BeNil())
   308  		})
   309  	})
   310  
   311  	Describe(".GetServiceByNameForOrg", func() {
   312  		It("returns the named service, populated with plans", func() {
   313  			service, err := serviceBuilder.GetServiceByNameForOrg("my-service1", "org1")
   314  			Expect(err).NotTo(HaveOccurred())
   315  
   316  			Expect(planBuilder.GetPlansForServiceForOrgCallCount()).To(Equal(1))
   317  			servName, orgName := planBuilder.GetPlansForServiceForOrgArgsForCall(0)
   318  			Expect(servName).To(Equal("service-guid1"))
   319  			Expect(orgName).To(Equal("org1"))
   320  
   321  			Expect(len(service.Plans)).To(Equal(2))
   322  			Expect(service.Plans[0].Name).To(Equal("service-plan1"))
   323  			Expect(service.Plans[1].Name).To(Equal("service-plan2"))
   324  			Expect(service.Plans[0].OrgNames).To(Equal([]string{"org1", "org2"}))
   325  		})
   326  	})
   327  
   328  	Describe(".GetServicesForBroker", func() {
   329  		It("returns all the services for a broker, fully populated", func() {
   330  			services, err := serviceBuilder.GetServicesForBroker("my-service-broker-guid1")
   331  			Expect(err).NotTo(HaveOccurred())
   332  
   333  			service := services[0]
   334  			Expect(service.Label).To(Equal("my-service1"))
   335  			Expect(len(service.Plans)).To(Equal(2))
   336  			Expect(service.Plans[0].Name).To(Equal("service-plan1"))
   337  			Expect(service.Plans[1].Name).To(Equal("service-plan2"))
   338  			Expect(service.Plans[0].OrgNames).To(Equal([]string{"org1", "org2"}))
   339  		})
   340  	})
   341  
   342  	Describe(".GetServicesForManyBrokers", func() {
   343  		It("returns all the services for an array of broker guids, fully populated", func() {
   344  			brokerGUIDs := []string{"my-service-broker-guid1", "my-service-broker-guid2"}
   345  			services, err := serviceBuilder.GetServicesForManyBrokers(brokerGUIDs)
   346  			Expect(err).NotTo(HaveOccurred())
   347  
   348  			Expect(services).To(HaveLen(2))
   349  
   350  			broker_service := services[0]
   351  			Expect(broker_service.Label).To(Equal("my-service1"))
   352  			Expect(len(broker_service.Plans)).To(Equal(2))
   353  			Expect(broker_service.Plans[0].Name).To(Equal("service-plan1"))
   354  			Expect(broker_service.Plans[1].Name).To(Equal("service-plan2"))
   355  			Expect(broker_service.Plans[0].OrgNames).To(Equal([]string{"org1", "org2"}))
   356  
   357  			broker_service2 := services[1]
   358  			Expect(broker_service2.Label).To(Equal("my-service2"))
   359  			Expect(len(broker_service2.Plans)).To(Equal(1))
   360  			Expect(broker_service2.Plans[0].Name).To(Equal("service-plan3"))
   361  		})
   362  
   363  		It("raises errors from the service repo", func() {
   364  			serviceRepo.ListServicesFromManyBrokersReturns([]models.ServiceOffering{}, errors.New("error"))
   365  			brokerGUIDs := []string{"my-service-broker-guid1", "my-service-broker-guid2"}
   366  			_, err := serviceBuilder.GetServicesForManyBrokers(brokerGUIDs)
   367  			Expect(err).To(HaveOccurred())
   368  		})
   369  
   370  		It("raises errors from the plan builder", func() {
   371  			planBuilder.GetPlansForManyServicesWithOrgsReturns(nil, errors.New("error"))
   372  			brokerGUIDs := []string{"my-service-broker-guid1", "my-service-broker-guid2"}
   373  			_, err := serviceBuilder.GetServicesForManyBrokers(brokerGUIDs)
   374  			Expect(err).To(HaveOccurred())
   375  		})
   376  	})
   377  
   378  	Describe(".GetServiceVisibleToOrg", func() {
   379  		It("Returns a service populated with plans visible to the provided org", func() {
   380  			service, err := serviceBuilder.GetServiceVisibleToOrg("my-service1", "org1")
   381  			Expect(err).NotTo(HaveOccurred())
   382  
   383  			Expect(service.Label).To(Equal("my-service1"))
   384  			Expect(len(service.Plans)).To(Equal(2))
   385  			Expect(service.Plans[0].Name).To(Equal("service-plan1"))
   386  			Expect(service.Plans[0].OrgNames).To(Equal([]string{"org1", "org2"}))
   387  		})
   388  
   389  		Context("When no plans are visible to the provided org", func() {
   390  			It("Returns nil", func() {
   391  				planBuilder.GetPlansVisibleToOrgReturns(nil, nil)
   392  				service, err := serviceBuilder.GetServiceVisibleToOrg("my-service1", "org3")
   393  				Expect(err).NotTo(HaveOccurred())
   394  
   395  				Expect(service).To(Equal(models.ServiceOffering{}))
   396  			})
   397  		})
   398  	})
   399  
   400  	Describe(".GetServicesVisibleToOrg", func() {
   401  		It("Returns services with plans visible to the provided org", func() {
   402  			planBuilder.GetPlansVisibleToOrgReturns([]models.ServicePlanFields{plan1, plan2}, nil)
   403  			services, err := serviceBuilder.GetServicesVisibleToOrg("org1")
   404  			Expect(err).NotTo(HaveOccurred())
   405  
   406  			service := services[0]
   407  			Expect(service.Label).To(Equal("my-service1"))
   408  			Expect(len(service.Plans)).To(Equal(2))
   409  			Expect(service.Plans[0].Name).To(Equal("service-plan1"))
   410  			Expect(service.Plans[0].OrgNames).To(Equal([]string{"org1", "org2"}))
   411  		})
   412  
   413  		Context("When no plans are visible to the provided org", func() {
   414  			It("Returns nil", func() {
   415  				planBuilder.GetPlansVisibleToOrgReturns(nil, nil)
   416  				services, err := serviceBuilder.GetServicesVisibleToOrg("org3")
   417  				Expect(err).NotTo(HaveOccurred())
   418  
   419  				Expect(services).To(BeNil())
   420  			})
   421  		})
   422  	})
   423  })