github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/api/cloudcontroller/ccv2/stack_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("Stack", func() {
    14  	var client *Client
    15  
    16  	BeforeEach(func() {
    17  		client = NewTestClient()
    18  	})
    19  
    20  	Describe("GetStack", func() {
    21  		Context("when the stack is found", func() {
    22  			BeforeEach(func() {
    23  				response := `{
    24  					"metadata": {
    25  						"guid": "some-stack-guid"
    26  					},
    27  					"entity": {
    28  						"name": "some-stack-name",
    29  						"description": "some stack description"
    30  					}
    31  				}`
    32  
    33  				server.AppendHandlers(
    34  					CombineHandlers(
    35  						VerifyRequest(http.MethodGet, "/v2/stacks/some-stack-guid"),
    36  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    37  					),
    38  				)
    39  			})
    40  
    41  			It("returns the stack and warnings", func() {
    42  				stack, warnings, err := client.GetStack("some-stack-guid")
    43  				Expect(err).ToNot(HaveOccurred())
    44  				Expect(stack).To(Equal(Stack{
    45  					Description: "some stack description",
    46  					GUID:        "some-stack-guid",
    47  					Name:        "some-stack-name",
    48  				}))
    49  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    50  			})
    51  		})
    52  
    53  		Context("when the client returns an error", func() {
    54  			BeforeEach(func() {
    55  				response := `{
    56  					"code": 250003,
    57  					"description": "The stack could not be found: some-stack-guid",
    58  					"error_code": "CF-StackNotFound"
    59  				}`
    60  				server.AppendHandlers(
    61  					CombineHandlers(
    62  						VerifyRequest(http.MethodGet, "/v2/stacks/some-stack-guid"),
    63  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    64  					),
    65  				)
    66  			})
    67  
    68  			It("returns the error and warnings", func() {
    69  				_, warnings, err := client.GetStack("some-stack-guid")
    70  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{
    71  					Message: "The stack could not be found: some-stack-guid",
    72  				}))
    73  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    74  			})
    75  		})
    76  	})
    77  
    78  	Describe("GetStacks", func() {
    79  		Context("when no errors are encountered", func() {
    80  			Context("when results are paginated", func() {
    81  				BeforeEach(func() {
    82  					response1 := `{
    83  						"next_url": "/v2/stacks?q=some-query:some-value&page=2",
    84  						"resources": [
    85  							{
    86  								"metadata": {
    87  									"guid": "some-stack-guid-1"
    88  								},
    89  								"entity": {
    90  									"name": "some-stack-name-1",
    91  									"description": "some stack description"
    92  								}
    93  							},
    94  							{
    95  								"metadata": {
    96  									"guid": "some-stack-guid-2"
    97  								},
    98  								"entity": {
    99  									"name": "some-stack-name-2",
   100  									"description": "some stack description"
   101  								}
   102  							}
   103  						]
   104  					}`
   105  					response2 := `{
   106  						"next_url": null,
   107  						"resources": [
   108  							{
   109  								"metadata": {
   110  									"guid": "some-stack-guid-3"
   111  								},
   112  								"entity": {
   113  									"name": "some-stack-name-3",
   114  									"description": "some stack description"
   115  								}
   116  							},
   117  							{
   118  								"metadata": {
   119  									"guid": "some-stack-guid-4"
   120  								},
   121  								"entity": {
   122  									"name": "some-stack-name-4",
   123  									"description": "some stack description"
   124  								}
   125  							}
   126  						]
   127  					}`
   128  					server.AppendHandlers(
   129  						CombineHandlers(
   130  							VerifyRequest(http.MethodGet, "/v2/stacks", "q=some-query:some-value"),
   131  							RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   132  						))
   133  					server.AppendHandlers(
   134  						CombineHandlers(
   135  							VerifyRequest(http.MethodGet, "/v2/stacks", "q=some-query:some-value&page=2"),
   136  							RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"warning-2"}}),
   137  						))
   138  				})
   139  
   140  				It("returns paginated results and all warnings", func() {
   141  					stacks, warnings, err := client.GetStacks(Query{
   142  						Filter:   "some-query",
   143  						Operator: EqualOperator,
   144  						Values:   []string{"some-value"},
   145  					})
   146  
   147  					Expect(err).NotTo(HaveOccurred())
   148  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   149  					Expect(stacks).To(Equal([]Stack{
   150  						{
   151  							Description: "some stack description",
   152  							GUID:        "some-stack-guid-1",
   153  							Name:        "some-stack-name-1",
   154  						},
   155  						{
   156  							Description: "some stack description",
   157  							GUID:        "some-stack-guid-2",
   158  							Name:        "some-stack-name-2",
   159  						},
   160  						{
   161  							Description: "some stack description",
   162  							GUID:        "some-stack-guid-3",
   163  							Name:        "some-stack-name-3",
   164  						},
   165  						{
   166  							Description: "some stack description",
   167  							GUID:        "some-stack-guid-4",
   168  							Name:        "some-stack-name-4",
   169  						},
   170  					}))
   171  				})
   172  			})
   173  		})
   174  
   175  		Context("when an error is encountered", func() {
   176  			BeforeEach(func() {
   177  				response := `{
   178    "code": 10001,
   179    "description": "Some Error",
   180    "error_code": "CF-SomeError"
   181  }`
   182  				server.AppendHandlers(
   183  					CombineHandlers(
   184  						VerifyRequest(http.MethodGet, "/v2/stacks"),
   185  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   186  					))
   187  			})
   188  
   189  			It("returns an error and all warnings", func() {
   190  				_, warnings, err := client.GetStacks()
   191  
   192  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   193  					ResponseCode: http.StatusTeapot,
   194  					V2ErrorResponse: ccerror.V2ErrorResponse{
   195  						Code:        10001,
   196  						Description: "Some Error",
   197  						ErrorCode:   "CF-SomeError",
   198  					},
   199  				}))
   200  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   201  			})
   202  		})
   203  	})
   204  })