github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/api/cloudcontroller/ccv3/service_instance_test.go (about)

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