github.com/LukasHeimann/cloudfoundrycli@v7.1.0+incompatible/api/cloudcontroller/ccv3/resource_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"os"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("Resource", func() {
    16  	Describe("V3 formatted resource", func() {
    17  		Describe("MarshalJSON", func() {
    18  			It("marshals the json properly", func() {
    19  				resource := Resource{
    20  					FilePath:    "some-file-1",
    21  					Mode:        os.FileMode(0744),
    22  					Checksum:    Checksum{Value: "some-sha-1"},
    23  					SizeInBytes: 1,
    24  				}
    25  				data, err := json.Marshal(resource)
    26  				Expect(err).ToNot(HaveOccurred())
    27  				Expect(data).To(MatchJSON(`{
    28  				"path":   "some-file-1",
    29  				"mode": "744",
    30  				"checksum": {"value":"some-sha-1"},
    31  				"size_in_bytes": 1
    32  			}`))
    33  			})
    34  		})
    35  
    36  		Describe("UnmarshalJSON", func() {
    37  			It("Unmarshals the json properly", func() {
    38  				raw := `{
    39  				"path":   "some-file-1",
    40  				"mode": "744",
    41  				"checksum": {"value":"some-sha-1"},
    42  				"size_in_bytes": 1
    43  			}`
    44  
    45  				var data Resource
    46  				err := json.Unmarshal([]byte(raw), &data)
    47  				Expect(err).ToNot(HaveOccurred())
    48  				Expect(data).To(Equal(Resource{
    49  					FilePath:    "some-file-1",
    50  					Mode:        os.FileMode(0744),
    51  					Checksum:    Checksum{Value: "some-sha-1"},
    52  					SizeInBytes: 1,
    53  				}))
    54  			})
    55  		})
    56  	})
    57  
    58  	Describe("Resource Match", func() {
    59  
    60  		var (
    61  			client           *Client
    62  			allResources     []Resource
    63  			matchedResources []Resource
    64  			warnings         Warnings
    65  			executeErr       error
    66  		)
    67  
    68  		BeforeEach(func() {
    69  			client, _ = NewTestClient()
    70  			allResources = []Resource{
    71  				{FilePath: "where are you", Checksum: Checksum{Value: "value1"}, SizeInBytes: 2},
    72  				{FilePath: "bar", Checksum: Checksum{Value: "value2"}, SizeInBytes: 3},
    73  			}
    74  		})
    75  
    76  		JustBeforeEach(func() {
    77  			matchedResources, warnings, executeErr = client.ResourceMatch(allResources)
    78  		})
    79  
    80  		When("the match is successful", func() {
    81  			When("the upload has application bits to upload", func() {
    82  
    83  				BeforeEach(func() {
    84  
    85  					expectedBody := `{
    86    "resources": [
    87      {
    88        "path": "where are you",
    89  			"checksum": {
    90          "value": "value1"
    91  			},
    92  			"size_in_bytes": 2,
    93  			"mode": "0"
    94      },
    95      {
    96        "path": "bar",
    97  			"checksum": {
    98          "value": "value2"
    99  			},
   100  			"size_in_bytes": 3,
   101  			"mode": "0"
   102      }
   103    ]
   104  }`
   105  
   106  					response := `{
   107    "resources": [
   108      {
   109        "checksum": {
   110  		    "value":"scooby dooby"
   111  		  },
   112  	    "size_in_bytes": 2,
   113        "path": "where are you? we got a job for you now",
   114  			"mode": "644"
   115      }
   116    ]
   117  }`
   118  
   119  					server.AppendHandlers(
   120  						CombineHandlers(
   121  							VerifyRequest(http.MethodPost, "/v3/resource_matches"),
   122  							VerifyJSON(expectedBody),
   123  							RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   124  						),
   125  					)
   126  				})
   127  
   128  				It("returns the matched resources", func() {
   129  					Expect(executeErr).NotTo(HaveOccurred())
   130  					Expect(warnings).To(ConsistOf("this is a warning"))
   131  					Expect(matchedResources).To(ConsistOf(Resource{
   132  						Checksum:    Checksum{Value: "scooby dooby"},
   133  						SizeInBytes: 2,
   134  						FilePath:    "where are you? we got a job for you now",
   135  						Mode:        os.FileMode(0644),
   136  					}))
   137  				})
   138  			})
   139  
   140  		})
   141  
   142  		When("the CC returns an error", func() {
   143  			BeforeEach(func() {
   144  				response := ` {
   145  					"errors": [
   146  						{
   147  							"code": 10008,
   148  							"detail": "Banana",
   149  							"title": "CF-Banana"
   150  						}
   151  					]
   152  				}`
   153  
   154  				server.AppendHandlers(
   155  					CombineHandlers(
   156  						VerifyRequest(http.MethodPost, "/v3/resource_matches"),
   157  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   158  					),
   159  				)
   160  			})
   161  
   162  			It("returns the error", func() {
   163  				Expect(executeErr).To(MatchError(ccerror.ResourceNotFoundError{Message: "Banana"}))
   164  				Expect(warnings).To(ConsistOf("this is a warning"))
   165  			})
   166  		})
   167  	})
   168  })