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