github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/service_offerings_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 Offering by Label", func() { 18 var server *ghttp.Server 19 AfterEach(func() { 20 server.Close() 21 }) 22 Describe("FindByLabel()", func() { 23 Context("Server return service offering by label", func() { 24 BeforeEach(func() { 25 server = ghttp.NewServer() 26 server.AppendHandlers( 27 ghttp.CombineHandlers( 28 ghttp.VerifyRequest(http.MethodGet, "/v2/services"), 29 ghttp.RespondWith(http.StatusOK, `{ 30 "total_results": 1, 31 "total_pages": 1, 32 "prev_url": null, 33 "next_url": null, 34 "resources": [ 35 { 36 "metadata": { 37 "guid": "14c83ad2-6fd4-439a-8c3a-d1a20f8a2381", 38 "url": "/v2/services/14c83ad2-6fd4-439a-8c3a-d1a20f8a2381", 39 "created_at": "2014-06-03T07:04:12Z", 40 "updated_at": "2017-03-17T20:05:06Z" 41 }, 42 "entity": { 43 "label": "cloudantNoSQLDB", 44 "provider": null, 45 "url": null, 46 "description": "Cloudant NoSQL DB is a fully managed data layer designed for modern web and mobile applications that leverages a flexible JSON schema. Cloudant is built upon and compatible with Apache CouchDB and accessible through a secure HTTPS API, which scales as your application grows. Cloudant is ISO27001 and SOC2 Type 1 certified, and all data is stored in triplicate across separate physical nodes in a cluster for HA/DR within a data center.", 47 "long_description": null, 48 "version": null, 49 "info_url": null, 50 "active": true, 51 "bindable": true, 52 "unique_id": "cloudant", 53 "tags": [ 54 "data_management", 55 "ibm_created", 56 "lite", 57 "ibm_dedicated_public" 58 ], 59 "requires": [ 60 61 ], 62 "documentation_url": null, 63 "service_broker_guid": "b39770d9-5e57-4b3d-a5ff-0b1c7c432597", 64 "plan_updateable": true, 65 "service_plans_url": "/v2/services/14c83ad2-6fd4-439a-8c3a-d1a20f8a2381/service_plans" 66 } 67 } 68 ] 69 70 }`), 71 ), 72 ) 73 }) 74 75 It("should return service offering by label", func() { 76 myserviceoffering, err := newServiceOffering(server.URL()).FindByLabel("cloudantNoSQLDB") 77 Expect(err).NotTo(HaveOccurred()) 78 Expect(myserviceoffering).ShouldNot(BeNil()) 79 Expect(myserviceoffering.GUID).Should(Equal("14c83ad2-6fd4-439a-8c3a-d1a20f8a2381")) 80 Expect(myserviceoffering.Label).Should(Equal("cloudantNoSQLDB")) 81 }) 82 83 }) 84 85 Context("Server return no space offering by label", func() { 86 BeforeEach(func() { 87 server = ghttp.NewServer() 88 server.AppendHandlers( 89 ghttp.CombineHandlers( 90 ghttp.VerifyRequest(http.MethodGet, "/v2/services"), 91 ghttp.RespondWith(http.StatusOK, `{ 92 "total_results": 0, 93 "resources": [ 94 ] 95 96 }`), 97 ), 98 ) 99 }) 100 101 It("should return no service offering", func() { 102 myserviceoffering, err := newServiceOffering(server.URL()).FindByLabel("cloudantNoSQLDB") 103 Expect(err).To(HaveOccurred()) 104 Expect(myserviceoffering).To(BeNil()) 105 }) 106 107 }) 108 Context("Server return error", func() { 109 BeforeEach(func() { 110 server = ghttp.NewServer() 111 server.SetAllowUnhandledRequests(true) 112 server.AppendHandlers( 113 ghttp.CombineHandlers( 114 ghttp.VerifyRequest(http.MethodGet, "/v2/services"), 115 ghttp.RespondWith(http.StatusInternalServerError, `{ 116 117 }`), 118 ), 119 ) 120 }) 121 122 It("should return error service offering", func() { 123 myserviceoffering, err := newServiceOffering(server.URL()).FindByLabel("cloudantNoSQLDB") 124 Expect(err).To(HaveOccurred()) 125 Expect(myserviceoffering).To(BeNil()) 126 }) 127 128 }) 129 130 }) 131 }) 132 133 func newServiceOffering(url string) ServiceOfferings { 134 135 sess, err := session.New() 136 if err != nil { 137 log.Fatal(err) 138 } 139 conf := sess.Config.Copy() 140 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 141 conf.Endpoint = &url 142 143 client := client.Client{ 144 Config: conf, 145 ServiceName: bluemix.MccpService, 146 } 147 return newServiceOfferingAPI(&client) 148 }