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

     1  package management
     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, "/v1/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, "/v1/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, "/v1/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, "/v1/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, "/v1/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, "/v1/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, "/v1/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, "/v1/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, "/v1/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, "/v1/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, "/v1/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, "/v1/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, "/v1/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, "/v1/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.ResourceGroup{
   467  					Name:      "test",
   468  					AccountID: "test-account-id",
   469  					QuotaID:   "test-quota-id",
   470  				})
   471  
   472  				Expect(err).ShouldNot(HaveOccurred())
   473  				Expect(group).ShouldNot(BeNil())
   474  				Expect(group.ID).Should(Equal("7f3f9f3ee8e64bf880ecec527c6f7c39"))
   475  			})
   476  		})
   477  
   478  		Context("when creation failed", func() {
   479  			BeforeEach(func() {
   480  				server = ghttp.NewServer()
   481  				server.AppendHandlers(
   482  					ghttp.CombineHandlers(
   483  						ghttp.VerifyRequest(http.MethodPost, "/v1/resource_groups"),
   484  						ghttp.VerifyJSONRepresenting(models.ResourceGroup{
   485  							Name:      "test",
   486  							AccountID: "test-account-id",
   487  							QuotaID:   "test-quota-id",
   488  						}),
   489  						ghttp.RespondWith(http.StatusUnauthorized, `{"Message":"Invalid Authorization"}`),
   490  					),
   491  				)
   492  			})
   493  			It("should return error", func() {
   494  				group, err := newTestResourceGroupRepo(server.URL()).Create(models.ResourceGroup{
   495  					Name:      "test",
   496  					AccountID: "test-account-id",
   497  					QuotaID:   "test-quota-id",
   498  				})
   499  
   500  				Expect(err).To(HaveOccurred())
   501  				Expect(group).To(BeNil())
   502  			})
   503  		})
   504  	})
   505  
   506  	Describe("Update()", func() {
   507  		Context("when update is successful", func() {
   508  			BeforeEach(func() {
   509  				isDefault := new(bool)
   510  				*isDefault = false
   511  				server = ghttp.NewServer()
   512  				server.AppendHandlers(
   513  					ghttp.CombineHandlers(
   514  						ghttp.VerifyRequest(http.MethodPatch, "/v1/resource_groups/7f3f9f3ee8e64bf880ecec527c6f7c39"),
   515  						ghttp.VerifyJSONRepresenting(ResourceGroupUpdateRequest{
   516  							Name:    "test",
   517  							QuotaID: "test-quota-id",
   518  							Default: isDefault,
   519  						}),
   520  						ghttp.RespondWith(http.StatusOK, `{
   521  							"id": "7f3f9f3ee8e64bf880ecec527c6f7c39",
   522  							"account_id": "b8b618cc651496dd7a0634264d071843",
   523  							"name": "test",
   524  							"default": false,
   525  							"state": "SUSPENDED",
   526  							"quota_id": "test-quota-id",
   527  							"quota_url": "/v1/quota_definitions/test-quota-id",
   528  							"payment_methods_url": "/v1/resource_groups/7f3f9f3ee8e64bf880ecec527c6f7c39/payment_methods",
   529  							"resource_linkages": [],
   530  							"teams_url": "/v1/resource_groups/7f3f9f3ee8e64bf880ecec527c6f7c39/teams",
   531  							"created_at": "2017-07-28T02:57:51.679Z",
   532  							"updated_at": "2017-07-28T02:57:51.679Z"
   533  						}`),
   534  					),
   535  				)
   536  			})
   537  			It("should return the updated resource group", func() {
   538  				isDefault := new(bool)
   539  				*isDefault = false
   540  				group, err := newTestResourceGroupRepo(server.URL()).Update("7f3f9f3ee8e64bf880ecec527c6f7c39", &ResourceGroupUpdateRequest{
   541  					Name:    "test",
   542  					QuotaID: "test-quota-id",
   543  					Default: isDefault,
   544  				})
   545  
   546  				Expect(err).ShouldNot(HaveOccurred())
   547  				Expect(group).ShouldNot(BeNil())
   548  				// TODO: BSS bug
   549  				// Expect(group.ID).Should(Equal("bar"))
   550  				Expect(group.AccountID).Should(Equal("b8b618cc651496dd7a0634264d071843"))
   551  				Expect(group.Name).Should(Equal("test"))
   552  				Expect(group.Default).Should(Equal(false))
   553  				Expect(group.State).Should(Equal("SUSPENDED"))
   554  				Expect(group.QuotaID).Should(Equal("test-quota-id"))
   555  				Expect(group.Linkages).Should(HaveLen(0))
   556  			})
   557  		})
   558  
   559  		Context("when not updating `default`", func() {
   560  			BeforeEach(func() {
   561  				server = ghttp.NewServer()
   562  				server.AppendHandlers(
   563  					ghttp.CombineHandlers(
   564  						ghttp.VerifyRequest(http.MethodPatch, "/v1/resource_groups/7f3f9f3ee8e64bf880ecec527c6f7c39"),
   565  						ghttp.VerifyJSONRepresenting(models.ResourceGroup{
   566  							Name:    "test",
   567  							QuotaID: "test-quota-id",
   568  						}),
   569  						ghttp.RespondWith(http.StatusOK, `{
   570  							"id": "7f3f9f3ee8e64bf880ecec527c6f7c39",
   571  							"account_id": "b8b618cc651496dd7a0634264d071843",
   572  							"name": "test",
   573  							"default": true,
   574  							"state": "SUSPENDED",
   575  							"quota_id": "test-quota-id",
   576  							"quota_url": "/v1/quota_definitions/test-quota-id",
   577  							"payment_methods_url": "/v1/resource_groups/7f3f9f3ee8e64bf880ecec527c6f7c39/payment_methods",
   578  							"resource_linkages": [],
   579  							"teams_url": "/v1/resource_groups/7f3f9f3ee8e64bf880ecec527c6f7c39/teams",
   580  							"created_at": "2017-07-28T02:57:51.679Z",
   581  							"updated_at": "2017-07-28T02:57:51.679Z"
   582  						}`),
   583  					),
   584  				)
   585  			})
   586  			It("should return the updated resource group", func() {
   587  				group, err := newTestResourceGroupRepo(server.URL()).Update("7f3f9f3ee8e64bf880ecec527c6f7c39", &ResourceGroupUpdateRequest{
   588  					Name:    "test",
   589  					QuotaID: "test-quota-id",
   590  				})
   591  
   592  				Expect(err).ShouldNot(HaveOccurred())
   593  				Expect(group).ShouldNot(BeNil())
   594  				// TODO: BSS bug
   595  				// Expect(group.ID).Should(Equal("bar"))
   596  				Expect(group.AccountID).Should(Equal("b8b618cc651496dd7a0634264d071843"))
   597  				Expect(group.Name).Should(Equal("test"))
   598  				Expect(group.Default).Should(Equal(true))
   599  				Expect(group.State).Should(Equal("SUSPENDED"))
   600  				Expect(group.QuotaID).Should(Equal("test-quota-id"))
   601  				Expect(group.Linkages).Should(HaveLen(0))
   602  			})
   603  		})
   604  
   605  		Context("when update failed", func() {
   606  			BeforeEach(func() {
   607  				server = ghttp.NewServer()
   608  				server.AppendHandlers(
   609  					ghttp.CombineHandlers(
   610  						ghttp.VerifyRequest(http.MethodPatch, "/v1/resource_groups/7f3f9f3ee8e64bf880ecec527c6f7c39"),
   611  						ghttp.VerifyJSONRepresenting(models.ResourceGroup{
   612  							QuotaID: "test-quota-id",
   613  						}),
   614  						ghttp.RespondWith(http.StatusUnauthorized, `{"Message":"Invalid Authorization"}`),
   615  					),
   616  				)
   617  			})
   618  			It("should return error", func() {
   619  				group, err := newTestResourceGroupRepo(server.URL()).Update("7f3f9f3ee8e64bf880ecec527c6f7c39", &ResourceGroupUpdateRequest{
   620  					QuotaID: "test-quota-id",
   621  				})
   622  
   623  				Expect(err).To(HaveOccurred())
   624  				Expect(group).To(BeNil())
   625  			})
   626  		})
   627  	})
   628  	Describe("Delete()", func() {
   629  		Context("When deletion is successful", func() {
   630  			BeforeEach(func() {
   631  				server = ghttp.NewServer()
   632  				server.AppendHandlers(
   633  					ghttp.CombineHandlers(
   634  						ghttp.VerifyRequest(http.MethodDelete, "/v1/resource_groups/abc"),
   635  						ghttp.RespondWith(http.StatusNoContent, ``),
   636  					),
   637  				)
   638  			})
   639  			It("should return success", func() {
   640  				err := newTestResourceGroupRepo(server.URL()).Delete("abc")
   641  				Expect(err).ShouldNot(HaveOccurred())
   642  			})
   643  		})
   644  
   645  		Context("When deletion failed", func() {
   646  			BeforeEach(func() {
   647  				server = ghttp.NewServer()
   648  				server.AppendHandlers(
   649  					ghttp.CombineHandlers(
   650  						ghttp.VerifyRequest(http.MethodDelete, "/v1/resource_groups/abc"),
   651  						ghttp.RespondWith(http.StatusNotFound, `{"message":"Not found"}`),
   652  					),
   653  				)
   654  			})
   655  			It("should return error", func() {
   656  				err := newTestResourceGroupRepo(server.URL()).Delete("abc")
   657  				Expect(err).Should(HaveOccurred())
   658  			})
   659  		})
   660  	})
   661  })
   662  
   663  func newTestResourceGroupRepo(url string) ResourceGroupRepository {
   664  	sess, err := session.New()
   665  	if err != nil {
   666  		log.Fatal(err)
   667  	}
   668  	conf := sess.Config.Copy()
   669  	conf.Endpoint = &url
   670  
   671  	client := client.Client{
   672  		Config:      conf,
   673  		ServiceName: bluemix.ResourceManagementService,
   674  	}
   675  
   676  	return newResourceGroupAPI(&client)
   677  }