Merge "App config path changed"
[nonrtric.git] / test / common / agent_api_functions.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2020 Nordix Foundation. All rights reserved.
5 #  ========================================================================
6 #  Licensed under the Apache License, Version 2.0 (the "License");
7 #  you may not use this file except in compliance with the License.
8 #  You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #  Unless required by applicable law or agreed to in writing, software
13 #  distributed under the License is distributed on an "AS IS" BASIS,
14 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #  See the License for the specific language governing permissions and
16 #  limitations under the License.
17 #  ============LICENSE_END=================================================
18 #
19
20 # This is a script that contains specific test functions for Policy Agent API
21
22 ### API functiond towards the Policy Agent
23
24 # Generic function to query the agent via the REST or DMAAP interface.
25 # Used by all other agent api test functions
26 # If operation prefix is '_BATCH' the the send and get response is split in two sequences,
27 # one for sending the requests and one for receiving the response
28 # but only when using the DMAAP interface
29 # REST or DMAAP is controlled of the base url of $ADAPTER
30 # arg: (GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (RESPONSE <correlation-id>)
31 # (Not for test scripts)
32 __do_curl_to_agent() {
33     echo "(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
34         paramError=0
35
36     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
37                 paramError=1
38     else
39                 timeout=""
40                 oper=""
41                 file=''
42                 httpcode=" -sw %{http_code}"
43                 accept=''
44                 content=''
45                 batch=0
46                 if [[ $1 == *"_BATCH" ]]; then
47                         batch=1
48                 fi
49                 if [ $# -gt 2 ]; then
50                         content=" -H Content-Type:application/json"
51                 fi
52                 if [ $1 == "GET" ] || [ $1 == "GET_BATCH" ]; then
53                         oper="GET"
54                         if [ $# -ne 2 ]; then
55                                 paramError=1
56                         fi
57                 elif [ $1 == "PUT" ] || [ $1 == "PUT_BATCH" ]; then
58                         oper="PUT"
59                         if [ $# -eq 3 ]; then
60                                 file=" --data-binary @$3"
61                         fi
62                         accept=" -H accept:application/json"
63                 elif [ $1 == "POST" ] || [ $1 == "POST_BATCH" ]; then
64                         oper="POST"
65                         accept=" -H accept:*/*"
66                         if [ $# -ne 2 ]; then
67                                 paramError=1
68                         fi
69                 elif [ $1 == "DELETE" ] || [ $1 == "DELETE_BATCH" ]; then
70                         oper="DELETE"
71                         if [ $# -ne 2 ]; then
72                                 paramError=1
73                         fi
74                 elif [ $1 == "RESPONSE" ]; then
75                         oper="RESPONSE"
76                         if [ $# -ne 2 ]; then
77                                 paramError=1
78                         fi
79                         if ! [ $ADAPTER == $DMAAPBASE ]; then
80                                 paramError=1
81                         fi
82                 else
83                         paramError=1
84                 fi
85         fi
86
87     if [ $paramError -eq 1 ]; then
88                 ((RES_CONF_FAIL++))
89         echo "-Incorrect number of parameters to __do_curl_agent " $@ >> $HTTPLOG
90         echo "-Expected: (GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (RESPONSE <correlation-id>) [<file>]" >> $HTTPLOG
91         echo "-Returning response 000" >> $HTTPLOG
92         echo "-000"
93         return 1
94     fi
95
96     if [ $ADAPTER == $RESTBASE ] || [ $ADAPTER == $RESTBASE_SECURE ]; then
97         url=" "${ADAPTER}${2}
98         oper=" -X "$oper
99         curlString="curl -k "${oper}${timeout}${httpcode}${accept}${content}${url}${file}
100         echo " CMD: "$curlString >> $HTTPLOG
101                 if [ $# -eq 3 ]; then
102                         echo " FILE: $(<$3)" >> $HTTPLOG
103                 fi
104
105                 # Do retry for configured response codes, otherwise only one attempt
106                 maxretries=5
107                 while [ $maxretries -ge 0 ]; do
108
109                         let maxretries=maxretries-1
110                         res=$($curlString)
111                         retcode=$?
112                         if [ $retcode -ne 0 ]; then
113                                 echo " RETCODE: "$retcode >> $HTTPLOG
114                                 echo "000"
115                                 return 1
116                         fi
117                         retry=0
118                         echo " RESP: "$res >> $HTTPLOG
119                         status=${res:${#res}-3}
120                         if [ ! -z "${AGENT_RETRY_CODES}" ]; then
121                                 for retrycode in $AGENT_RETRY_CODES; do
122                                         if [ $retrycode -eq $status ]; then
123                                                 echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED  >> $HTTPLOG
124                                                 sleep 1
125                                                 retry=1
126                                         fi
127                                 done
128                         fi
129                         if [ $retry -eq 0 ]; then
130                                 maxretries=-1
131                         fi
132                 done
133         echo $res
134         return 0
135     else
136                 if [ $oper != "RESPONSE" ]; then
137                         requestUrl=$2
138                         if [ $1 == "PUT" ] && [ $# -eq 3 ]; then
139                                 payload="$(cat $3 | tr -d '\n' | tr -d ' ' )"
140                                 echo "payload: "$payload >> $HTTPLOG
141                                 file=" --data-binary "$payload
142                         fi
143                         #urlencode the request url since it will be carried by send-request url
144                         requestUrl=$(python3 -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))"  "$2")
145                         url=" "${ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
146                         curlString="curl -X POST${timeout}${httpcode}${content}${url}${file}"
147                         echo " CMD: "$curlString >> $HTTPLOG
148                         res=$($curlString)
149                         retcode=$?
150                         if [ $retcode -ne 0 ]; then
151                                 echo " RETCODE: "$retcode >> $HTTPLOG
152                                 echo "000"
153                                 return 1
154                         fi
155                         echo " RESP: "$res >> $HTTPLOG
156                         status=${res:${#res}-3}
157                         if [ $status -ne 200 ]; then
158                                 echo "000"
159                                 return 1
160                         fi
161                         cid=${res:0:${#res}-3}
162                         if [[ $batch -eq 1 ]]; then
163                                 echo $cid"200"
164                                 return 0
165                         fi
166                 fi
167                 if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then
168                         if [ $oper == "RESPONSE" ]; then
169                                 cid=$2
170                         fi
171                         url=" "${ADAPTER}"/receive-response?correlationid="${cid}
172                         curlString="curl -X GET"${timeout}${httpcode}${url}
173                         echo " CMD: "$curlString >> $HTTPLOG
174                         res=$($curlString)
175                         retcode=$?
176                         if [ $retcode -ne 0 ]; then
177                                 echo " RETCODE: "$retcode >> $HTTPLOG
178                                 echo "000"
179                                 return 1
180                         fi
181                         echo " RESP: "$res >> $HTTPLOG
182                         status=${res:${#res}-3}
183                         TS=$SECONDS
184                         # wait of the reply from the agent...
185                         while [ $status -eq 204 ]; do
186                                 if [ $(($SECONDS - $TS)) -gt 90 ]; then
187                                         echo " RETCODE: (timeout after 90s)" >> $HTTPLOG
188                                         echo "000"
189                                         return 1
190                                 fi
191                                 sleep 0.01
192                                 echo " CMD: "$curlString >> $HTTPLOG
193                                 res=$($curlString)
194                                 if [ $retcode -ne 0 ]; then
195                                         echo " RETCODE: "$retcode >> $HTTPLOG
196                                         echo "000"
197                                         return 1
198                                 fi
199                                 echo " RESP: "$res >> $HTTPLOG
200                                 status=${res:${#res}-3}
201                         done
202                         if [ $status -eq 200 ]; then
203                                 body=${res:0:${#res}-3}
204                                 echo $body
205                                 return 0
206                         fi
207                         echo "Status not 200, returning response 000" >> $HTTPLOG
208                         echo "0000"
209                         return 1
210                 fi
211     fi
212 }
213
214
215 #########################################################
216 #### Test case functions A1 Policy management service
217 #########################################################
218
219 # This function compare the size, towards a target value, of a json array returned from <url> of the Policy Agent.
220 # This is done immediately by setting PASS or FAIL or wait up to and optional timeout before setting PASS or FAIL
221 # args: json:<url> <target-value> [<timeout-in-seconds]
222 # (Function for test scripts)
223 api_equal() {
224     echo "(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
225         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
226                 if [[ $1 == "json:"* ]]; then
227                         __var_test "Policy Agent" $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT"/" $1 "=" $2 $3
228                         return 0
229                 fi
230         fi
231
232         ((RES_CONF_FAIL++))
233         __print_err "needs two or three args: json:<json-array-param> <target-value> [ timeout ]" $@
234         return 1
235 }
236
237 # API Test function: GET /policies
238 # args: <response-code> <ric-id>|NORIC <service-id>|NOSERVICE <policy-ype-id>|NOTYPE [ NOID | [<policy-id> <ric-id> <service-id> EMPTY|<policy-type-id> <template-file>]*]
239 # (Function for test scripts)
240 api_get_policies() {
241         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
242     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
243         ((RES_TEST++))
244         paramError=0
245         if [ $# -lt 4 ]; then
246                 paramError=1
247         elif [ $# -eq 5 ] && [ $5 != "NOID" ]; then
248                 paramError=1
249         elif [ $# -gt 4 ] && [ $(($#%5)) -ne 4 ]; then
250                 paramError=1
251         fi
252
253     if [ $paramError -ne 0 ]; then
254         __print_err "<response-code> <ric-id>|NORIC <service-id>|NOSERVICE <policy-ype-id>|NOTYPE [ NOID | [<policy-id> <ric-id> <service-id> EMPTY|<policy-type-id> <template-file>]*]" $@
255         return 1
256     fi
257         queryparams=""
258         if [ $2 != "NORIC" ]; then
259                 queryparams="?ric="$2
260         fi
261         if [ $3 != "NOSERVICE" ]; then
262                 if [ -z $queryparams ]; then
263                         queryparams="?service="$3
264                 else
265                         queryparams=$queryparams"&service="$3
266                 fi
267         fi
268         if [ $4 != "NOTYPE" ]; then
269                 if [ -z $queryparams ]; then
270                         queryparams="?type="$4
271                 else
272                         queryparams=$queryparams"&type="$4
273                 fi
274         fi
275
276         query="/policies"$queryparams
277     res="$(__do_curl_to_agent GET $query)"
278     status=${res:${#res}-3}
279
280         if [ $status -ne $1 ]; then
281                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
282                 ((RES_FAIL++))
283                 return 1
284         fi
285
286         if [ $# -gt 4 ]; then
287                 if [ $# -eq 5 ] && [ $5 == "NOID" ]; then
288                         targetJson="["
289                 else
290                         body=${res:0:${#res}-3}
291                         targetJson="["
292                         arr=(${@:5})
293
294                         for ((i=0; i<$(($#-4)); i=i+5)); do
295
296                                 if [ "$targetJson" != "[" ]; then
297                                         targetJson=$targetJson","
298                                 fi
299                                 targetJson=$targetJson"{\"id\":\"${arr[$i]}\",\"lastModified\":\"????\",\"ric\":\"${arr[$i+1]}\",\"service\":\"${arr[$i+2]}\",\"type\":"
300                                 if [ "${arr[$i+3]}" == "EMPTY" ]; then
301                                         targetJson=$targetJson"\"\","
302                                 else
303                                         targetJson=$targetJson"\"${arr[$i+3]}\","
304                                 fi
305                                 file=".p.json"
306                                 sed 's/XXX/'${arr[$i]}'/g' ${arr[$i+4]} > $file
307                                 json=$(cat $file)
308                                 targetJson=$targetJson"\"json\":"$json"}"
309                         done
310                 fi
311
312                 targetJson=$targetJson"]"
313                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
314                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
315
316                 if [ $res -ne 0 ]; then
317                         echo -e $RED" FAIL, returned body not correct"$ERED
318                         ((RES_FAIL++))
319                         return 1
320                 fi
321         fi
322
323         ((RES_PASS++))
324         echo -e $GREEN" PASS"$EGREEN
325         return 0
326
327 }
328
329 # API Test function: GET /policy
330 #args: <response-code>  <policy-id> [<template-file>]
331 # (Function for test scripts)
332 api_get_policy() {
333         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
334     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
335         ((RES_TEST++))
336
337     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
338         __print_err "<response-code>  <policy-id> [<template-file>] " $@
339         return 1
340     fi
341
342         query="/policy?id=$2"
343         res="$(__do_curl_to_agent GET $query)"
344         status=${res:${#res}-3}
345
346         if [ $status -ne $1 ]; then
347                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
348                 ((RES_FAIL++))
349                 return 1
350         fi
351
352         if [ $# -eq 3 ]; then
353                 #Create a policy json to compare with
354                 body=${res:0:${#res}-3}
355                 file=".p.json"
356                 sed 's/XXX/'${2}'/g' $3 > $file
357                 targetJson=$(< $file)
358                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
359                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
360                 if [ $res -ne 0 ]; then
361                         echo -e $RED" FAIL, returned body not correct"$ERED
362                         ((RES_FAIL++))
363                         return 1
364                 fi
365         fi
366
367         ((RES_PASS++))
368         echo -e $GREEN" PASS"$EGREEN
369         return 0
370 }
371
372 # API Test function: PUT /policy
373 # args: <response-code> <service-name> <ric-id> <policytype-id> <policy-id> <transient> <template-file> [<count>]
374 # (Function for test scripts)
375 api_put_policy() {
376         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
377     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
378         ((RES_TEST++))
379
380     if [ $# -lt 7 ] || [ $# -gt 8 ]; then
381         __print_err "<response-code> <service-name> <ric-id> <policytype-id> <policy-id> <transient> <template-file> [<count>]" $@
382         return 1
383     fi
384
385         ric=$3
386         count=0
387         max=1
388
389         if [ $# -eq 8 ]; then
390                 max=$8
391         fi
392
393         pid=$5
394         file=$7
395
396         while [ $count -lt $max ]; do
397                 query="/policy?id=$pid&ric=$ric&service=$2"
398
399                 if [ $4 != "NOTYPE" ]; then
400                         query=$query"&type=$4"
401                 fi
402
403                 if [ $6 != NOTRANSIENT ]; then
404                         query=$query"&transient=$6"
405                 fi
406
407                 file=".p.json"
408                 sed 's/XXX/'${pid}'/g' $7 > $file
409         res="$(__do_curl_to_agent PUT $query $file)"
410         status=${res:${#res}-3}
411                 echo -ne " Creating "$count"("$max")${SAMELINE}"
412                 if [ $status -ne $1 ]; then
413                         let pid=$pid+1
414                         echo " Created "$count"?("$max")"
415                         echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
416                         ((RES_FAIL++))
417                         return 1
418                 fi
419
420                 let pid=$pid+1
421                 let count=$count+1
422                 echo -ne " Created  "$count"("$max")${SAMELINE}"
423         done
424         echo ""
425
426         ((RES_PASS++))
427         echo -e $GREEN" PASS"$EGREEN
428         return 0
429 }
430
431 # API Test function: PUT /policy to run in batch
432 # args: <response-code> <service-name> <ric-id> <policytype-id> <policy-id> <transient> <template-file> [<count>]
433 # (Function for test scripts)
434 api_put_policy_batch() {
435         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
436     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
437         ((RES_TEST++))
438
439     if [ $# -lt 7 ] || [ $# -gt 8 ]; then
440         __print_err "<response-code> <service-name> <ric-id> <policytype-id> <policy-id> <transient> <template-file> [<count>]" $@
441         return 1
442     fi
443
444         ric=$3
445         count=0
446         max=1
447
448         if [ $# -eq 8 ]; then
449                 max=$8
450         fi
451
452         pid=$5
453         file=$7
454         ARR=""
455         while [ $count -lt $max ]; do
456                 query="/policy?id=$pid&ric=$ric&service=$2"
457
458                 if [ $4 != "NOTYPE" ]; then
459                         query=$query"&type=$4"
460                 fi
461
462                 if [ $6 != NOTRANSIENT ]; then
463                         query=$query"&transient=$6"
464                 fi
465
466                 file=".p.json"
467                 sed 's/XXX/'${pid}'/g' $7 > $file
468         res="$(__do_curl_to_agent PUT_BATCH $query $file)"
469         status=${res:${#res}-3}
470                 echo -ne " Requested(batch) "$count"("$max")${SAMELINE}"
471
472                 if [ $status -ne 200 ]; then
473                         let pid=$pid+1
474                         echo " Requested(batch) "$count"?("$max")"
475                         echo -e $RED" FAIL. Exepected status 200 (in request), got "$status $ERED
476                         ((RES_FAIL++))
477                         return 1
478                 fi
479                 cid=${res:0:${#res}-3}
480                 ARR=$ARR" "$cid
481                 let pid=$pid+1
482                 let count=$count+1
483                 echo -ne " Requested(batch)  "$count"("$max")${SAMELINE}"
484         done
485
486         echo ""
487         count=0
488         for cid in $ARR; do
489
490         res="$(__do_curl_to_agent RESPONSE $cid)"
491         status=${res:${#res}-3}
492                 echo -ne " Created(batch) "$count"("$max")${SAMELINE}"
493
494                 if [ $status -ne $1 ]; then
495                         let pid=$pid+1
496                         echo " Created(batch) "$count"?("$max")"
497                         echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
498                         ((RES_FAIL++))
499                         return 1
500                 fi
501
502                 let count=$count+1
503                 echo -ne " Created(batch)  "$count"("$max")${SAMELINE}"
504         done
505
506         echo ""
507
508         ((RES_PASS++))
509         echo -e $GREEN" PASS"$EGREEN
510         return 0
511 }
512
513 # API Test function: PUT /policy to run in i parallel for a number of rics
514 # args: <response-code> <service-name> <ric-id-base> <number-of-rics> <policytype-id> <policy-start-id> <transient> <template-file> <count-per-ric> <number-of-threads>
515 # (Function for test scripts)
516 api_put_policy_parallel() {
517         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
518     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
519         ((RES_TEST++))
520
521     if [ $# -ne 10 ]; then
522         __print_err " <response-code> <service-name> <ric-id-base> <number-of-rics> <policytype-id> <policy-start-id> <transient> <template-file> <count-per-ric> <number-of-threads>" $@
523         return 1
524     fi
525         resp_code=$1; shift;
526         serv=$1; shift
527         ric_base=$1; shift;
528         num_rics=$1; shift;
529         type=$1; shift;
530         start_id=$1; shift;
531         transient=$1; shift;
532         template=$1; shift;
533         count=$1; shift;
534         pids=$1; shift;
535
536         if [ $ADAPTER != $RESTBASE ] && [ $ADAPTER != $RESTBASE_SECURE ]; then
537                 echo " Info - api_put_policy_parallel uses only the agent REST interface - create over dmaap in parallel is not supported"
538                 echo " Info - will execute over agent REST"
539         fi
540
541         if [ $serv == "NOSERVICE" ]; then
542                 serv=""
543         fi
544         query="/policy?service=$serv"
545
546         if [ $type != "NOTYPE" ]; then
547                 query=$query"&type=$type"
548         fi
549
550         if [ $transient != NOTRANSIENT ]; then
551                 query=$query"&transient=$transient"
552         fi
553
554         urlbase=${ADAPTER}${query}
555
556         for ((i=1; i<=$pids; i++))
557         do
558                 echo "" > ".pid${i}.res.txt"
559                 echo $resp_code $urlbase $ric_base $num_rics $start_id $template $count $pids $i > ".pid${i}.txt"
560                 echo $i
561         done  | xargs -n 1 -I{} -P $pids bash -c '{
562                 arg=$(echo {})
563                 echo " Parallel process $arg started"
564                 tmp=$(< ".pid${arg}.txt")
565                 python3 ../common/create_policies_process.py $tmp > .pid${arg}.res.txt
566         }'
567         msg=""
568         for ((i=1; i<=$pids; i++))
569         do
570                 file=".pid${i}.res.txt"
571                 tmp=$(< $file)
572                 if [ -z "$tmp" ]; then
573                         echo " Process $i : unknown result (result file empty"
574                         msg="failed"
575                 else
576                         res=${tmp:0:1}
577                         if [ $res == "0" ]; then
578                                 echo " Process $i : OK"
579                         else
580                                 echo " Process $i : failed - "${tmp:1}
581                                 msg="failed"
582                         fi
583                 fi
584         done
585         if [ -z $msg ]; then
586                 echo " $(($count*$num_rics)) policies created/updated"
587                 ((RES_PASS++))
588                 echo -e $GREEN" PASS"$EGREEN
589                 return 0
590         fi
591
592         echo -e $RED" FAIL. One of more processes failed to execute" $ERED
593         ((RES_FAIL++))
594         return 1
595 }
596
597 # API Test function: DELETE /policy
598 # args: <response-code> <policy-id> [count]
599 # (Function for test scripts)
600 api_delete_policy() {
601         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
602     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
603         ((RES_TEST++))
604
605     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
606         __print_err "<response-code> <policy-id> [count]" $@
607         return 1
608     fi
609
610         count=0
611         max=1
612
613         if [ $# -eq 3 ]; then
614                 max=$3
615         fi
616
617         pid=$2
618
619         while [ $count -lt $max ]; do
620                 query="/policy?id="$pid
621                 res="$(__do_curl_to_agent DELETE $query)"
622                 status=${res:${#res}-3}
623                 echo -ne " Deleting "$count"("$max")${SAMELINE}"
624
625                 if [ $status -ne $1 ]; then
626                         echo " Deleted "$count"?("$max")"
627                         echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
628                         ((RES_FAIL++))
629                         return 1
630                 fi
631                 let pid=$pid+1
632                 let count=$count+1
633                 echo -ne " Deleted  "$count"("$max")${SAMELINE}"
634         done
635         echo ""
636
637         ((RES_PASS++))
638         echo -e $GREEN" PASS"$EGREEN
639         return 0
640 }
641
642 # API Test function: DELETE /policy to run in batch
643 # args: <response-code> <policy-id> [count]
644 # (Function for test scripts)
645 api_delete_policy_batch() {
646         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
647     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
648         ((RES_TEST++))
649
650     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
651         __print_err "<response-code> <policy-id> [count]" $@
652         return 1
653     fi
654
655         count=0
656         max=1
657
658         if [ $# -eq 3 ]; then
659                 max=$3
660         fi
661
662         pid=$2
663         ARR=""
664         while [ $count -lt $max ]; do
665                 query="/policy?id="$pid
666                 res="$(__do_curl_to_agent DELETE_BATCH $query)"
667                 status=${res:${#res}-3}
668                 echo -ne " Requested(batch) "$count"("$max")${SAMELINE}"
669
670                 if [ $status -ne 200 ]; then
671                         let pid=$pid+1
672                         echo " Requested(batch) "$count"?("$max")"
673                         echo -e $RED" FAIL. Exepected status 200 (in request), got "$status $ERED
674                         ((RES_FAIL++))
675                         return 1
676                 fi
677                 cid=${res:0:${#res}-3}
678                 ARR=$ARR" "$cid
679                 let pid=$pid+1
680                 let count=$count+1
681                 echo -ne " Requested(batch)  "$count"("$max")${SAMELINE}"
682         done
683
684         echo ""
685
686         count=0
687         for cid in $ARR; do
688
689         res="$(__do_curl_to_agent RESPONSE $cid)"
690         status=${res:${#res}-3}
691                 echo -ne " Deleted(batch) "$count"("$max")${SAMELINE}"
692
693                 if [ $status -ne $1 ]; then
694                         let pid=$pid+1
695                         echo " Deleted(batch) "$count"?("$max")"
696                         echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
697                         ((RES_FAIL++))
698                         return 1
699                 fi
700
701                 let count=$count+1
702                 echo -ne " Deleted(batch)  "$count"("$max")${SAMELINE}"
703         done
704
705         ((RES_PASS++))
706         echo -e $GREEN" PASS"$EGREEN
707         return 0
708 }
709
710 # API Test function: DELETE /policy to run in i parallel for a number of rics
711 # args: <response-code> <number-of-rics> <policy-start-id> <count-per-ric> <number-of-threads>
712 # (Function for test scripts)
713 api_delete_policy_parallel() {
714         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
715     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
716         ((RES_TEST++))
717
718     if [ $# -ne 5 ]; then
719         __print_err " <response-code> <ric-id-base> <number-of-rics> <policy-start-id> <count-per-ric> <number-of-threads>" $@
720         return 1
721     fi
722         resp_code=$1; shift;
723         num_rics=$1; shift;
724         start_id=$1; shift;
725         count=$1; shift;
726         pids=$1; shift;
727
728         if [ $ADAPTER != $RESTBASE ] && [ $ADAPTER != $RESTBASE_SECURE ]; then
729                 echo " Info - api_delete_policy_parallel uses only the agent REST interface - create over dmaap in parallel is not supported"
730                 echo " Info - will execute over agent REST"
731         fi
732
733         query="/policy"
734
735         urlbase=${ADAPTER}${query}
736
737         for ((i=1; i<=$pids; i++))
738         do
739                 echo "" > ".pid${i}.del.res.txt"
740                 echo $resp_code $urlbase $num_rics $start_id $count $pids $i > ".pid${i}.del.txt"
741                 echo $i
742         done  | xargs -n 1 -I{} -P $pids bash -c '{
743                 arg=$(echo {})
744                 echo " Parallel process $arg started"
745                 tmp=$(< ".pid${arg}.del.txt")
746                 python3 ../common/delete_policies_process.py $tmp > .pid${arg}.del.res.txt
747         }'
748         msg=""
749         for ((i=1; i<=$pids; i++))
750         do
751                 file=".pid${i}.del.res.txt"
752                 tmp=$(< $file)
753                 if [ -z "$tmp" ]; then
754                         echo " Process $i : unknown result (result file empty"
755                         msg="failed"
756                 else
757                         res=${tmp:0:1}
758                         if [ $res == "0" ]; then
759                                 echo " Process $i : OK"
760                         else
761                                 echo " Process $i : failed - "${tmp:1}
762                                 msg="failed"
763                         fi
764                 fi
765         done
766         if [ -z $msg ]; then
767                 echo " $(($count*$num_rics)) deleted"
768                 ((RES_PASS++))
769                 echo -e $GREEN" PASS"$EGREEN
770                 return 0
771         fi
772
773         echo -e $RED" FAIL. One of more processes failed to execute" $ERED
774         ((RES_FAIL++))
775         return 1
776 }
777
778 # API Test function: GET /policy_ids
779 # args: <response-code> <ric-id>|NORIC <service-id>|NOSERVICE <type-id>|NOTYPE ([<policy-instance-id]*|NOID)
780 # (Function for test scripts)
781 api_get_policy_ids() {
782         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
783     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
784         ((RES_TEST++))
785
786     if [ $# -lt 4 ]; then
787                 __print_err "<response-code> <ric-id>|NORIC <service-id>|NOSERVICE <type-id>|NOTYPE ([<policy-instance-id]*|NOID)" $@
788                 return 1
789         fi
790
791         queryparams=""
792
793         if [ $2 != "NORIC" ]; then
794                 queryparams="?ric="$2
795         fi
796
797         if [ $3 != "NOSERVICE" ]; then
798                 if [ -z $queryparams ]; then
799                         queryparams="?service="$3
800                 else
801                         queryparams=$queryparams"&service="$3
802                 fi
803         fi
804         if [ $4 != "NOTYPE" ]; then
805                 if [ -z $queryparams ]; then
806                         queryparams="?type="$4
807                 else
808                         queryparams=$queryparams"&type="$4
809                 fi
810         fi
811
812         query="/policy_ids"$queryparams
813     res="$(__do_curl_to_agent GET $query)"
814     status=${res:${#res}-3}
815
816         if [ $status -ne $1 ]; then
817                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
818                 ((RES_FAIL++))
819                 return 1
820         fi
821
822         if [ $# -gt 4 ]; then
823                 body=${res:0:${#res}-3}
824                 targetJson="["
825
826                 for pid in ${@:5} ; do
827                         if [ "$targetJson" != "[" ]; then
828                                 targetJson=$targetJson","
829                         fi
830                         if [ $pid != "NOID" ]; then
831                                 targetJson=$targetJson"\"$pid\""
832                         fi
833                 done
834
835                 targetJson=$targetJson"]"
836                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
837                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
838
839                 if [ $res -ne 0 ]; then
840                         echo -e $RED" FAIL, returned body not correct"$ERED
841                         ((RES_FAIL++))
842                         return 1
843                 fi
844         fi
845
846         ((RES_PASS++))
847         echo -e $GREEN" PASS"$EGREEN
848         return 0
849 }
850
851 # API Test function: GET /policy_schema
852 # args: <response-code> <policy-type-id> [<schema-file>]
853 # (Function for test scripts)
854 api_get_policy_schema() {
855         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
856     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
857         ((RES_TEST++))
858
859     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
860         __print_err "<response-code> <policy-type-id> [<schema-file>]" $@
861         return 1
862     fi
863
864         query="/policy_schema?id=$2"
865         res="$(__do_curl_to_agent GET $query)"
866         status=${res:${#res}-3}
867
868         if [ $status -ne $1 ]; then
869                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
870                 ((RES_FAIL++))
871                 return 1
872         fi
873
874         if [ $# -eq 3 ]; then
875
876                 body=${res:0:${#res}-3}
877
878                 targetJson=$(< $3)
879                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
880                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
881
882                 if [ $res -ne 0 ]; then
883                         echo -e $RED" FAIL, returned body not correct"$ERED
884                         ((RES_FAIL++))
885                         return 1
886                 fi
887         fi
888
889         ((RES_PASS++))
890         echo -e $GREEN" PASS"$EGREEN
891         return 0
892 }
893
894 # API Test function: GET /policy_schemas
895 # args: <response-code>  <ric-id>|NORIC [<schema-file>|NOFILE]*
896 # (Function for test scripts)
897 api_get_policy_schemas() {
898         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
899     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
900         ((RES_TEST++))
901
902     if [ $# -lt 2 ]; then
903         __print_err "<response-code> <ric-id>|NORIC [<schema-file>|NOFILE]*" $@
904         return 1
905     fi
906
907         query="/policy_schemas"
908         if [ $2 != "NORIC" ]; then
909                 query=$query"?ric="$2
910         fi
911
912         res="$(__do_curl_to_agent GET $query)"
913         status=${res:${#res}-3}
914
915         if [ $status -ne $1 ]; then
916                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
917                 ((RES_FAIL++))
918                 return 1
919         fi
920
921         if [ $# -gt 2 ]; then
922                 body=${res:0:${#res}-3}
923                 targetJson="["
924
925                 for file in ${@:3} ; do
926                         if [ "$targetJson" != "[" ]; then
927                                 targetJson=$targetJson","
928                         fi
929                         if [ $file == "NOFILE" ]; then
930                                 targetJson=$targetJson"{}"
931                         else
932                                 targetJson=$targetJson$(< $file)
933                         fi
934                 done
935
936                 targetJson=$targetJson"]"
937                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
938                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
939
940                 if [ $res -ne 0 ]; then
941                         echo -e $RED" FAIL, returned body not correct"$ERED
942                         ((RES_FAIL++))
943                         return 1
944                 fi
945         fi
946
947         ((RES_PASS++))
948         echo -e $GREEN" PASS"$EGREEN
949         return 0
950 }
951
952 # API Test function: GET /policy_status
953 # arg: <response-code> <policy-id> (STD <enforce-status> [<reason>])|(OSC <instance-status> <has-been-deleted>)
954 # (Function for test scripts)
955 api_get_policy_status() {
956         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
957     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
958         ((RES_TEST++))
959     if [ $# -lt 4 ] || [ $# -gt 5 ]; then
960                 __print_err "<response-code> <policy-id> (STD <enforce-status> [<reason>])|(OSC <instance-status> <has-been-deleted>)" $@
961                 return 1
962         fi
963
964         targetJson=""
965
966         if [ $3 == "STD" ]; then
967                 targetJson="{\"enforceStatus\":\"$4\""
968                 if [ $# -eq 5 ]; then
969                         targetJson=$targetJson",\"reason\":\"$5\""
970                 fi
971                 targetJson=$targetJson"}"
972         elif [ $3 == "OSC" ]; then
973                 targetJson="{\"instance_status\":\"$4\""
974                 if [ $# -eq 5 ]; then
975                         targetJson=$targetJson",\"has_been_deleted\":\"$5\""
976                 fi
977                 targetJson=$targetJson",\"created_at\":\"????\"}"
978         else
979                 __print_err "<response-code> (STD <enforce-status> [<reason>])|(OSC <instance-status> <has-been-deleted>)" $@
980                 return 1
981         fi
982
983         query="/policy_status?id="$2
984
985         res="$(__do_curl_to_agent GET $query)"
986     status=${res:${#res}-3}
987
988         if [ $status -ne $1 ]; then
989                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
990                 ((RES_FAIL++))
991                 return 1
992         fi
993
994         echo "TARGET JSON: $targetJson" >> $HTTPLOG
995         body=${res:0:${#res}-3}
996         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
997
998         if [ $res -ne 0 ]; then
999                 echo -e $RED" FAIL, returned body not correct"$ERED
1000                 ((RES_FAIL++))
1001                 return 1
1002         fi
1003
1004         ((RES_PASS++))
1005         echo -e $GREEN" PASS"$EGREEN
1006         return 0
1007 }
1008
1009 # API Test function: GET /policy_types
1010 # args: <response-code> [<ric-id>|NORIC [<policy-type-id>|EMPTY [<policy-type-id>]*]]
1011 # (Function for test scripts)
1012 api_get_policy_types() {
1013         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1014     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1015         ((RES_TEST++))
1016
1017     if [ $# -lt 1 ]; then
1018                 __print_err "<response-code> [<ric-id>|NORIC [<policy-type-id>|EMPTY [<policy-type-id>]*]]" $@
1019                 return 1
1020         fi
1021
1022         if [ $# -eq 1 ]; then
1023                 query="/policy_types"
1024         elif [ $2 == "NORIC" ]; then
1025                 query="/policy_types"
1026         else
1027                 query="/policy_types?ric=$2"
1028         fi
1029
1030     res="$(__do_curl_to_agent GET $query)"
1031     status=${res:${#res}-3}
1032
1033         if [ $status -ne $1 ]; then
1034                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1035                 ((RES_FAIL++))
1036                 return 1
1037         fi
1038
1039         if [ $# -gt 2 ]; then
1040                 body=${res:0:${#res}-3}
1041                 targetJson="["
1042
1043                 for pid in ${@:3} ; do
1044                         if [ "$targetJson" != "[" ]; then
1045                                 targetJson=$targetJson","
1046                         fi
1047                         if [ $pid == "EMPTY" ]; then
1048                                 pid=""
1049                         fi
1050                         targetJson=$targetJson"\"$pid\""
1051                 done
1052
1053                 targetJson=$targetJson"]"
1054                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1055                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1056
1057                 if [ $res -ne 0 ]; then
1058                         echo -e $RED" FAIL, returned body not correct"$ERED
1059                         ((RES_FAIL++))
1060                         return 1
1061                 fi
1062         fi
1063
1064         ((RES_PASS++))
1065         echo -e $GREEN" PASS"$EGREEN
1066         return 0
1067 }
1068
1069 #########################################################
1070 #### Test case functions Health check
1071 #########################################################
1072
1073 # API Test function: GET /status
1074 # args: <response-code>
1075 # (Function for test scripts)
1076 api_get_status() {
1077         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1078     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1079         ((RES_TEST++))
1080     if [ $# -ne 1 ]; then
1081                 __print_err "<response-code>" $@
1082                 return 1
1083         fi
1084     query="/status"
1085     res="$(__do_curl_to_agent GET $query)"
1086     status=${res:${#res}-3}
1087
1088         if [ $status -ne $1 ]; then
1089                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1090                 ((RES_FAIL++))
1091                 return 1
1092         fi
1093
1094         ((RES_PASS++))
1095         echo -e $GREEN" PASS"$EGREEN
1096         return 0
1097 }
1098
1099 #########################################################
1100 #### Test case functions RIC Repository
1101 #########################################################
1102
1103 # API Test function: GET /ric
1104 # args: <reponse-code> <management-element-id> [<ric-id>]
1105 # (Function for test scripts)
1106 api_get_ric() {
1107         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1108     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1109         ((RES_TEST++))
1110     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
1111                 __print_err "<reponse-code> <management-element-id> [<ric-id>]" $@
1112                 return 1
1113         fi
1114
1115         query="/ric?managedElementId="$2
1116
1117     res="$(__do_curl_to_agent GET $query)"
1118     status=${res:${#res}-3}
1119
1120         if [ $status -ne $1 ]; then
1121                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1122                 ((RES_FAIL++))
1123                 return 1
1124         fi
1125
1126         if [ $# -eq 3 ]; then
1127                 body=${res:0:${#res}-3}
1128                 if [ "$body" != "$3" ]; then
1129                         echo -e $RED" FAIL, returned body not correct"$ERED
1130                         ((RES_FAIL++))
1131                         return 1
1132                 fi
1133         fi
1134
1135         ((RES_PASS++))
1136         echo -e $GREEN" PASS"$EGREEN
1137         return 0
1138 }
1139
1140 # API test function: GET /rics
1141 # args: <reponse-code> <policy-type-id>|NOTYPE [<space-separate-string-of-ricinfo>]
1142 # example of <space-separate-string-of-ricinfo> = "ricsim_g1_1:me1_ricsim_g1_1,me2_ricsim_g1_1:1,2,4 ricsim_g1_1:me2_........."
1143 # format of ric-info:  <ric-id>:<list-of-mes>:<list-of-policy-type-ids>
1144 # (Function for test scripts)
1145 api_get_rics() {
1146         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1147     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1148         ((RES_TEST++))
1149
1150     if [ $# -lt 2 ]; then
1151                 __print_err "<reponse-code> <policy-type-id>|NOTYPE [<space-separate-string-of-ricinfo>]" $@
1152                 return 1
1153         fi
1154
1155         query="/rics"
1156         if [ $2 != "NOTYPE" ]; then
1157         query="/rics?policyType="$2
1158         fi
1159
1160     res="$(__do_curl_to_agent GET $query)"
1161     status=${res:${#res}-3}
1162
1163         if [ $status -ne $1 ]; then
1164                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1165                 ((RES_FAIL++))
1166                 return 1
1167         fi
1168
1169         if [ $# -gt 2 ]; then
1170                 body=${res:0:${#res}-3}
1171                 res=$(python3 ../common/create_rics_json.py ".tmp_rics.json" "$3" )
1172                 if [ $res -ne 0 ]; then
1173                         echo -e $RED" FAIL, could not create target ric info json"$ERED
1174                         ((RES_FAIL++))
1175                         return 1
1176                 fi
1177
1178                 targetJson=$(<.tmp_rics.json)
1179         echo "TARGET JSON: $targetJson" >> $HTTPLOG
1180                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1181                 if [ $res -ne 0 ]; then
1182                         echo -e $RED" FAIL, returned body not correct"$ERED
1183                         ((RES_FAIL++))
1184                         return 1
1185                 fi
1186         fi
1187
1188         ((RES_PASS++))
1189         echo -e $GREEN" PASS"$EGREEN
1190         return 0
1191 }
1192
1193 ##################################################################
1194 #### API Test case functions Service registry and supervision ####
1195 ##################################################################
1196
1197 # API test function: PUT /service
1198 # args: <response-code>  <service-name> <keepalive-timeout> <callbackurl>
1199 # (Function for test scripts)
1200 api_put_service() {
1201         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1202     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1203         ((RES_TEST++))
1204     if [ $# -ne 4 ]; then
1205         __print_err "<response-code>  <service-name> <keepalive-timeout> <callbackurl>" $@
1206         return 1
1207     fi
1208
1209     query="/service"
1210     json="{\"callbackUrl\": \""$4"\",\"keepAliveIntervalSeconds\": \""$3"\",\"serviceName\": \""$2"\"}"
1211     file=".tmp.json"
1212         echo "$json" > $file
1213
1214     res="$(__do_curl_to_agent PUT $query $file)"
1215     status=${res:${#res}-3}
1216
1217         if [ $status -ne $1 ]; then
1218                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1219                 ((RES_FAIL++))
1220                 return 1
1221         fi
1222
1223         ((RES_PASS++))
1224         echo -e $GREEN" PASS"$EGREEN
1225         return 0
1226 }
1227
1228 # API test function: GET /services
1229 #args: <response-code> [ (<query-service-name> <target-service-name> <keepalive-timeout> <callbackurl>) | (NOSERVICE <target-service-name> <keepalive-timeout> <callbackurl> [<target-service-name> <keepalive-timeout> <callbackurl>]* )]
1230 # (Function for test scripts)
1231 api_get_services() {
1232         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1233     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1234         ((RES_TEST++))
1235         #Number of accepted parameters: 1, 2, 4, 7, 10, 13,...
1236         paramError=1
1237         if [ $# -eq 1 ]; then
1238                 paramError=0
1239         elif [ $# -eq 2 ] && [ $2 != "NOSERVICE" ]; then
1240                 paramError=0
1241         elif [ $# -eq 5 ]; then
1242                 paramError=0
1243         elif [ $# -gt 5 ] && [ $2 == "NOSERVICE" ]; then
1244                 argLen=$(($#-2))
1245                 if [ $(($argLen%3)) -eq 0 ]; then
1246                         paramError=0
1247                 fi
1248         fi
1249
1250     if [ $paramError -ne 0 ]; then
1251                 __print_err "<response-code> [ (<query-service-name> <target-service-name> <keepalive-timeout> <callbackurl>) | (NOSERVICE <target-service-name> <keepalive-timeout> <callbackurl> [<target-service-name> <keepalive-timeout> <callbackurl>]* )]" $@
1252                 return 1
1253         fi
1254
1255     query="/services"
1256
1257     if [ $# -gt 1 ] && [ $2 != "NOSERVICE" ]; then
1258         query="/services?name="$2
1259         fi
1260
1261     res="$(__do_curl_to_agent GET $query)"
1262     status=${res:${#res}-3}
1263
1264         if [ $status -ne $1 ]; then
1265                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1266                 ((RES_FAIL++))
1267                 return 1
1268         fi
1269
1270         if [ $# -gt 2 ]; then
1271                 variableArgCount=$(($#-2))
1272                 body=${res:0:${#res}-3}
1273         targetJson="["
1274                 shift; shift;
1275                 cntr=0
1276                 while [ $cntr -lt $variableArgCount ]; do
1277                         servicename=$1; shift;
1278                         timeout=$1; shift;
1279                         callback=$1; shift;
1280                         if [ $cntr -gt 0 ]; then
1281                                 targetJson=$targetJson","
1282                         fi
1283                         # timeSinceLastActivitySeconds value cannot be checked since value varies
1284                         targetJson=$targetJson"{\"serviceName\": \""$servicename"\",\"keepAliveIntervalSeconds\": "$timeout",\"timeSinceLastActivitySeconds\":\"????\",\"callbackUrl\": \""$callback"\"}"
1285                         let cntr=cntr+3
1286                 done
1287                 targetJson=$targetJson"]"
1288                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1289                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1290                 if [ $res -ne 0 ]; then
1291                         echo -e $RED" FAIL, returned body not correct"$ERED
1292                         ((RES_FAIL++))
1293                         return 1
1294                 fi
1295         fi
1296
1297         ((RES_PASS++))
1298         echo -e $GREEN" PASS"$EGREEN
1299         return 0
1300 }
1301
1302 # API test function: GET /services  (only checking service names)
1303 # args: <response-code> [<service-name>]*"
1304 # (Function for test scripts)
1305 api_get_service_ids() {
1306         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1307     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1308         ((RES_TEST++))
1309
1310     if [ $# -lt 1 ]; then
1311                 __print_err "<response-code> [<service-name>]*" $@
1312                 return 1
1313         fi
1314
1315     query="/services"
1316     res="$(__do_curl_to_agent GET $query)"
1317     status=${res:${#res}-3}
1318
1319         if [ $status -ne $1 ]; then
1320                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1321                 ((RES_FAIL++))
1322                 return 1
1323         fi
1324
1325         body=${res:0:${#res}-3}
1326         targetJson="["
1327         for rapp in ${@:2} ; do
1328                 if [ "$targetJson" != "[" ]; then
1329                         targetJson=$targetJson","
1330                 fi
1331                 targetJson=$targetJson"{\"callbackUrl\":\"????\",\"keepAliveIntervalSeconds\":\"????\",\"serviceName\":\""$rapp"\",\"timeSinceLastActivitySeconds\":\"????\"}"
1332         done
1333
1334         targetJson=$targetJson"]"
1335         echo "TARGET JSON: $targetJson" >> $HTTPLOG
1336         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1337
1338         if [ $res -ne 0 ]; then
1339                 echo -e $RED" FAIL, returned body not correct"$ERED
1340                 ((RES_FAIL++))
1341                 return 1
1342         fi
1343
1344         ((RES_PASS++))
1345         echo -e $GREEN" PASS"$EGREEN
1346         return 0
1347 }
1348
1349 # API test function: DELETE /services
1350 # args: <response-code> <service-name>
1351 # (Function for test scripts)
1352 api_delete_services() {
1353         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1354     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1355         ((RES_TEST++))
1356
1357     if [ $# -ne 2 ]; then
1358                 __print_err "<response-code> <service-name>" $@
1359                 return 1
1360         fi
1361
1362     query="/services?name="$2
1363     res="$(__do_curl_to_agent DELETE $query)"
1364     status=${res:${#res}-3}
1365
1366         if [ $status -ne $1 ]; then
1367                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1368                 ((RES_FAIL++))
1369                 return 1
1370         fi
1371
1372         ((RES_PASS++))
1373         echo -e $GREEN" PASS"$EGREEN
1374         return 0
1375 }
1376
1377 # API test function: PUT /services/keepalive
1378 # args: <response-code> <service-name>
1379 # (Function for test scripts)
1380 api_put_services_keepalive() {
1381         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1382     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1383         ((RES_TEST++))
1384
1385     if [ $# -ne 2 ]; then
1386                 __print_err "<response-code> <service-name>" $@
1387                 return 1
1388         fi
1389
1390     query="/services/keepalive?name="$2
1391     res="$(__do_curl_to_agent PUT $query)"
1392     status=${res:${#res}-3}
1393
1394         if [ $status -ne $1 ]; then
1395                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1396                 ((RES_FAIL++))
1397                 return 1
1398         fi
1399
1400         ((RES_PASS++))
1401         echo -e $GREEN" PASS"$EGREEN
1402         return 0
1403 }
1404