github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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.Tags).To(Equal([]string{"tag1", "tag2"}))
   177  				Expect(fields.DashboardURL).To(Equal("https://fake/dashboard/url"))
   178  				Expect(fields.LastOperation.Type).To(Equal("create"))
   179  				Expect(fields.LastOperation.State).To(Equal("in progress"))
   180  				Expect(fields.LastOperation.Description).To(Equal("fake state description"))
   181  				Expect(fields.LastOperation.CreatedAt).To(Equal("fake created at"))
   182  				Expect(fields.LastOperation.UpdatedAt).To(Equal("fake updated at"))
   183  			})
   184  
   185  			Context("When created_at is null", func() {
   186  				It("unmarshalls the service instance resource model", func() {
   187  					var resourceWithNullCreatedAt ServiceInstanceResource
   188  					metadata := `"metadata": {
   189  								"guid": "fake-guid",
   190  								"url": "/v2/service_instances/fake-guid",
   191  								"created_at": null,
   192  								"updated_at": "2015-01-13T18:52:08+00:00"
   193  							}`
   194  					stringWithNullCreatedAt := fmt.Sprintf(instanceString, metadata)
   195  
   196  					err := json.Unmarshal([]byte(stringWithNullCreatedAt), &resourceWithNullCreatedAt)
   197  					Expect(err).ToNot(HaveOccurred())
   198  				})
   199  			})
   200  
   201  			Context("When created_at is missing", func() {
   202  				It("unmarshalls the service instance resource model", func() {
   203  					var resourceWithMissingCreatedAt ServiceInstanceResource
   204  
   205  					metadata := `"metadata": {
   206  								"guid": "fake-guid",
   207  								"url": "/v2/service_instances/fake-guid",
   208  								"updated_at": "2015-01-13T18:52:08+00:00"
   209  							}`
   210  					stringWithMissingCreatedAt := fmt.Sprintf(instanceString, metadata)
   211  
   212  					err := json.Unmarshal([]byte(stringWithMissingCreatedAt), &resourceWithMissingCreatedAt)
   213  					Expect(err).ToNot(HaveOccurred())
   214  				})
   215  			})
   216  		})
   217  
   218  		Describe("#ToModel", func() {
   219  			It("unmarshalls the service instance resource model", func() {
   220  				instance := resource.ToModel()
   221  
   222  				Expect(instance.ServiceInstanceFields.GUID).To(Equal("fake-guid"))
   223  				Expect(instance.ServiceInstanceFields.Name).To(Equal("fake service name"))
   224  				Expect(instance.ServiceInstanceFields.Tags).To(Equal([]string{"tag1", "tag2"}))
   225  				Expect(instance.ServiceInstanceFields.DashboardURL).To(Equal("https://fake/dashboard/url"))
   226  				Expect(instance.ServiceInstanceFields.LastOperation.Type).To(Equal("create"))
   227  				Expect(instance.ServiceInstanceFields.LastOperation.State).To(Equal("in progress"))
   228  				Expect(instance.ServiceInstanceFields.LastOperation.Description).To(Equal("fake state description"))
   229  				Expect(instance.ServiceInstanceFields.LastOperation.CreatedAt).To(Equal("fake created at"))
   230  				Expect(instance.ServiceInstanceFields.LastOperation.UpdatedAt).To(Equal("fake updated at"))
   231  
   232  				Expect(instance.ServicePlan.GUID).To(Equal("fake-service-plan-guid"))
   233  				Expect(instance.ServicePlan.Free).To(BeTrue())
   234  				Expect(instance.ServicePlan.Description).To(Equal("fake-description"))
   235  				Expect(instance.ServicePlan.Public).To(BeTrue())
   236  				Expect(instance.ServicePlan.Active).To(BeTrue())
   237  				Expect(instance.ServicePlan.ServiceOfferingGUID).To(Equal("fake-service-guid"))
   238  
   239  				Expect(instance.ServiceBindings[0].GUID).To(Equal("fake-service-binding-guid"))
   240  				Expect(instance.ServiceBindings[0].URL).To(Equal("http://fake/url"))
   241  				Expect(instance.ServiceBindings[0].AppGUID).To(Equal("fake-app-guid"))
   242  			})
   243  		})
   244  	})
   245  
   246  	Context("Old brokers (no last_operation)", func() {
   247  		Describe("#ToFields", func() {
   248  			It("unmarshalls the fields of a service instance resource", func() {
   249  				fields := resourceWithNullLastOp.ToFields()
   250  
   251  				Expect(fields.GUID).To(Equal("fake-guid"))
   252  				Expect(fields.Name).To(Equal("fake service name"))
   253  				Expect(fields.Tags).To(Equal([]string{"tag1", "tag2"}))
   254  				Expect(fields.DashboardURL).To(Equal("https://fake/dashboard/url"))
   255  				Expect(fields.LastOperation.Type).To(Equal(""))
   256  				Expect(fields.LastOperation.State).To(Equal(""))
   257  				Expect(fields.LastOperation.Description).To(Equal(""))
   258  			})
   259  		})
   260  
   261  		Describe("#ToModel", func() {
   262  			It("unmarshalls the service instance resource model", func() {
   263  				instance := resourceWithNullLastOp.ToModel()
   264  
   265  				Expect(instance.ServiceInstanceFields.GUID).To(Equal("fake-guid"))
   266  				Expect(instance.ServiceInstanceFields.Name).To(Equal("fake service name"))
   267  				Expect(instance.ServiceInstanceFields.Tags).To(Equal([]string{"tag1", "tag2"}))
   268  				Expect(instance.ServiceInstanceFields.DashboardURL).To(Equal("https://fake/dashboard/url"))
   269  
   270  				Expect(instance.ServiceInstanceFields.LastOperation.Type).To(Equal(""))
   271  				Expect(instance.ServiceInstanceFields.LastOperation.State).To(Equal(""))
   272  				Expect(instance.ServiceInstanceFields.LastOperation.Description).To(Equal(""))
   273  
   274  				Expect(instance.ServicePlan.GUID).To(Equal("fake-service-plan-guid"))
   275  				Expect(instance.ServicePlan.Free).To(BeTrue())
   276  				Expect(instance.ServicePlan.Description).To(Equal("fake-description"))
   277  				Expect(instance.ServicePlan.Public).To(BeTrue())
   278  				Expect(instance.ServicePlan.Active).To(BeTrue())
   279  				Expect(instance.ServicePlan.ServiceOfferingGUID).To(Equal("fake-service-guid"))
   280  
   281  				Expect(instance.ServiceBindings[0].GUID).To(Equal("fake-service-binding-guid"))
   282  				Expect(instance.ServiceBindings[0].URL).To(Equal("http://fake/url"))
   283  				Expect(instance.ServiceBindings[0].AppGUID).To(Equal("fake-app-guid"))
   284  			})
   285  		})
   286  	})
   287  })