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

     1  package ccv3_test
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     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  				"pagination": {
    47  					"total_results": 0,
    48  					"total_pages": 1,
    49  					"first": {
    50  						"href": "https://fake.com/v3/banana?page=1&per_page=50"
    51  					},
    52  					"last": {
    53  						"href": "https://fake.com/v3/banana?page=2&per_page=50"
    54  					},
    55  					"next": {
    56  						"href":"https://fake.com/v3/banana?page=2&per_page=50"
    57  					},
    58  					"previous": null
    59  				},
    60  				"resources": [
    61  					{
    62  						"metadata": {
    63  							"guid": "app-guid-1",
    64  							"updated_at": null
    65  						},
    66  						"entity": {
    67  							"name": "app-name-1"
    68  						}
    69  					},
    70  					{
    71  						"metadata": {
    72  							"guid": "app-guid-2",
    73  							"updated_at": null
    74  						},
    75  						"entity": {
    76  							"name": "app-name-2"
    77  						}
    78  					}
    79  				]
    80  			}`)
    81  
    82  			err := json.Unmarshal(raw, &page)
    83  			Expect(err).ToNot(HaveOccurred())
    84  		})
    85  
    86  		It("should populate NextURL", func() {
    87  			Expect(page.NextPage()).To(Equal("https://fake.com/v3/banana?page=2&per_page=50"))
    88  		})
    89  
    90  		It("should hold onto the whole resource blob", func() {
    91  			Expect(string(page.ResourcesBytes)).To(MatchJSON(`[
    92  					{
    93  						"metadata": {
    94  							"guid": "app-guid-1",
    95  							"updated_at": null
    96  						},
    97  						"entity": {
    98  							"name": "app-name-1"
    99  						}
   100  					},
   101  					{
   102  						"metadata": {
   103  							"guid": "app-guid-2",
   104  							"updated_at": null
   105  						},
   106  						"entity": {
   107  							"name": "app-name-2"
   108  						}
   109  					}
   110  				]`))
   111  		})
   112  	})
   113  
   114  	Describe("Resources", func() {
   115  		BeforeEach(func() {
   116  			raw := []byte(`[
   117  					{
   118  						"metadata": {
   119  							"guid": "app-guid-1",
   120  							"updated_at": null
   121  						},
   122  						"entity": {
   123  							"name": "app-name-1"
   124  						}
   125  					},
   126  					{
   127  						"metadata": {
   128  							"guid": "app-guid-2",
   129  							"updated_at": null
   130  						},
   131  						"entity": {
   132  							"name": "app-name-2"
   133  						}
   134  					}
   135  				]`)
   136  
   137  			page.ResourcesBytes = raw
   138  		})
   139  
   140  		It("can unmarshal the list of resources into the given struct", func() {
   141  			items, err := page.Resources()
   142  			Expect(err).ToNot(HaveOccurred())
   143  
   144  			Expect(items).To(ConsistOf(
   145  				testItem{GUID: "app-guid-1", Name: "app-name-1"},
   146  				testItem{GUID: "app-guid-2", Name: "app-name-2"},
   147  			))
   148  		})
   149  	})
   150  })