-
Notifications
You must be signed in to change notification settings - Fork 13
/
test-em-all.sh
executable file
·427 lines (336 loc) · 15.7 KB
/
test-em-all.sh
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/env bash
### Sample usage:
#
# for local run
# HOST=localhost PORT=8765 ./test-em-all.sh
# with docker compose
# HOST=localhost PORT=8765 ./test-em-all.sh start stop
#
echo -e "Starting 'Store μServices' for [end-2-end] testing....\n"
: ${HOST=localhost}
: ${PORT=8765}
: ${PROD_CODE=P0001}
: ${PROD_CODE_1=P0002}
: ${CUSTOMER_NAME=dockerCustomer001}
function assertCurl() {
local expectedHttpCode=$1
local curlCmd="$2 -w \"%{http_code}\""
local result=$(eval ${curlCmd})
local httpCode="${result:(-3)}"
RESPONSE='' && (( ${#result} > 3 )) && RESPONSE="${result%???}"
if [[ "$httpCode" = "$expectedHttpCode" ]]
then
if [[ "$httpCode" = "200" ]]
then
echo "Test OK (HTTP Code: $httpCode)"
else
echo "Test OK (HTTP Code: $httpCode, $RESPONSE)"
fi
return 0
else
echo "Test FAILED, EXPECTED HTTP Code: $expectedHttpCode, GOT: $httpCode, WILL ABORT!"
echo "- Failing command: $curlCmd"
echo "- Response Body: $RESPONSE"
return 1
fi
}
function assertEqual() {
local expected=$1
local actual=$2
if [[ "$actual" = "$expected" ]]
then
echo "Test OK (actual value: $actual)"
return 0
else
echo "Test FAILED, EXPECTED VALUE: $expected, ACTUAL VALUE: $actual, WILL ABORT"
return 1
fi
}
function testUrl() {
url=$@
if curl ${url} -ks -f -o /dev/null
then
return 0
else
return 1
fi;
}
function waitForService() {
url=$@
echo -n "Wait for: $url... "
n=0
until testUrl ${url}
do
n=$((n + 1))
if [[ ${n} == 100 ]]
then
echo " Give up"
exit 1
else
sleep 3
echo -n ", retry #$n "
fi
done
echo -e "\n DONE, continues...\n"
}
function recreateComposite() {
local identifier=$1
local composite=$2
local baseURL=$3
local methodType=$4
echo "calling URL" http://${HOST}:${PORT}/${baseURL} " with body -" $composite
# assertCurl 200 "curl -X DELETE -k http://${HOST}:${PORT}/${baseURL}/${identifier} -s"
COMPOSITE_RESPONSE=$(curl -X ${methodType} -k http://${HOST}:${PORT}/${baseURL} -H "Content-Type: application/json" \
--data "$composite")
echo "Response from caller - " ${COMPOSITE_RESPONSE}
echo " "
}
function setupTestData() {
body="{\"productCode\":\"$PROD_CODE"
body+=\
'","productName":"product name A","price":100, "imageUrl":"https://www.ikea.com/in/en/images/products/saellskaplig-jug-patterned-green__0941744_pe795674_s5.jpg?f=xl","description": "A Beautiful Product"}'
# Creating Product
echo "creating product with code - " $PROD_CODE
recreateComposite "$PROD_CODE" "$body" "catalog-service/api/catalog" "POST"
body="{\"productCode\":\"$PROD_CODE_1"
body+=\
'","productName":"product name B","price":9.99, "imageUrl":"https://cdn.igp.com/f_auto,q_auto,t_pnopt12prodlp/products/p-you-are-my-penguin-personalized-magic-mug-265224-m.jpg","description": "Nice Product"}'
# Creating Product
echo "creating product with code - " $PROD_CODE_1
recreateComposite "$PROD_CODE_1" "$body" "catalog-service/api/catalog" "POST"
# waiting for kafka to process the catalog creation request, as it is first time kafka initialization takes time
sleep 3
# Verify that a normal request works, expect record exists with product code
assertCurl 200 "curl -k http://$HOST:$PORT/inventory-service/api/inventory/$PROD_CODE"
assertEqual \"${PROD_CODE}\" $(echo ${RESPONSE} | jq .productCode)
body="{\"productCode\":\"$PROD_CODE"
body+=\
'","availableQuantity":100}'
# Update the product available Quantity
recreateComposite $(echo "$RESPONSE" | jq -r .id) "$body" "inventory-service/api/inventory/$(echo "$RESPONSE" | jq -r .id)" "PUT"
# Verify that a normal request works, expect record exists with product code_1
assertCurl 200 "curl -k http://$HOST:$PORT/inventory-service/api/inventory/$PROD_CODE_1"
assertEqual \"${PROD_CODE_1}\" $(echo ${RESPONSE} | jq .productCode)
body="{\"productCode\":\"$PROD_CODE_1"
body+=\
'","availableQuantity":50}'
# Update the product_1 available Quantity
recreateComposite $(echo "$RESPONSE" | jq -r .id) "$body" "inventory-service/api/inventory/$(echo "$RESPONSE" | jq -r .id)" "PUT"
# Verify that communication between catalog-service and inventory service is established
assertCurl 200 "curl -k http://$HOST:$PORT/catalog-service/api/catalog/productCode/$PROD_CODE_1?fetchInStock=true"
assertEqual \"${PROD_CODE_1}\" $(echo ${RESPONSE} | jq .productCode)
assertEqual true $(echo ${RESPONSE} | jq .inStock)
body="{\"name\": \"$CUSTOMER_NAME"
body+=\
'","email": "[email protected]","phone":"9876543210","address": "docker Address","amountAvailable":1000}'
# Creating Customer
recreateComposite "$CUSTOMER_NAME" "$body" "payment-service/api/customers" "POST"
CUSTOMER_ID=$(echo ${COMPOSITE_RESPONSE} | jq .customerId)
}
function verifyAPIs() {
# Step 1 Creating Order and it should be CONFIRMED
body="{\"customerId\": $CUSTOMER_ID"
body+=\
',"items":[{"productCode": '
body+="\"$PROD_CODE"
body+=\
'","quantity": 10,"productPrice": 5}],"deliveryAddress": {"addressLine1": "string","addressLine2": "string","city": "string","state": "string","zipCode": "string","country": "string"}}'
echo " "
# Creating Order
recreateComposite "$CUSTOMER_NAME" "$body" "order-service/api/orders" "POST"
local ORDER_ID=$(echo ${COMPOSITE_RESPONSE} | jq .orderId)
echo "Sleeping for 8 sec as it is first order, letting kafka start in all services. Processing orderId -" $ORDER_ID
sleep 8
# Verify that order processing is completed and status is CONFIRMED
assertCurl 200 "curl -k http://$HOST:$PORT/order-service/api/orders/$ORDER_ID"
assertEqual $ORDER_ID $(echo ${RESPONSE} | jq .orderId)
assertEqual $CUSTOMER_ID $(echo ${RESPONSE} | jq .customerId)
assertEqual \"CONFIRMED\" $(echo ${RESPONSE} | jq .status)
assertEqual null $(echo ${RESPONSE} | jq .source)
# Verify that amountAvailable is deducted as per order
assertCurl 200 "curl -k http://$HOST:$PORT/payment-service/api/customers/$CUSTOMER_ID"
assertEqual 950 $(echo ${RESPONSE} | jq .amountAvailable)
# Step2, Order Should be rejected
body="{\"customerId\": $CUSTOMER_ID"
body+=\
',"items":[{"productCode": '
body+="\"$PROD_CODE"
body+=\
'","quantity": 100,"productPrice": 5}],"deliveryAddress": {"addressLine1": "string","addressLine2": "string","city": "string","state": "string","zipCode": "string","country": "string"}}'
echo " "
# Creating 2nd Order, this should ROLLBACK as Inventory is not available
recreateComposite "$CUSTOMER_NAME" "$body" "order-service/api/orders" "POST"
local ORDER_ID=$(echo ${COMPOSITE_RESPONSE} | jq .orderId)
echo "Sleeping for 3 sec for processing of orderId -" $ORDER_ID
sleep 3
# Verify that order processing is completed and status is ROLLBACK
assertCurl 200 "curl -k http://$HOST:$PORT/order-service/api/orders/$ORDER_ID"
assertEqual $ORDER_ID $(echo ${RESPONSE} | jq .orderId)
assertEqual $CUSTOMER_ID $(echo ${RESPONSE} | jq .customerId)
assertEqual \"ROLLBACK\" $(echo ${RESPONSE} | jq .status)
assertEqual \"INVENTORY\" $(echo ${RESPONSE} | jq .source)
# Verify that amountAvailable is not deducted as per order
assertCurl 200 "curl -k http://$HOST:$PORT/payment-service/api/customers/$CUSTOMER_ID"
assertEqual 950 $(echo ${RESPONSE} | jq .amountAvailable)
# Step 3, Order Should be CONFIRMED
body="{\"customerId\": $CUSTOMER_ID"
body+=\
',"items":[{"productCode": '
body+="\"$PROD_CODE"
body+=\
'","quantity": 80,"productPrice": 10}],"deliveryAddress": {"addressLine1": "string","addressLine2": "string","city": "string","state": "string","zipCode": "string","country": "string"}}'
echo " "
# Creating 3rd Order, this should CONFIRMED as Inventory is available
recreateComposite "$CUSTOMER_NAME" "$body" "order-service/api/orders" "POST"
local ORDER_ID=$(echo ${COMPOSITE_RESPONSE} | jq .orderId)
echo "Sleeping for 3 sec for processing of orderId -" $ORDER_ID
sleep 3
# Verify that order processing is completed and status is CONFIRMED
assertCurl 200 "curl -k http://$HOST:$PORT/order-service/api/orders/$ORDER_ID"
assertEqual $ORDER_ID $(echo ${RESPONSE} | jq .orderId)
assertEqual $CUSTOMER_ID $(echo ${RESPONSE} | jq .customerId)
assertEqual \"CONFIRMED\" $(echo ${RESPONSE} | jq .status)
assertEqual null $(echo ${RESPONSE} | jq .source)
# Verify that amountAvailable is deducted as per order
assertCurl 200 "curl -k http://$HOST:$PORT/payment-service/api/customers/$CUSTOMER_ID"
assertEqual 150 $(echo ${RESPONSE} | jq .amountAvailable)
# Step 4, Order Should be ROLLBACK
body="{\"customerId\": $CUSTOMER_ID"
body+=\
',"items":[{"productCode": '
body+="\"$PROD_CODE"
body+=\
'","quantity": 8,"productPrice": 20}],"deliveryAddress": {"addressLine1": "string","addressLine2": "string","city": "string","state": "string","zipCode": "string","country": "string"}}'
echo " "
# Creating 4th Order, this should ROLLBACK as amount is not available for customer
recreateComposite "$CUSTOMER_NAME" "$body" "order-service/api/orders" "POST"
local ORDER_ID=$(echo ${COMPOSITE_RESPONSE} | jq .orderId)
echo "Sleeping for 3 sec for processing of orderId -" $ORDER_ID
sleep 3
# Verify that order processing is completed and status is ROLLBACK
assertCurl 200 "curl -k http://$HOST:$PORT/order-service/api/orders/$ORDER_ID"
assertEqual $ORDER_ID $(echo ${RESPONSE} | jq .orderId)
assertEqual $CUSTOMER_ID $(echo ${RESPONSE} | jq .customerId)
assertEqual \"ROLLBACK\" $(echo ${RESPONSE} | jq .status)
assertEqual \"PAYMENT\" $(echo ${RESPONSE} | jq .source)
# Verify that amountAvailable is not deducted as per order cant be processed
assertCurl 200 "curl -k http://$HOST:$PORT/payment-service/api/customers/$CUSTOMER_ID"
assertEqual 150 $(echo ${RESPONSE} | jq .amountAvailable)
# Step 5, Order Should be REJECTED
body="{\"customerId\": $CUSTOMER_ID"
body+=\
',"items":[{"productCode": '
body+="\"$PROD_CODE"
body+=\
'","quantity": 20,"productPrice": 20}],"deliveryAddress": {"addressLine1": "string","addressLine2": "string","city": "string","state": "string","zipCode": "string","country": "string"}}'
echo " "
# Creating 5th Order, this should Reject as amount is not available for customer & inventory not available
recreateComposite "$CUSTOMER_NAME" "$body" "order-service/api/orders" "POST"
local ORDER_ID=$(echo ${COMPOSITE_RESPONSE} | jq .orderId)
echo "Sleeping for 3 sec for processing of orderId -" $ORDER_ID
sleep 3
# Verify that order processing is completed and status is ROLLBACK
assertCurl 200 "curl -k http://$HOST:$PORT/order-service/api/orders/$ORDER_ID"
assertEqual $ORDER_ID $(echo ${RESPONSE} | jq .orderId)
assertEqual $CUSTOMER_ID $(echo ${RESPONSE} | jq .customerId)
assertEqual \"REJECTED\" $(echo ${RESPONSE} | jq .status)
assertEqual null $(echo ${RESPONSE} | jq .source)
# Verify that amountAvailable is not deducted as per order cant be processed
assertCurl 200 "curl -k http://$HOST:$PORT/payment-service/api/customers/$CUSTOMER_ID"
assertEqual 150 $(echo ${RESPONSE} | jq .amountAvailable)
echo " "
# Step 6 Creating Order and it should be CONFIRMED
body="{\"customerId\": $CUSTOMER_ID"
body+=\
',"items":[{"productCode": '
body+="\"$PROD_CODE"
body+=\
'","quantity": 1,"productPrice": 10},{"productCode": '
body+="\"$PROD_CODE_1"
body+=\
'","quantity": 5,"productPrice": 10}],"deliveryAddress": {"addressLine1": "string","addressLine2": "string","city": "string","state": "string","zipCode": "string","country": "string"}}'
echo " "
# Creating Order
recreateComposite "$CUSTOMER_NAME" "$body" "order-service/api/orders" "POST"
local ORDER_ID=$(echo ${COMPOSITE_RESPONSE} | jq .orderId)
echo "Sleeping for 3 sec as it is, Processing orderId -" $ORDER_ID
sleep 3
# Verify that order processing is completed and status is CONFIRMED
assertCurl 200 "curl -k http://$HOST:$PORT/order-service/api/orders/$ORDER_ID"
assertEqual $ORDER_ID $(echo ${RESPONSE} | jq .orderId)
assertEqual $CUSTOMER_ID $(echo ${RESPONSE} | jq .customerId)
assertEqual \"CONFIRMED\" $(echo ${RESPONSE} | jq .status)
assertEqual null $(echo ${RESPONSE} | jq .source)
# Verify that amountAvailable is deducted as per order
assertCurl 200 "curl -k http://$HOST:$PORT/payment-service/api/customers/$CUSTOMER_ID"
assertEqual 90 $(echo ${RESPONSE} | jq .amountAvailable)
echo " "
# Step 7 Creating Order with one available and it should be ROLLBACK
body="{\"customerId\": $CUSTOMER_ID"
body+=\
',"items":[{"productCode": '
body+="\"$PROD_CODE"
body+=\
'","quantity": 1,"productPrice": 10},{"productCode": '
body+="\"$PROD_CODE_1"
body+=\
'","quantity": 500,"productPrice": 9.99}],"deliveryAddress": {"addressLine1": "string","addressLine2": "string","city": "string","state": "string","zipCode": "string","country": "string"}}'
echo " "
# Creating Order
recreateComposite "$CUSTOMER_NAME" "$body" "order-service/api/orders" "POST"
local ORDER_ID=$(echo ${COMPOSITE_RESPONSE} | jq .orderId)
echo "Sleeping for 3 sec as it is, Processing orderId -" $ORDER_ID
sleep 3
# Verify that order processing is completed and status is REJECTED
assertCurl 200 "curl -k http://$HOST:$PORT/order-service/api/orders/$ORDER_ID"
assertEqual $ORDER_ID $(echo ${RESPONSE} | jq .orderId)
assertEqual $CUSTOMER_ID $(echo ${RESPONSE} | jq .customerId)
assertEqual \"REJECTED\" $(echo ${RESPONSE} | jq .status)
assertEqual null $(echo ${RESPONSE} | jq .source)
# Verify that amountAvailable is not deducted as per order
assertCurl 200 "curl -k http://$HOST:$PORT/payment-service/api/customers/$CUSTOMER_ID"
assertEqual 90 $(echo ${RESPONSE} | jq .amountAvailable)
}
set -e
echo "Start Tests:" `date`
echo "HOST=${HOST}"
echo "PORT=${PORT}"
if [[ $@ == *"start"* ]]
then
echo "Restarting the test environment..."
echo "$ docker compose -f docker-compose.yml down --remove-orphans -v"
docker compose -f deployment/docker-compose.yml down --remove-orphans -v
echo "$ docker compose up -d"
docker compose -f deployment/docker-compose.yml up -d
fi
if [[ $@ == *"setup"* ]]
then
echo "Restarting the test environment..."
echo "$ docker compose -f docker-compose-tools.yml down --remove-orphans -v"
docker compose -f deployment/docker-compose-tools.yml down --remove-orphans -v
echo "$ docker compose up -d"
docker compose -f deployment/docker-compose-tools.yml up -d
fi
waitForService curl -k http://${HOST}:${PORT}/actuator/health
# waiting for services to come up
echo "Sleeping for 60 sec for services to start"
sleep 60
waitForService curl -k http://${HOST}:${PORT}/CATALOG-SERVICE/catalog-service/actuator/health
waitForService curl -k http://${HOST}:${PORT}/INVENTORY-SERVICE/inventory-service/actuator/health
waitForService curl -k http://${HOST}:${PORT}/ORDER-SERVICE/order-service/actuator/health
waitForService curl -k http://${HOST}:${PORT}/PAYMENT-SERVICE/payment-service/actuator/health
setupTestData
verifyAPIs
echo "End, all tests OK:" `date`
if [[ $@ == *"stop"* ]]
then
echo "We are done, stopping the test environment..."
echo "$ docker compose -f docker-compose.yml down --remove-orphans -v"
docker compose -f deployment/docker-compose.yml down --remove-orphans -v
fi
if [[ $@ == *"teardown"* ]]
then
echo "We are done, stopping the test environment..."
echo "$ docker compose -f docker-compose-tools.yml down --remove-orphans -v"
docker compose -f deployment/docker-compose-tools.yml down --remove-orphans -v
fi