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