github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/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  	"code.cloudfoundry.org/cli/resources"
     9  	. "code.cloudfoundry.org/cli/resources"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  type testItem struct {
    15  	Name string
    16  	GUID string
    17  }
    18  
    19  func (t *testItem) UnmarshalJSON(data []byte) error {
    20  	var item struct {
    21  		Metadata struct {
    22  			GUID string `json:"guid"`
    23  		} `json:"metadata"`
    24  		Entity struct {
    25  			Name string `json:"name"`
    26  		} `json:"entity"`
    27  	}
    28  	err := cloudcontroller.DecodeJSON(data, &item)
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	t.GUID = item.Metadata.GUID
    34  	t.Name = item.Entity.Name
    35  	return nil
    36  }
    37  
    38  var _ = Describe("Paginated Resources", func() {
    39  	var page *PaginatedResources
    40  
    41  	BeforeEach(func() {
    42  		page = NewPaginatedResources(testItem{})
    43  	})
    44  
    45  	Context("unmarshaling from paginated request", func() {
    46  		var raw []byte
    47  
    48  		BeforeEach(func() {
    49  			raw = []byte(`{
    50  				"pagination": {
    51  					"total_results": 0,
    52  					"total_pages": 1,
    53  					"first": {
    54  						"href": "https://fake.com/v3/banana?page=1&per_page=50"
    55  					},
    56  					"last": {
    57  						"href": "https://fake.com/v3/banana?page=2&per_page=50"
    58  					},
    59  					"next": {
    60  						"href":"https://fake.com/v3/banana?page=2&per_page=50"
    61  					},
    62  					"previous": null
    63  				},
    64  				"resources": [
    65  					{
    66  						"metadata": {
    67  							"guid": "app-guid-1",
    68  							"updated_at": null
    69  						},
    70  						"entity": {
    71  							"name": "app-name-1"
    72  						}
    73  					},
    74  					{
    75  						"metadata": {
    76  							"guid": "app-guid-2",
    77  							"updated_at": null
    78  						},
    79  						"entity": {
    80  							"name": "app-name-2"
    81  						}
    82  					}
    83  				]
    84  			}`)
    85  
    86  			err := json.Unmarshal(raw, &page)
    87  			Expect(err).ToNot(HaveOccurred())
    88  		})
    89  
    90  		It("should populate NextURL", func() {
    91  			Expect(page.NextPage()).To(Equal("https://fake.com/v3/banana?page=2&per_page=50"))
    92  		})
    93  
    94  		It("should hold onto the whole resource blob", func() {
    95  			Expect(string(page.ResourcesBytes)).To(MatchJSON(`[
    96  					{
    97  						"metadata": {
    98  							"guid": "app-guid-1",
    99  							"updated_at": null
   100  						},
   101  						"entity": {
   102  							"name": "app-name-1"
   103  						}
   104  					},
   105  					{
   106  						"metadata": {
   107  							"guid": "app-guid-2",
   108  							"updated_at": null
   109  						},
   110  						"entity": {
   111  							"name": "app-name-2"
   112  						}
   113  					}
   114  				]`))
   115  		})
   116  	})
   117  
   118  	Describe("Resources", func() {
   119  		BeforeEach(func() {
   120  			raw := []byte(`[
   121  					{
   122  						"metadata": {
   123  							"guid": "app-guid-1",
   124  							"updated_at": null
   125  						},
   126  						"entity": {
   127  							"name": "app-name-1"
   128  						}
   129  					},
   130  					{
   131  						"metadata": {
   132  							"guid": "app-guid-2",
   133  							"updated_at": null
   134  						},
   135  						"entity": {
   136  							"name": "app-name-2"
   137  						}
   138  					}
   139  				]`)
   140  
   141  			page.ResourcesBytes = raw
   142  		})
   143  
   144  		It("can unmarshal the list of resources into the given struct", func() {
   145  			items, err := page.Resources()
   146  			Expect(err).ToNot(HaveOccurred())
   147  
   148  			Expect(items).To(ConsistOf(
   149  				testItem{GUID: "app-guid-1", Name: "app-name-1"},
   150  				testItem{GUID: "app-guid-2", Name: "app-name-2"},
   151  			))
   152  		})
   153  	})
   154  
   155  	Describe("IncludedResources", func() {
   156  		var raw []byte
   157  
   158  		BeforeEach(func() {
   159  			raw = []byte(`{
   160  				"pagination": {
   161  					"next": {
   162  						"href":"https://fake.com/v3/banana?page=2&per_page=50"
   163  					}
   164  				},
   165  				"resources": [
   166  					{
   167  						"metadata": {
   168  							"guid": "app-guid-2",
   169  							"updated_at": null
   170  						},
   171  						"entity": {
   172  							"name": "app-name-2"
   173  						}
   174  					}
   175  				],
   176  				"included": {
   177        				"users": [
   178  						{
   179  							"guid": "user-guid-1",
   180  							"username": "user-name-1",
   181  							"origin": "uaa"
   182  						}
   183  					],
   184        				"organizations": [
   185  						{
   186  							"guid": "org-guid-1",
   187  							"name": "org-name-1"
   188  						}
   189  					]
   190  				}
   191  			}`)
   192  
   193  			err := json.Unmarshal(raw, &page)
   194  			Expect(err).ToNot(HaveOccurred())
   195  		})
   196  
   197  		It("can unmarshal the list of included resources into an appropriate struct", func() {
   198  			Expect(page.IncludedResources.Users).To(ConsistOf(
   199  				resources.User{GUID: "user-guid-1", Username: "user-name-1", Origin: "uaa"},
   200  			))
   201  			Expect(page.IncludedResources.Organizations).To(ConsistOf(
   202  				Organization{GUID: "org-guid-1", Name: "org-name-1"},
   203  			))
   204  		})
   205  	})
   206  })