github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/api/service_plan_visibility_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/configuration/coreconfig"
    10  	"github.com/cloudfoundry/cli/cf/net"
    11  	"github.com/cloudfoundry/cli/cf/terminal/terminalfakes"
    12  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    13  	testnet "github.com/cloudfoundry/cli/testhelpers/net"
    14  
    15  	. "github.com/cloudfoundry/cli/cf/api"
    16  	"github.com/cloudfoundry/cli/cf/trace/tracefakes"
    17  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("Service Plan Visibility Repository", func() {
    23  	var (
    24  		testServer  *httptest.Server
    25  		testHandler *testnet.TestHandler
    26  		configRepo  coreconfig.ReadWriter
    27  		repo        CloudControllerServicePlanVisibilityRepository
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		configRepo = testconfig.NewRepositoryWithDefaults()
    32  		gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
    33  		repo = NewCloudControllerServicePlanVisibilityRepository(configRepo, gateway)
    34  	})
    35  
    36  	AfterEach(func() {
    37  		testServer.Close()
    38  	})
    39  
    40  	setupTestServer := func(reqs ...testnet.TestRequest) {
    41  		testServer, testHandler = testnet.NewServer(reqs)
    42  		configRepo.SetAPIEndpoint(testServer.URL)
    43  	}
    44  
    45  	Describe(".Create", func() {
    46  		BeforeEach(func() {
    47  			setupTestServer(apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    48  				Method:   "POST",
    49  				Path:     "/v2/service_plan_visibilities",
    50  				Matcher:  testnet.RequestBodyMatcher(`{"service_plan_guid":"service_plan_guid", "organization_guid":"org_guid"}`),
    51  				Response: testnet.TestResponse{Status: http.StatusCreated},
    52  			}))
    53  		})
    54  
    55  		It("creates a service plan visibility", func() {
    56  			err := repo.Create("service_plan_guid", "org_guid")
    57  
    58  			Expect(testHandler).To(HaveAllRequestsCalled())
    59  			Expect(err).NotTo(HaveOccurred())
    60  		})
    61  	})
    62  
    63  	Describe(".List", func() {
    64  		BeforeEach(func() {
    65  			setupTestServer(firstPlanVisibilityRequest, secondPlanVisibilityRequest)
    66  		})
    67  
    68  		It("returns service plans", func() {
    69  			servicePlansVisibilitiesFields, err := repo.List()
    70  
    71  			Expect(err).NotTo(HaveOccurred())
    72  			Expect(testHandler).To(HaveAllRequestsCalled())
    73  			Expect(len(servicePlansVisibilitiesFields)).To(Equal(2))
    74  			Expect(servicePlansVisibilitiesFields[0].GUID).To(Equal("request-guid-1"))
    75  			Expect(servicePlansVisibilitiesFields[0].ServicePlanGUID).To(Equal("service-plan-guid-1"))
    76  			Expect(servicePlansVisibilitiesFields[0].OrganizationGUID).To(Equal("org-guid-1"))
    77  			Expect(servicePlansVisibilitiesFields[1].GUID).To(Equal("request-guid-2"))
    78  			Expect(servicePlansVisibilitiesFields[1].ServicePlanGUID).To(Equal("service-plan-guid-2"))
    79  			Expect(servicePlansVisibilitiesFields[1].OrganizationGUID).To(Equal("org-guid-2"))
    80  		})
    81  	})
    82  
    83  	Describe(".Delete", func() {
    84  		It("deletes a service plan visibility", func() {
    85  			servicePlanVisibilityGUID := "the-service-plan-visibility-guid"
    86  			setupTestServer(apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    87  				Method:  "DELETE",
    88  				Path:    "/v2/service_plan_visibilities/" + servicePlanVisibilityGUID,
    89  				Matcher: testnet.EmptyQueryParamMatcher(),
    90  				Response: testnet.TestResponse{
    91  					Status: http.StatusNoContent,
    92  				},
    93  			}))
    94  
    95  			err := repo.Delete(servicePlanVisibilityGUID)
    96  			Expect(err).ToNot(HaveOccurred())
    97  		})
    98  	})
    99  
   100  	Describe(".Search", func() {
   101  		It("finds the service plan visibilities that match the given query parameters", func() {
   102  			setupTestServer(searchPlanVisibilityRequest)
   103  
   104  			servicePlansVisibilitiesFields, err := repo.Search(map[string]string{"service_plan_guid": "service-plan-guid-1", "organization_guid": "org-guid-1"})
   105  			Expect(err).ToNot(HaveOccurred())
   106  			Expect(testHandler).To(HaveAllRequestsCalled())
   107  			Expect(len(servicePlansVisibilitiesFields)).To(Equal(1))
   108  			Expect(servicePlansVisibilitiesFields[0].GUID).To(Equal("request-guid-1"))
   109  			Expect(servicePlansVisibilitiesFields[0].ServicePlanGUID).To(Equal("service-plan-guid-1"))
   110  			Expect(servicePlansVisibilitiesFields[0].OrganizationGUID).To(Equal("org-guid-1"))
   111  		})
   112  	})
   113  })
   114  
   115  var firstPlanVisibilityRequest = apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   116  	Method: "GET",
   117  	Path:   "/v2/service_plan_visibilities",
   118  	Response: testnet.TestResponse{
   119  		Status: http.StatusOK,
   120  		Body: `{
   121    "total_results": 2,
   122    "total_pages": 2,
   123    "next_url": "/v2/service_plan_visibilities?page=2",
   124    "resources": [
   125      {
   126        "metadata": {
   127          "guid": "request-guid-1"
   128        },
   129        "entity": {
   130          "service_plan_guid": "service-plan-guid-1",
   131          "organization_guid": "org-guid-1"
   132        }
   133      }
   134    ]
   135  }`,
   136  	},
   137  })
   138  
   139  var secondPlanVisibilityRequest = apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   140  	Method: "GET",
   141  	Path:   "/v2/service_plan_visibilities?page=2",
   142  	Response: testnet.TestResponse{
   143  		Status: http.StatusOK,
   144  		Body: `{
   145    "total_results": 2,
   146    "total_pages": 2,
   147    "resources": [
   148      {
   149        "metadata": {
   150          "guid": "request-guid-2"
   151        },
   152        "entity": {
   153          "service_plan_guid": "service-plan-guid-2",
   154          "organization_guid": "org-guid-2"
   155        }
   156      }
   157    ]
   158  }`,
   159  	},
   160  })
   161  
   162  var searchPlanVisibilityRequest = apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   163  	Method: "GET",
   164  	Path:   "/v2/service_plan_visibilities?q=service_plan_guid%3Aservice-plan-guid-1%3Borganization_guid%3Aorg-guid-1",
   165  	Response: testnet.TestResponse{
   166  		Status: http.StatusOK,
   167  		Body: `{
   168    "total_results": 1,
   169    "total_pages": 1,
   170    "resources": [
   171      {
   172        "metadata": {
   173          "guid": "request-guid-1"
   174        },
   175        "entity": {
   176          "service_plan_guid": "service-plan-guid-1",
   177          "organization_guid": "org-guid-1"
   178        }
   179      }
   180    ]
   181  }`,
   182  	},
   183  })