github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/resource/resourcev2/managementv2/resource_group_test.go (about)

     1  package managementv2
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  
     7  	"github.com/IBM-Cloud/bluemix-go"
     8  
     9  	"github.com/IBM-Cloud/bluemix-go/client"
    10  	"github.com/IBM-Cloud/bluemix-go/models"
    11  	"github.com/IBM-Cloud/bluemix-go/session"
    12  	"github.com/onsi/gomega/ghttp"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("ResourceGroups", func() {
    19  	var server *ghttp.Server
    20  	AfterEach(func() {
    21  		server.Close()
    22  	})
    23  
    24  	Describe("List()", func() {
    25  		Context("When there is no user group", func() {
    26  			BeforeEach(func() {
    27  				server = ghttp.NewServer()
    28  				server.AppendHandlers(
    29  					ghttp.CombineHandlers(
    30  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_groups"),
    31  						ghttp.RespondWith(http.StatusOK, `{"resources":[]}`),
    32  					),
    33  				)
    34  			})
    35  			It("should return zero user group", func() {
    36  				repo := newTestResourceGroupRepo(server.URL())
    37  				groups, err := repo.List(&ResourceGroupQuery{})
    38  
    39  				Expect(err).ShouldNot(HaveOccurred())
    40  				Expect(groups).Should(BeEmpty())
    41  			})
    42  		})
    43  		Context("When there is one user group", func() {
    44  			BeforeEach(func() {
    45  				server = ghttp.NewServer()
    46  				server.AppendHandlers(
    47  					ghttp.CombineHandlers(
    48  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_groups"),
    49  						ghttp.RespondWith(http.StatusOK, `{
    50  							"resources": [{
    51  								"id": "foo",
    52  								"account_id": "abcdefg",
    53  								"name": "test-group",
    54  								"default": true,
    55  								"state": "ACTIVE",
    56  								"quota_id": "abcdefg",
    57  								"payment_method_id": "payment1",
    58  								"resource_linkages": []
    59  							}]
    60  						}`),
    61  					),
    62  				)
    63  			})
    64  			It("should return zero user group", func() {
    65  				repo := newTestResourceGroupRepo(server.URL())
    66  				groups, err := repo.List(&ResourceGroupQuery{})
    67  
    68  				Expect(err).ShouldNot(HaveOccurred())
    69  
    70  				Expect(groups).Should(HaveLen(1))
    71  				group := groups[0]
    72  				Expect(group.ID).Should(Equal("foo"))
    73  				Expect(group.AccountID).Should(Equal("abcdefg"))
    74  				Expect(group.Name).Should(Equal("test-group"))
    75  				Expect(group.Default).Should(Equal(true))
    76  				Expect(group.State).Should(Equal("ACTIVE"))
    77  				Expect(group.QuotaID).Should(Equal("abcdefg"))
    78  				Expect(group.PaymentMethodID).Should(Equal("payment1"))
    79  				Expect(group.Linkages).Should(BeEmpty())
    80  			})
    81  		})
    82  
    83  		Context("When there are multiple user groups", func() {
    84  			BeforeEach(func() {
    85  				server = ghttp.NewServer()
    86  				server.AppendHandlers(
    87  					ghttp.CombineHandlers(
    88  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_groups"),
    89  						ghttp.RespondWith(http.StatusOK, `{
    90  							"resources": [{
    91  								"id": "foo",
    92  								"account_id": "abcdefg",
    93  								"name": "test-group",
    94  								"default": true,
    95  								"state": "ACTIVE",
    96  								"quota_id": "abcdefg",
    97  								"payment_method_id": "payment1",
    98  								"resource_linkages": []
    99  							},{
   100  								"id": "bar",
   101  								"account_id": "xyz",
   102  								"name": "test-group2",
   103  								"default": false,
   104  								"state": "SUSPENDED",
   105  								"quota_id": "xyz",
   106  								"payment_method_id": "payment2",
   107  								"resource_linkages": [{
   108  									"resource_id": "abc",
   109  									"resource_origin": "CF_ORG"
   110  								},{
   111  									"resource_id": "def",
   112  									"resource_origin": "IMS"
   113  								}]
   114  							}]
   115  						}`),
   116  					),
   117  				)
   118  			})
   119  			It("should return all of them", func() {
   120  				repo := newTestResourceGroupRepo(server.URL())
   121  				groups, err := repo.List(&ResourceGroupQuery{})
   122  
   123  				Expect(err).ShouldNot(HaveOccurred())
   124  
   125  				Expect(groups).Should(HaveLen(2))
   126  				group := groups[0]
   127  				Expect(group.ID).Should(Equal("foo"))
   128  				Expect(group.AccountID).Should(Equal("abcdefg"))
   129  				Expect(group.Name).Should(Equal("test-group"))
   130  				Expect(group.Default).Should(Equal(true))
   131  				Expect(group.State).Should(Equal("ACTIVE"))
   132  				Expect(group.QuotaID).Should(Equal("abcdefg"))
   133  				Expect(group.PaymentMethodID).Should(Equal("payment1"))
   134  				Expect(group.Linkages).Should(BeEmpty())
   135  
   136  				group = groups[1]
   137  				Expect(group.ID).Should(Equal("bar"))
   138  				Expect(group.AccountID).Should(Equal("xyz"))
   139  				Expect(group.Name).Should(Equal("test-group2"))
   140  				Expect(group.Default).Should(Equal(false))
   141  				Expect(group.State).Should(Equal("SUSPENDED"))
   142  				Expect(group.QuotaID).Should(Equal("xyz"))
   143  				Expect(group.PaymentMethodID).Should(Equal("payment2"))
   144  				Expect(group.Linkages).Should(HaveLen(2))
   145  				Expect(group.Linkages[0].ResourceID).Should(Equal("abc"))
   146  				Expect(group.Linkages[0].ResourceOrigin.String()).Should(Equal("CF_ORG"))
   147  				Expect(group.Linkages[1].ResourceID).Should(Equal("def"))
   148  				Expect(group.Linkages[1].ResourceOrigin.String()).Should(Equal("IMS"))
   149  			})
   150  		})
   151  
   152  		Context("Query by account ID", func() {
   153  			BeforeEach(func() {
   154  				server = ghttp.NewServer()
   155  				server.AppendHandlers(
   156  					ghttp.CombineHandlers(
   157  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_groups", "account_id=abc"),
   158  						ghttp.RespondWith(http.StatusOK, `{"resources":[]}`),
   159  					),
   160  				)
   161  			})
   162  			It("should get HTTP query 'accout_id'", func() {
   163  				repo := newTestResourceGroupRepo(server.URL())
   164  				groups, err := repo.List(&ResourceGroupQuery{
   165  					AccountID: "abc",
   166  				})
   167  
   168  				Expect(err).ShouldNot(HaveOccurred())
   169  				Expect(groups).Should(BeEmpty())
   170  			})
   171  		})
   172  
   173  		Context("Query by default", func() {
   174  			BeforeEach(func() {
   175  				server = ghttp.NewServer()
   176  				server.AppendHandlers(
   177  					ghttp.CombineHandlers(
   178  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_groups", "default=true"),
   179  						ghttp.RespondWith(http.StatusOK, `{"resources":[]}`),
   180  					),
   181  				)
   182  			})
   183  			It("should get HTTP query 'accout_id'", func() {
   184  				repo := newTestResourceGroupRepo(server.URL())
   185  				groups, err := repo.List(&ResourceGroupQuery{
   186  					Default: true,
   187  				})
   188  
   189  				Expect(err).ShouldNot(HaveOccurred())
   190  				Expect(groups).Should(BeEmpty())
   191  			})
   192  		})
   193  
   194  		Context("Query by resource ID and origin", func() {
   195  			BeforeEach(func() {
   196  				server = ghttp.NewServer()
   197  				server.AppendHandlers(
   198  					ghttp.CombineHandlers(
   199  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_groups", "resource_id=abc&resource_origin=CF_ORG"),
   200  						ghttp.RespondWith(http.StatusOK, `{"resources":[]}`),
   201  					),
   202  				)
   203  			})
   204  			It("should get HTTP query 'accout_id'", func() {
   205  				repo := newTestResourceGroupRepo(server.URL())
   206  				groups, err := repo.List(&ResourceGroupQuery{
   207  					ResourceID:     "abc",
   208  					ResourceOrigin: "CF_ORG",
   209  				})
   210  
   211  				Expect(err).ShouldNot(HaveOccurred())
   212  				Expect(groups).Should(BeEmpty())
   213  			})
   214  		})
   215  
   216  		Context("Query by multiple filters", func() {
   217  			BeforeEach(func() {
   218  				server = ghttp.NewServer()
   219  				server.AppendHandlers(
   220  					ghttp.CombineHandlers(
   221  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_groups", "default=true&resource_id=abc&resource_origin=CF_ORG"),
   222  						ghttp.RespondWith(http.StatusOK, `{"resources":[]}`),
   223  					),
   224  				)
   225  			})
   226  			It("should get HTTP query 'accout_id'", func() {
   227  				repo := newTestResourceGroupRepo(server.URL())
   228  				groups, err := repo.List(&ResourceGroupQuery{
   229  					Default:        true,
   230  					ResourceID:     "abc",
   231  					ResourceOrigin: "CF_ORG",
   232  				})
   233  
   234  				Expect(err).ShouldNot(HaveOccurred())
   235  				Expect(groups).Should(BeEmpty())
   236  			})
   237  		})
   238  
   239  		Context("When there is backend error", func() {
   240  			BeforeEach(func() {
   241  				server = ghttp.NewServer()
   242  				server.AppendHandlers(
   243  					ghttp.CombineHandlers(
   244  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_groups"),
   245  						ghttp.RespondWith(http.StatusBadRequest, `{"resources":[]}`),
   246  					),
   247  				)
   248  			})
   249  			It("should return error", func() {
   250  				repo := newTestResourceGroupRepo(server.URL())
   251  				groups, err := repo.List(&ResourceGroupQuery{})
   252  
   253  				Expect(err).Should(HaveOccurred())
   254  				Expect(groups).Should(BeEmpty())
   255  			})
   256  		})
   257  	})
   258  
   259  	Describe("FindByName()", func() {
   260  		Context("When no resource group returned", func() {
   261  			BeforeEach(func() {
   262  				server = ghttp.NewServer()
   263  				server.AppendHandlers(
   264  					ghttp.CombineHandlers(
   265  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_groups"),
   266  						ghttp.RespondWith(http.StatusOK, `{"resources":[]}`),
   267  					),
   268  				)
   269  			})
   270  			It("should return no resource group", func() {
   271  				groups, err := newTestResourceGroupRepo(server.URL()).FindByName(&ResourceGroupQuery{}, "test")
   272  
   273  				Expect(err).Should(HaveOccurred())
   274  				Expect(groups).Should(BeEmpty())
   275  			})
   276  		})
   277  		Context("When there is one user group returned having the same", func() {
   278  			BeforeEach(func() {
   279  				server = ghttp.NewServer()
   280  				server.AppendHandlers(
   281  					ghttp.CombineHandlers(
   282  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_groups"),
   283  						ghttp.RespondWith(http.StatusOK, `{
   284  							"resources": [{
   285  								"id": "foo",
   286  								"account_id": "abcdefg",
   287  								"name": "test-group",
   288  								"default": true,
   289  								"state": "ACTIVE",
   290  								"quota_id": "abcdefg",
   291  								"payment_method_id": "payment1",
   292  								"resource_linkages": []
   293  							}]
   294  						}`),
   295  					),
   296  				)
   297  			})
   298  			It("should return that resource group", func() {
   299  				groups, err := newTestResourceGroupRepo(server.URL()).FindByName(&ResourceGroupQuery{}, "test-group")
   300  
   301  				Expect(err).ShouldNot(HaveOccurred())
   302  
   303  				Expect(groups).Should(HaveLen(1))
   304  				group := groups[0]
   305  				Expect(group.ID).Should(Equal("foo"))
   306  				Expect(group.AccountID).Should(Equal("abcdefg"))
   307  				Expect(group.Name).Should(Equal("test-group"))
   308  				Expect(group.Default).Should(Equal(true))
   309  				Expect(group.State).Should(Equal("ACTIVE"))
   310  				Expect(group.QuotaID).Should(Equal("abcdefg"))
   311  				Expect(group.PaymentMethodID).Should(Equal("payment1"))
   312  				Expect(group.Linkages).Should(BeEmpty())
   313  			})
   314  		})
   315  
   316  		Context("When there are multiple resource groups having same name returned", func() {
   317  			BeforeEach(func() {
   318  				server = ghttp.NewServer()
   319  				server.AppendHandlers(
   320  					ghttp.CombineHandlers(
   321  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_groups"),
   322  						ghttp.RespondWith(http.StatusOK, `{
   323  							"resources": [{
   324  								"id": "foo",
   325  								"account_id": "test-account",
   326  								"name": "test-group",
   327  								"default": true,
   328  								"state": "ACTIVE",
   329  								"quota_id": "abcdefg",
   330  								"payment_method_id": "payment1",
   331  								"resource_linkages": []
   332  							},{
   333  								"id": "bar",
   334  								"account_id": "test-account2",
   335  								"name": "test-group",
   336  								"default": false,
   337  								"state": "SUSPENDED",
   338  								"quota_id": "xyz",
   339  								"payment_method_id": "payment2",
   340  								"resource_linkages": [{
   341  									"resource_id": "abc",
   342  									"resource_origin": "CF_ORG"
   343  								},{
   344  									"resource_id": "def",
   345  									"resource_origin": "IMS"
   346  								}]
   347  							}]
   348  						}`),
   349  					),
   350  				)
   351  			})
   352  			It("should return all of them", func() {
   353  				groups, err := newTestResourceGroupRepo(server.URL()).FindByName(&ResourceGroupQuery{}, "test-group")
   354  
   355  				Expect(err).ShouldNot(HaveOccurred())
   356  
   357  				Expect(groups).Should(HaveLen(2))
   358  				group := groups[0]
   359  				Expect(group.ID).Should(Equal("foo"))
   360  				Expect(group.AccountID).Should(Equal("test-account"))
   361  				Expect(group.Name).Should(Equal("test-group"))
   362  				Expect(group.Default).Should(Equal(true))
   363  				Expect(group.State).Should(Equal("ACTIVE"))
   364  				Expect(group.QuotaID).Should(Equal("abcdefg"))
   365  				Expect(group.PaymentMethodID).Should(Equal("payment1"))
   366  				Expect(group.Linkages).Should(BeEmpty())
   367  
   368  				group = groups[1]
   369  				Expect(group.ID).Should(Equal("bar"))
   370  				Expect(group.AccountID).Should(Equal("test-account2"))
   371  				Expect(group.Name).Should(Equal("test-group"))
   372  				Expect(group.Default).Should(Equal(false))
   373  				Expect(group.State).Should(Equal("SUSPENDED"))
   374  				Expect(group.QuotaID).Should(Equal("xyz"))
   375  				Expect(group.PaymentMethodID).Should(Equal("payment2"))
   376  				Expect(group.Linkages).Should(HaveLen(2))
   377  				Expect(group.Linkages[0].ResourceID).Should(Equal("abc"))
   378  				Expect(group.Linkages[0].ResourceOrigin.String()).Should(Equal("CF_ORG"))
   379  				Expect(group.Linkages[1].ResourceID).Should(Equal("def"))
   380  				Expect(group.Linkages[1].ResourceOrigin.String()).Should(Equal("IMS"))
   381  			})
   382  		})
   383  
   384  		Context("When there are multiple resource group returned, but none have that name", func() {
   385  			BeforeEach(func() {
   386  				server = ghttp.NewServer()
   387  				server.AppendHandlers(
   388  					ghttp.CombineHandlers(
   389  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_groups", "account_id=abc"),
   390  						ghttp.RespondWith(http.StatusOK, `{
   391  							"resources": [{
   392  								"id": "foo",
   393  								"account_id": "abcdefg",
   394  								"name": "test-group",
   395  								"default": true,
   396  								"state": "ACTIVE",
   397  								"quota_id": "abcdefg",
   398  								"payment_method_id": "payment1",
   399  								"resource_linkages": []
   400  							},{
   401  								"id": "bar",
   402  								"account_id": "xyz",
   403  								"name": "test-group2",
   404  								"default": false,
   405  								"state": "SUSPENDED",
   406  								"quota_id": "xyz",
   407  								"payment_method_id": "payment2",
   408  								"resource_linkages": [{
   409  									"resource_id": "abc",
   410  									"resource_origin": "CF_ORG"
   411  								},{
   412  									"resource_id": "def",
   413  									"resource_origin": "IMS"
   414  								}]
   415  							}]
   416  						}`),
   417  					),
   418  				)
   419  			})
   420  			It("should no resource group", func() {
   421  				groups, err := newTestResourceGroupRepo(server.URL()).FindByName(&ResourceGroupQuery{
   422  					AccountID: "abc",
   423  				}, "foo")
   424  
   425  				Expect(err).Should(HaveOccurred())
   426  				Expect(groups).Should(BeEmpty())
   427  			})
   428  		})
   429  
   430  		Context("When there is backend error", func() {
   431  			BeforeEach(func() {
   432  				server = ghttp.NewServer()
   433  				server.AppendHandlers(
   434  					ghttp.CombineHandlers(
   435  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_groups"),
   436  						ghttp.RespondWith(http.StatusBadRequest, `{"resources":[]}`),
   437  					),
   438  				)
   439  			})
   440  			It("should return error", func() {
   441  				groups, err := newTestResourceGroupRepo(server.URL()).FindByName(&ResourceGroupQuery{}, "foo")
   442  
   443  				Expect(err).Should(HaveOccurred())
   444  				Expect(groups).Should(BeEmpty())
   445  			})
   446  		})
   447  	})
   448  
   449  	Describe("Create()", func() {
   450  		Context("when creation is successful", func() {
   451  			BeforeEach(func() {
   452  				server = ghttp.NewServer()
   453  				server.AppendHandlers(
   454  					ghttp.CombineHandlers(
   455  						ghttp.VerifyRequest(http.MethodPost, "/v2/resource_groups"),
   456  						ghttp.VerifyJSONRepresenting(models.ResourceGroup{
   457  							Name:      "test",
   458  							AccountID: "test-account-id",
   459  							QuotaID:   "test-quota-id",
   460  						}),
   461  						ghttp.RespondWith(http.StatusOK, `{"id":"7f3f9f3ee8e64bf880ecec527c6f7c39"}`),
   462  					),
   463  				)
   464  			})
   465  			It("should return the new resource group", func() {
   466  				group, err := newTestResourceGroupRepo(server.URL()).Create(models.ResourceGroupv2{
   467  					ResourceGroup: models.ResourceGroup{
   468  						Name:      "test",
   469  						AccountID: "test-account-id",
   470  						QuotaID:   "test-quota-id",
   471  					},
   472  				})
   473  
   474  				Expect(err).ShouldNot(HaveOccurred())
   475  				Expect(group).ShouldNot(BeNil())
   476  				Expect(group.ID).Should(Equal("7f3f9f3ee8e64bf880ecec527c6f7c39"))
   477  			})
   478  		})
   479  
   480  		Context("when creation failed", func() {
   481  			BeforeEach(func() {
   482  				server = ghttp.NewServer()
   483  				server.AppendHandlers(
   484  					ghttp.CombineHandlers(
   485  						ghttp.VerifyRequest(http.MethodPost, "/v2/resource_groups"),
   486  						ghttp.VerifyJSONRepresenting(models.ResourceGroup{
   487  							Name:      "test",
   488  							AccountID: "test-account-id",
   489  							QuotaID:   "test-quota-id",
   490  						}),
   491  						ghttp.RespondWith(http.StatusUnauthorized, `{"Message":"Invalid Authorization"}`),
   492  					),
   493  				)
   494  			})
   495  			It("should return error", func() {
   496  				group, err := newTestResourceGroupRepo(server.URL()).Create(models.ResourceGroupv2{
   497  					ResourceGroup: models.ResourceGroup{
   498  						Name:      "test",
   499  						AccountID: "test-account-id",
   500  						QuotaID:   "test-quota-id",
   501  					},
   502  				})
   503  
   504  				Expect(err).To(HaveOccurred())
   505  				Expect(group).To(BeNil())
   506  			})
   507  		})
   508  	})
   509  
   510  	Describe("Update()", func() {
   511  		Context("when update is successful", func() {
   512  			BeforeEach(func() {
   513  				isDefault := new(bool)
   514  				*isDefault = false
   515  				server = ghttp.NewServer()
   516  				server.AppendHandlers(
   517  					ghttp.CombineHandlers(
   518  						ghttp.VerifyRequest(http.MethodPatch, "/v2/resource_groups/7f3f9f3ee8e64bf880ecec527c6f7c39"),
   519  						ghttp.VerifyJSONRepresenting(ResourceGroupUpdateRequest{
   520  							Name:    "test",
   521  							QuotaID: "test-quota-id",
   522  							Default: isDefault,
   523  						}),
   524  						ghttp.RespondWith(http.StatusOK, `{
   525  							"id": "7f3f9f3ee8e64bf880ecec527c6f7c39",
   526  							"account_id": "b8b618cc651496dd7a0634264d071843",
   527  							"name": "test",
   528  							"default": false,
   529  							"state": "SUSPENDED",
   530  							"quota_id": "test-quota-id",
   531  							"quota_url": "/v2/quota_definitions/test-quota-id",
   532  							"payment_methods_url": "/v2/resource_groups/7f3f9f3ee8e64bf880ecec527c6f7c39/payment_methods",
   533  							"resource_linkages": [],
   534  							"teams_url": "/v2/resource_groups/7f3f9f3ee8e64bf880ecec527c6f7c39/teams",
   535  							"created_at": "2017-07-28T02:57:51.679Z",
   536  							"updated_at": "2017-07-28T02:57:51.679Z"
   537  						}`),
   538  					),
   539  				)
   540  			})
   541  			It("should return the updated resource group", func() {
   542  				isDefault := new(bool)
   543  				*isDefault = false
   544  				group, err := newTestResourceGroupRepo(server.URL()).Update("7f3f9f3ee8e64bf880ecec527c6f7c39", &ResourceGroupUpdateRequest{
   545  					Name:    "test",
   546  					QuotaID: "test-quota-id",
   547  					Default: isDefault,
   548  				})
   549  
   550  				Expect(err).ShouldNot(HaveOccurred())
   551  				Expect(group).ShouldNot(BeNil())
   552  				// TODO: BSS bug
   553  				// Expect(group.ID).Should(Equal("bar"))
   554  				Expect(group.AccountID).Should(Equal("b8b618cc651496dd7a0634264d071843"))
   555  				Expect(group.Name).Should(Equal("test"))
   556  				Expect(group.Default).Should(Equal(false))
   557  				Expect(group.State).Should(Equal("SUSPENDED"))
   558  				Expect(group.QuotaID).Should(Equal("test-quota-id"))
   559  				Expect(group.Linkages).Should(HaveLen(0))
   560  			})
   561  		})
   562  
   563  		Context("when not updating `default`", func() {
   564  			BeforeEach(func() {
   565  				server = ghttp.NewServer()
   566  				server.AppendHandlers(
   567  					ghttp.CombineHandlers(
   568  						ghttp.VerifyRequest(http.MethodPatch, "/v2/resource_groups/7f3f9f3ee8e64bf880ecec527c6f7c39"),
   569  						ghttp.VerifyJSONRepresenting(models.ResourceGroup{
   570  							Name:    "test",
   571  							QuotaID: "test-quota-id",
   572  						}),
   573  						ghttp.RespondWith(http.StatusOK, `{
   574  							"id": "7f3f9f3ee8e64bf880ecec527c6f7c39",
   575  							"account_id": "b8b618cc651496dd7a0634264d071843",
   576  							"name": "test",
   577  							"default": true,
   578  							"state": "SUSPENDED",
   579  							"quota_id": "test-quota-id",
   580  							"quota_url": "/v2/quota_definitions/test-quota-id",
   581  							"payment_methods_url": "/v2/resource_groups/7f3f9f3ee8e64bf880ecec527c6f7c39/payment_methods",
   582  							"resource_linkages": [],
   583  							"teams_url": "/v2/resource_groups/7f3f9f3ee8e64bf880ecec527c6f7c39/teams",
   584  							"created_at": "2017-07-28T02:57:51.679Z",
   585  							"updated_at": "2017-07-28T02:57:51.679Z"
   586  						}`),
   587  					),
   588  				)
   589  			})
   590  			It("should return the updated resource group", func() {
   591  				group, err := newTestResourceGroupRepo(server.URL()).Update("7f3f9f3ee8e64bf880ecec527c6f7c39", &ResourceGroupUpdateRequest{
   592  					Name:    "test",
   593  					QuotaID: "test-quota-id",
   594  				})
   595  
   596  				Expect(err).ShouldNot(HaveOccurred())
   597  				Expect(group).ShouldNot(BeNil())
   598  				// TODO: BSS bug
   599  				// Expect(group.ID).Should(Equal("bar"))
   600  				Expect(group.AccountID).Should(Equal("b8b618cc651496dd7a0634264d071843"))
   601  				Expect(group.Name).Should(Equal("test"))
   602  				Expect(group.Default).Should(Equal(true))
   603  				Expect(group.State).Should(Equal("SUSPENDED"))
   604  				Expect(group.QuotaID).Should(Equal("test-quota-id"))
   605  				Expect(group.Linkages).Should(HaveLen(0))
   606  			})
   607  		})
   608  
   609  		Context("when update failed", func() {
   610  			BeforeEach(func() {
   611  				server = ghttp.NewServer()
   612  				server.AppendHandlers(
   613  					ghttp.CombineHandlers(
   614  						ghttp.VerifyRequest(http.MethodPatch, "/v2/resource_groups/7f3f9f3ee8e64bf880ecec527c6f7c39"),
   615  						ghttp.VerifyJSONRepresenting(models.ResourceGroup{
   616  							QuotaID: "test-quota-id",
   617  						}),
   618  						ghttp.RespondWith(http.StatusUnauthorized, `{"Message":"Invalid Authorization"}`),
   619  					),
   620  				)
   621  			})
   622  			It("should return error", func() {
   623  				group, err := newTestResourceGroupRepo(server.URL()).Update("7f3f9f3ee8e64bf880ecec527c6f7c39", &ResourceGroupUpdateRequest{
   624  					QuotaID: "test-quota-id",
   625  				})
   626  
   627  				Expect(err).To(HaveOccurred())
   628  				Expect(group).To(BeNil())
   629  			})
   630  		})
   631  	})
   632  	Describe("Delete()", func() {
   633  		Context("When deletion is successful", func() {
   634  			BeforeEach(func() {
   635  				server = ghttp.NewServer()
   636  				server.AppendHandlers(
   637  					ghttp.CombineHandlers(
   638  						ghttp.VerifyRequest(http.MethodDelete, "/v2/resource_groups/abc"),
   639  						ghttp.RespondWith(http.StatusNoContent, ``),
   640  					),
   641  				)
   642  			})
   643  			It("should return success", func() {
   644  				err := newTestResourceGroupRepo(server.URL()).Delete("abc")
   645  				Expect(err).ShouldNot(HaveOccurred())
   646  			})
   647  		})
   648  
   649  		Context("When deletion failed", func() {
   650  			BeforeEach(func() {
   651  				server = ghttp.NewServer()
   652  				server.AppendHandlers(
   653  					ghttp.CombineHandlers(
   654  						ghttp.VerifyRequest(http.MethodDelete, "/v2/resource_groups/abc"),
   655  						ghttp.RespondWith(http.StatusNotFound, `{"message":"Not found"}`),
   656  					),
   657  				)
   658  			})
   659  			It("should return error", func() {
   660  				err := newTestResourceGroupRepo(server.URL()).Delete("abc")
   661  				Expect(err).Should(HaveOccurred())
   662  			})
   663  		})
   664  	})
   665  })
   666  
   667  func newTestResourceGroupRepo(url string) ResourceGroupRepository {
   668  	sess, err := session.New()
   669  	if err != nil {
   670  		log.Fatal(err)
   671  	}
   672  	conf := sess.Config.Copy()
   673  	conf.Endpoint = &url
   674  
   675  	client := client.Client{
   676  		Config:      conf,
   677  		ServiceName: bluemix.ResourceManagementServicev2,
   678  	}
   679  
   680  	return newResourceGroupAPI(&client)
   681  }