github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/resources/service_instance_resource_test.go (about)

     1  package resources_test
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/types"
     7  
     8  	. "code.cloudfoundry.org/cli/resources"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/ginkgo/extensions/table"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("service instance resource", func() {
    15  	DescribeTable(
    16  		"Marshaling and Unmarshaling",
    17  		func(serviceInstance ServiceInstance, serialized string) {
    18  			By("marshaling", func() {
    19  				Expect(json.Marshal(serviceInstance)).To(MatchJSON(serialized))
    20  			})
    21  
    22  			By("unmarshaling", func() {
    23  				var parsed ServiceInstance
    24  				Expect(json.Unmarshal([]byte(serialized), &parsed)).NotTo(HaveOccurred())
    25  				Expect(parsed).To(Equal(serviceInstance))
    26  			})
    27  		},
    28  		Entry("empty", ServiceInstance{}, `{}`),
    29  		Entry("type", ServiceInstance{Type: "fake-type"}, `{"type": "fake-type"}`),
    30  		Entry("name", ServiceInstance{Name: "fake-name"}, `{"name": "fake-name"}`),
    31  		Entry("guid", ServiceInstance{GUID: "fake-guid"}, `{"guid": "fake-guid"}`),
    32  		Entry("tags", ServiceInstance{Tags: types.NewOptionalStringSlice("foo", "bar")}, `{"tags": ["foo", "bar"]}`),
    33  		Entry("tags empty", ServiceInstance{Tags: types.NewOptionalStringSlice()}, `{"tags": []}`),
    34  		Entry("syslog", ServiceInstance{SyslogDrainURL: types.NewOptionalString("https://fake-syslog.com")}, `{"syslog_drain_url": "https://fake-syslog.com"}`),
    35  		Entry("syslog empty", ServiceInstance{SyslogDrainURL: types.NewOptionalString("")}, `{"syslog_drain_url": ""}`),
    36  		Entry("route", ServiceInstance{RouteServiceURL: types.NewOptionalString("https://fake-route.com")}, `{"route_service_url": "https://fake-route.com"}`),
    37  		Entry("route empty", ServiceInstance{RouteServiceURL: types.NewOptionalString("https://fake-route.com")}, `{"route_service_url": "https://fake-route.com"}`),
    38  		Entry("dashboard", ServiceInstance{DashboardURL: types.NewOptionalString("https://fake-dashboard.com")}, `{"dashboard_url": "https://fake-dashboard.com"}`),
    39  		Entry("dashboard empty", ServiceInstance{DashboardURL: types.NewOptionalString("https://fake-dashboard.com")}, `{"dashboard_url": "https://fake-dashboard.com"}`),
    40  		Entry("upgrade available", ServiceInstance{UpgradeAvailable: types.NewOptionalBoolean(false)}, `{"upgrade_available": false}`),
    41  		Entry(
    42  			"credentials",
    43  			ServiceInstance{
    44  				Credentials: types.NewOptionalObject(map[string]interface{}{
    45  					"foo": "bar",
    46  					"baz": false,
    47  				}),
    48  			},
    49  			`{
    50  				"credentials": {
    51  					"foo": "bar",
    52  					"baz": false
    53  				}
    54  			}`,
    55  		),
    56  		Entry(
    57  			"credentials empty",
    58  			ServiceInstance{
    59  				Credentials: types.NewOptionalObject(map[string]interface{}{}),
    60  			},
    61  			`{
    62  				"credentials": {}
    63  			}`,
    64  		),
    65  		Entry(
    66  			"parameters",
    67  			ServiceInstance{
    68  				Parameters: types.NewOptionalObject(map[string]interface{}{
    69  					"tomato": "potato",
    70  					"baz":    true,
    71  				}),
    72  			},
    73  			`{
    74  				"parameters": {
    75  					"tomato": "potato",
    76  					"baz": true
    77  				}
    78  			}`,
    79  		),
    80  		Entry(
    81  			"parameters empty",
    82  			ServiceInstance{
    83  				Parameters: types.NewOptionalObject(map[string]interface{}{}),
    84  			},
    85  			`{
    86  				"parameters": {}
    87  			}`,
    88  		),
    89  		Entry(
    90  			"last operation",
    91  			ServiceInstance{
    92  				LastOperation: LastOperation{Type: CreateOperation, State: OperationInProgress},
    93  			},
    94  			`{
    95  				"last_operation": {
    96  					"type": "create",
    97  					"state": "in progress"
    98  				}
    99  			}`,
   100  		),
   101  		Entry(
   102  			"space guid",
   103  			ServiceInstance{SpaceGUID: "fake-space-guid"},
   104  			`{
   105  				"relationships": {
   106  					"space": {
   107  						"data": {
   108  							"guid": "fake-space-guid"
   109  						}
   110  					}
   111  				}
   112              }`,
   113  		),
   114  		Entry(
   115  			"plan guid",
   116  			ServiceInstance{ServicePlanGUID: "fake-plan-guid"},
   117  			`{
   118  				"relationships": {
   119  					"service_plan": {
   120  						"data": {
   121  							"guid": "fake-plan-guid"
   122  						}
   123  					}
   124  				}
   125              }`,
   126  		),
   127  		Entry(
   128  			"maintenance info version",
   129  			ServiceInstance{MaintenanceInfoVersion: "3.2.1"},
   130  			`{
   131  				"maintenance_info": {
   132  					"version": "3.2.1"
   133  				}
   134  			}`,
   135  		),
   136  		Entry(
   137  			"metadata",
   138  			ServiceInstance{
   139  				Metadata: &Metadata{
   140  					Labels: map[string]types.NullString{
   141  						"foo": types.NewNullString("bar"),
   142  						"baz": types.NewNullString(),
   143  					},
   144  				},
   145  			},
   146  			`{
   147  				"metadata": {
   148  					"labels": {
   149  						"foo": "bar",
   150  						"baz": null
   151  					}
   152  				}
   153  			}`,
   154  		),
   155  		Entry(
   156  			"everything",
   157  			ServiceInstance{
   158  				Type:                   UserProvidedServiceInstance,
   159  				GUID:                   "fake-guid",
   160  				Name:                   "fake-space-guid",
   161  				SpaceGUID:              "fake-space-guid",
   162  				ServicePlanGUID:        "fake-service-plan-guid",
   163  				Tags:                   types.NewOptionalStringSlice("foo", "bar"),
   164  				SyslogDrainURL:         types.NewOptionalString("https://fake-syslog.com"),
   165  				RouteServiceURL:        types.NewOptionalString("https://fake-route.com"),
   166  				DashboardURL:           types.NewOptionalString("https://fake-dashboard.com"),
   167  				UpgradeAvailable:       types.NewOptionalBoolean(true),
   168  				MaintenanceInfoVersion: "1.0.0",
   169  				Credentials: types.NewOptionalObject(map[string]interface{}{
   170  					"foo": "bar",
   171  					"baz": false,
   172  				}),
   173  				Parameters: types.NewOptionalObject(map[string]interface{}{
   174  					"tomato": "potato",
   175  					"baz":    true,
   176  				}),
   177  				LastOperation: LastOperation{
   178  					Type:  "create",
   179  					State: "in progress",
   180  				},
   181  				Metadata: &Metadata{
   182  					Labels: map[string]types.NullString{
   183  						"foo": types.NewNullString("bar"),
   184  						"baz": types.NewNullString(),
   185  					},
   186  				},
   187  			},
   188  			`{
   189  				"type": "user-provided",
   190  				"guid": "fake-guid",
   191  				"name": "fake-space-guid",
   192  				"tags": ["foo", "bar"],
   193  				"syslog_drain_url": "https://fake-syslog.com",
   194  				"route_service_url": "https://fake-route.com",
   195  				"dashboard_url": "https://fake-dashboard.com",
   196  				"maintenance_info": {
   197  					"version": "1.0.0"
   198  				},
   199  				"upgrade_available": true,
   200  				"credentials": {
   201  					"foo": "bar",
   202  					"baz": false
   203  				},
   204  				"parameters": {
   205  					"tomato": "potato",
   206  					"baz": true
   207  				},
   208  				"last_operation": {
   209  					"type": "create",
   210  					"state": "in progress"
   211  				},
   212  				"metadata": {
   213  					"labels": {
   214  						"foo": "bar",
   215  						"baz": null
   216  					}
   217  				},
   218  				"relationships": {
   219  					"service_plan": {
   220  						"data": {
   221  							"guid": "fake-service-plan-guid"
   222  						}
   223  					},
   224  					"space": {
   225  						"data": {
   226  							"guid": "fake-space-guid"
   227  						}
   228  					}
   229  				}
   230              }`,
   231  		),
   232  	)
   233  })