github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/api/cloudcontroller/ccv3/relationship_list_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     8  	"code.cloudfoundry.org/cli/resources"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("RelationshipList", func() {
    15  	var (
    16  		client *Client
    17  	)
    18  
    19  	BeforeEach(func() {
    20  		client, _ = NewTestClient()
    21  	})
    22  
    23  	Describe("EntitleIsolationSegmentToOrganizations", func() {
    24  		When("the delete is successful", func() {
    25  			BeforeEach(func() {
    26  				response := `{
    27  					"data": [
    28  						{
    29  							"guid": "some-relationship-guid-1"
    30  						},
    31  						{
    32  							"guid": "some-relationship-guid-2"
    33  						}
    34  					]
    35  				}`
    36  
    37  				requestBody := map[string][]map[string]string{
    38  					"data": {{"guid": "org-guid-1"}, {"guid": "org-guid-2"}},
    39  				}
    40  				server.AppendHandlers(
    41  					CombineHandlers(
    42  						VerifyRequest(http.MethodPost, "/v3/isolation_segments/some-iso-guid/relationships/organizations"),
    43  						VerifyJSONRepresenting(requestBody),
    44  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    45  					),
    46  				)
    47  			})
    48  
    49  			It("returns all relationships and warnings", func() {
    50  				relationships, warnings, err := client.EntitleIsolationSegmentToOrganizations("some-iso-guid", []string{"org-guid-1", "org-guid-2"})
    51  				Expect(err).NotTo(HaveOccurred())
    52  				Expect(warnings).To(ConsistOf("this is a warning"))
    53  				Expect(relationships).To(Equal(resources.RelationshipList{
    54  					GUIDs: []string{"some-relationship-guid-1", "some-relationship-guid-2"},
    55  				}))
    56  			})
    57  		})
    58  
    59  		When("the cloud controller returns errors and warnings", func() {
    60  			BeforeEach(func() {
    61  				response := `{
    62  					"errors": [
    63  						{
    64  							"code": 10008,
    65  							"detail": "The request is semantically invalid: command presence",
    66  							"title": "CF-UnprocessableEntity"
    67  						}
    68  					]
    69  				}`
    70  				server.AppendHandlers(
    71  					CombineHandlers(
    72  						VerifyRequest(http.MethodPost, "/v3/isolation_segments/some-iso-guid/relationships/organizations"),
    73  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    74  					),
    75  				)
    76  			})
    77  
    78  			It("returns the error and all warnings", func() {
    79  				_, warnings, err := client.EntitleIsolationSegmentToOrganizations("some-iso-guid", []string{"org-guid-1", "org-guid-2"})
    80  				Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{
    81  					ResponseCode: http.StatusTeapot,
    82  					V3ErrorResponse: ccerror.V3ErrorResponse{
    83  						Errors: []ccerror.V3Error{
    84  							{
    85  								Code:   10008,
    86  								Detail: "The request is semantically invalid: command presence",
    87  								Title:  "CF-UnprocessableEntity",
    88  							},
    89  						},
    90  					},
    91  				}))
    92  				Expect(warnings).To(ConsistOf("this is a warning"))
    93  			})
    94  		})
    95  	})
    96  
    97  	Describe("ShareServiceInstanceToSpaces", func() {
    98  		var (
    99  			serviceInstanceGUID string
   100  			spaceGUIDs          []string
   101  
   102  			relationshipList resources.RelationshipList
   103  			warnings         Warnings
   104  			executeErr       error
   105  		)
   106  
   107  		BeforeEach(func() {
   108  			serviceInstanceGUID = "some-service-instance-guid"
   109  			spaceGUIDs = []string{"some-space-guid", "some-other-space-guid"}
   110  		})
   111  
   112  		JustBeforeEach(func() {
   113  			relationshipList, warnings, executeErr = client.ShareServiceInstanceToSpaces(serviceInstanceGUID, spaceGUIDs)
   114  		})
   115  
   116  		When("no errors are encountered", func() {
   117  			BeforeEach(func() {
   118  				response := `{
   119  					"data": [
   120  						{
   121  							"guid": "some-space-guid"
   122  						},
   123  						{
   124  							"guid": "some-other-space-guid"
   125  						}
   126  					]
   127  				}`
   128  
   129  				requestBody := map[string][]map[string]string{
   130  					"data": {{"guid": "some-space-guid"}, {"guid": "some-other-space-guid"}},
   131  				}
   132  
   133  				server.AppendHandlers(
   134  					CombineHandlers(
   135  						VerifyRequest(http.MethodPost, "/v3/service_instances/some-service-instance-guid/relationships/shared_spaces"),
   136  						VerifyJSONRepresenting(requestBody),
   137  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   138  					),
   139  				)
   140  			})
   141  
   142  			It("returns all relationships and warnings", func() {
   143  				Expect(executeErr).NotTo(HaveOccurred())
   144  				Expect(warnings).To(ConsistOf("this is a warning"))
   145  				Expect(relationshipList).To(Equal(resources.RelationshipList{
   146  					GUIDs: []string{"some-space-guid", "some-other-space-guid"},
   147  				}))
   148  			})
   149  		})
   150  
   151  		When("the cloud controller returns errors and warnings", func() {
   152  			BeforeEach(func() {
   153  				response := `{
   154  					"errors": [
   155  						{
   156  							"code": 10008,
   157  							"detail": "The request is semantically invalid: command presence",
   158  							"title": "CF-UnprocessableEntity"
   159  						}
   160  					]
   161  				}`
   162  				server.AppendHandlers(
   163  					CombineHandlers(
   164  						VerifyRequest(http.MethodPost, "/v3/service_instances/some-service-instance-guid/relationships/shared_spaces"),
   165  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   166  					),
   167  				)
   168  			})
   169  
   170  			It("returns the error and all warnings", func() {
   171  				Expect(executeErr).To(MatchError(ccerror.V3UnexpectedResponseError{
   172  					ResponseCode: http.StatusTeapot,
   173  					V3ErrorResponse: ccerror.V3ErrorResponse{
   174  						Errors: []ccerror.V3Error{
   175  							{
   176  								Code:   10008,
   177  								Detail: "The request is semantically invalid: command presence",
   178  								Title:  "CF-UnprocessableEntity",
   179  							},
   180  						},
   181  					},
   182  				}))
   183  				Expect(warnings).To(ConsistOf("this is a warning"))
   184  			})
   185  		})
   186  	})
   187  })