github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+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  	"github.com/cloudfoundry/cli/cf/api/apifakes"
     9  	"github.com/cloudfoundry/cli/cf/net"
    10  	"github.com/cloudfoundry/cli/cf/terminal/terminalfakes"
    11  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    12  	testnet "github.com/cloudfoundry/cli/testhelpers/net"
    13  
    14  	. "github.com/cloudfoundry/cli/cf/api"
    15  	"github.com/cloudfoundry/cli/cf/trace/tracefakes"
    16  	. "github.com/cloudfoundry/cli/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.LastOperation.Type).To(Equal("create"))
    85  		Expect(instance1.LastOperation.State).To(Equal("in progress"))
    86  		Expect(instance1.LastOperation.Description).To(Equal("50% done"))
    87  		Expect(instance1.ServicePlan.Name).To(Equal("spark"))
    88  		Expect(instance1.ServiceOffering.Label).To(Equal("cleardb"))
    89  		Expect(instance1.ServiceOffering.Label).To(Equal("cleardb"))
    90  		Expect(instance1.ServiceOffering.Provider).To(Equal("cleardb-provider"))
    91  		Expect(instance1.ServiceOffering.Version).To(Equal("n/a"))
    92  		Expect(len(instance1.ApplicationNames)).To(Equal(2))
    93  		Expect(instance1.ApplicationNames[0]).To(Equal("app1"))
    94  		Expect(instance1.ApplicationNames[1]).To(Equal("app2"))
    95  	})
    96  })
    97  
    98  func createServiceSummaryRepo(req testnet.TestRequest) (ts *httptest.Server, handler *testnet.TestHandler, repo ServiceSummaryRepository) {
    99  	ts, handler = testnet.NewServer([]testnet.TestRequest{req})
   100  	configRepo := testconfig.NewRepositoryWithDefaults()
   101  	configRepo.SetAPIEndpoint(ts.URL)
   102  	gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
   103  	repo = NewCloudControllerServiceSummaryRepository(configRepo, gateway)
   104  	return
   105  }