github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/api/cloudcontroller/ccv2/service_test.go (about)

     1  package ccv2_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/ghttp"
    11  )
    12  
    13  var _ = Describe("Service", func() {
    14  	var client *Client
    15  
    16  	BeforeEach(func() {
    17  		client = NewTestClient()
    18  	})
    19  
    20  	Describe("GetService", func() {
    21  		Context("when the service exists", func() {
    22  			Context("when the value of the 'extra' json key is non-empty", func() {
    23  				BeforeEach(func() {
    24  					response := `{
    25  						"metadata": {
    26  							"guid": "some-service-guid"
    27  						},
    28  						"entity": {
    29  							"label": "some-service",
    30  							"description": "some-description",
    31  							"documentation_url": "some-url",
    32  							"extra": "{\"provider\":{\"name\":\"The name\"},\"listing\":{\"imageUrl\":\"http://catgifpage.com/cat.gif\",\"blurb\":\"fake broker that is fake\",\"longDescription\":\"A long time ago, in a galaxy far far away...\"},\"displayName\":\"The Fake Broker\",\"shareable\":true}"
    33  						}
    34  					}`
    35  					server.AppendHandlers(
    36  						CombineHandlers(
    37  							VerifyRequest(http.MethodGet, "/v2/services/some-service-guid"),
    38  							RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    39  						),
    40  					)
    41  				})
    42  
    43  				It("returns the service and warnings", func() {
    44  					service, warnings, err := client.GetService("some-service-guid")
    45  					Expect(err).NotTo(HaveOccurred())
    46  
    47  					Expect(service).To(Equal(Service{
    48  						GUID:             "some-service-guid",
    49  						Label:            "some-service",
    50  						Description:      "some-description",
    51  						DocumentationURL: "some-url",
    52  						Extra: ServiceExtra{
    53  							Shareable: true,
    54  						},
    55  					}))
    56  					Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    57  				})
    58  			})
    59  
    60  			Context("when the value of the 'extra' json key is null", func() {
    61  				BeforeEach(func() {
    62  					response := `{
    63  						"metadata": {
    64  							"guid": "some-service-guid"
    65  						},
    66  						"entity": {
    67  							"extra": null
    68  						}
    69  					}`
    70  					server.AppendHandlers(
    71  						CombineHandlers(
    72  							VerifyRequest(http.MethodGet, "/v2/services/some-service-guid"),
    73  							RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    74  						),
    75  					)
    76  				})
    77  
    78  				It("returns extra.shareable == 'false'", func() {
    79  					service, _, err := client.GetService("some-service-guid")
    80  					Expect(err).NotTo(HaveOccurred())
    81  
    82  					Expect(service).To(Equal(Service{
    83  						GUID:  "some-service-guid",
    84  						Extra: ServiceExtra{Shareable: false},
    85  					}))
    86  				})
    87  			})
    88  
    89  			Context("when the value of the 'extra' json key is the empty string", func() {
    90  				BeforeEach(func() {
    91  					response := `{
    92  						"metadata": {
    93  							"guid": "some-service-guid"
    94  						},
    95  						"entity": {
    96  							"extra": ""
    97  						}
    98  					}`
    99  					server.AppendHandlers(
   100  						CombineHandlers(
   101  							VerifyRequest(http.MethodGet, "/v2/services/some-service-guid"),
   102  							RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   103  						),
   104  					)
   105  				})
   106  
   107  				It("returns extra.shareable == 'false'", func() {
   108  					service, _, err := client.GetService("some-service-guid")
   109  					Expect(err).NotTo(HaveOccurred())
   110  
   111  					Expect(service).To(Equal(Service{
   112  						GUID:  "some-service-guid",
   113  						Extra: ServiceExtra{Shareable: false},
   114  					}))
   115  				})
   116  			})
   117  
   118  			Context("when the key 'extra' is not in the json response", func() {
   119  				BeforeEach(func() {
   120  					response := `{
   121  						"metadata": {
   122  							"guid": "some-service-guid"
   123  						}
   124  					}`
   125  					server.AppendHandlers(
   126  						CombineHandlers(
   127  							VerifyRequest(http.MethodGet, "/v2/services/some-service-guid"),
   128  							RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   129  						),
   130  					)
   131  				})
   132  
   133  				It("returns extra.shareable == 'false'", func() {
   134  					service, _, err := client.GetService("some-service-guid")
   135  					Expect(err).NotTo(HaveOccurred())
   136  
   137  					Expect(service).To(Equal(Service{
   138  						GUID:  "some-service-guid",
   139  						Extra: ServiceExtra{Shareable: false},
   140  					}))
   141  				})
   142  			})
   143  		})
   144  
   145  		Context("when the service does not exist (testing general error case)", func() {
   146  			BeforeEach(func() {
   147  				response := `{
   148  					"description": "The service could not be found: non-existant-service-guid",
   149  					"error_code": "CF-ServiceNotFound",
   150  					"code": 120003
   151  				}`
   152  
   153  				server.AppendHandlers(
   154  					CombineHandlers(
   155  						VerifyRequest(http.MethodGet, "/v2/services/non-existant-service-guid"),
   156  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   157  					))
   158  			})
   159  
   160  			It("returns an error and warnings", func() {
   161  				_, warnings, err := client.GetService("non-existant-service-guid")
   162  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{Message: "The service could not be found: non-existant-service-guid"}))
   163  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   164  			})
   165  		})
   166  	})
   167  })