-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathproduct_test.go
More file actions
299 lines (263 loc) · 9.64 KB
/
product_test.go
File metadata and controls
299 lines (263 loc) · 9.64 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package megaport
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/suite"
)
// ProductClientTestSuite tests the product client
type ProductClientTestSuite struct {
ClientTestSuite
}
func TestProductClientTestSuite(t *testing.T) {
t.Parallel()
suite.Run(t, new(ProductClientTestSuite))
}
func (suite *ProductClientTestSuite) SetupTest() {
suite.mux = http.NewServeMux()
suite.server = httptest.NewServer(suite.mux)
suite.client = NewClient(nil, nil)
url, _ := url.Parse(suite.server.URL)
suite.client.BaseURL = url
}
func (suite *ProductClientTestSuite) TearDownTest() {
suite.server.Close()
}
// TestExecuteOrder tests the ExecuteOrder method
func (suite *ProductClientTestSuite) TestExecuteOrder() {
ctx := context.Background()
productSvc := suite.client.ProductService
jblob := `{
"message": "test-message",
"terms": "test-terms",
"data": [
{"technicalServiceUid": "36b3f68e-2f54-4331-bf94-f8984449365f"}
]
}`
portOrder := []PortOrder{
{
Name: "test-port",
Term: 12,
PortSpeed: 10000,
LocationID: 226,
Virtual: false,
Market: "US",
MarketplaceVisibility: false,
},
}
suite.mux.HandleFunc("/v4/networkdesign/buy", func(w http.ResponseWriter, r *http.Request) {
var wrapper struct {
NetworkDesign []PortOrder `json:"networkDesign"`
DiscountCodes []string `json:"discountCodes"`
}
err := json.NewDecoder(r.Body).Decode(&wrapper)
if err != nil {
suite.FailNowf("could not decode json", "could not decode json %v", err)
}
orders := wrapper.NetworkDesign
wantOrder := portOrder[0]
gotOrder := orders[0]
suite.testMethod(r, http.MethodPost)
fmt.Fprint(w, jblob)
suite.Equal(wantOrder.Name, gotOrder.Name)
suite.Equal(wantOrder.Term, gotOrder.Term)
suite.Equal(wantOrder.PortSpeed, gotOrder.PortSpeed)
suite.Equal(wantOrder.LocationID, gotOrder.LocationID)
suite.Equal(wantOrder.Virtual, gotOrder.Virtual)
suite.Equal(wantOrder.MarketplaceVisibility, gotOrder.MarketplaceVisibility)
suite.Equal([]string{}, wrapper.DiscountCodes)
})
wantRes := PtrTo([]byte(jblob))
gotRes, err := productSvc.ExecuteOrder(ctx, portOrder)
suite.NoError(err)
suite.Equal(wantRes, gotRes)
}
// TestModifyProduct tests the ModifyProduct method
func (suite *ProductClientTestSuite) TestModifyProduct() {
ctx := context.Background()
productSvc := suite.client.ProductService
productUid := "36b3f68e-2f54-4331-bf94-f8984449365f"
jblob := `{
"message": "Product [36b3f68e-2f54-4331-bf94-f8984449365f] has been updated",
"terms": "This data is subject to the Acceptable Use Policy https://www.megaport.com/legal/acceptable-use-policy",
"data": {
"serviceName": "updated-test-product",
"name": "updated-test-product",
"secondaryName": null,
"technicalServiceId": 185927,
"technicalServiceUid": "ef60d544-00e1-4ccc-bcff-3e2050bface5",
"requestedDate": 1706202200307,
"configuredDate": null,
"currentEstimatedDelivery": null,
"companyName": "test-company",
"companyId": 1153,
"billingContactName": null,
"billingContactId": null,
"adminContactName": null,
"adminContactId": null,
"technicalContactName": null,
"technicalContactId": null,
"salesName": null,
"salesId": null,
"billableId": 177726,
"billableUsageAlgorithm": null,
"productType": "megaport",
"provisioningStatus": "DEPLOYABLE",
"failedReason": null,
"inAdvanceBillingStatus": null,
"provisioningItems": [],
"tags": [],
"vxcDistanceBand": null,
"intercapPath": null,
"marketplaceVisibility": false,
"vxcPermitted": true,
"vxcAutoApproval": false,
"createDate": 1706202200307,
"terminationDate": null,
"contractStartDate": null,
"contractTermMonths": 1,
"rateType": "MONTHLY",
"trialAgreement": false,
"payerCompanyId": null,
"nonPayerCompanyId": null,
"minimumSpeed": null,
"maximumSpeed": null,
"rateLimit": 10000,
"errorMessage": null,
"lagId": null,
"aggregationId": null,
"lagPrimary": null,
"market": "USA",
"accountManager": null,
"promptUid": null,
"components": [],
"attributes": [],
"aLocation": null,
"bLocation": null,
"aMetro": null,
"aCountry": null,
"aLocationId": null,
"bLocationId": null,
"bMetro": null,
"bCountry": null,
"attributeTags": {},
"createdBy": "32df7107-fdca-4c2a-8ccb-c6867813b3f2",
"buyoutPort": false,
"virtual": false,
"locked": false,
"adminLocked": false,
"bgpShutdownDefault": false,
"originDomain": null
}
}`
productType := PRODUCT_MEGAPORT
wantReq := &ModifyProductRequest{
ProductID: productUid,
ProductType: productType,
Name: "updated-test-product",
CostCentre: "US",
MarketplaceVisibility: PtrTo(false),
}
path := fmt.Sprintf("/v2/product/%s/%s", productType, productUid)
suite.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
v := new(ModifyProductRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
suite.FailNowf("could not decode json", "could not decode json %v", err)
}
suite.testMethod(r, http.MethodPut)
fmt.Fprint(w, jblob)
suite.Equal(wantReq, v)
})
wantRes := &ModifyProductResponse{
IsUpdated: true,
}
gotRes, err := productSvc.ModifyProduct(ctx, wantReq)
suite.NoError(err)
suite.Equal(wantRes, gotRes)
}
// TestDeleteProduct tests the DeleteProduct method
func (suite *ProductClientTestSuite) TestDeleteProduct() {
ctx := context.Background()
productSvc := suite.client.ProductService
productUid := "36b3f68e-2f54-4331-bf94-f8984449365f"
jblob := `{
"message": "Action [CANCEL_NOW Service 36b3f68e-2f54-4331-bf94-f8984449365f] has been done.",
"terms": "This data is subject to the Acceptable Use Policy https://www.megaport.com/legal/acceptable-use-policy"
}`
req := &DeleteProductRequest{
ProductID: productUid,
DeleteNow: true,
}
path := "/v3/product/" + req.ProductID + "/action/CANCEL_NOW"
suite.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
suite.testMethod(r, http.MethodPost)
fmt.Fprint(w, jblob)
})
wantRes := &DeleteProductResponse{}
gotRes, err := productSvc.DeleteProduct(ctx, req)
suite.NoError(err)
suite.Equal(wantRes, gotRes)
}
// TestRestoreProduct tests the RestoreProduct method
func (suite *ProductClientTestSuite) TestRestoreProduct() {
ctx := context.Background()
productSvc := suite.client.ProductService
productUid := "36b3f68e-2f54-4331-bf94-f8984449365f"
jblob := `{
"message": "Action [UN_CANCEL Service 36b3f68e-2f54-4331-bf94-f8984449365f] has been done.",
"terms": "This data is subject to the Acceptable Use Policy https://www.megaport.com/legal/acceptable-use-policy"
}`
path := "/v3/product/" + productUid + "/action/UN_CANCEL"
suite.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
suite.testMethod(r, http.MethodPost)
fmt.Fprint(w, jblob)
})
wantRes := &RestoreProductResponse{}
gotRes, err := productSvc.RestoreProduct(ctx, productUid)
suite.NoError(err)
suite.Equal(wantRes, gotRes)
}
// TestManageProductLuck tests the ManageProductLock method
func (suite *ProductClientTestSuite) TestManageProductLuck() {
ctx := context.Background()
productSvc := suite.client.ProductService
productUid := "36b3f68e-2f54-4331-bf94-f8984449365f"
jblob := `{
"message": "Service locked",
"terms": "This data is subject to the Acceptable Use Policy https://www.megaport.com/legal/acceptable-use-policy",
"data": {
"productId":999999,"productUid":"36b3f68e-2f54-4331-bf94-f8984449365f","productName":"test-port","productType":"MEGAPORT","provisioningStatus":"LIVE","createDate":0,"createdBy":"","portSpeed":10000,"terminateDate":0,"liveDate":0,"market":"US","locationId":226,"usageAlgorithm":"","marketplaceVisibility":false,"vxcpermitted":true,"vxcAutoApproval":false,"secondaryName":"test-secondary-name","lagPrimary":false,"lagId":0,"aggregationId":0,"companyUid":"32df7107-fdca-4c2a-8ccb-c6867813b3f2","companyName":"test-company","contractStartDate":1706104800000,"contractEndDate":1737727200000,"contractTermMonths":12,"attributeTags":null,"virtual":false,"buyoutPort":false,"locked":true,"adminLocked":false,"cancelable":true,"resources":{"interface":{"demarcation":"","description":"","id":0,"loa_template":"","media":"","name":"","port_speed":0,"resource_name":"","resource_type":"","up":0
}
}
}
}`
req := &ManageProductLockRequest{
ProductID: productUid,
ShouldLock: true,
}
suite.mux.HandleFunc(fmt.Sprintf("/v2/product/%s/lock", productUid), func(w http.ResponseWriter, r *http.Request) {
suite.testMethod(r, http.MethodPost)
fmt.Fprint(w, jblob)
})
wantRes := &ManageProductLockResponse{}
gotRes, err := productSvc.ManageProductLock(ctx, req)
suite.NoError(err)
suite.Equal(wantRes, gotRes)
}
func (suite *ProductClientTestSuite) TestListProductResourceTags() {
ctx := context.Background()
productSvc := suite.client.ProductService
productUid := "36b3f68e-2f54-4331-bf94-f8984449365f"
suite.mux.HandleFunc(fmt.Sprintf("/v2/product/%s/tags", productUid), func(w http.ResponseWriter, r *http.Request) {
suite.testMethod(r, http.MethodGet)
fmt.Fprint(w, resourceTagJSONBlob)
})
res, err := productSvc.ListProductResourceTags(ctx, productUid)
suite.NoError(err)
suite.EqualValues(testProductResourceTags, res)
}