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

     1  package planbuilder_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/actors/planbuilder"
     7  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     8  	"code.cloudfoundry.org/cli/cf/api/organizations/organizationsfakes"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("Plan builder", func() {
    15  	var (
    16  		builder planbuilder.PlanBuilder
    17  
    18  		planRepo       *apifakes.OldFakeServicePlanRepo
    19  		visibilityRepo *apifakes.FakeServicePlanVisibilityRepository
    20  		orgRepo        *organizationsfakes.FakeOrganizationRepository
    21  
    22  		plan1 models.ServicePlanFields
    23  		plan2 models.ServicePlanFields
    24  
    25  		org1 models.Organization
    26  		org2 models.Organization
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		planbuilder.PlanToOrgsVisibilityMap = nil
    31  		planbuilder.OrgToPlansVisibilityMap = nil
    32  		planRepo = new(apifakes.OldFakeServicePlanRepo)
    33  		visibilityRepo = new(apifakes.FakeServicePlanVisibilityRepository)
    34  		orgRepo = new(organizationsfakes.FakeOrganizationRepository)
    35  		builder = planbuilder.NewBuilder(planRepo, visibilityRepo, orgRepo)
    36  
    37  		plan1 = models.ServicePlanFields{
    38  			Name:                "service-plan1",
    39  			GUID:                "service-plan1-guid",
    40  			ServiceOfferingGUID: "service-guid1",
    41  		}
    42  		plan2 = models.ServicePlanFields{
    43  			Name:                "service-plan2",
    44  			GUID:                "service-plan2-guid",
    45  			ServiceOfferingGUID: "service-guid1",
    46  		}
    47  
    48  		planRepo.SearchReturns = map[string][]models.ServicePlanFields{
    49  			"service-guid1": {plan1, plan2},
    50  		}
    51  		org1 = models.Organization{}
    52  		org1.Name = "org1"
    53  		org1.GUID = "org1-guid"
    54  
    55  		org2 = models.Organization{}
    56  		org2.Name = "org2"
    57  		org2.GUID = "org2-guid"
    58  		visibilityRepo.ListReturns([]models.ServicePlanVisibilityFields{
    59  			{ServicePlanGUID: "service-plan1-guid", OrganizationGUID: "org1-guid"},
    60  			{ServicePlanGUID: "service-plan1-guid", OrganizationGUID: "org2-guid"},
    61  			{ServicePlanGUID: "service-plan2-guid", OrganizationGUID: "org1-guid"},
    62  		}, nil)
    63  		visibilityRepo.SearchReturns([]models.ServicePlanVisibilityFields{
    64  			{ServicePlanGUID: "service-plan1-guid", OrganizationGUID: "org1-guid"},
    65  			{ServicePlanGUID: "service-plan2-guid", OrganizationGUID: "org1-guid"},
    66  		}, nil)
    67  		orgRepo.GetManyOrgsByGUIDReturns([]models.Organization{org1, org2}, nil)
    68  	})
    69  
    70  	Describe(".AttachOrgsToPlans", func() {
    71  		It("returns plans fully populated with the orgnames that have visibility", func() {
    72  			barePlans := []models.ServicePlanFields{plan1, plan2}
    73  
    74  			plans, err := builder.AttachOrgsToPlans(barePlans)
    75  			Expect(err).ToNot(HaveOccurred())
    76  
    77  			Expect(plans[0].OrgNames).To(Equal([]string{"org1", "org2"}))
    78  		})
    79  	})
    80  
    81  	Describe(".AttachOrgToPlans", func() {
    82  		It("returns plans fully populated with the orgnames that have visibility", func() {
    83  			orgRepo.FindByNameReturns(org1, nil)
    84  			barePlans := []models.ServicePlanFields{plan1, plan2}
    85  
    86  			plans, err := builder.AttachOrgToPlans(barePlans, "org1")
    87  			Expect(err).ToNot(HaveOccurred())
    88  
    89  			Expect(plans[0].OrgNames).To(Equal([]string{"org1"}))
    90  		})
    91  	})
    92  
    93  	Describe(".GetPlansForServiceWithOrgs", func() {
    94  		It("returns all the plans for the service with the provided guid", func() {
    95  			plans, err := builder.GetPlansForServiceWithOrgs("service-guid1")
    96  			Expect(err).ToNot(HaveOccurred())
    97  
    98  			Expect(len(plans)).To(Equal(2))
    99  			Expect(plans[0].Name).To(Equal("service-plan1"))
   100  			Expect(plans[0].OrgNames).To(Equal([]string{"org1", "org2"}))
   101  			Expect(plans[1].Name).To(Equal("service-plan2"))
   102  		})
   103  	})
   104  
   105  	Describe(".GetPlansForManyServicesWithOrgs", func() {
   106  		It("returns all the plans for all service in a list of guids", func() {
   107  			planRepo.ListPlansFromManyServicesReturns = []models.ServicePlanFields{
   108  				plan1, plan2,
   109  			}
   110  			serviceGUIDs := []string{"service-guid1", "service-guid2"}
   111  			plans, err := builder.GetPlansForManyServicesWithOrgs(serviceGUIDs)
   112  			Expect(err).ToNot(HaveOccurred())
   113  			Expect(orgRepo.GetManyOrgsByGUIDCallCount()).To(Equal(1))
   114  			Expect(orgRepo.GetManyOrgsByGUIDArgsForCall(0)).To(ConsistOf("org1-guid", "org2-guid"))
   115  
   116  			Expect(len(plans)).To(Equal(2))
   117  			Expect(plans[0].Name).To(Equal("service-plan1"))
   118  			Expect(plans[0].OrgNames).To(Equal([]string{"org1", "org2"}))
   119  			Expect(plans[1].Name).To(Equal("service-plan2"))
   120  		})
   121  
   122  		It("returns errors from the service plan repo", func() {
   123  			planRepo.ListPlansFromManyServicesError = errors.New("Error")
   124  			serviceGUIDs := []string{"service-guid1", "service-guid2"}
   125  			_, err := builder.GetPlansForManyServicesWithOrgs(serviceGUIDs)
   126  			Expect(err).To(HaveOccurred())
   127  		})
   128  	})
   129  
   130  	Describe(".GetPlansForService", func() {
   131  		It("returns all the plans for the service with the provided guid", func() {
   132  			plans, err := builder.GetPlansForService("service-guid1")
   133  			Expect(err).ToNot(HaveOccurred())
   134  
   135  			Expect(len(plans)).To(Equal(2))
   136  			Expect(plans[0].Name).To(Equal("service-plan1"))
   137  			Expect(plans[0].OrgNames).To(BeNil())
   138  			Expect(plans[1].Name).To(Equal("service-plan2"))
   139  		})
   140  	})
   141  
   142  	Describe(".GetPlansForServiceForOrg", func() {
   143  		It("returns all the plans for the service with the provided guid", func() {
   144  			orgRepo.FindByNameReturns(org1, nil)
   145  			plans, err := builder.GetPlansForServiceForOrg("service-guid1", "org1")
   146  			Expect(err).ToNot(HaveOccurred())
   147  
   148  			Expect(len(plans)).To(Equal(2))
   149  			Expect(plans[0].Name).To(Equal("service-plan1"))
   150  			Expect(plans[0].OrgNames).To(Equal([]string{"org1"}))
   151  			Expect(plans[1].Name).To(Equal("service-plan2"))
   152  		})
   153  	})
   154  
   155  	Describe(".GetPlansVisibleToOrg", func() {
   156  		It("returns all the plans visible to the named org", func() {
   157  			plans, err := builder.GetPlansVisibleToOrg("org1")
   158  			Expect(err).ToNot(HaveOccurred())
   159  
   160  			Expect(len(plans)).To(Equal(2))
   161  			Expect(plans[0].Name).To(Equal("service-plan1"))
   162  			Expect(plans[0].OrgNames).To(Equal([]string{"org1", "org2"}))
   163  		})
   164  	})
   165  })