github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/service_summary_test.go (about)

     1  package api_test
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     9  	"code.cloudfoundry.org/cli/cf/net"
    10  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    11  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    12  	testnet "code.cloudfoundry.org/cli/util/testhelpers/net"
    13  
    14  	. "code.cloudfoundry.org/cli/cf/api"
    15  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    16  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  var _ = Describe("ServiceSummaryRepository", func() {
    22  	var serviceInstanceSummariesResponse testnet.TestResponse
    23  
    24  	BeforeEach(func() {
    25  		serviceInstanceSummariesResponse = testnet.TestResponse{Status: http.StatusOK, Body: `
    26  			{
    27  			  "apps":[
    28  				{
    29  				  "name":"app1",
    30  				  "service_names":[
    31  					"my-service-instance"
    32  				  ]
    33  				},{
    34  				  "name":"app2",
    35  				  "service_names":[
    36  					"my-service-instance"
    37  				  ]
    38  				}
    39  			  ],
    40  			  "services": [
    41  					{
    42  					  "guid": "my-service-instance-guid",
    43  					  "name": "my-service-instance",
    44  					  "bound_app_count": 2,
    45  					  "last_operation": {
    46  						  "type": "create",
    47  						  "state": "in progress",
    48  							"description": "50% done"
    49  					  },
    50  						"service_plan": {
    51  							"guid": "service-plan-guid",
    52  							"name": "spark",
    53  							"service": {
    54  								"guid": "service-offering-guid",
    55  								"label": "cleardb",
    56  								"provider": "cleardb-provider",
    57  								"version": "n/a"
    58  							}
    59  					  }
    60  					}
    61  			  ]
    62  			}`,
    63  		}
    64  	})
    65  
    66  	It("gets a summary of services in the given space", func() {
    67  		req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    68  			Method:   "GET",
    69  			Path:     "/v2/spaces/my-space-guid/summary",
    70  			Response: serviceInstanceSummariesResponse,
    71  		})
    72  
    73  		ts, handler, repo := createServiceSummaryRepo(req)
    74  		defer ts.Close()
    75  
    76  		serviceInstances, apiErr := repo.GetSummariesInCurrentSpace()
    77  		Expect(handler).To(HaveAllRequestsCalled())
    78  
    79  		Expect(apiErr).NotTo(HaveOccurred())
    80  		Expect(1).To(Equal(len(serviceInstances)))
    81  
    82  		instance1 := serviceInstances[0]
    83  		Expect(instance1.Name).To(Equal("my-service-instance"))
    84  		Expect(instance1.GUID).To(Equal("my-service-instance-guid"))
    85  		Expect(instance1.LastOperation.Type).To(Equal("create"))
    86  		Expect(instance1.LastOperation.State).To(Equal("in progress"))
    87  		Expect(instance1.LastOperation.Description).To(Equal("50% done"))
    88  		Expect(instance1.ServicePlan.Name).To(Equal("spark"))
    89  		Expect(instance1.ServiceOffering.Label).To(Equal("cleardb"))
    90  		Expect(instance1.ServiceOffering.Label).To(Equal("cleardb"))
    91  		Expect(instance1.ServiceOffering.Provider).To(Equal("cleardb-provider"))
    92  		Expect(instance1.ServiceOffering.Version).To(Equal("n/a"))
    93  		Expect(len(instance1.ApplicationNames)).To(Equal(2))
    94  		Expect(instance1.ApplicationNames[0]).To(Equal("app1"))
    95  		Expect(instance1.ApplicationNames[1]).To(Equal("app2"))
    96  	})
    97  })
    98  
    99  func createServiceSummaryRepo(req testnet.TestRequest) (ts *httptest.Server, handler *testnet.TestHandler, repo ServiceSummaryRepository) {
   100  	ts, handler = testnet.NewServer([]testnet.TestRequest{req})
   101  	configRepo := testconfig.NewRepositoryWithDefaults()
   102  	configRepo.SetAPIEndpoint(ts.URL)
   103  	gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
   104  	repo = NewCloudControllerServiceSummaryRepository(configRepo, gateway)
   105  	return
   106  }