github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/api/cloudcontroller/ccv3/service_instance_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/resources"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("Service Instance", func() {
    17  	var client *Client
    18  
    19  	BeforeEach(func() {
    20  		client, _ = NewTestClient()
    21  	})
    22  
    23  	Describe("GetServiceInstances", func() {
    24  		var (
    25  			query Query
    26  
    27  			instances  []resources.ServiceInstance
    28  			warnings   Warnings
    29  			executeErr error
    30  		)
    31  
    32  		JustBeforeEach(func() {
    33  			instances, _, warnings, executeErr = client.GetServiceInstances(query)
    34  		})
    35  
    36  		When("service instances exist", func() {
    37  			BeforeEach(func() {
    38  				response1 := fmt.Sprintf(`
    39  					{
    40  						 "pagination": {
    41  								"next": {
    42  									 "href": "%s/v3/service_instances?names=some-service-instance-name&page=2"
    43  								}
    44  						 },
    45  						 "resources": [
    46  								{
    47  									 "guid": "service-instance-1-guid",
    48  									 "name": "service-instance-1-name"
    49  								},
    50  								{
    51  									 "guid": "service-instance-2-guid",
    52  									 "name": "service-instance-2-name"
    53  								}
    54  						 ]
    55  					}`, server.URL())
    56  
    57  				response2 := `
    58  					{
    59  						 "pagination": {
    60  								"next": {
    61  									 "href": null
    62  								}
    63  						 },
    64  						 "resources": [
    65  								{
    66  									 "guid": "service-instance-3-guid",
    67  									 "name": "service-instance-3-name"
    68  								}
    69  						 ]
    70  					}`
    71  
    72  				server.AppendHandlers(
    73  					CombineHandlers(
    74  						VerifyRequest(http.MethodGet, "/v3/service_instances", "names=some-service-instance-name"),
    75  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}),
    76  					),
    77  					CombineHandlers(
    78  						VerifyRequest(http.MethodGet, "/v3/service_instances", "names=some-service-instance-name&page=2"),
    79  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"warning-2"}}),
    80  					),
    81  				)
    82  
    83  				query = Query{
    84  					Key:    NameFilter,
    85  					Values: []string{"some-service-instance-name"},
    86  				}
    87  			})
    88  
    89  			It("returns a list of service instances with their associated warnings", func() {
    90  				Expect(executeErr).ToNot(HaveOccurred())
    91  
    92  				Expect(instances).To(ConsistOf(
    93  					resources.ServiceInstance{
    94  						GUID: "service-instance-1-guid",
    95  						Name: "service-instance-1-name",
    96  					},
    97  					resources.ServiceInstance{
    98  						GUID: "service-instance-2-guid",
    99  						Name: "service-instance-2-name",
   100  					},
   101  					resources.ServiceInstance{
   102  						GUID: "service-instance-3-guid",
   103  						Name: "service-instance-3-name",
   104  					},
   105  				))
   106  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   107  			})
   108  		})
   109  
   110  		When("the cloud controller returns errors and warnings", func() {
   111  			BeforeEach(func() {
   112  				response := `{
   113  				"errors": [
   114  					{
   115  						"code": 42424,
   116  						"detail": "Some detailed error message",
   117  						"title": "CF-SomeErrorTitle"
   118  					},
   119  					{
   120  						"code": 11111,
   121  						"detail": "Some other detailed error message",
   122  						"title": "CF-SomeOtherErrorTitle"
   123  					}
   124  				]
   125  			}`
   126  				server.AppendHandlers(
   127  					CombineHandlers(
   128  						VerifyRequest(http.MethodGet, "/v3/service_instances"),
   129  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   130  					),
   131  				)
   132  			})
   133  
   134  			It("returns the error and all warnings", func() {
   135  				Expect(executeErr).To(MatchError(ccerror.MultiError{
   136  					ResponseCode: http.StatusTeapot,
   137  					Errors: []ccerror.V3Error{
   138  						{
   139  							Code:   42424,
   140  							Detail: "Some detailed error message",
   141  							Title:  "CF-SomeErrorTitle",
   142  						},
   143  						{
   144  							Code:   11111,
   145  							Detail: "Some other detailed error message",
   146  							Title:  "CF-SomeOtherErrorTitle",
   147  						},
   148  					},
   149  				}))
   150  				Expect(warnings).To(ConsistOf("this is a warning"))
   151  			})
   152  		})
   153  	})
   154  })