-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestAPIs.sh
More file actions
274 lines (229 loc) · 6.84 KB
/
testAPIs.sh
File metadata and controls
274 lines (229 loc) · 6.84 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
#!/bin/bash
starttime=$(date +%s)
# We need cURL
if [ ! -x "$(which curl)" ]; then
echo "Couldn't find cURL. Please install cURL to run this script"
exit 1
fi
bold=$(tput bold)
normal=$(tput sgr0)
calledByName=$0
APIAddress="http://localhost:3000"
currentAPI="init"
currentOperation="none"
delay=2
startAssetID=$RANDOM
assetIDOne="Asset-$startAssetID"
startAssetID=$(($startAssetID + 1))
assetIDTwo="Asset-$startAssetID"
# Some utility functions for formatting the output and handling state
border () {
local str=$1
local len=${#str}
local i
for (( i = 0; i < len + 4; ++i )); do
printf '-'
done
printf "\n| $bold%s$normal |\n" "$str"
for (( i = 0; i < len + 4; ++i )); do
printf '-'
done
echo
}
printSep () {
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' "+"
}
APIStart () {
currentAPI=$1
border "$1"
}
APIComplete () {
echo ""
echo "[Completed $currentAPI requests successfully]"
echo ""
printSep
}
operationStart () {
currentOperation=$1
echo ""
#printSep
echo "$bold$1$normal"
#printSep
}
operationEnd () {
if [ $? -ne 0 ]; then
echo ""
echo "Error trying to execute operation [$currentOperation] - $currentAPI"
exit 1
fi
sleep $delay
}
# END utility functions
function usage {
echo "A shell script that runs through the DBoM APIs."
echo ""
echo "usage: $calledByName [-hd] [-u gw-url]"
echo " -h get help"
echo " -d delay delay between steps in seconds [2s]"
echo " -u gw-url specify url to gateway [http://localhost:3000]"
printSep
}
while getopts ":hd:u:" opt; do
case ${opt} in
h ) usage
exit 0
;;
d )
delay=${OPTARG}
;;
u )
APIAddress=${OPTARG}
;;
\? ) echo "Invalid option: -$OPTARG" >&2
echo ""
usage
exit 1
;;
esac
done
echo "Using random asset IDs $assetIDOne and $assetIDTwo."
## START CREATE API TEST
APIStart "Create API"
operationStart "Create $assetIDOne on Channel C1"
curl -f --location --request POST "$APIAddress/api/v1/repo/DB1/chan/C1/asset/$assetIDOne" \
--header 'Content-Type: application/json' \
--data-raw '{
"standardVersion": 1.0,
"documentName": "Test Asset 01",
"documentCreator": "DBoM Organisation",
"documentCreatedDate": "2020-10-01T10:06:47+0000",
"assetType": "HardwareComponent",
"assetSubType": "SubType1",
"assetManufacturer": "DBoM Organisation",
"assetModelNumber": "ABCXYZ",
"assetDescription": "A DBoM Asset",
"assetMetadata": {
"aKey": "aValue"
},
"manufactureSignature": "UNSIGNED(TEST)"
}'
operationEnd
operationStart "Create $assetIDTwo on Channel C1"
curl -f --location --request POST "$APIAddress/api/v1/repo/DB1/chan/C1/asset/$assetIDTwo" \
--header 'Content-Type: application/json' \
--data-raw '{
"standardVersion": 1.0,
"documentName": "Test Asset 02",
"documentCreator": "DBoM Organisation",
"documentCreatedDate": "2020-10-01T10:06:47+0000",
"assetType": "HardwareComponent",
"assetSubType": "SubType1",
"assetManufacturer": "DBoM Organisation",
"assetModelNumber": "ABCXYZ",
"assetDescription": "A DBoM Asset",
"assetMetadata": {
"aKey": "aValue"
},
"manufactureSignature": "UNSIGNED(TEST)"
}'
operationEnd
APIComplete
## END CREATE API TEST
## START RETRIEVE API TEST
APIStart "Retrieve API"
operationStart "Retrieve $assetIDOne from Channel C1"
curl -f --location --request GET "$APIAddress/api/v1/repo/DB1/chan/C1/asset/$assetIDOne"
operationEnd
operationStart "Retrieve $assetIDTwo from Channel C1"
curl -f --location --request GET "$APIAddress/api/v1/repo/DB1/chan/C1/asset/$assetIDTwo"
operationEnd
APIComplete
## END RETRIEVE API TEST
## START UPDATE API TEST
APIStart "Update API"
operationStart "Update $assetIDOne on Channel C1"
curl -f --location --request PUT "$APIAddress/api/v1/repo/DB1/chan/C1/asset/$assetIDOne" \
--header 'Content-Type: application/json' \
--data-raw '{
"standardVersion": 1.0,
"documentName": "Test Asset 01 Updated",
"documentCreator": "DBoM Organisation",
"documentCreatedDate": "2020-10-01T10:06:47+0000",
"assetType": "HardwareComponent",
"assetSubType": "SubType1",
"assetManufacturer": "DBoM Organisation",
"assetModelNumber": "ABCXYZ",
"assetDescription": "A DBoM Asset",
"assetMetadata": {
"aKey": "aValue"
},
"manufactureSignature": "UNSIGNED(TEST)"
}'
operationEnd
APIComplete
## END UPDATE API TEST
## START ATTACH AND DETACH API TESTS
APIStart "Attach and Detach APIs"
operationStart "Attaching $assetIDTwo [Parent] to $assetIDOne [Child]"
curl -f --location --request POST "$APIAddress/api/v1/repo/DB1/chan/C1/asset/$assetIDTwo/attach" \
--header 'Content-Type: application/json' \
--data-raw "{
\"role\": \"SomeRole\",
\"subRole\": \"SomeSubRole\",
\"repoID\": \"DB1\",
\"channelID\": \"C1\",
\"assetID\": \"$assetIDOne\"
}"
operationEnd
operationStart "Detaching $assetIDTwo [Parent] from $assetIDOne [Child]"
curl -f --location --request POST "$APIAddress/api/v1/repo/DB1/chan/C1/asset/$assetIDTwo/detach" \
--header 'Content-Type: application/json' \
--data-raw "{
\"repoID\": \"DB1\",
\"channelID\": \"C1\",
\"assetID\": \"$assetIDOne\"
}"
operationEnd
operationStart "Attaching $assetIDOne [Parent] to $assetIDTwo [Child]"
curl -f --location --request POST "$APIAddress/api/v1/repo/DB1/chan/C1/asset/$assetIDOne/attach" \
--header 'Content-Type: application/json' \
--data-raw "{
\"role\": \"SomeRole\",
\"subRole\": \"SomeSubRole\",
\"repoID\": \"DB1\",
\"channelID\": \"C1\",
\"assetID\": \"$assetIDTwo\"
}"
operationEnd
APIComplete
## END ATTACH AND DETACH API TESTS
## START TRANSFER API TEST
APIStart "Transfer API"
operationStart "Transfer Asset $assetIDOne from Channel C1 to Channel C2"
curl --location --request POST "$APIAddress/api/v1/repo/DB1/chan/C1/asset/$assetIDOne/transfer" \
--header 'Content-Type: application/json' \
--data-raw "{
\"transferDescription\": \"transferred\",
\"repoID\": \"DB1\",
\"channelID\": \"C2\",
\"assetID\": \"$assetIDOne\"
}"
operationEnd
APIComplete
## END TRANSFER API TEST
## START AUDIT API TEST
APIStart "Audit API"
operationStart "Retrieve audit trail of $assetIDOne from Channel C1 (pre transfer)"
curl -f --location --request GET "$APIAddress/api/v1/repo/DB1/chan/C1/asset/$assetIDOne/trail"
operationEnd
APIComplete
## END AUDIT API TEST
## START EXPORT API TEST
APIStart "Export API"
operationStart "Export asset graph from $assetIDOne, now transferred to C2"
curl -f --location --request GET "$APIAddress/api/v1/repo/DB1/chan/C2/asset/$assetIDOne/export"
operationEnd
APIComplete
## END EXPORT API TEST
echo "All requests completed successfully!"
echo "Total execution time : $(($(date +%s)-starttime)) secs."