github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+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"
     7  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     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  				"pagination": {
    49  					"total_results": 0,
    50  					"total_pages": 1,
    51  					"first": {
    52  						"href": "https://fake.com/v3/banana?page=1&per_page=50"
    53  					},
    54  					"last": {
    55  						"href": "https://fake.com/v3/banana?page=2&per_page=50"
    56  					},
    57  					"next": {
    58  						"href":"https://fake.com/v3/banana?page=2&per_page=50"
    59  					},
    60  					"previous": null
    61  				},
    62  				"resources": [
    63  					{
    64  						"metadata": {
    65  							"guid": "app-guid-1",
    66  							"updated_at": null
    67  						},
    68  						"entity": {
    69  							"name": "app-name-1"
    70  						}
    71  					},
    72  					{
    73  						"metadata": {
    74  							"guid": "app-guid-2",
    75  							"updated_at": null
    76  						},
    77  						"entity": {
    78  							"name": "app-name-2"
    79  						}
    80  					}
    81  				]
    82  			}`)
    83  
    84  			err := json.Unmarshal(raw, &page)
    85  			Expect(err).ToNot(HaveOccurred())
    86  		})
    87  
    88  		It("should populate NextURL", func() {
    89  			Expect(page.NextPage()).To(Equal("https://fake.com/v3/banana?page=2&per_page=50"))
    90  		})
    91  
    92  		It("should hold onto the whole resource blob", func() {
    93  			Expect(string(page.ResourcesBytes)).To(MatchJSON(`[
    94  					{
    95  						"metadata": {
    96  							"guid": "app-guid-1",
    97  							"updated_at": null
    98  						},
    99  						"entity": {
   100  							"name": "app-name-1"
   101  						}
   102  					},
   103  					{
   104  						"metadata": {
   105  							"guid": "app-guid-2",
   106  							"updated_at": null
   107  						},
   108  						"entity": {
   109  							"name": "app-name-2"
   110  						}
   111  					}
   112  				]`))
   113  		})
   114  	})
   115  
   116  	Describe("Resources", func() {
   117  		BeforeEach(func() {
   118  			raw := []byte(`[
   119  					{
   120  						"metadata": {
   121  							"guid": "app-guid-1",
   122  							"updated_at": null
   123  						},
   124  						"entity": {
   125  							"name": "app-name-1"
   126  						}
   127  					},
   128  					{
   129  						"metadata": {
   130  							"guid": "app-guid-2",
   131  							"updated_at": null
   132  						},
   133  						"entity": {
   134  							"name": "app-name-2"
   135  						}
   136  					}
   137  				]`)
   138  
   139  			page.ResourcesBytes = raw
   140  		})
   141  
   142  		It("can unmarshal the list of resources into the given struct", func() {
   143  			items, err := page.Resources()
   144  			Expect(err).ToNot(HaveOccurred())
   145  
   146  			Expect(items).To(ConsistOf(
   147  				testItem{GUID: "app-guid-1", Name: "app-name-1"},
   148  				testItem{GUID: "app-guid-2", Name: "app-name-2"},
   149  			))
   150  		})
   151  	})
   152  
   153  	Describe("IncludedResources", func() {
   154  		var raw []byte
   155  
   156  		BeforeEach(func() {
   157  			raw = []byte(`{
   158  				"pagination": {
   159  					"next": {
   160  						"href":"https://fake.com/v3/banana?page=2&per_page=50"
   161  					}
   162  				},
   163  				"resources": [
   164  					{
   165  						"metadata": {
   166  							"guid": "app-guid-2",
   167  							"updated_at": null
   168  						},
   169  						"entity": {
   170  							"name": "app-name-2"
   171  						}
   172  					}
   173  				],
   174  				"included": {
   175        				"users": [
   176  						{
   177  							"guid": "user-guid-1",
   178  							"username": "user-name-1",
   179  							"origin": "uaa"
   180  						}
   181  					]
   182  				}
   183  			}`)
   184  
   185  			err := json.Unmarshal(raw, &page)
   186  			Expect(err).ToNot(HaveOccurred())
   187  		})
   188  
   189  		It("can unmarshal the list of included resources into an appropriate struct", func() {
   190  			Expect(page.IncludedResources.UserResource).To(ConsistOf(
   191  				User{GUID: "user-guid-1", Username: "user-name-1", Origin: "uaa"},
   192  			))
   193  		})
   194  	})
   195  })