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