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