-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathtest.js
314 lines (254 loc) · 8.22 KB
/
test.js
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Node
const { promisify } = require("util");
// Npm
const test = require("ava");
const dotEnv = require("dotenv");
const grpc = require("@grpc/grpc-js");
const protoLoader = require("@grpc/proto-loader");
const fetch = require("node-fetch");
const dotenvExpand = require("dotenv-expand");
const { resolve } = require("path");
const productData = require("../src/productcatalogservice/products/products.json");
const myEnv = dotEnv.config({
path: resolve(__dirname, "../.env"),
});
dotenvExpand.expand(myEnv);
// Local
const data = require("./data.json");
// Functions
const deepCopy = (obj) => JSON.parse(JSON.stringify(obj));
const arrayIntersection = (a, b) => a.filter((x) => b.indexOf(x) !== -1);
const isEmpty = (obj) => Object.keys(obj).length === 0;
// Main
let adsGet = null;
let cartAdd = null,
cartGet = null,
cartEmpty = null;
let checkoutOrder = null;
let currencySupported = null,
currencyConvert = null;
let charge = null;
let recommend = null;
let productList = null,
productGet = null,
productSearch = null;
let shippingQuote = null,
shippingOrder = null;
const {
AD_SERVICE_ADDR = "",
CART_SERVICE_ADDR = "",
CHECKOUT_SERVICE_ADDR = "",
CURRENCY_SERVICE_ADDR = "",
PAYMENT_SERVICE_ADDR = "",
PRODUCT_CATALOG_SERVICE_ADDR = "",
RECOMMENDATION_SERVICE_ADDR = "",
SHIPPING_SERVICE_ADDR = "",
EMAIL_SERVICE_ADDR = "",
} = process.env;
test.before(() => {
const oteldemo = grpc.loadPackageDefinition(
protoLoader.loadSync("./demo.proto")
).oteldemo;
const adClient = new oteldemo.AdService(
AD_SERVICE_ADDR,
grpc.credentials.createInsecure()
);
adsGet = promisify(adClient.getAds).bind(adClient);
const cartClient = new oteldemo.CartService(
CART_SERVICE_ADDR,
grpc.credentials.createInsecure()
);
cartAdd = promisify(cartClient.addItem).bind(cartClient);
cartGet = promisify(cartClient.getCart).bind(cartClient);
cartEmpty = promisify(cartClient.emptyCart).bind(cartClient);
const checkoutClient = new oteldemo.CheckoutService(
CHECKOUT_SERVICE_ADDR,
grpc.credentials.createInsecure()
);
checkoutOrder = promisify(checkoutClient.placeOrder).bind(checkoutClient);
const currencyClient = new oteldemo.CurrencyService(
CURRENCY_SERVICE_ADDR,
grpc.credentials.createInsecure()
);
currencySupported = promisify(currencyClient.getSupportedCurrencies).bind(
currencyClient
);
currencyConvert = promisify(currencyClient.convert).bind(currencyClient);
const paymentClient = new oteldemo.PaymentService(
PAYMENT_SERVICE_ADDR,
grpc.credentials.createInsecure()
);
charge = promisify(paymentClient.charge).bind(paymentClient);
const productCatalogClient = new oteldemo.ProductCatalogService(
PRODUCT_CATALOG_SERVICE_ADDR,
grpc.credentials.createInsecure()
);
productList = promisify(productCatalogClient.listProducts).bind(
productCatalogClient
);
productGet = promisify(productCatalogClient.getProduct).bind(
productCatalogClient
);
productSearch = promisify(productCatalogClient.searchProducts).bind(
productCatalogClient
);
const recommendationClient = new oteldemo.RecommendationService(
RECOMMENDATION_SERVICE_ADDR,
grpc.credentials.createInsecure()
);
recommend = promisify(recommendationClient.listRecommendations).bind(
recommendationClient
);
const shippingClient = new oteldemo.ShippingService(
SHIPPING_SERVICE_ADDR,
grpc.credentials.createInsecure()
);
shippingQuote = promisify(shippingClient.getQuote).bind(shippingClient);
shippingOrder = promisify(shippingClient.shipOrder).bind(shippingClient);
});
// --------------- Ad Service ---------------
test("ad: get", async (t) => {
const req = data.ad;
const res = await adsGet(req);
t.is(res.ads.length, 2);
t.truthy(res.ads[0].redirectUrl);
t.truthy(res.ads[1].redirectUrl);
t.truthy(res.ads[0].text);
t.truthy(res.ads[1].text);
});
// --------------- Cart Service ---------------
test("cart: all", async (t) => {
const req = data.cart;
const userIdReq = { userId: req.userId };
// Empty Cart
let res = await cartEmpty(userIdReq);
t.truthy(isEmpty(res));
// Add to Cart
res = await cartAdd(req);
t.truthy(isEmpty(res));
// Check Cart Content
res = await cartGet(userIdReq);
t.is(res.items.length, 1);
t.is(res.items[0].productId, req.item.productId);
t.is(res.items[0].quantity, req.item.quantity);
// Empty Cart
res = await cartEmpty(userIdReq);
t.truthy(isEmpty(res));
// Check Cart Content
res = await cartGet(userIdReq);
t.truthy(isEmpty(res));
});
// --------------- Currency Service ---------------
test("currency: supported", async (t) => {
const res = await currencySupported({});
t.is(res.currencyCodes.length, 33);
});
test("currency: convert", async (t) => {
const req = data.currency;
const res = await currencyConvert(req);
t.is(res.currencyCode, "CAD");
t.is(res.units, 442);
t.true(res.nanos >= 599380800);
});
// --------------- Checkout Service ---------------
test("checkout: place order", async (t) => {
const req = data.checkout;
const res = await checkoutOrder(req);
t.truthy(res.order.orderId);
t.truthy(res.order.shippingTrackingId);
t.truthy(res.order.shippingAddress);
t.is(res.order.shippingCost.currencyCode, "USD");
});
// --------------- Email Service ---------------
// TODO
test("email: confirmation", async (t) => {
const req = data.email;
const res = await fetch(`${EMAIL_SERVICE_ADDR}/send_order_confirmation`, {
method: "POST",
body: JSON.stringify(req),
headers: { "Contenty-Type": "application/json" },
});
t.truthy(true);
});
// --------------- Payment Service ---------------
test("payment: valid credit card", (t) => {
const req = data.charge;
return charge(req).then((res) => {
t.truthy(res.transactionId);
});
});
test("payment: invalid credit card", (t) => {
const req = deepCopy(data.charge);
req.creditCard.creditCardNumber = "0000-0000-0000-0000";
return charge(req).catch((err) => {
t.is(err.details, "Credit card info is invalid.");
});
});
test("payment: amex credit card not allowed", (t) => {
const req = deepCopy(data.charge);
req.creditCard.creditCardNumber = "3714 496353 98431";
return charge(req).catch((err) => {
t.is(
err.details,
"Sorry, we cannot process amex credit cards. Only VISA or MasterCard is accepted."
);
});
});
test("payment: expired credit card", (t) => {
const req = deepCopy(data.charge);
req.creditCard.creditCardExpirationYear = 2021;
return charge(req).catch((err) => {
t.is(err.details, "The credit card (ending 0454) expired on 1/2021.");
});
});
// --------------- Product Catalog Service ---------------
test("product: list", async (t) => {
const res = await productList({});
t.is(res.products.length, 10);
});
test("product: get", async (t) => {
const productId = "OLJCESPC7Z";
const res = await productGet({ id: productId });
t.is(res.name, productData.products.find(({ id }) => id === productId).name);
t.truthy(res.description);
t.truthy(res.picture);
t.truthy(res.priceUsd);
t.truthy(res.categories);
});
test("product: search", async (t) => {
const res = await productSearch({ query: "Roof Binoculars" });
t.is(res.results.length, 1);
const [result] = res.results;
t.is(
result.name,
productData.products.find(({ name }) => name.includes("Binoculars")).name
);
});
// --------------- Recommendation Service ---------------
test("recommendation: list products", async (t) => {
const req = deepCopy(data.recommend);
const res = await recommend(req);
t.is(res.productIds.length, 5);
t.is(arrayIntersection(res.productIds, req.productIds).length, 0);
});
// --------------- Shipping Service ---------------
test("shipping: quote", async (t) => {
const req = data.shipping;
const res = await shippingQuote(req);
t.is(res.costUsd.units, 17);
t.is(res.costUsd.nanos, 800000000);
});
test("shipping: empty quote", async (t) => {
const req = deepCopy(data.shipping);
req.items = [];
const res = await shippingQuote(req);
t.falsy(res.costUsd.units);
t.falsy(res.costUsd.nanos);
});
test("shipping: order", async (t) => {
const req = data.shipping;
const res = await shippingOrder(req);
t.truthy(res.trackingId);
});