github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/service_plans_test.go (about)

     1  package mccpv2
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  
     7  	bluemix "github.com/IBM-Cloud/bluemix-go"
     8  	"github.com/IBM-Cloud/bluemix-go/client"
     9  	bluemixHttp "github.com/IBM-Cloud/bluemix-go/http"
    10  	"github.com/IBM-Cloud/bluemix-go/session"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	"github.com/onsi/gomega/ghttp"
    15  )
    16  
    17  var _ = Describe("Service Plan by Label", func() {
    18  	var server *ghttp.Server
    19  	AfterEach(func() {
    20  		server.Close()
    21  	})
    22  	Describe("FindPlanInServiceOffering()", func() {
    23  		Context("Server return service plan", func() {
    24  			BeforeEach(func() {
    25  				server = ghttp.NewServer()
    26  				server.AppendHandlers(
    27  					ghttp.CombineHandlers(
    28  						ghttp.VerifyRequest(http.MethodGet, "/v2/service_plans"),
    29  						ghttp.RespondWith(http.StatusOK, `{
    30  							"total_results": 2,
    31  							"total_pages": 1,
    32  							"prev_url": null,
    33  							"next_url": null,
    34  							"resources": [
    35  								{
    36  									"metadata": {
    37  										"guid": "e72c6030-mccpe3-4477-9fb1-ca2b0408cbcb",
    38  										"url": "/v2/service_plans/e72c6030-mccpe3-4477-9fb1-ca2b0408cbcb",
    39  										"created_at": "2016-09-08T12:55:17Z",
    40  										"updated_at": "2017-03-17T20:05:06Z"
    41  									},
    42  									"entity": {
    43  										"name": "Lite",
    44  										"free": true,
    45  										"description": "The Lite plan provides access to the full functionality of Cloudant for development and evaluation. The plan has a set amount of provisioned throughput capacity as shown and includes a max of 1GB of encrypted data storage.",
    46  										"service_guid": "14c83ad2-6fd4-439a-8c3a-d1a20f8a2381",
    47  										"unique_id": "cloudant-lite",
    48  										"public": true,
    49  										"active": true,
    50  										"service_url": "/v2/services/14c83ad2-6fd4-439a-8c3a-d1a20f8a2381",
    51  										"service_instances_url": "/v2/service_plans/e72c6030-mccpe3-4477-9fb1-ca2b0408cbcb/service_instances"
    52  									}
    53  								},
    54  								{
    55  									"metadata": {
    56  										"guid": "f5b75238-51ea-4f52-a520-caec9015a68d",
    57  										"url": "/v2/service_plans/f5b75238-51ea-4f52-a520-caec9015a68d",
    58  										"created_at": "2016-09-08T12:55:17Z",
    59  										"updated_at": "2017-03-17T20:05:06Z"
    60  									},
    61  									"entity": {
    62  										"name": "Standard",
    63  										"free": false,
    64  										"description": "The Standard plan provides access to the full functionality of Cloudant and can scale as needed for all use cases. The provisioned throughput capacity starts at 100 lookups/sec, 50 writes/sec, and 5 queries/sec with three additional tiers of capacity that can be toggled in the Cloudant Dashboard to meet application throughput requirements. The Standard plan includes 20GB of encrypted data storage, with additional storage metered for purchase.",
    65  										"service_guid": "14c83ad2-6fd4-439a-8c3a-d1a20f8a2381",
    66  										"unique_id": "cloudant-standard",
    67  										"public": true,
    68  										"active": true,
    69  										"service_url": "/v2/services/14c83ad2-6fd4-439a-8c3a-d1a20f8a2381",
    70  										"service_instances_url": "/v2/service_plans/f5b75238-51ea-4f52-a520-caec9015a68d/service_instances"
    71  									}
    72  								}
    73  							]
    74  
    75  						}`),
    76  					),
    77  				)
    78  			})
    79  
    80  			It("should return service plan", func() {
    81  				myserviceplan, err := newServicePlan(server.URL()).FindPlanInServiceOffering("14c83ad2-6fd4-439a-8c3a-d1a20f8a2381", "Lite")
    82  				Expect(err).NotTo(HaveOccurred())
    83  				Expect(myserviceplan).ShouldNot(BeNil())
    84  				Expect(myserviceplan.GUID).Should(Equal("e72c6030-mccpe3-4477-9fb1-ca2b0408cbcb"))
    85  				Expect(myserviceplan.Name).Should(Equal("Lite"))
    86  			})
    87  
    88  		})
    89  
    90  		Context("Server return no space plan", func() {
    91  			BeforeEach(func() {
    92  				server = ghttp.NewServer()
    93  				server.AppendHandlers(
    94  					ghttp.CombineHandlers(
    95  						ghttp.VerifyRequest(http.MethodGet, "/v2/service_plans"),
    96  						ghttp.RespondWith(http.StatusOK, `{
    97  							"total_results": 0,
    98  							"resources": [
    99  							]
   100  															
   101  						}`),
   102  					),
   103  				)
   104  			})
   105  
   106  			It("should return no service plan", func() {
   107  				myserviceplan, err := newServicePlan(server.URL()).FindPlanInServiceOffering("14c83ad2-6fd4-439a-8c3a-d1a20f8a2381", "Lite")
   108  				Expect(err).To(HaveOccurred())
   109  				Expect(myserviceplan).To(BeNil())
   110  			})
   111  
   112  		})
   113  		Context("Server return error", func() {
   114  			BeforeEach(func() {
   115  				server = ghttp.NewServer()
   116  				server.SetAllowUnhandledRequests(true)
   117  				server.AppendHandlers(
   118  					ghttp.CombineHandlers(
   119  						ghttp.VerifyRequest(http.MethodGet, "/v2/service_plans"),
   120  						ghttp.RespondWith(http.StatusInternalServerError, `{
   121  															
   122  						}`),
   123  					),
   124  				)
   125  			})
   126  
   127  			It("should return error service plan", func() {
   128  				myserviceplan, err := newServicePlan(server.URL()).FindPlanInServiceOffering("14c83ad2-6fd4-439a-8c3a-d1a20f8a2381", "Lite")
   129  				Expect(err).To(HaveOccurred())
   130  				Expect(myserviceplan).To(BeNil())
   131  			})
   132  
   133  		})
   134  
   135  	})
   136  })
   137  
   138  func newServicePlan(url string) ServicePlans {
   139  
   140  	sess, err := session.New()
   141  	if err != nil {
   142  		log.Fatal(err)
   143  	}
   144  	conf := sess.Config.Copy()
   145  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   146  	conf.Endpoint = &url
   147  
   148  	client := client.Client{
   149  		Config:      conf,
   150  		ServiceName: bluemix.MccpService,
   151  	}
   152  
   153  	return newServicePlanAPI(&client)
   154  }