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