github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/api/cloudcontroller/ccv2/application_test.go (about)

     1  package ccv2_test
     2  
     3  import (
     4  	"net/http"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     9  	"code.cloudfoundry.org/cli/types"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("Application", func() {
    16  	var client *Client
    17  
    18  	BeforeEach(func() {
    19  		client = NewTestClient()
    20  	})
    21  
    22  	Describe("CreateApplication", func() {
    23  		Context("when the update is successful", func() {
    24  			Context("when setting the minimum", func() { // are we **only** encoding the things we want
    25  				BeforeEach(func() {
    26  					response := `
    27  						{
    28  							"metadata": {
    29  								"guid": "some-app-guid"
    30  							},
    31  							"entity": {
    32  								"name": "some-app-name",
    33  								"space_guid": "some-space-guid"
    34  							}
    35  						}`
    36  					requestBody := map[string]string{
    37  						"name":       "some-app-name",
    38  						"space_guid": "some-space-guid",
    39  					}
    40  					server.AppendHandlers(
    41  						CombineHandlers(
    42  							VerifyRequest(http.MethodPost, "/v2/apps"),
    43  							VerifyJSONRepresenting(requestBody),
    44  							RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    45  						),
    46  					)
    47  				})
    48  
    49  				It("returns the created object and warnings", func() {
    50  					app, warnings, err := client.CreateApplication(Application{
    51  						Name:      "some-app-name",
    52  						SpaceGUID: "some-space-guid",
    53  					})
    54  					Expect(err).NotTo(HaveOccurred())
    55  
    56  					Expect(app).To(Equal(Application{
    57  						GUID: "some-app-guid",
    58  						Name: "some-app-name",
    59  					}))
    60  					Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    61  				})
    62  			})
    63  		})
    64  
    65  		Context("when the create returns an error", func() {
    66  			BeforeEach(func() {
    67  				response := `
    68  					{
    69  						"description": "Request invalid due to parse error: Field: name, Error: Missing field name, Field: space_guid, Error: Missing field space_guid",
    70  						"error_code": "CF-MessageParseError",
    71  						"code": 1001
    72  					}
    73  			`
    74  				server.AppendHandlers(
    75  					CombineHandlers(
    76  						VerifyRequest(http.MethodPost, "/v2/apps"),
    77  						RespondWith(http.StatusBadRequest, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    78  					),
    79  				)
    80  			})
    81  
    82  			It("returns the error and warnings", func() {
    83  				_, warnings, err := client.CreateApplication(Application{})
    84  				Expect(err).To(MatchError(ccerror.BadRequestError{Message: "Request invalid due to parse error: Field: name, Error: Missing field name, Field: space_guid, Error: Missing field space_guid"}))
    85  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    86  			})
    87  		})
    88  	})
    89  
    90  	Describe("GetApplication", func() {
    91  		BeforeEach(func() {
    92  			response := `{
    93  						"metadata": {
    94  							"guid": "app-guid-1",
    95  							"updated_at": null
    96  						},
    97  						"entity": {
    98  							"buildpack": "ruby 1.6.29",
    99  							"command": "some-command",
   100  							"detected_start_command": "echo 'I am a banana'",
   101  							"disk_quota": 586,
   102  							"detected_buildpack": null,
   103  							"docker_credentials": {
   104  								"username": "docker-username",
   105  								"password": "docker-password"
   106  							},
   107  							"docker_image": "some-docker-path",
   108  							"environment_json": {
   109  								"key1": "val1",
   110  								"key2": 83493475092347,
   111  								"key3": true,
   112  								"key4": 75821.521
   113  							},
   114  							"health_check_timeout": 120,
   115  							"health_check_type": "port",
   116  							"health_check_http_endpoint": "/",
   117  							"instances": 13,
   118  							"memory": 1024,
   119  							"name": "app-name-1",
   120  							"package_state": "FAILED",
   121  							"package_updated_at": "2015-03-10T23:11:54Z",
   122  							"stack_guid": "some-stack-guid",
   123  							"staging_failed_description": "some-staging-failed-description",
   124  							"staging_failed_reason": "some-reason",
   125  							"state": "STOPPED"
   126  						}
   127  			}`
   128  			server.AppendHandlers(
   129  				CombineHandlers(
   130  					VerifyRequest(http.MethodGet, "/v2/apps/app-guid-1"),
   131  					RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   132  				),
   133  			)
   134  		})
   135  
   136  		Context("when apps exist", func() {
   137  			It("returns the app", func() {
   138  				app, warnings, err := client.GetApplication("app-guid-1")
   139  				Expect(err).NotTo(HaveOccurred())
   140  
   141  				updatedAt, err := time.Parse(time.RFC3339, "2015-03-10T23:11:54Z")
   142  				Expect(err).NotTo(HaveOccurred())
   143  
   144  				Expect(app).To(Equal(Application{
   145  					Buildpack:            types.FilteredString{IsSet: true, Value: "ruby 1.6.29"},
   146  					Command:              types.FilteredString{IsSet: true, Value: "some-command"},
   147  					DetectedBuildpack:    types.FilteredString{},
   148  					DetectedStartCommand: types.FilteredString{IsSet: true, Value: "echo 'I am a banana'"},
   149  					DiskQuota:            586,
   150  					DockerCredentials: DockerCredentials{
   151  						Username: "docker-username",
   152  						Password: "docker-password",
   153  					},
   154  					DockerImage: "some-docker-path",
   155  					EnvironmentVariables: map[string]string{
   156  						"key1": "val1",
   157  						"key2": "83493475092347",
   158  						"key3": "true",
   159  						"key4": "75821.521",
   160  					},
   161  					GUID:                     "app-guid-1",
   162  					HealthCheckTimeout:       120,
   163  					HealthCheckType:          "port",
   164  					HealthCheckHTTPEndpoint:  "/",
   165  					Instances:                types.NullInt{Value: 13, IsSet: true},
   166  					Memory:                   1024,
   167  					Name:                     "app-name-1",
   168  					PackageState:             ApplicationPackageFailed,
   169  					PackageUpdatedAt:         updatedAt,
   170  					StackGUID:                "some-stack-guid",
   171  					StagingFailedDescription: "some-staging-failed-description",
   172  					StagingFailedReason:      "some-reason",
   173  					State:                    ApplicationStopped,
   174  				}))
   175  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   176  			})
   177  		})
   178  	})
   179  
   180  	Describe("GetApplications", func() {
   181  		BeforeEach(func() {
   182  			response1 := `{
   183  				"next_url": "/v2/apps?q=space_guid:some-space-guid&page=2",
   184  				"resources": [
   185  					{
   186  						"metadata": {
   187  							"guid": "app-guid-1",
   188  							"updated_at": null
   189  						},
   190  						"entity": {
   191  							"buildpack": "ruby 1.6.29",
   192  							"detected_start_command": "echo 'I am a banana'",
   193  							"disk_quota": 586,
   194  							"detected_buildpack": null,
   195  							"health_check_type": "port",
   196  							"health_check_http_endpoint": "/",
   197  							"instances": 13,
   198  							"memory": 1024,
   199  							"name": "app-name-1",
   200  							"package_state": "FAILED",
   201  							"package_updated_at": "2015-03-10T23:11:54Z",
   202  							"stack_guid": "some-stack-guid",
   203  							"staging_failed_reason": "some-reason",
   204  							"state": "STOPPED"
   205  						}
   206  					},
   207  					{
   208  						"metadata": {
   209  							"guid": "app-guid-2",
   210  							"updated_at": null
   211  						},
   212  						"entity": {
   213  							"name": "app-name-2",
   214  							"detected_buildpack": "ruby 1.6.29",
   215  							"package_updated_at": null
   216  						}
   217  					}
   218  				]
   219  			}`
   220  			response2 := `{
   221  				"next_url": null,
   222  				"resources": [
   223  					{
   224  						"metadata": {
   225  							"guid": "app-guid-3",
   226  							"updated_at": null
   227  						},
   228  						"entity": {
   229  							"name": "app-name-3"
   230  						}
   231  					},
   232  					{
   233  						"metadata": {
   234  							"guid": "app-guid-4",
   235  							"updated_at": null
   236  						},
   237  						"entity": {
   238  							"name": "app-name-4"
   239  						}
   240  					}
   241  				]
   242  			}`
   243  			server.AppendHandlers(
   244  				CombineHandlers(
   245  					VerifyRequest(http.MethodGet, "/v2/apps", "q=space_guid:some-space-guid"),
   246  					RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   247  				),
   248  			)
   249  			server.AppendHandlers(
   250  				CombineHandlers(
   251  					VerifyRequest(http.MethodGet, "/v2/apps", "q=space_guid:some-space-guid&page=2"),
   252  					RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
   253  				),
   254  			)
   255  		})
   256  
   257  		Context("when apps exist", func() {
   258  			It("returns all the queried apps", func() {
   259  				apps, warnings, err := client.GetApplications(Query{
   260  					Filter:   SpaceGUIDFilter,
   261  					Operator: EqualOperator,
   262  					Values:   []string{"some-space-guid"},
   263  				})
   264  				Expect(err).NotTo(HaveOccurred())
   265  
   266  				updatedAt, err := time.Parse(time.RFC3339, "2015-03-10T23:11:54Z")
   267  				Expect(err).NotTo(HaveOccurred())
   268  
   269  				Expect(apps).To(ConsistOf([]Application{
   270  					{
   271  						Buildpack:               types.FilteredString{IsSet: true, Value: "ruby 1.6.29"},
   272  						DetectedBuildpack:       types.FilteredString{},
   273  						DetectedStartCommand:    types.FilteredString{IsSet: true, Value: "echo 'I am a banana'"},
   274  						DiskQuota:               586,
   275  						GUID:                    "app-guid-1",
   276  						HealthCheckType:         "port",
   277  						HealthCheckHTTPEndpoint: "/",
   278  						Instances:               types.NullInt{Value: 13, IsSet: true},
   279  						Memory:                  1024,
   280  						Name:                    "app-name-1",
   281  						PackageState:            ApplicationPackageFailed,
   282  						PackageUpdatedAt:        updatedAt,
   283  						StackGUID:               "some-stack-guid",
   284  						StagingFailedReason:     "some-reason",
   285  						State:                   ApplicationStopped,
   286  					},
   287  					{
   288  						Name:              "app-name-2",
   289  						GUID:              "app-guid-2",
   290  						DetectedBuildpack: types.FilteredString{IsSet: true, Value: "ruby 1.6.29"},
   291  					},
   292  					{Name: "app-name-3", GUID: "app-guid-3"},
   293  					{Name: "app-name-4", GUID: "app-guid-4"},
   294  				}))
   295  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"}))
   296  			})
   297  		})
   298  	})
   299  
   300  	Describe("UpdateApplication", func() {
   301  		Context("when the update is successful", func() {
   302  			Context("when updating all fields", func() { //are we encoding everything correctly?
   303  				BeforeEach(func() {
   304  					response1 := `{
   305  				"metadata": {
   306  					"guid": "some-app-guid",
   307  					"updated_at": null
   308  				},
   309  				"entity": {
   310  					"detected_start_command": "echo 'I am a banana'",
   311  					"disk_quota": 586,
   312  					"detected_buildpack": null,
   313  					"docker_credentials": {
   314  						"username": "docker-username",
   315  						"password": "docker-password"
   316  					},
   317  					"docker_image": "some-docker-path",
   318  					"environment_json": {
   319  						"key1": "val1",
   320  						"key2": 83493475092347,
   321  						"key3": true,
   322  						"key4": 75821.521
   323  					},
   324  					"health_check_timeout": 120,
   325  					"health_check_type": "some-health-check-type",
   326  					"health_check_http_endpoint": "/anything",
   327  					"instances": 0,
   328  					"memory": 1024,
   329  					"name": "app-name-1",
   330  					"package_updated_at": "2015-03-10T23:11:54Z",
   331  					"stack_guid": "some-stack-guid",
   332  					"state": "STARTED"
   333  				}
   334  			}`
   335  					expectedBody := map[string]interface{}{
   336  						"buildpack":  "",
   337  						"command":    "",
   338  						"disk_quota": 586,
   339  						"docker_credentials": map[string]string{
   340  							"username": "docker-username",
   341  							"password": "docker-password",
   342  						},
   343  						"docker_image": "some-docker-path",
   344  						"environment_json": map[string]string{
   345  							"key1": "val1",
   346  							"key2": "83493475092347",
   347  							"key3": "true",
   348  							"key4": "75821.521",
   349  						},
   350  						"health_check_http_endpoint": "/anything",
   351  						"health_check_type":          "some-health-check-type",
   352  						"instances":                  0,
   353  						"memory":                     1024,
   354  						"stack_guid":                 "some-stack-guid",
   355  						"state":                      "STARTED",
   356  					}
   357  
   358  					server.AppendHandlers(
   359  						CombineHandlers(
   360  							VerifyRequest(http.MethodPut, "/v2/apps/some-app-guid"),
   361  							VerifyJSONRepresenting(expectedBody),
   362  							RespondWith(http.StatusCreated, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   363  						),
   364  					)
   365  				})
   366  
   367  				It("returns the updated object and warnings and sends all updated field", func() {
   368  					app, warnings, err := client.UpdateApplication(Application{
   369  						Buildpack: types.FilteredString{IsSet: true, Value: ""},
   370  						Command:   types.FilteredString{IsSet: true, Value: ""},
   371  						DiskQuota: 586,
   372  						DockerCredentials: DockerCredentials{
   373  							Username: "docker-username",
   374  							Password: "docker-password",
   375  						},
   376  						DockerImage: "some-docker-path",
   377  						EnvironmentVariables: map[string]string{
   378  							"key1": "val1",
   379  							"key2": "83493475092347",
   380  							"key3": "true",
   381  							"key4": "75821.521",
   382  						},
   383  						GUID: "some-app-guid",
   384  						HealthCheckHTTPEndpoint: "/anything",
   385  						HealthCheckType:         "some-health-check-type",
   386  						Instances:               types.NullInt{Value: 0, IsSet: true},
   387  						Memory:                  1024,
   388  						StackGUID:               "some-stack-guid",
   389  						State:                   ApplicationStarted,
   390  					})
   391  					Expect(err).NotTo(HaveOccurred())
   392  
   393  					updatedAt, err := time.Parse(time.RFC3339, "2015-03-10T23:11:54Z")
   394  					Expect(err).NotTo(HaveOccurred())
   395  
   396  					Expect(app).To(Equal(Application{
   397  						DetectedBuildpack:    types.FilteredString{},
   398  						DetectedStartCommand: types.FilteredString{IsSet: true, Value: "echo 'I am a banana'"},
   399  						DiskQuota:            586,
   400  						DockerCredentials: DockerCredentials{
   401  							Username: "docker-username",
   402  							Password: "docker-password",
   403  						},
   404  						DockerImage: "some-docker-path",
   405  						EnvironmentVariables: map[string]string{
   406  							"key1": "val1",
   407  							"key2": "83493475092347",
   408  							"key3": "true",
   409  							"key4": "75821.521",
   410  						},
   411  						GUID: "some-app-guid",
   412  						HealthCheckHTTPEndpoint: "/anything",
   413  						HealthCheckTimeout:      120,
   414  						HealthCheckType:         "some-health-check-type",
   415  						Instances:               types.NullInt{Value: 0, IsSet: true},
   416  						Memory:                  1024,
   417  						Name:                    "app-name-1",
   418  						PackageUpdatedAt:        updatedAt,
   419  						StackGUID:               "some-stack-guid",
   420  						State:                   ApplicationStarted,
   421  					}))
   422  					Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   423  				})
   424  			})
   425  
   426  			Context("when only updating one field", func() { // are we **only** encoding the things we want
   427  				BeforeEach(func() {
   428  					response1 := `{
   429  				"metadata": {
   430  					"guid": "some-app-guid",
   431  					"updated_at": null
   432  				},
   433  				"entity": {
   434  					"buildpack": "ruby 1.6.29",
   435  					"detected_start_command": "echo 'I am a banana'",
   436  					"disk_quota": 586,
   437  					"detected_buildpack": null,
   438  					"health_check_type": "some-health-check-type",
   439  					"health_check_http_endpoint": "/",
   440  					"instances": 13,
   441  					"memory": 1024,
   442  					"name": "app-name-1",
   443  					"package_updated_at": "2015-03-10T23:11:54Z",
   444  					"stack_guid": "some-stack-guid",
   445  					"state": "STOPPED"
   446  				}
   447  			}`
   448  					server.AppendHandlers(
   449  						CombineHandlers(
   450  							VerifyRequest(http.MethodPut, "/v2/apps/some-app-guid"),
   451  							VerifyBody([]byte(`{"health_check_type":"some-health-check-type"}`)),
   452  							RespondWith(http.StatusCreated, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   453  						),
   454  					)
   455  				})
   456  
   457  				It("returns the updated object and warnings and sends only updated field", func() {
   458  					app, warnings, err := client.UpdateApplication(Application{
   459  						GUID:            "some-app-guid",
   460  						HealthCheckType: "some-health-check-type",
   461  					})
   462  					Expect(err).NotTo(HaveOccurred())
   463  
   464  					updatedAt, err := time.Parse(time.RFC3339, "2015-03-10T23:11:54Z")
   465  					Expect(err).NotTo(HaveOccurred())
   466  
   467  					Expect(app).To(Equal(Application{
   468  						Buildpack:               types.FilteredString{IsSet: true, Value: "ruby 1.6.29"},
   469  						DetectedBuildpack:       types.FilteredString{},
   470  						DetectedStartCommand:    types.FilteredString{IsSet: true, Value: "echo 'I am a banana'"},
   471  						DiskQuota:               586,
   472  						GUID:                    "some-app-guid",
   473  						HealthCheckType:         "some-health-check-type",
   474  						HealthCheckHTTPEndpoint: "/",
   475  						Instances:               types.NullInt{Value: 13, IsSet: true},
   476  						Memory:                  1024,
   477  						Name:                    "app-name-1",
   478  						PackageUpdatedAt:        updatedAt,
   479  						StackGUID:               "some-stack-guid",
   480  						State:                   ApplicationStopped,
   481  					}))
   482  					Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   483  				})
   484  			})
   485  		})
   486  
   487  		Context("when the update returns an error", func() {
   488  			BeforeEach(func() {
   489  				response := `
   490  {
   491    "code": 210002,
   492    "description": "The app could not be found: some-app-guid",
   493    "error_code": "CF-AppNotFound"
   494  }
   495  			`
   496  				server.AppendHandlers(
   497  					CombineHandlers(
   498  						VerifyRequest(http.MethodPut, "/v2/apps/some-app-guid"),
   499  						// VerifyBody([]byte(`{"health_check_type":"some-health-check-type"}`)),
   500  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   501  					),
   502  				)
   503  			})
   504  
   505  			It("returns the error and warnings", func() {
   506  				_, warnings, err := client.UpdateApplication(Application{
   507  					GUID:            "some-app-guid",
   508  					HealthCheckType: "some-health-check-type",
   509  				})
   510  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{Message: "The app could not be found: some-app-guid"}))
   511  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   512  			})
   513  		})
   514  	})
   515  
   516  	Describe("RestageApplication", func() {
   517  		Context("when the restage is successful", func() {
   518  			BeforeEach(func() {
   519  				response := `{
   520  					"metadata": {
   521  						"guid": "some-app-guid",
   522  						"url": "/v2/apps/some-app-guid"
   523  					},
   524  					"entity": {
   525  						"buildpack": "ruby 1.6.29",
   526  						"detected_start_command": "echo 'I am a banana'",
   527  						"disk_quota": 586,
   528  						"detected_buildpack": null,
   529  						"docker_image": "some-docker-path",
   530  						"health_check_type": "some-health-check-type",
   531  						"health_check_http_endpoint": "/anything",
   532  						"instances": 13,
   533  						"memory": 1024,
   534  						"name": "app-name-1",
   535  						"package_updated_at": "2015-03-10T23:11:54Z",
   536  						"stack_guid": "some-stack-guid",
   537  						"state": "STARTED"
   538  					}
   539  				}`
   540  
   541  				server.AppendHandlers(
   542  					CombineHandlers(
   543  						VerifyRequest(http.MethodPost, "/v2/apps/some-app-guid/restage"),
   544  						RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   545  					),
   546  				)
   547  			})
   548  
   549  			It("returns the updated object and warnings and sends all updated field", func() {
   550  				app, warnings, err := client.RestageApplication(Application{
   551  					DockerImage:             "some-docker-path",
   552  					GUID:                    "some-app-guid",
   553  					HealthCheckType:         "some-health-check-type",
   554  					HealthCheckHTTPEndpoint: "/anything",
   555  					State: ApplicationStarted,
   556  				})
   557  				Expect(err).NotTo(HaveOccurred())
   558  
   559  				updatedAt, err := time.Parse(time.RFC3339, "2015-03-10T23:11:54Z")
   560  				Expect(err).NotTo(HaveOccurred())
   561  
   562  				Expect(app).To(Equal(Application{
   563  					Buildpack:               types.FilteredString{IsSet: true, Value: "ruby 1.6.29"},
   564  					DetectedBuildpack:       types.FilteredString{},
   565  					DetectedStartCommand:    types.FilteredString{IsSet: true, Value: "echo 'I am a banana'"},
   566  					DiskQuota:               586,
   567  					DockerImage:             "some-docker-path",
   568  					GUID:                    "some-app-guid",
   569  					HealthCheckType:         "some-health-check-type",
   570  					HealthCheckHTTPEndpoint: "/anything",
   571  					Instances:               types.NullInt{Value: 13, IsSet: true},
   572  					Memory:                  1024,
   573  					Name:                    "app-name-1",
   574  					PackageUpdatedAt:        updatedAt,
   575  					StackGUID:               "some-stack-guid",
   576  					State:                   ApplicationStarted,
   577  				}))
   578  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   579  			})
   580  		})
   581  
   582  		Context("when the restage returns an error", func() {
   583  			BeforeEach(func() {
   584  				response := `
   585  {
   586    "code": 210002,
   587    "description": "The app could not be found: some-app-guid",
   588    "error_code": "CF-AppNotFound"
   589  }
   590  			`
   591  				server.AppendHandlers(
   592  					CombineHandlers(
   593  						VerifyRequest(http.MethodPost, "/v2/apps/some-app-guid/restage"),
   594  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   595  					),
   596  				)
   597  			})
   598  
   599  			It("returns the error and warnings", func() {
   600  				_, warnings, err := client.RestageApplication(Application{
   601  					GUID:            "some-app-guid",
   602  					HealthCheckType: "some-health-check-type",
   603  				})
   604  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{Message: "The app could not be found: some-app-guid"}))
   605  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   606  			})
   607  		})
   608  	})
   609  
   610  	Describe("GetRouteApplications", func() {
   611  		Context("when the route guid is not found", func() {
   612  			BeforeEach(func() {
   613  				response := `
   614  {
   615    "code": 210002,
   616    "description": "The route could not be found: some-route-guid",
   617    "error_code": "CF-RouteNotFound"
   618  }
   619  			`
   620  				server.AppendHandlers(
   621  					CombineHandlers(
   622  						VerifyRequest(http.MethodGet, "/v2/routes/some-route-guid/apps"),
   623  						RespondWith(http.StatusNotFound, response),
   624  					),
   625  				)
   626  			})
   627  
   628  			It("returns an error", func() {
   629  				_, _, err := client.GetRouteApplications("some-route-guid")
   630  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{
   631  					Message: "The route could not be found: some-route-guid",
   632  				}))
   633  			})
   634  		})
   635  
   636  		Context("when there are applications associated with this route", func() {
   637  			BeforeEach(func() {
   638  				response1 := `{
   639  				"next_url": "/v2/routes/some-route-guid/apps?q=space_guid:some-space-guid&page=2",
   640  				"resources": [
   641  					{
   642  						"metadata": {
   643  							"guid": "app-guid-1",
   644  							"updated_at": null
   645  						},
   646  						"entity": {
   647  							"name": "app-name-1"
   648  						}
   649  					},
   650  					{
   651  						"metadata": {
   652  							"guid": "app-guid-2",
   653  							"updated_at": null
   654  						},
   655  						"entity": {
   656  							"name": "app-name-2"
   657  						}
   658  					}
   659  				]
   660  			}`
   661  				response2 := `{
   662  				"next_url": null,
   663  				"resources": [
   664  					{
   665  						"metadata": {
   666  							"guid": "app-guid-3",
   667  							"updated_at": null
   668  						},
   669  						"entity": {
   670  							"name": "app-name-3"
   671  						}
   672  					},
   673  					{
   674  						"metadata": {
   675  							"guid": "app-guid-4",
   676  							"updated_at": null
   677  						},
   678  						"entity": {
   679  							"name": "app-name-4"
   680  						}
   681  					}
   682  				]
   683  			}`
   684  				server.AppendHandlers(
   685  					CombineHandlers(
   686  						VerifyRequest(http.MethodGet, "/v2/routes/some-route-guid/apps", "q=space_guid:some-space-guid"),
   687  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   688  					),
   689  				)
   690  				server.AppendHandlers(
   691  					CombineHandlers(
   692  						VerifyRequest(http.MethodGet, "/v2/routes/some-route-guid/apps", "q=space_guid:some-space-guid&page=2"),
   693  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
   694  					),
   695  				)
   696  			})
   697  
   698  			It("returns all the applications and all warnings", func() {
   699  				apps, warnings, err := client.GetRouteApplications("some-route-guid", Query{
   700  					Filter:   SpaceGUIDFilter,
   701  					Operator: EqualOperator,
   702  					Values:   []string{"some-space-guid"},
   703  				})
   704  				Expect(err).NotTo(HaveOccurred())
   705  				Expect(apps).To(ConsistOf([]Application{
   706  					{Name: "app-name-1", GUID: "app-guid-1"},
   707  					{Name: "app-name-2", GUID: "app-guid-2"},
   708  					{Name: "app-name-3", GUID: "app-guid-3"},
   709  					{Name: "app-name-4", GUID: "app-guid-4"},
   710  				}))
   711  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"}))
   712  			})
   713  		})
   714  
   715  		Context("when there are no applications associated with this route", func() {
   716  			BeforeEach(func() {
   717  				response := `{
   718  				"next_url": "",
   719  				"resources": []
   720  			}`
   721  				server.AppendHandlers(
   722  					CombineHandlers(
   723  						VerifyRequest(http.MethodGet, "/v2/routes/some-route-guid/apps"),
   724  						RespondWith(http.StatusOK, response),
   725  					),
   726  				)
   727  			})
   728  
   729  			It("returns an empty list of applications", func() {
   730  				apps, _, err := client.GetRouteApplications("some-route-guid")
   731  				Expect(err).NotTo(HaveOccurred())
   732  				Expect(apps).To(BeEmpty())
   733  			})
   734  		})
   735  	})
   736  })