github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/api/resources/service_instances_test.go (about) 1 package resources_test 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 . "code.cloudfoundry.org/cli/cf/api/resources" 8 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 ) 12 13 var _ = Describe("ServiceInstanceResource", func() { 14 var resource, resourceWithNullLastOp ServiceInstanceResource 15 16 BeforeEach(func() { 17 err := json.Unmarshal([]byte(` 18 { 19 "metadata": { 20 "guid": "fake-guid", 21 "url": "/v2/service_instances/fake-guid", 22 "created_at": "2015-01-13T18:52:08+00:00", 23 "updated_at": null 24 }, 25 "entity": { 26 "name": "fake service name", 27 "credentials": { 28 }, 29 "service_plan_guid": "fake-service-plan-guid", 30 "space_guid": "fake-space-guid", 31 "dashboard_url": "https://fake/dashboard/url", 32 "type": "managed_service_instance", 33 "space_url": "/v2/spaces/fake-space-guid", 34 "service_plan_url": "/v2/service_plans/fake-service-plan-guid", 35 "service_bindings_url": "/v2/service_instances/fake-guid/service_bindings", 36 "last_operation": { 37 "type": "create", 38 "state": "in progress", 39 "description": "fake state description", 40 "created_at": "fake created at", 41 "updated_at": "fake updated at" 42 }, 43 "tags": [ "tag1", "tag2" ], 44 "service_plan": { 45 "metadata": { 46 "guid": "fake-service-plan-guid" 47 }, 48 "entity": { 49 "name": "fake-service-plan-name", 50 "free": true, 51 "description": "fake-description", 52 "public": true, 53 "active": true, 54 "service_guid": "fake-service-guid" 55 } 56 }, 57 "service_bindings": [{ 58 "metadata": { 59 "guid": "fake-service-binding-guid", 60 "url": "http://fake/url" 61 }, 62 "entity": { 63 "app_guid": "fake-app-guid" 64 } 65 }] 66 } 67 }`), &resource) 68 69 Expect(err).ToNot(HaveOccurred()) 70 71 err = json.Unmarshal([]byte(` 72 { 73 "metadata": { 74 "guid": "fake-guid", 75 "url": "/v2/service_instances/fake-guid", 76 "created_at": "2015-01-13T18:52:08+00:00", 77 "updated_at": null 78 }, 79 "entity": { 80 "name": "fake service name", 81 "credentials": { 82 }, 83 "service_plan_guid": "fake-service-plan-guid", 84 "space_guid": "fake-space-guid", 85 "dashboard_url": "https://fake/dashboard/url", 86 "type": "managed_service_instance", 87 "space_url": "/v2/spaces/fake-space-guid", 88 "service_plan_url": "/v2/service_plans/fake-service-plan-guid", 89 "service_bindings_url": "/v2/service_instances/fake-guid/service_bindings", 90 "last_operation": null, 91 "tags": [ "tag1", "tag2" ], 92 "service_plan": { 93 "metadata": { 94 "guid": "fake-service-plan-guid" 95 }, 96 "entity": { 97 "name": "fake-service-plan-name", 98 "free": true, 99 "description": "fake-description", 100 "public": true, 101 "active": true, 102 "service_guid": "fake-service-guid" 103 } 104 }, 105 "service_bindings": [{ 106 "metadata": { 107 "guid": "fake-service-binding-guid", 108 "url": "http://fake/url" 109 }, 110 "entity": { 111 "app_guid": "fake-app-guid" 112 } 113 }] 114 } 115 }`), &resourceWithNullLastOp) 116 117 Expect(err).ToNot(HaveOccurred()) 118 }) 119 120 Context("Async brokers", func() { 121 var instanceString string 122 123 BeforeEach(func() { 124 instanceString = ` 125 { 126 %s, 127 "entity": { 128 "name": "fake service name", 129 "credentials": { 130 }, 131 "service_plan_guid": "fake-service-plan-guid", 132 "space_guid": "fake-space-guid", 133 "dashboard_url": "https://fake/dashboard/url", 134 "type": "managed_service_instance", 135 "space_url": "/v2/spaces/fake-space-guid", 136 "service_plan_url": "/v2/service_plans/fake-service-plan-guid", 137 "service_bindings_url": "/v2/service_instances/fake-guid/service_bindings", 138 "last_operation": { 139 "type": "create", 140 "state": "in progress", 141 "description": "fake state description", 142 "updated_at": "fake updated at" 143 }, 144 "service_plan": { 145 "metadata": { 146 "guid": "fake-service-plan-guid" 147 }, 148 "entity": { 149 "name": "fake-service-plan-name", 150 "free": true, 151 "description": "fake-description", 152 "public": true, 153 "active": true, 154 "service_guid": "fake-service-guid" 155 } 156 }, 157 "service_bindings": [{ 158 "metadata": { 159 "guid": "fake-service-binding-guid", 160 "url": "http://fake/url" 161 }, 162 "entity": { 163 "app_guid": "fake-app-guid" 164 } 165 }] 166 } 167 }` 168 }) 169 170 Describe("#ToFields", func() { 171 It("unmarshalls the fields of a service instance resource", func() { 172 fields := resource.ToFields() 173 174 Expect(fields.GUID).To(Equal("fake-guid")) 175 Expect(fields.Name).To(Equal("fake service name")) 176 Expect(fields.Type).To(Equal("managed_service_instance")) 177 Expect(fields.Tags).To(Equal([]string{"tag1", "tag2"})) 178 Expect(fields.DashboardURL).To(Equal("https://fake/dashboard/url")) 179 Expect(fields.LastOperation.Type).To(Equal("create")) 180 Expect(fields.LastOperation.State).To(Equal("in progress")) 181 Expect(fields.LastOperation.Description).To(Equal("fake state description")) 182 Expect(fields.LastOperation.CreatedAt).To(Equal("fake created at")) 183 Expect(fields.LastOperation.UpdatedAt).To(Equal("fake updated at")) 184 }) 185 186 Context("When created_at is null", func() { 187 It("unmarshalls the service instance resource model", func() { 188 var resourceWithNullCreatedAt ServiceInstanceResource 189 metadata := `"metadata": { 190 "guid": "fake-guid", 191 "url": "/v2/service_instances/fake-guid", 192 "created_at": null, 193 "updated_at": "2015-01-13T18:52:08+00:00" 194 }` 195 stringWithNullCreatedAt := fmt.Sprintf(instanceString, metadata) 196 197 err := json.Unmarshal([]byte(stringWithNullCreatedAt), &resourceWithNullCreatedAt) 198 Expect(err).ToNot(HaveOccurred()) 199 }) 200 }) 201 202 Context("When created_at is missing", func() { 203 It("unmarshalls the service instance resource model", func() { 204 var resourceWithMissingCreatedAt ServiceInstanceResource 205 206 metadata := `"metadata": { 207 "guid": "fake-guid", 208 "url": "/v2/service_instances/fake-guid", 209 "updated_at": "2015-01-13T18:52:08+00:00" 210 }` 211 stringWithMissingCreatedAt := fmt.Sprintf(instanceString, metadata) 212 213 err := json.Unmarshal([]byte(stringWithMissingCreatedAt), &resourceWithMissingCreatedAt) 214 Expect(err).ToNot(HaveOccurred()) 215 }) 216 }) 217 }) 218 219 Describe("#ToModel", func() { 220 It("unmarshalls the service instance resource model", func() { 221 instance := resource.ToModel() 222 223 Expect(instance.ServiceInstanceFields.GUID).To(Equal("fake-guid")) 224 Expect(instance.ServiceInstanceFields.Name).To(Equal("fake service name")) 225 Expect(instance.ServiceInstanceFields.Type).To(Equal("managed_service_instance")) 226 Expect(instance.ServiceInstanceFields.Tags).To(Equal([]string{"tag1", "tag2"})) 227 Expect(instance.ServiceInstanceFields.DashboardURL).To(Equal("https://fake/dashboard/url")) 228 Expect(instance.ServiceInstanceFields.LastOperation.Type).To(Equal("create")) 229 Expect(instance.ServiceInstanceFields.LastOperation.State).To(Equal("in progress")) 230 Expect(instance.ServiceInstanceFields.LastOperation.Description).To(Equal("fake state description")) 231 Expect(instance.ServiceInstanceFields.LastOperation.CreatedAt).To(Equal("fake created at")) 232 Expect(instance.ServiceInstanceFields.LastOperation.UpdatedAt).To(Equal("fake updated at")) 233 234 Expect(instance.ServicePlan.GUID).To(Equal("fake-service-plan-guid")) 235 Expect(instance.ServicePlan.Free).To(BeTrue()) 236 Expect(instance.ServicePlan.Description).To(Equal("fake-description")) 237 Expect(instance.ServicePlan.Public).To(BeTrue()) 238 Expect(instance.ServicePlan.Active).To(BeTrue()) 239 Expect(instance.ServicePlan.ServiceOfferingGUID).To(Equal("fake-service-guid")) 240 241 Expect(instance.ServiceBindings[0].GUID).To(Equal("fake-service-binding-guid")) 242 Expect(instance.ServiceBindings[0].URL).To(Equal("http://fake/url")) 243 Expect(instance.ServiceBindings[0].AppGUID).To(Equal("fake-app-guid")) 244 }) 245 }) 246 }) 247 248 Context("Old brokers (no last_operation)", func() { 249 Describe("#ToFields", func() { 250 It("unmarshalls the fields of a service instance resource", func() { 251 fields := resourceWithNullLastOp.ToFields() 252 253 Expect(fields.GUID).To(Equal("fake-guid")) 254 Expect(fields.Name).To(Equal("fake service name")) 255 Expect(fields.Tags).To(Equal([]string{"tag1", "tag2"})) 256 Expect(fields.DashboardURL).To(Equal("https://fake/dashboard/url")) 257 Expect(fields.LastOperation.Type).To(Equal("")) 258 Expect(fields.LastOperation.State).To(Equal("")) 259 Expect(fields.LastOperation.Description).To(Equal("")) 260 }) 261 }) 262 263 Describe("#ToModel", func() { 264 It("unmarshalls the service instance resource model", func() { 265 instance := resourceWithNullLastOp.ToModel() 266 267 Expect(instance.ServiceInstanceFields.GUID).To(Equal("fake-guid")) 268 Expect(instance.ServiceInstanceFields.Name).To(Equal("fake service name")) 269 Expect(instance.ServiceInstanceFields.Type).To(Equal("managed_service_instance")) 270 Expect(instance.ServiceInstanceFields.Tags).To(Equal([]string{"tag1", "tag2"})) 271 Expect(instance.ServiceInstanceFields.DashboardURL).To(Equal("https://fake/dashboard/url")) 272 273 Expect(instance.ServiceInstanceFields.LastOperation.Type).To(Equal("")) 274 Expect(instance.ServiceInstanceFields.LastOperation.State).To(Equal("")) 275 Expect(instance.ServiceInstanceFields.LastOperation.Description).To(Equal("")) 276 277 Expect(instance.ServicePlan.GUID).To(Equal("fake-service-plan-guid")) 278 Expect(instance.ServicePlan.Free).To(BeTrue()) 279 Expect(instance.ServicePlan.Description).To(Equal("fake-description")) 280 Expect(instance.ServicePlan.Public).To(BeTrue()) 281 Expect(instance.ServicePlan.Active).To(BeTrue()) 282 Expect(instance.ServicePlan.ServiceOfferingGUID).To(Equal("fake-service-guid")) 283 284 Expect(instance.ServiceBindings[0].GUID).To(Equal("fake-service-binding-guid")) 285 Expect(instance.ServiceBindings[0].URL).To(Equal("http://fake/url")) 286 Expect(instance.ServiceBindings[0].AppGUID).To(Equal("fake-app-guid")) 287 }) 288 }) 289 }) 290 })