github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/api/resources/service_instances_test.go (about)

     1  package resources_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	. "github.com/cloudfoundry/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          "gateway_data": null,
    32          "dashboard_url": "https://fake/dashboard/url",
    33          "type": "managed_service_instance",
    34          "space_url": "/v2/spaces/fake-space-guid",
    35          "service_plan_url": "/v2/service_plans/fake-service-plan-guid",
    36          "service_bindings_url": "/v2/service_instances/fake-guid/service_bindings",
    37          "last_operation": {
    38            "type": "create",
    39            "state": "in progress",
    40            "description": "fake state description",
    41            "created_at": "fake created at",
    42            "updated_at": "fake updated at"
    43          },
    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          "gateway_data": null,
    86          "dashboard_url": "https://fake/dashboard/url",
    87          "type": "managed_service_instance",
    88          "space_url": "/v2/spaces/fake-space-guid",
    89          "service_plan_url": "/v2/service_plans/fake-service-plan-guid",
    90          "service_bindings_url": "/v2/service_instances/fake-guid/service_bindings",
    91          "last_operation": null,
    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  								"gateway_data": null,
   134  								"dashboard_url": "https://fake/dashboard/url",
   135  								"type": "managed_service_instance",
   136  								"space_url": "/v2/spaces/fake-space-guid",
   137  								"service_plan_url": "/v2/service_plans/fake-service-plan-guid",
   138  								"service_bindings_url": "/v2/service_instances/fake-guid/service_bindings",
   139  								"last_operation": {
   140  									"type": "create",
   141  									"state": "in progress",
   142  									"description": "fake state description",
   143  									"updated_at": "fake updated at"
   144  								},
   145  								"service_plan": {
   146  									"metadata": {
   147  										"guid": "fake-service-plan-guid"
   148  									},
   149  									"entity": {
   150  										"name": "fake-service-plan-name",
   151  										"free": true,
   152  										"description": "fake-description",
   153  										"public": true,
   154  										"active": true,
   155  										"service_guid": "fake-service-guid"
   156  									}
   157  								},
   158  								"service_bindings": [{
   159  									"metadata": {
   160  										"guid": "fake-service-binding-guid",
   161  										"url": "http://fake/url"
   162  									},
   163  									"entity": {
   164  										"app_guid": "fake-app-guid"
   165  									}
   166  								}]
   167  							}
   168  						}`
   169  		})
   170  
   171  		Describe("#ToFields", func() {
   172  			It("unmarshalls the fields of a service instance resource", func() {
   173  				fields := resource.ToFields()
   174  
   175  				Expect(fields.Guid).To(Equal("fake-guid"))
   176  				Expect(fields.Name).To(Equal("fake service name"))
   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.DashboardUrl).To(Equal("https://fake/dashboard/url"))
   225  				Expect(instance.ServiceInstanceFields.LastOperation.Type).To(Equal("create"))
   226  				Expect(instance.ServiceInstanceFields.LastOperation.State).To(Equal("in progress"))
   227  				Expect(instance.ServiceInstanceFields.LastOperation.Description).To(Equal("fake state description"))
   228  				Expect(instance.ServiceInstanceFields.LastOperation.CreatedAt).To(Equal("fake created at"))
   229  				Expect(instance.ServiceInstanceFields.LastOperation.UpdatedAt).To(Equal("fake updated at"))
   230  
   231  				Expect(instance.ServicePlan.Guid).To(Equal("fake-service-plan-guid"))
   232  				Expect(instance.ServicePlan.Free).To(BeTrue())
   233  				Expect(instance.ServicePlan.Description).To(Equal("fake-description"))
   234  				Expect(instance.ServicePlan.Public).To(BeTrue())
   235  				Expect(instance.ServicePlan.Active).To(BeTrue())
   236  				Expect(instance.ServicePlan.ServiceOfferingGuid).To(Equal("fake-service-guid"))
   237  
   238  				Expect(instance.ServiceBindings[0].Guid).To(Equal("fake-service-binding-guid"))
   239  				Expect(instance.ServiceBindings[0].Url).To(Equal("http://fake/url"))
   240  				Expect(instance.ServiceBindings[0].AppGuid).To(Equal("fake-app-guid"))
   241  			})
   242  		})
   243  	})
   244  
   245  	Context("Old brokers (no last_operation)", func() {
   246  		Describe("#ToFields", func() {
   247  			It("unmarshalls the fields of a service instance resource", func() {
   248  				fields := resourceWithNullLastOp.ToFields()
   249  
   250  				Expect(fields.Guid).To(Equal("fake-guid"))
   251  				Expect(fields.Name).To(Equal("fake service name"))
   252  				Expect(fields.DashboardUrl).To(Equal("https://fake/dashboard/url"))
   253  				Expect(fields.LastOperation.Type).To(Equal(""))
   254  				Expect(fields.LastOperation.State).To(Equal(""))
   255  				Expect(fields.LastOperation.Description).To(Equal(""))
   256  			})
   257  		})
   258  
   259  		Describe("#ToModel", func() {
   260  			It("unmarshalls the service instance resource model", func() {
   261  				instance := resourceWithNullLastOp.ToModel()
   262  
   263  				Expect(instance.ServiceInstanceFields.Guid).To(Equal("fake-guid"))
   264  				Expect(instance.ServiceInstanceFields.Name).To(Equal("fake service name"))
   265  				Expect(instance.ServiceInstanceFields.DashboardUrl).To(Equal("https://fake/dashboard/url"))
   266  
   267  				Expect(instance.ServiceInstanceFields.LastOperation.Type).To(Equal(""))
   268  				Expect(instance.ServiceInstanceFields.LastOperation.State).To(Equal(""))
   269  				Expect(instance.ServiceInstanceFields.LastOperation.Description).To(Equal(""))
   270  
   271  				Expect(instance.ServicePlan.Guid).To(Equal("fake-service-plan-guid"))
   272  				Expect(instance.ServicePlan.Free).To(BeTrue())
   273  				Expect(instance.ServicePlan.Description).To(Equal("fake-description"))
   274  				Expect(instance.ServicePlan.Public).To(BeTrue())
   275  				Expect(instance.ServicePlan.Active).To(BeTrue())
   276  				Expect(instance.ServicePlan.ServiceOfferingGuid).To(Equal("fake-service-guid"))
   277  
   278  				Expect(instance.ServiceBindings[0].Guid).To(Equal("fake-service-binding-guid"))
   279  				Expect(instance.ServiceBindings[0].Url).To(Equal("http://fake/url"))
   280  				Expect(instance.ServiceBindings[0].AppGuid).To(Equal("fake-app-guid"))
   281  			})
   282  		})
   283  	})
   284  })