github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/actor/v2action/service_broker_test.go (about) 1 package v2action_test 2 3 import ( 4 "errors" 5 6 . "github.com/onsi/ginkgo" 7 . "github.com/onsi/gomega" 8 9 "code.cloudfoundry.org/cli/actor/actionerror" 10 . "code.cloudfoundry.org/cli/actor/v2action" 11 "code.cloudfoundry.org/cli/actor/v2action/v2actionfakes" 12 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 13 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 14 ) 15 16 var _ = Describe("Service Broker", func() { 17 var ( 18 actor *Actor 19 fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient 20 ) 21 22 BeforeEach(func() { 23 fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient) 24 actor = NewActor(fakeCloudControllerClient, nil, nil) 25 }) 26 27 Describe("CreateServiceBroker", func() { 28 var ( 29 executeErr error 30 warnings Warnings 31 serviceBroker ServiceBroker 32 ) 33 34 JustBeforeEach(func() { 35 serviceBroker, warnings, executeErr = actor.CreateServiceBroker("broker-name", "username", "password", "https://broker.com", "a-space-guid") 36 }) 37 38 When("there are no errors", func() { 39 BeforeEach(func() { 40 fakeCloudControllerClient.CreateServiceBrokerReturns(ccv2.ServiceBroker{GUID: "some-broker-guid"}, []string{"a-warning", "another-warning"}, nil) 41 }) 42 43 It("creates the service broker with the correct values", func() { 44 Expect(fakeCloudControllerClient.CreateServiceBrokerCallCount()).To(Equal(1)) 45 brokerName, username, password, url, space := fakeCloudControllerClient.CreateServiceBrokerArgsForCall(0) 46 Expect(brokerName).To(Equal("broker-name")) 47 Expect(username).To(Equal("username")) 48 Expect(password).To(Equal("password")) 49 Expect(url).To(Equal("https://broker.com")) 50 Expect(space).To(Equal("a-space-guid")) 51 52 Expect(warnings).To(Equal(Warnings{"a-warning", "another-warning"})) 53 Expect(executeErr).NotTo(HaveOccurred()) 54 Expect(serviceBroker.GUID).To(Equal("some-broker-guid")) 55 }) 56 }) 57 58 When("there is an error", func() { 59 BeforeEach(func() { 60 fakeCloudControllerClient.CreateServiceBrokerReturns(ccv2.ServiceBroker{}, []string{"one-warning", "two-warnings"}, errors.New("error creating broker")) 61 }) 62 63 It("returns the errors and warnings", func() { 64 Expect(warnings).To(Equal(Warnings{"one-warning", "two-warnings"})) 65 Expect(executeErr).To(MatchError("error creating broker")) 66 }) 67 }) 68 }) 69 70 Describe("GetServiceBrokers", func() { 71 var ( 72 executeErr error 73 warnings Warnings 74 serviceBrokers []ServiceBroker 75 ) 76 77 JustBeforeEach(func() { 78 serviceBrokers, warnings, executeErr = actor.GetServiceBrokers() 79 }) 80 81 BeforeEach(func() { 82 fakeCloudControllerClient.GetServiceBrokersReturns([]ccv2.ServiceBroker{ 83 {GUID: "some-broker-guid"}}, 84 []string{"a-warning", "another-warning"}, nil) 85 }) 86 87 It("retrieves all service brokers", func() { 88 Expect(serviceBrokers).To(HaveLen(1)) 89 Expect(serviceBrokers[0].GUID).To(Equal("some-broker-guid")) 90 Expect(warnings).To(Equal(Warnings{"a-warning", "another-warning"})) 91 }) 92 93 When("there is an error", func() { 94 BeforeEach(func() { 95 fakeCloudControllerClient.GetServiceBrokersReturns([]ccv2.ServiceBroker{}, []string{"one-warning", "two-warnings"}, errors.New("error getting brokers")) 96 }) 97 98 It("returns the errors and warnings", func() { 99 Expect(warnings).To(Equal(Warnings{"one-warning", "two-warnings"})) 100 Expect(executeErr).To(MatchError("error getting brokers")) 101 }) 102 }) 103 }) 104 105 Describe("GetServiceBrokerByName", func() { 106 var ( 107 executeErr error 108 warnings Warnings 109 serviceBroker ServiceBroker 110 ) 111 112 JustBeforeEach(func() { 113 serviceBroker, warnings, executeErr = actor.GetServiceBrokerByName("broker-name") 114 }) 115 116 When("there are no errors", func() { 117 When("a service broker exists", func() { 118 BeforeEach(func() { 119 fakeCloudControllerClient.GetServiceBrokersReturns([]ccv2.ServiceBroker{{GUID: "some-broker-guid"}}, []string{"a-warning", "another-warning"}, nil) 120 }) 121 122 It("gets the service broker", func() { 123 Expect(fakeCloudControllerClient.GetServiceBrokersCallCount()).To(Equal(1)) 124 filter := fakeCloudControllerClient.GetServiceBrokersArgsForCall(0) 125 Expect(filter).To(Equal([]ccv2.Filter{{Type: constant.NameFilter, Operator: constant.EqualOperator, Values: []string{"broker-name"}}})) 126 127 Expect(warnings).To(Equal(Warnings{"a-warning", "another-warning"})) 128 Expect(executeErr).NotTo(HaveOccurred()) 129 Expect(serviceBroker.GUID).To(Equal("some-broker-guid")) 130 }) 131 }) 132 133 }) 134 135 When("there is an error", func() { 136 When("calling the client", func() { 137 BeforeEach(func() { 138 fakeCloudControllerClient.GetServiceBrokersReturns([]ccv2.ServiceBroker{{GUID: "some-broker-guid"}}, []string{"one-warning", "two-warnings"}, errors.New("error creating broker")) 139 }) 140 141 It("returns the errors and warnings", func() { 142 Expect(warnings).To(Equal(Warnings{"one-warning", "two-warnings"})) 143 Expect(executeErr).To(MatchError("error creating broker")) 144 }) 145 }) 146 147 When("a service broker does not exist", func() { 148 BeforeEach(func() { 149 fakeCloudControllerClient.GetServiceBrokersReturns([]ccv2.ServiceBroker{}, []string{"a-warning", "another-warning"}, nil) 150 }) 151 152 It("raises a service broker not found error", func() { 153 Expect(fakeCloudControllerClient.GetServiceBrokersCallCount()).To(Equal(1)) 154 filter := fakeCloudControllerClient.GetServiceBrokersArgsForCall(0) 155 Expect(filter).To(Equal([]ccv2.Filter{{Type: constant.NameFilter, Operator: constant.EqualOperator, Values: []string{"broker-name"}}})) 156 157 Expect(warnings).To(Equal(Warnings{"a-warning", "another-warning"})) 158 Expect(executeErr).To(MatchError(actionerror.ServiceBrokerNotFoundError{Name: "broker-name"})) 159 }) 160 }) 161 }) 162 }) 163 })