github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv2/paginated_resources_test.go (about) 1 package ccv2_test 2 3 import ( 4 "encoding/json" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller" 7 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 ) 11 12 type testItem struct { 13 Name string 14 GUID string 15 } 16 17 func (t *testItem) UnmarshalJSON(data []byte) error { 18 var item struct { 19 Metadata struct { 20 GUID string `json:"guid"` 21 } `json:"metadata"` 22 Entity struct { 23 Name string `json:"name"` 24 } `json:"entity"` 25 } 26 err := cloudcontroller.DecodeJSON(data, &item) 27 if err != nil { 28 return err 29 } 30 31 t.GUID = item.Metadata.GUID 32 t.Name = item.Entity.Name 33 return nil 34 } 35 36 var _ = Describe("Paginated Resources", func() { 37 var page *PaginatedResources 38 39 BeforeEach(func() { 40 page = NewPaginatedResources(testItem{}) 41 }) 42 43 Context("unmarshaling from paginated request", func() { 44 var raw []byte 45 46 BeforeEach(func() { 47 raw = []byte(`{ 48 "next_url": "https://no-idea/some-cc-url&page=2", 49 "resources": [ 50 { 51 "metadata": { 52 "guid": "app-guid-1", 53 "updated_at": null 54 }, 55 "entity": { 56 "name": "app-name-1" 57 } 58 }, 59 { 60 "metadata": { 61 "guid": "app-guid-2", 62 "updated_at": null 63 }, 64 "entity": { 65 "name": "app-name-2" 66 } 67 } 68 ] 69 }`) 70 71 err := json.Unmarshal(raw, &page) 72 Expect(err).ToNot(HaveOccurred()) 73 }) 74 75 It("should populate the next_url", func() { 76 Expect(page.NextURL).To(Equal("https://no-idea/some-cc-url&page=2")) 77 }) 78 79 It("should hold onto the whole resource blob", func() { 80 Expect(string(page.ResourcesBytes)).To(MatchJSON(`[ 81 { 82 "metadata": { 83 "guid": "app-guid-1", 84 "updated_at": null 85 }, 86 "entity": { 87 "name": "app-name-1" 88 } 89 }, 90 { 91 "metadata": { 92 "guid": "app-guid-2", 93 "updated_at": null 94 }, 95 "entity": { 96 "name": "app-name-2" 97 } 98 } 99 ]`)) 100 }) 101 }) 102 103 Describe("Resources", func() { 104 BeforeEach(func() { 105 raw := []byte(`[ 106 { 107 "metadata": { 108 "guid": "app-guid-1", 109 "updated_at": null 110 }, 111 "entity": { 112 "name": "app-name-1" 113 } 114 }, 115 { 116 "metadata": { 117 "guid": "app-guid-2", 118 "updated_at": null 119 }, 120 "entity": { 121 "name": "app-name-2" 122 } 123 } 124 ]`) 125 126 page.ResourcesBytes = raw 127 }) 128 129 It("can unmarshal the list of resources into the given struct", func() { 130 items, err := page.Resources() 131 Expect(err).ToNot(HaveOccurred()) 132 133 Expect(items).To(ConsistOf( 134 testItem{GUID: "app-guid-1", Name: "app-name-1"}, 135 testItem{GUID: "app-guid-2", Name: "app-name-2"}, 136 )) 137 }) 138 }) 139 })