github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/actor/v7action/label_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/resources"
     5  	"errors"
     6  
     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/types"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("UpdateApplicationLabelsByApplicationName", func() {
    16  	var (
    17  		actor                     *Actor
    18  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    19  		fakeSharedActor           *v7actionfakes.FakeSharedActor
    20  		fakeConfig                *v7actionfakes.FakeConfig
    21  		warnings                  Warnings
    22  		executeErr                error
    23  		resourceName              string
    24  		spaceGUID                 string
    25  		labels                    map[string]types.NullString
    26  	)
    27  
    28  	BeforeEach(func() {
    29  		fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
    30  		fakeSharedActor = new(v7actionfakes.FakeSharedActor)
    31  		fakeConfig = new(v7actionfakes.FakeConfig)
    32  		actor = NewActor(fakeCloudControllerClient, fakeConfig, fakeSharedActor, nil)
    33  	})
    34  
    35  	Context("UpdateApplicationLabelsByApplicationName", func() {
    36  		JustBeforeEach(func() {
    37  			warnings, executeErr = actor.UpdateApplicationLabelsByApplicationName(resourceName, spaceGUID, labels)
    38  		})
    39  
    40  		When("there are no client errors", func() {
    41  			BeforeEach(func() {
    42  				fakeCloudControllerClient.GetApplicationsReturns(
    43  					[]resources.Application{resources.Application{GUID: "some-guid"}},
    44  					ccv3.Warnings([]string{"warning-1", "warning-2"}),
    45  					nil,
    46  				)
    47  				fakeCloudControllerClient.UpdateApplicationReturns(
    48  					resources.Application{},
    49  					ccv3.Warnings{"set-app-labels-warnings"},
    50  					nil,
    51  				)
    52  			})
    53  
    54  			It("sets the app labels", func() {
    55  				Expect(fakeCloudControllerClient.UpdateApplicationCallCount()).To(Equal(1))
    56  				sentApp := fakeCloudControllerClient.UpdateApplicationArgsForCall(0)
    57  				Expect(executeErr).ToNot(HaveOccurred())
    58  				Expect(sentApp.Metadata.Labels).To(BeEquivalentTo(labels))
    59  			})
    60  
    61  			It("aggregates warnings", func() {
    62  				Expect(warnings).To(ConsistOf("warning-1", "warning-2", "set-app-labels-warnings"))
    63  			})
    64  		})
    65  
    66  		When("there are client errors", func() {
    67  			When("GetApplications fails", func() {
    68  				BeforeEach(func() {
    69  					fakeCloudControllerClient.GetApplicationsReturns(
    70  						[]resources.Application{resources.Application{GUID: "some-guid"}},
    71  						ccv3.Warnings([]string{"warning-failure-1", "warning-failure-2"}),
    72  						errors.New("get-apps-error"),
    73  					)
    74  				})
    75  
    76  				It("returns the error and all warnings", func() {
    77  					Expect(executeErr).To(HaveOccurred())
    78  					Expect(warnings).To(ConsistOf("warning-failure-1", "warning-failure-2"))
    79  					Expect(executeErr).To(MatchError("get-apps-error"))
    80  				})
    81  			})
    82  
    83  			When("UpdateApplication fails", func() {
    84  				BeforeEach(func() {
    85  					fakeCloudControllerClient.GetApplicationsReturns(
    86  						[]resources.Application{resources.Application{GUID: "some-guid"}},
    87  						ccv3.Warnings([]string{"warning-1", "warning-2"}),
    88  						nil,
    89  					)
    90  					fakeCloudControllerClient.UpdateApplicationReturns(
    91  						resources.Application{},
    92  						ccv3.Warnings{"set-app-labels-warnings"},
    93  						errors.New("update-application-error"),
    94  					)
    95  				})
    96  
    97  				It("returns the error and all warnings", func() {
    98  					Expect(executeErr).To(HaveOccurred())
    99  					Expect(warnings).To(ConsistOf("warning-1", "warning-2", "set-app-labels-warnings"))
   100  					Expect(executeErr).To(MatchError("update-application-error"))
   101  				})
   102  			})
   103  
   104  		})
   105  	})
   106  
   107  	Context("UpdateOrganizationLabelsByOrganizationName", func() {
   108  		JustBeforeEach(func() {
   109  			warnings, executeErr = actor.UpdateOrganizationLabelsByOrganizationName(resourceName, labels)
   110  		})
   111  
   112  		When("there are no client errors", func() {
   113  			BeforeEach(func() {
   114  				fakeCloudControllerClient.GetOrganizationsReturns(
   115  					[]resources.Organization{resources.Organization{GUID: "some-guid"}},
   116  					ccv3.Warnings([]string{"warning-1", "warning-2"}),
   117  					nil,
   118  				)
   119  				fakeCloudControllerClient.UpdateOrganizationReturns(
   120  					resources.Organization{},
   121  					ccv3.Warnings{"set-org"},
   122  					nil,
   123  				)
   124  			})
   125  
   126  			It("sets the org labels", func() {
   127  				Expect(fakeCloudControllerClient.UpdateOrganizationCallCount()).To(Equal(1))
   128  				sentOrg := fakeCloudControllerClient.UpdateOrganizationArgsForCall(0)
   129  				Expect(executeErr).ToNot(HaveOccurred())
   130  				Expect(sentOrg.Metadata.Labels).To(BeEquivalentTo(labels))
   131  			})
   132  
   133  			It("aggregates warnings", func() {
   134  				Expect(warnings).To(ConsistOf("warning-1", "warning-2", "set-org"))
   135  			})
   136  		})
   137  
   138  		When("there are client errors", func() {
   139  			When("fetching the organization fails", func() {
   140  				BeforeEach(func() {
   141  					fakeCloudControllerClient.GetOrganizationsReturns(
   142  						[]resources.Organization{resources.Organization{GUID: "some-guid"}},
   143  						ccv3.Warnings([]string{"warning-failure-1", "warning-failure-2"}),
   144  						errors.New("get-orgs-error"),
   145  					)
   146  				})
   147  
   148  				It("returns the error and all warnings", func() {
   149  					Expect(executeErr).To(HaveOccurred())
   150  					Expect(warnings).To(ConsistOf("warning-failure-1", "warning-failure-2"))
   151  					Expect(executeErr).To(MatchError("get-orgs-error"))
   152  				})
   153  			})
   154  
   155  			When("updating the organization fails", func() {
   156  				BeforeEach(func() {
   157  					fakeCloudControllerClient.GetOrganizationsReturns(
   158  						[]resources.Organization{resources.Organization{GUID: "some-guid"}},
   159  						ccv3.Warnings([]string{"warning-1", "warning-2"}),
   160  						nil,
   161  					)
   162  					fakeCloudControllerClient.UpdateOrganizationReturns(
   163  						resources.Organization{},
   164  						ccv3.Warnings{"set-org"},
   165  						errors.New("update-orgs-error"),
   166  					)
   167  				})
   168  				It("returns the error and all warnings", func() {
   169  					Expect(executeErr).To(HaveOccurred())
   170  					Expect(warnings).To(ConsistOf("warning-1", "warning-2", "set-org"))
   171  					Expect(executeErr).To(MatchError("update-orgs-error"))
   172  				})
   173  			})
   174  		})
   175  	})
   176  })