README and function documentation updates
[nonrtric.git] / test / common / ecs_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 container/service management functions and test functions for ECS
21
22 ## Access to ECS
23 # Host name may be changed if app started by kube
24 # Direct access
25 ECS_HTTPX="http"
26 ECS_HOST_NAME=$LOCALHOST_NAME
27 ECS_PATH=$ECS_HTTPX"://"$ECS_HOST_NAME":"$ECS_EXTERNAL_PORT
28
29 # ECS_ADAPTER used for switch between REST and DMAAP (only REST supported currently)
30 ECS_ADAPTER_TYPE="REST"
31 ECS_ADAPTER=$ECS_PATH
32
33 # Make curl retries towards ECS for http response codes set in this env var, space separated list of codes
34 ECS_RETRY_CODES=""
35
36 ###########################
37 ### ECS functions
38 ###########################
39
40 # All calls to ECS will be directed to the ECS REST interface from now on
41 # args: -
42 # (Function for test scripts)
43 use_ecs_rest_http() {
44         echo -e $BOLD"ECS protocol setting"$EBOLD
45         echo -e " Using $BOLD http $EBOLD and $BOLD REST $EBOLD towards ECS"
46         ECS_HTTPX="http"
47         ECS_PATH=$ECS_HTTPX"://"$ECS_HOST_NAME":"$ECS_EXTERNAL_PORT
48
49         ECS_ADAPTER_TYPE="REST"
50         ECS_ADAPTER=$ECS_PATH
51         echo ""
52 }
53
54 # All calls to ECS will be directed to the ECS REST interface from now on
55 # args: -
56 # (Function for test scripts)
57 use_ecs_rest_https() {
58         echo -e $BOLD"ECS protocol setting"$EBOLD
59         echo -e " Using $BOLD https $EBOLD and $BOLD REST $EBOLD towards ECS"
60         ECS_HTTPX="https"
61         ECS_PATH=$ECS_HTTPX"://"$ECS_HOST_NAME":"$ECS_EXTERNAL_SECURE_PORT
62
63         ECS_ADAPTER_TYPE="REST"
64         ECS_ADAPTER=$ECS_PATH
65         echo ""
66 }
67
68 # All calls to ECS will be directed to the ECS dmaap interface over http from now on
69 # args: -
70 # (Function for test scripts)
71 use_ecs_dmaap_http() {
72         echo -e $BOLD"ECS dmaap protocol setting"$EBOLD
73         echo -e $RED" - NOT SUPPORTED - "$ERED
74         echo -e " Using $BOLD http $EBOLD and $BOLD DMAAP $EBOLD towards ECS"
75         ECS_ADAPTER_TYPE="MR-HTTP"
76         echo ""
77 }
78
79 # All calls to ECS will be directed to the ECS dmaap interface over https from now on
80 # args: -
81 # (Function for test scripts)
82 use_ecs_dmaap_https() {
83         echo -e $BOLD"RICSIM protocol setting"$EBOLD
84         echo -e $RED" - NOT SUPPORTED - "$ERED
85         echo -e " Using $BOLD https $EBOLD and $BOLD REST $EBOLD towards ECS"
86         ECS_ADAPTER_TYPE="MR-HTTPS"
87         echo ""
88 }
89
90 # Start the ECS
91 # args: PROXY|NOPROXY <config-file>
92 # (Function for test scripts)
93 start_ecs() {
94
95         echo -e $BOLD"Starting $ECS_DISPLAY_NAME"$EBOLD
96
97         if [ $RUNMODE == "KUBE" ]; then
98
99                 # Check if app shall be fully managed by the test script
100                 __check_included_image "ECS"
101                 retcode_i=$?
102
103                 # Check if app shall only be used by the testscipt
104                 __check_prestarted_image "ECS"
105                 retcode_p=$?
106
107                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
108                         echo -e $RED"The $ECS_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
109                         echo -e $RED"The $ECS_APP_NAME will not be started"$ERED
110                         exit
111                 fi
112                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
113                         echo -e $RED"The $ECS_APP_NAME app is included both as managed and prestarted in this test script"$ERED
114                         echo -e $RED"The $ECS_APP_NAME will not be started"$ERED
115                         exit
116                 fi
117
118
119                 if [ $retcode_p -eq 0 ]; then
120                         echo -e " Using existing $ECS_APP_NAME deployment and service"
121                         echo " Setting ECS replicas=1"
122                         __kube_scale deployment $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
123                 fi
124
125                 # Check if app shall be fully managed by the test script
126                 if [ $retcode_i -eq 0 ]; then
127                         echo -e " Creating $ECS_APP_NAME app and expose service"
128
129                         #Check if nonrtric namespace exists, if not create it
130                         __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
131
132                         export ECS_APP_NAME
133                         export KUBE_NONRTRIC_NAMESPACE
134                         export ECS_IMAGE
135                         export ECS_INTERNAL_PORT
136                         export ECS_INTERNAL_SECURE_PORT
137                         export ECS_EXTERNAL_PORT
138                         export ECS_EXTERNAL_SECURE_PORT
139                         export ECS_CONFIG_MOUNT_PATH
140                         export ECS_CONFIG_CONFIGMAP_NAME=$ECS_APP_NAME"-config"
141                         export ECS_DATA_CONFIGMAP_NAME=$ECS_APP_NAME"-data"
142                         export ECS_CONTAINER_MNT_DIR
143
144                         if [ $1 == "PROXY" ]; then
145                                 ECS_HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_CONFIG_PORT  #Set if proxy is started
146                                 ECS_HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_CONFIG_HOST_NAME #Set if proxy is started
147                                 if [ $ECS_HTTP_PROXY_CONFIG_PORT -eq 0 ] || [ -z "$ECS_HTTP_PROXY_CONFIG_HOST_NAME" ]; then
148                                         echo -e $YELLOW" Warning: HTTP PROXY will not be configured, proxy app not started"$EYELLOW
149                                 else
150                                         echo " Configured with http proxy"
151                                 fi
152                         else
153                                 ECS_HTTP_PROXY_CONFIG_PORT=0
154                                 ECS_HTTP_PROXY_CONFIG_HOST_NAME=""
155                                 echo " Configured without http proxy"
156                         fi
157                         export ECS_HTTP_PROXY_CONFIG_PORT
158                         export ECS_HTTP_PROXY_CONFIG_HOST_NAME
159
160                         # Create config map for config
161                         datafile=$PWD/tmp/$ECS_CONFIG_FILE
162                         cp $2 $datafile
163                         output_yaml=$PWD/tmp/ecs_cfc.yaml
164                         __kube_create_configmap $ECS_CONFIG_CONFIGMAP_NAME $KUBE_NONRTRIC_NAMESPACE autotest ECS $datafile $output_yaml
165
166                         # Create pvc
167                         input_yaml=$SIM_GROUP"/"$ECS_COMPOSE_DIR"/"pvc.yaml
168                         output_yaml=$PWD/tmp/ecs_pvc.yaml
169                         __kube_create_instance pvc $ECS_APP_NAME $input_yaml $output_yaml
170
171                         # Create service
172                         input_yaml=$SIM_GROUP"/"$ECS_COMPOSE_DIR"/"svc.yaml
173                         output_yaml=$PWD/tmp/ecs_svc.yaml
174                         __kube_create_instance service $ECS_APP_NAME $input_yaml $output_yaml
175
176                         # Create app
177                         input_yaml=$SIM_GROUP"/"$ECS_COMPOSE_DIR"/"app.yaml
178                         output_yaml=$PWD/tmp/ecs_app.yaml
179                         __kube_create_instance app $ECS_APP_NAME $input_yaml $output_yaml
180                 fi
181
182                 echo " Retrieving host and ports for service..."
183                 ECS_HOST_NAME=$(__kube_get_service_host $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
184                 ECS_EXTERNAL_PORT=$(__kube_get_service_port $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE "http")
185                 ECS_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE "https")
186
187                 echo " Host IP, http port, https port: $ECS_HOST_NAME $ECS_EXTERNAL_PORT $ECS_EXTERNAL_SECURE_PORT"
188
189                 if [ $ECS_HTTPX == "http" ]; then
190                         ECS_PATH=$ECS_HTTPX"://"$ECS_HOST_NAME":"$ECS_EXTERNAL_PORT
191                 else
192                         ECS_PATH=$ECS_HTTPX"://"$ECS_HOST_NAME":"$ECS_EXTERNAL_SECURE_PORT
193                 fi
194
195                 __check_service_start $ECS_APP_NAME $ECS_PATH$ECS_ALIVE_URL
196
197                 if [ $ECS_ADAPTER_TYPE == "REST" ]; then
198                         ECS_ADAPTER=$ECS_PATH
199                 fi
200         else
201                 __check_included_image 'ECS'
202                 if [ $? -eq 1 ]; then
203                         echo -e $RED"The ECS app is not included in this test script"$ERED
204                         echo -e $RED"ECS will not be started"$ERED
205                         exit 1
206                 fi
207
208                 curdir=$PWD
209                 cd $SIM_GROUP
210                 cd ecs
211                 cd $ECS_HOST_MNT_DIR
212                 #cd ..
213                 if [ -d db ]; then
214                         if [ "$(ls -A $DIR)" ]; then
215                                 echo -e $BOLD" Cleaning files in mounted dir: $PWD/db"$EBOLD
216                                 rm -rf db/*  &> /dev/null
217                                 if [ $? -ne 0 ]; then
218                                         echo -e $RED" Cannot remove database files in: $PWD"$ERED
219                                         exit 1
220                                 fi
221                         fi
222                 else
223                         echo " No files in mounted dir or dir does not exists"
224                 fi
225                 cd $curdir
226
227                 export ECS_APP_NAME
228                 export ECS_APP_NAME_ALIAS
229                 export ECS_HOST_MNT_DIR
230                 export ECS_CONTAINER_MNT_DIR
231                 export ECS_CONFIG_MOUNT_PATH
232                 export ECS_CONFIG_FILE
233                 export ECS_INTERNAL_PORT
234                 export ECS_EXTERNAL_PORT
235                 export ECS_INTERNAL_SECURE_PORT
236                 export ECS_EXTERNAL_SECURE_PORT
237                 export DOCKER_SIM_NWNAME
238
239                 if [ $1 == "PROXY" ]; then
240                         ECS_HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_CONFIG_PORT  #Set if proxy is started
241                         ECS_HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_CONFIG_HOST_NAME #Set if proxy is started
242                         if [ $ECS_HTTP_PROXY_CONFIG_PORT -eq 0 ] || [ -z "$ECS_HTTP_PROXY_CONFIG_HOST_NAME" ]; then
243                                 echo -e $YELLOW" Warning: HTTP PROXY will not be configured, proxy app not started"$EYELLOW
244                         else
245                                 echo " Configured with http proxy"
246                         fi
247                 else
248                         ECS_HTTP_PROXY_CONFIG_PORT=0
249                         ECS_HTTP_PROXY_CONFIG_HOST_NAME=""
250                         echo " Configured without http proxy"
251                 fi
252                 export ECS_HTTP_PROXY_CONFIG_PORT
253                 export ECS_HTTP_PROXY_CONFIG_HOST_NAME
254
255                 dest_file=$SIM_GROUP/$ECS_COMPOSE_DIR/$ECS_HOST_MNT_DIR/$ECS_CONFIG_FILE
256
257                 envsubst < $2 > $dest_file
258
259                 __start_container $ECS_COMPOSE_DIR NODOCKERARGS 1 $ECS_APP_NAME
260
261                 __check_service_start $ECS_APP_NAME $ECS_PATH$ECS_ALIVE_URL
262         fi
263         echo ""
264         return 0
265 }
266
267 # Restart ECS
268 # args: -
269 # (Function for test scripts)
270 restart_ecs() {
271         echo -e $BOLD"Re-starting ECS"$EBOLD
272         docker restart $ECS_APP_NAME &> ./tmp/.dockererr
273         if [ $? -ne 0 ]; then
274                 __print_err "Could not restart $ECS_APP_NAME" $@
275                 cat ./tmp/.dockererr
276                 ((RES_CONF_FAIL++))
277                 return 1
278         fi
279
280         __check_service_start $ECS_APP_NAME $ECS_PATH$ECS_ALIVE_URL
281         echo ""
282         return 0
283 }
284
285 # Turn on debug level tracing in ECS
286 # args: -
287 # (Function for test scripts)
288 set_ecs_debug() {
289         echo -e $BOLD"Setting ecs debug logging"$EBOLD
290         curlString="$ECS_PATH$ECS_ACTUATOR -X POST  -H Content-Type:application/json -d {\"configuredLevel\":\"debug\"}"
291         result=$(__do_curl "$curlString")
292         if [ $? -ne 0 ]; then
293                 __print_err "Could not set debug mode" $@
294                 ((RES_CONF_FAIL++))
295                 return 1
296         fi
297         echo ""
298         return 0
299 }
300
301 # Turn on trace level tracing in ECS
302 # args: -
303 # (Function for test scripts)
304 set_ecs_trace() {
305         echo -e $BOLD"Setting ecs trace logging"$EBOLD
306         curlString="$ECS_PATH/actuator/loggers/org.oransc.enrichment -X POST  -H Content-Type:application/json -d {\"configuredLevel\":\"trace\"}"
307         result=$(__do_curl "$curlString")
308         if [ $? -ne 0 ]; then
309                 __print_err "Could not set trace mode" $@
310                 ((RES_CONF_FAIL++))
311                 return 1
312         fi
313         echo ""
314         return 0
315 }
316
317 # Perform curl retries when making direct call to ECS for the specified http response codes
318 # Speace separated list of http response codes
319 # args: [<response-code>]*
320 use_ecs_retries() {
321         echo -e $BOLD"Do curl retries to the ECS REST inteface for these response codes:$@"$EBOLD
322         ECS_RETRY_CODES=$@
323         echo ""
324         return 0
325 }
326
327
328 # Tests if a variable value in the ECS is equal to a target value and and optional timeout.
329 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
330 # equal to the target or not.
331 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
332 # before setting pass or fail depending on if the variable value becomes equal to the target
333 # value or not.
334 # (Function for test scripts)
335 ecs_equal() {
336         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
337                 __var_test ECS "$ECS_PATH/" $1 "=" $2 $3
338         else
339                 __print_err "Wrong args to ecs_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
340         fi
341 }
342
343
344 ##########################################
345 ######### A1-E Enrichment  API ##########
346 ##########################################
347 #Function prefix: ecs_api_a1
348
349 # API Test function: GET /A1-EI​/v1​/eitypes​/{eiTypeId}​/eijobs
350 # args: <response-code> <type-id>  <owner-id>|NOOWNER [ EMPTY | <job-id>+ ]
351 # args (flat uri structure): <response-code> <type-id>|NOTYPE  <owner-id>|NOOWNER [ EMPTY | <job-id>+ ]
352 # (Function for test scripts)
353 ecs_api_a1_get_job_ids() {
354         __log_test_start $@
355
356         if [ -z "$FLAT_A1_EI" ]; then
357                 # Valid number of parameters 4,5,6 etc
358         if [ $# -lt 3 ]; then
359                         __print_err "<response-code> <type-id>  <owner-id>|NOOWNER [ EMPTY | <job-id>+ ]" $@
360                         return 1
361                 fi
362         else
363                 echo -e $YELLOW"USING NOT CONFIRMED INTERFACE - FLAT URI STRUCTURE"$EYELLOW
364                 # Valid number of parameters 4,5,6 etc
365         if [ $# -lt 3 ]; then
366                         __print_err "<response-code> <type-id>|NOTYPE  <owner-id>|NOOWNER [ EMPTY | <job-id>+ ]" $@
367                         return 1
368                 fi
369         fi
370         search=""
371         if [ $3 != "NOWNER" ]; then
372                 search="?owner="$3
373         fi
374
375         if [  -z "$FLAT_A1_EI" ]; then
376                 query="/A1-EI/v1/eitypes/$2/eijobs$search"
377         else
378                 if [ $2 != "NOTYPE" ]; then
379                         if [ -z "$search" ]; then
380                                 search="?eiTypeId="$2
381                         else
382                                 search=$search"&eiTypeId="$2
383                         fi
384                 fi
385                 query="/A1-EI/v1/eijobs$search"
386         fi
387     res="$(__do_curl_to_api ECS GET $query)"
388     status=${res:${#res}-3}
389
390         if [ $status -ne $1 ]; then
391                 __log_test_fail_status_code $1 $status
392                 return 1
393         fi
394
395         if [ $# -gt 3 ]; then
396                 body=${res:0:${#res}-3}
397                 targetJson="["
398
399                 for pid in ${@:4} ; do
400                         if [ "$targetJson" != "[" ]; then
401                                 targetJson=$targetJson","
402                         fi
403                         if [ $pid != "EMPTY" ]; then
404                                 targetJson=$targetJson"\"$pid\""
405                         fi
406                 done
407
408                 targetJson=$targetJson"]"
409                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
410                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
411
412                 if [ $res -ne 0 ]; then
413                         __log_test_fail_body
414                         return 1
415                 fi
416         fi
417
418         __log_test_pass
419         return 0
420 }
421
422 # API Test function: GET ​/A1-EI​/v1​/eitypes​/{eiTypeId}
423 # args: <response-code> <type-id> [<schema-file>]
424 # (Function for test scripts)
425 ecs_api_a1_get_type() {
426         __log_test_start $@
427
428     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
429                 __print_err "<response-code> <type-id> [<schema-file>]" $@
430                 return 1
431         fi
432
433         query="/A1-EI/v1/eitypes/$2"
434     res="$(__do_curl_to_api ECS GET $query)"
435     status=${res:${#res}-3}
436
437         if [ $status -ne $1 ]; then
438                 __log_test_fail_status_code $1 $status
439                 return 1
440         fi
441
442         if [ $# -eq 3 ]; then
443                 body=${res:0:${#res}-3}
444                 if [ -f $3 ]; then
445                         schema=$(cat $3)
446                 else
447                         __log_test_fail_general "Schema file "$3", does not exist"
448                         return 1
449                 fi
450                 if [ -z "$FLAT_A1_EI" ]; then
451                         targetJson="{\"eiJobParametersSchema\":$schema}"
452                 else
453                         targetJson=$schema
454                 fi
455                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
456                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
457
458                 if [ $res -ne 0 ]; then
459                         __log_test_fail_body
460                         return 1
461                 fi
462         fi
463
464         __log_test_pass
465         return 0
466 }
467
468 # API Test function: GET /A1-EI/v1/eitypes
469 # args: <response-code> [ (EMPTY | [<type-id>]+) ]
470 # (Function for test scripts)
471 ecs_api_a1_get_type_ids() {
472         __log_test_start $@
473
474     if [ $# -lt 1 ]; then
475                 __print_err "<response-code> [ (EMPTY | [<type-id>]+) ]" $@
476                 return 1
477         fi
478
479         query="/A1-EI/v1/eitypes"
480     res="$(__do_curl_to_api ECS GET $query)"
481     status=${res:${#res}-3}
482
483         if [ $status -ne $1 ]; then
484                 __log_test_fail_status_code $1 $status
485                 return 1
486         fi
487         if [ $# -gt 1 ]; then
488                 body=${res:0:${#res}-3}
489                 targetJson="["
490                 if [ $2 != "EMPTY" ]; then
491                         for pid in ${@:2} ; do
492                                 if [ "$targetJson" != "[" ]; then
493                                         targetJson=$targetJson","
494                                 fi
495                                 targetJson=$targetJson"\"$pid\""
496                         done
497                 fi
498                 targetJson=$targetJson"]"
499                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
500                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
501
502                 if [ $res -ne 0 ]; then
503                         __log_test_fail_body
504                         return 1
505                 fi
506         fi
507
508         __log_test_pass
509         return 0
510 }
511
512 # API Test function: GET ​/A1-EI​/v1​/eitypes​/{eiTypeId}​/eijobs​/{eiJobId}​/status
513 # args: <response-code> <type-id> <job-id> [<status>]
514 # args (flat uri structure): <response-code> <job-id> [<status> [<timeout>]]
515 # (Function for test scripts)
516 ecs_api_a1_get_job_status() {
517         __log_test_start $@
518
519         if [ -z "$FLAT_A1_EI" ]; then
520                 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
521                         __print_err "<response-code> <type-id> <job-id> [<status>]" $@
522                         return 1
523                 fi
524
525                 query="/A1-EI/v1/eitypes/$2/eijobs/$3/status"
526
527                 res="$(__do_curl_to_api ECS GET $query)"
528                 status=${res:${#res}-3}
529
530                 if [ $status -ne $1 ]; then
531                         __log_test_fail_status_code $1 $status
532                         return 1
533                 fi
534                 if [ $# -eq 4 ]; then
535                         body=${res:0:${#res}-3}
536                         targetJson="{\"operationalState\": \"$4\"}"
537                         echo " TARGET JSON: $targetJson" >> $HTTPLOG
538                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
539
540                         if [ $res -ne 0 ]; then
541                                 __log_test_fail_body
542                                 return 1
543                         fi
544                 fi
545         else
546                 echo -e $YELLOW"USING NOT CONFIRMED INTERFACE - FLAT URI STRUCTURE"$EYELLOW
547                 if [ $# -lt 2 ] && [ $# -gt 4 ]; then
548                         __print_err "<response-code> <job-id> [<status> [<timeout>]]" $@
549                         return 1
550                 fi
551
552                 query="/A1-EI/v1/eijobs/$2/status"
553
554                 start=$SECONDS
555                 for (( ; ; )); do
556                         res="$(__do_curl_to_api ECS GET $query)"
557                         status=${res:${#res}-3}
558
559                         if [ $# -eq 4 ]; then
560                                 duration=$((SECONDS-start))
561                                 echo -ne " Response=${status} after ${duration} seconds, waiting for ${3} ${SAMELINE}"
562                                 if [ $duration -gt $4 ]; then
563                                         echo ""
564                                         duration=-1  #Last iteration
565                                 fi
566                         else
567                                 duration=-1 #single test, no wait
568                         fi
569
570                         if [ $status -ne $1 ]; then
571                                 if [ $duration -eq -1 ]; then
572                                         __log_test_fail_status_code $1 $status
573                                         return 1
574                                 fi
575                         fi
576                         if [ $# -ge 3 ] && [ $status -eq $1 ]; then
577                                 body=${res:0:${#res}-3}
578                                 targetJson="{\"eiJobStatus\": \"$3\"}"
579                                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
580                                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
581
582                                 if [ $res -ne 0 ]; then
583                                         if [ $duration -eq -1 ]; then
584                                                 __log_test_fail_body
585                                                 return 1
586                                         fi
587                                 else
588                                         duration=-1  #Goto pass
589                                 fi
590                         fi
591                         if [ $duration -eq -1 ]; then
592                                 if [ $# -eq 4 ]; then
593                                         echo ""
594                                 fi
595                                 __log_test_pass
596                                 return 0
597                         else
598                                 sleep 1
599                         fi
600                 done
601         fi
602
603         __log_test_pass
604         return 0
605 }
606
607 # API Test function: GET ​/A1-EI​/v1​/eitypes​/{eiTypeId}​/eijobs​/{eiJobId}
608 # args: <response-code> <type-id> <job-id> [<target-url> <owner-id> <template-job-file>]
609 # args (flat uri structure): <response-code> <job-id> [<type-id> <target-url> <owner-id> <template-job-file>]
610 # (Function for test scripts)
611 ecs_api_a1_get_job() {
612         __log_test_start $@
613
614         if [  -z "$FLAT_A1_EI" ]; then
615                 if [ $# -ne 3 ] && [ $# -ne 6 ]; then
616                         __print_err "<response-code> <type-id> <job-id> [<target-url> <owner-id> <template-job-file>]" $@
617                         return 1
618                 fi
619                 query="/A1-EI/v1/eitypes/$2/eijobs/$3"
620         else
621                 echo -e $YELLOW"USING NOT CONFIRMED INTERFACE - FLAT URI STRUCTURE"$EYELLOW
622                 if [ $# -ne 2 ] && [ $# -ne 7 ]; then
623                         __print_err "<response-code> <job-id> [<type-id> <target-url> <owner-id> <notification-url> <template-job-file>]" $@
624                         return 1
625                 fi
626                 query="/A1-EI/v1/eijobs/$2"
627         fi
628     res="$(__do_curl_to_api ECS GET $query)"
629     status=${res:${#res}-3}
630
631         if [ $status -ne $1 ]; then
632                 __log_test_fail_status_code $1 $status
633                 return 1
634         fi
635
636         if [  -z "$FLAT_A1_EI" ]; then
637                 if [ $# -eq 6 ]; then
638                         body=${res:0:${#res}-3}
639
640                         if [ -f $6 ]; then
641                                 jobfile=$(cat $6)
642                                 jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
643                         else
644                                 _log_test_fail_general "Job template file "$6", does not exist"
645                                 return 1
646                         fi
647                         targetJson="{\"targetUri\": \"$4\",\"jobOwner\": \"$5\",\"jobParameters\": $jobfile}"
648                         echo " TARGET JSON: $targetJson" >> $HTTPLOG
649                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
650
651                         if [ $res -ne 0 ]; then
652                                 __log_test_fail_body
653                                 return 1
654                         fi
655                 fi
656         else
657                 if [ $# -eq 7 ]; then
658                         body=${res:0:${#res}-3}
659
660                         if [ -f $7 ]; then
661                                 jobfile=$(cat $7)
662                                 jobfile=$(echo "$jobfile" | sed "s/XXXX/$2/g")
663                         else
664                                 _log_test_fail_general "Job template file "$6", does not exist"
665                                 return 1
666                         fi
667                         targetJson="{\"eiTypeId\": \"$3\", \"jobResultUri\": \"$4\",\"jobOwner\": \"$5\",\"jobStatusNotificationUri\": \"$6\",\"jobDefinition\": $jobfile}"
668                         echo " TARGET JSON: $targetJson" >> $HTTPLOG
669                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
670
671                         if [ $res -ne 0 ]; then
672                                 __log_test_fail_body
673                                 return 1
674                         fi
675                 fi
676         fi
677
678         __log_test_pass
679         return 0
680 }
681
682 # API Test function: DELETE ​/A1-EI​/v1​/eitypes​/{eiTypeId}​/eijobs​/{eiJobId}
683 # args: <response-code> <type-id> <job-id>
684 # args (flat uri structure): <response-code> <job-id>
685 # (Function for test scripts)
686 ecs_api_a1_delete_job() {
687         __log_test_start $@
688
689         if [  -z "$FLAT_A1_EI" ]; then
690                 if [ $# -ne 3 ]; then
691                         __print_err "<response-code> <type-id> <job-id>" $@
692                         return 1
693                 fi
694
695                 query="/A1-EI/v1/eitypes/$2/eijobs/$3"
696         else
697                 echo -e $YELLOW"USING NOT CONFIRMED INTERFACE - FLAT URI STRUCTURE"$EYELLOW
698                 if [ $# -ne 2 ]; then
699                         __print_err "<response-code> <job-id>" $@
700                         return 1
701                 fi
702                 query="/A1-EI/v1/eijobs/$2"
703         fi
704     res="$(__do_curl_to_api ECS DELETE $query)"
705     status=${res:${#res}-3}
706
707         if [ $status -ne $1 ]; then
708                 __log_test_fail_status_code $1 $status
709                 return 1
710         fi
711
712         __log_test_pass
713         return 0
714 }
715
716 # API Test function: PUT ​/A1-EI​/v1​/eitypes​/{eiTypeId}​/eijobs​/{eiJobId}
717 # args: <response-code> <type-id> <job-id> <target-url> <owner-id> <template-job-file>
718 # args (flat uri structure): <response-code> <job-id> <type-id> <target-url> <owner-id> <notification-url> <template-job-file>
719 # (Function for test scripts)
720 ecs_api_a1_put_job() {
721         __log_test_start $@
722
723         if [  -z "$FLAT_A1_EI" ]; then
724                 if [ $# -lt 6 ]; then
725                         __print_err "<response-code> <type-id> <job-id> <target-url> <owner-id> <template-job-file>" $@
726                         return 1
727                 fi
728                 if [ -f $6 ]; then
729                         jobfile=$(cat $6)
730                         jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
731                 else
732                         _log_test_fail_general "Job template file "$6", does not exist"
733                         return 1
734                 fi
735
736                 inputJson="{\"targetUri\": \"$4\",\"jobOwner\": \"$5\",\"jobParameters\": $jobfile}"
737                 file="./tmp/.p.json"
738                 echo "$inputJson" > $file
739
740                 query="/A1-EI/v1/eitypes/$2/eijobs/$3"
741         else
742                 echo -e $YELLOW"USING NOT CONFIRMED INTERFACE - FLAT URI STRUCTURE"$EYELLOW
743                 if [ $# -lt 7 ]; then
744                         __print_err "<response-code> <job-id> <type-id> <target-url> <owner-id> <notification-url> <template-job-file>" $@
745                         return 1
746                 fi
747                 if [ -f $7 ]; then
748                         jobfile=$(cat $7)
749                         jobfile=$(echo "$jobfile" | sed "s/XXXX/$2/g")
750                 else
751                         _log_test_fail_general "Job template file "$7", does not exist"
752                         return 1
753                 fi
754
755                 inputJson="{\"eiTypeId\": \"$3\", \"jobResultUri\": \"$4\",\"jobOwner\": \"$5\",\"jobStatusNotificationUri\": \"$6\",\"jobDefinition\": $jobfile}"
756                 file="./tmp/.p.json"
757                 echo "$inputJson" > $file
758
759                 query="/A1-EI/v1/eijobs/$2"
760         fi
761
762     res="$(__do_curl_to_api ECS PUT $query $file)"
763     status=${res:${#res}-3}
764
765         if [ $status -ne $1 ]; then
766                 __log_test_fail_status_code $1 $status
767                 return 1
768         fi
769
770         __log_test_pass
771         return 0
772 }
773
774
775 ##########################################
776 ####   Enrichment Data Producer API   ####
777 ##########################################
778 # Function prefix: ecs_api_edp
779
780 # API Test function: GET /ei-producer/v1/eitypes
781 # args: <response-code> [ EMPTY | <type-id>+]
782 # (Function for test scripts)
783 ecs_api_edp_get_type_ids() {
784         __log_test_start $@
785
786     if [ $# -lt 1 ]; then
787                 __print_err "<response-code> [ EMPTY | <type-id>+]" $@
788                 return 1
789         fi
790
791         query="/ei-producer/v1/eitypes"
792     res="$(__do_curl_to_api ECS GET $query)"
793     status=${res:${#res}-3}
794
795         if [ $status -ne $1 ]; then
796                 __log_test_fail_status_code $1 $status
797                 return 1
798         fi
799
800         if [ $# -gt 1 ]; then
801                 body=${res:0:${#res}-3}
802                 targetJson="["
803                 if [ $2 != "EMPTY" ]; then
804                         for pid in ${@:2} ; do
805                                 if [ "$targetJson" != "[" ]; then
806                                         targetJson=$targetJson","
807                                 fi
808                                 targetJson=$targetJson"\"$pid\""
809                         done
810                 fi
811                 targetJson=$targetJson"]"
812                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
813                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
814
815                 if [ $res -ne 0 ]; then
816                         __log_test_fail_body
817                         return 1
818                 fi
819         fi
820
821         __log_test_pass
822         return 0
823 }
824
825 # API Test function: GET /ei-producer/v1/eiproducers/{eiProducerId}/status
826 # args: <response-code> <producer-id> [<status> [<timeout>]]
827 # (Function for test scripts)
828 ecs_api_edp_get_producer_status() {
829         __log_test_start $@
830
831     if [ $# -lt 2 ] || [ $# -gt 4 ]; then
832                 __print_err "<response-code> <producer-id> [<status> [<timeout>]]" $@
833                 return 1
834         fi
835
836         query="/ei-producer/v1/eiproducers/$2/status"
837         start=$SECONDS
838         for (( ; ; )); do
839                 res="$(__do_curl_to_api ECS GET $query)"
840                 status=${res:${#res}-3}
841
842                 if [ $# -eq 4 ]; then
843                         duration=$((SECONDS-start))
844                         echo -ne " Response=${status} after ${duration} seconds, waiting for ${3} ${SAMELINE}"
845                         if [ $duration -gt $4 ]; then
846                                 echo ""
847                                 duration=-1  #Last iteration
848                         fi
849                 else
850                         duration=-1 #single test, no wait
851                 fi
852
853                 if [ $status -ne $1 ]; then
854                         if [ $duration -eq -1 ]; then
855                                 __log_test_fail_status_code $1 $status
856                                 return 1
857                         fi
858                 fi
859                 if [ $# -ge 3 ] && [ $status -eq $1 ]; then
860                         body=${res:0:${#res}-3}
861                         targetJson="{\"operational_state\": \"$3\"}"
862                         echo " TARGET JSON: $targetJson" >> $HTTPLOG
863                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
864
865                         if [ $res -ne 0 ]; then
866                                 if [ $duration -eq -1 ]; then
867                                         __log_test_fail_body
868                                         return 1
869                                 fi
870                         else
871                                 duration=-1  #Goto pass
872                         fi
873                 fi
874                 if [ $duration -eq -1 ]; then
875                         if [ $# -eq 4 ]; then
876                                 echo ""
877                         fi
878                         __log_test_pass
879                         return 0
880                 else
881                         sleep 1
882                 fi
883         done
884 }
885
886
887 # API Test function: GET /ei-producer/v1/eiproducers
888 # args (v1_1): <response-code> [ EMPTY | <producer-id>+]
889 # (Function for test scripts)
890 ecs_api_edp_get_producer_ids() {
891         __log_test_start $@
892
893     if [ $# -lt 1 ]; then
894                 __print_err "<response-code> [ EMPTY | <producer-id>+]" $@
895                 return 1
896         fi
897
898         query="/ei-producer/v1/eiproducers"
899     res="$(__do_curl_to_api ECS GET $query)"
900     status=${res:${#res}-3}
901
902         if [ $status -ne $1 ]; then
903                 __log_test_fail_status_code $1 $status
904                 return 1
905         fi
906
907         if [ $# -gt 1 ]; then
908                 body=${res:0:${#res}-3}
909                 targetJson="["
910
911                 for pid in ${@:2} ; do
912                         if [ "$targetJson" != "[" ]; then
913                                 targetJson=$targetJson","
914                         fi
915                         if [ $pid != "EMPTY" ]; then
916                                 targetJson=$targetJson"\"$pid\""
917                         fi
918                 done
919
920                 targetJson=$targetJson"]"
921                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
922                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
923
924                 if [ $res -ne 0 ]; then
925                         __log_test_fail_body
926                         return 1
927                 fi
928         fi
929
930         __log_test_pass
931         return 0
932 }
933
934 # API Test function: GET /ei-producer/v1/eiproducers
935 # args (v1_2): <response-code> [ ( NOTYPE | <type-id> ) [ EMPTY | <producer-id>+] ]
936 # (Function for test scripts)
937 ecs_api_edp_get_producer_ids_2() {
938         __log_test_start $@
939
940     if [ $# -lt 1 ]; then
941                 __print_err "<response-code> [ ( NOTYPE | <type-id> ) [ EMPTY | <producer-id>+] ]" $@
942                 return 1
943         fi
944
945         query="/ei-producer/v1/eiproducers"
946         if [ $# -gt 1 ] && [ $2 != "NOTYPE" ]; then
947                 query=$query"?ei_type_id=$2"
948         fi
949     res="$(__do_curl_to_api ECS GET $query)"
950     status=${res:${#res}-3}
951
952         if [ $status -ne $1 ]; then
953                 __log_test_fail_status_code $1 $status
954                 return 1
955         fi
956
957         if [ $# -gt 2 ]; then
958                 body=${res:0:${#res}-3}
959                 targetJson="["
960
961                 for pid in ${@:3} ; do
962                         if [ "$targetJson" != "[" ]; then
963                                 targetJson=$targetJson","
964                         fi
965                         if [ $pid != "EMPTY" ]; then
966                                 targetJson=$targetJson"\"$pid\""
967                         fi
968                 done
969
970                 targetJson=$targetJson"]"
971                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
972                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
973
974                 if [ $res -ne 0 ]; then
975                         __log_test_fail_body
976                         return 1
977                 fi
978         fi
979
980         __log_test_pass
981         return 0
982 }
983
984 # API Test function: GET /ei-producer/v1/eitypes/{eiTypeId}
985 # args: (v1_1) <response-code> <type-id> [<job-schema-file> (EMPTY | [<producer-id>]+)]
986 # (Function for test scripts)
987 ecs_api_edp_get_type() {
988         __log_test_start $@
989
990         paramError=1
991         if [ $# -eq 2 ]; then
992                 paramError=0
993         fi
994         if [ $# -gt 3 ]; then
995                 paramError=0
996         fi
997     if [ $paramError -ne 0 ]; then
998                 __print_err "<response-code> <type-id> [<job-schema-file> 'EMPTY' | ([<producer-id>]+)]" $@
999                 return 1
1000         fi
1001
1002         query="/ei-producer/v1/eitypes/$2"
1003     res="$(__do_curl_to_api ECS GET $query)"
1004     status=${res:${#res}-3}
1005
1006         if [ $status -ne $1 ]; then
1007                 __log_test_fail_status_code $1 $status
1008                 return 1
1009         fi
1010         if [ $# -gt 3 ]; then
1011                 body=${res:0:${#res}-3}
1012
1013                 if [ -f $3 ]; then
1014                         schema=$(cat $3)
1015                 else
1016                         __log_test_fail_general "Job template file "$3", does not exist"
1017                         return 1
1018                 fi
1019
1020                 targetJson=""
1021                 if [ $4 != "EMPTY" ]; then
1022                         for pid in ${@:4} ; do
1023                                 if [ "$targetJson" != "" ]; then
1024                                         targetJson=$targetJson","
1025                                 fi
1026                                 targetJson=$targetJson"\"$pid\""
1027                         done
1028                 fi
1029                 targetJson="{\"ei_job_data_schema\":$schema, \"ei_producer_ids\": [$targetJson]}"
1030
1031                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1032                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1033
1034                 if [ $res -ne 0 ]; then
1035                         __log_test_fail_body
1036                         return 1
1037                 fi
1038         fi
1039         __log_test_pass
1040         return 0
1041 }
1042
1043 # API Test function: GET /ei-producer/v1/eitypes/{eiTypeId}
1044 # args: (v1_2) <response-code> <type-id> [<job-schema-file> ]
1045 # (Function for test scripts)
1046 ecs_api_edp_get_type_2() {
1047         __log_test_start $@
1048
1049         paramError=1
1050         if [ $# -eq 2 ]; then
1051                 paramError=0
1052         fi
1053         if [ $# -eq 3 ]; then
1054                 paramError=0
1055         fi
1056     if [ $paramError -ne 0 ]; then
1057                 __print_err "<response-code> <type-id> [<job-schema-file> ]" $@
1058                 return 1
1059         fi
1060
1061         query="/ei-producer/v1/eitypes/$2"
1062     res="$(__do_curl_to_api ECS GET $query)"
1063     status=${res:${#res}-3}
1064
1065         if [ $status -ne $1 ]; then
1066                 __log_test_fail_status_code $1 $status
1067                 return 1
1068         fi
1069         if [ $# -eq 3 ]; then
1070                 body=${res:0:${#res}-3}
1071
1072                 if [ -f $3 ]; then
1073                         schema=$(cat $3)
1074                 else
1075                         __log_test_fail_general "Job template file "$3", does not exist"
1076                         return 1
1077                 fi
1078
1079                 targetJson="{\"ei_job_data_schema\":$schema}"
1080
1081                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1082                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1083
1084                 if [ $res -ne 0 ]; then
1085                         __log_test_fail_body
1086                         return 1
1087                 fi
1088         fi
1089         __log_test_pass
1090         return 0
1091 }
1092
1093 # API Test function: PUT /ei-producer/v1/eitypes/{eiTypeId}
1094 # args: (v1_2) <response-code> <type-id> <job-schema-file>
1095 # (Function for test scripts)
1096 ecs_api_edp_put_type_2() {
1097         __log_test_start $@
1098
1099     if [ $# -ne 3 ]; then
1100                 __print_err "<response-code> <type-id> <job-schema-file>" $@
1101                 return 1
1102         fi
1103
1104         if [ ! -f $3 ]; then
1105                 __log_test_fail_general "Job schema file "$3", does not exist"
1106                 return 1
1107         fi
1108         schema=$(cat $3)
1109         input_json="{\"ei_job_data_schema\":$schema}"
1110         file="./tmp/put_type.json"
1111         echo $input_json > $file
1112
1113         query="/ei-producer/v1/eitypes/$2"
1114     res="$(__do_curl_to_api ECS PUT $query $file)"
1115     status=${res:${#res}-3}
1116
1117         if [ $status -ne $1 ]; then
1118                 __log_test_fail_status_code $1 $status
1119                 return 1
1120         fi
1121
1122         __log_test_pass
1123         return 0
1124 }
1125
1126 # API Test function: DELETE /ei-producer/v1/eitypes/{eiTypeId}
1127 # args: (v1_2) <response-code> <type-id>
1128 # (Function for test scripts)
1129 ecs_api_edp_delete_type_2() {
1130         __log_test_start $@
1131
1132     if [ $# -ne 2 ]; then
1133                 __print_err "<response-code> <type-id>" $@
1134                 return 1
1135         fi
1136
1137         query="/ei-producer/v1/eitypes/$2"
1138     res="$(__do_curl_to_api ECS DELETE $query)"
1139     status=${res:${#res}-3}
1140
1141         if [ $status -ne $1 ]; then
1142                 __log_test_fail_status_code $1 $status
1143                 return 1
1144         fi
1145
1146         __log_test_pass
1147         return 0
1148 }
1149
1150 # API Test function: GET /ei-producer/v1/eiproducers/{eiProducerId}
1151 # args: (v1_1) <response-code> <producer-id> [<job-callback> <supervision-callback> (EMPTY | [<type-id> <schema-file>]+) ]
1152 # (Function for test scripts)
1153 ecs_api_edp_get_producer() {
1154         __log_test_start $@
1155
1156         #Possible arg count: 2, 5 6, 8, 10 etc
1157         paramError=1
1158         if [ $# -eq 2 ]; then
1159                 paramError=0
1160         fi
1161         if [ $# -eq 5 ] && [ "$5" == "EMPTY" ]; then
1162                 paramError=0
1163         fi
1164         variablecount=$(($#-4))
1165         if [ $# -gt 5 ] && [ $(($variablecount%2)) -eq 0 ]; then
1166                 paramError=0
1167         fi
1168
1169     if [ $paramError -ne 0 ]; then
1170                 __print_err "<response-code> <producer-id> [<job-callback> <supervision-callback> (NOID | [<type-id> <schema-file>]+) ]" $@
1171                 return 1
1172         fi
1173
1174         query="/ei-producer/v1/eiproducers/$2"
1175     res="$(__do_curl_to_api ECS GET $query)"
1176     status=${res:${#res}-3}
1177
1178         if [ $status -ne $1 ]; then
1179                 __log_test_fail_status_code $1 $status
1180                 return 1
1181         fi
1182
1183         if [ $# -gt 2 ]; then
1184                 body=${res:0:${#res}-3}
1185                 targetJson="["
1186                 if [ $# -gt 5 ]; then
1187                         arr=(${@:5})
1188                         for ((i=0; i<$(($#-5)); i=i+2)); do
1189                                 if [ "$targetJson" != "[" ]; then
1190                                         targetJson=$targetJson","
1191                                 fi
1192                                 if [ -f ${arr[$i+1]} ]; then
1193                                         schema=$(cat ${arr[$i+1]})
1194                                 else
1195                                         _log_test_fail_general "Schema file "${arr[$i+1]}", does not exist"
1196                                         return 1
1197                                 fi
1198
1199                                 targetJson=$targetJson"{\"ei_type_identity\":\"${arr[$i]}\",\"ei_job_data_schema\":$schema}"
1200                         done
1201                 fi
1202                 targetJson=$targetJson"]"
1203                 if [ $# -gt 4 ]; then
1204                         targetJson="{\"supported_ei_types\":$targetJson,\"ei_job_callback_url\": \"$3\",\"ei_producer_supervision_callback_url\": \"$4\"}"
1205                 fi
1206                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1207                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1208
1209                 if [ $res -ne 0 ]; then
1210                         __log_test_fail_body
1211                         return 1
1212                 fi
1213         fi
1214
1215         __log_test_pass
1216         return 0
1217 }
1218
1219 # API Test function: GET /ei-producer/v1/eiproducers/{eiProducerId}
1220 # args (v1_2): <response-code> <producer-id> [<job-callback> <supervision-callback> (EMPTY | <type-id>+) ]
1221 # (Function for test scripts)
1222 ecs_api_edp_get_producer_2() {
1223         __log_test_start $@
1224
1225         #Possible arg count: 2, 5, 6, 7, 8 etc
1226         paramError=1
1227         if [ $# -eq 2 ]; then
1228                 paramError=0
1229         fi
1230         if [ $# -eq 5 ] && [ "$5" == "EMPTY" ]; then
1231                 paramError=0
1232         fi
1233         if [ $# -ge 5 ]; then
1234                 paramError=0
1235         fi
1236
1237     if [ $paramError -ne 0 ]; then
1238                 __print_err "<response-code> <producer-id> [<job-callback> <supervision-callback> (EMPTY | <type-id>+) ]" $@
1239                 return 1
1240         fi
1241
1242         query="/ei-producer/v1/eiproducers/$2"
1243     res="$(__do_curl_to_api ECS GET $query)"
1244     status=${res:${#res}-3}
1245
1246         if [ $status -ne $1 ]; then
1247                 __log_test_fail_status_code $1 $status
1248                 return 1
1249         fi
1250
1251         if [ $# -gt 2 ]; then
1252                 body=${res:0:${#res}-3}
1253                 targetJson="["
1254                 if [ $# -gt 4 ] && [ "$5" != "EMPTY" ]; then
1255                         arr=(${@:5})
1256                         for ((i=0; i<$(($#-4)); i=i+1)); do
1257                                 if [ "$targetJson" != "[" ]; then
1258                                         targetJson=$targetJson","
1259                                 fi
1260                                 targetJson=$targetJson"\"${arr[$i]}\""
1261                         done
1262                 fi
1263                 targetJson=$targetJson"]"
1264                 if [ $# -gt 4 ]; then
1265                         targetJson="{\"supported_ei_types\":$targetJson,\"ei_job_callback_url\": \"$3\",\"ei_producer_supervision_callback_url\": \"$4\"}"
1266                 fi
1267                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1268                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1269
1270                 if [ $res -ne 0 ]; then
1271                         __log_test_fail_body
1272                         return 1
1273                 fi
1274         fi
1275
1276         __log_test_pass
1277         return 0
1278 }
1279
1280 # API Test function: DELETE /ei-producer/v1/eiproducers/{eiProducerId}
1281 # args: <response-code> <producer-id>
1282 # (Function for test scripts)
1283 ecs_api_edp_delete_producer() {
1284         __log_test_start $@
1285
1286     if [ $# -lt 2 ]; then
1287                 __print_err "<response-code> <producer-id>" $@
1288                 return 1
1289         fi
1290
1291         query="/ei-producer/v1/eiproducers/$2"
1292     res="$(__do_curl_to_api ECS DELETE $query)"
1293     status=${res:${#res}-3}
1294
1295         if [ $status -ne $1 ]; then
1296                 __log_test_fail_status_code $1 $status
1297                 return 1
1298         fi
1299
1300         __log_test_pass
1301         return 0
1302 }
1303
1304 # API Test function: PUT /ei-producer/v1/eiproducers/{eiProducerId}
1305 # args: (v1_1) <response-code> <producer-id> <job-callback> <supervision-callback> NOTYPE|[<type-id> <schema-file>]+
1306 # (Function for test scripts)
1307 ecs_api_edp_put_producer() {
1308         __log_test_start $@
1309
1310         #Valid number of parametrer 5,6,8,10,
1311         paramError=1
1312         if  [ $# -eq 5 ] && [ "$5" == "NOTYPE" ]; then
1313                 paramError=0
1314         elif [ $# -gt 5 ] && [ $(($#%2)) -eq 0 ]; then
1315                 paramError=0
1316         fi
1317         if [ $paramError -ne 0 ]; then
1318                 __print_err "<response-code> <producer-id> <job-callback> <supervision-callback> NOTYPE|[<type-id> <schema-file>]+" $@
1319                 return 1
1320         fi
1321
1322         inputJson="["
1323         if [ $# -gt 5 ]; then
1324                 arr=(${@:5})
1325                 for ((i=0; i<$(($#-5)); i=i+2)); do
1326                         if [ "$inputJson" != "[" ]; then
1327                                 inputJson=$inputJson","
1328                         fi
1329                         if [ -f ${arr[$i+1]} ]; then
1330                                 schema=$(cat ${arr[$i+1]})
1331                         else
1332                                 _log_test_fail_general "Schema file "${arr[$i+1]}", does not exist"
1333                                 return 1
1334                         fi
1335                         inputJson=$inputJson"{\"ei_type_identity\":\"${arr[$i]}\",\"ei_job_data_schema\":$schema}"
1336                 done
1337         fi
1338         inputJson="\"supported_ei_types\":"$inputJson"]"
1339
1340         inputJson=$inputJson",\"ei_job_callback_url\": \"$3\",\"ei_producer_supervision_callback_url\": \"$4\""
1341
1342         inputJson="{"$inputJson"}"
1343
1344         file="./tmp/.p.json"
1345         echo "$inputJson" > $file
1346         query="/ei-producer/v1/eiproducers/$2"
1347     res="$(__do_curl_to_api ECS PUT $query $file)"
1348     status=${res:${#res}-3}
1349
1350         if [ $status -ne $1 ]; then
1351                 __log_test_fail_status_code $1 $status
1352                 return 1
1353         fi
1354
1355         __log_test_pass
1356         return 0
1357 }
1358
1359 # API Test function: PUT /ei-producer/v1/eiproducers/{eiProducerId}
1360 # args: (v1_2) <response-code> <producer-id> <job-callback> <supervision-callback> NOTYPE|[<type-id>+]
1361 # (Function for test scripts)
1362 ecs_api_edp_put_producer_2() {
1363         __log_test_start $@
1364
1365         #Valid number of parametrer 5,6,8,10,
1366         paramError=1
1367         if  [ $# -eq 5 ] && [ "$5" == "NOTYPE" ]; then
1368                 paramError=0
1369         elif [ $# -ge 5 ]; then
1370                 paramError=0
1371         fi
1372         if [ $paramError -ne 0 ]; then
1373                 __print_err "<response-code> <producer-id> <job-callback> <supervision-callback> NOTYPE|[<type-id>+]" $@
1374                 return 1
1375         fi
1376
1377         inputJson="["
1378         if [ $# -gt 4 ] && [ "$5" != "NOTYPE" ]; then
1379                 arr=(${@:5})
1380                 for ((i=0; i<$(($#-4)); i=i+1)); do
1381                         if [ "$inputJson" != "[" ]; then
1382                                 inputJson=$inputJson","
1383                         fi
1384                         inputJson=$inputJson"\""${arr[$i]}"\""
1385                 done
1386         fi
1387         inputJson="\"supported_ei_types\":"$inputJson"]"
1388
1389         inputJson=$inputJson",\"ei_job_callback_url\": \"$3\",\"ei_producer_supervision_callback_url\": \"$4\""
1390
1391         inputJson="{"$inputJson"}"
1392
1393         file="./tmp/.p.json"
1394         echo "$inputJson" > $file
1395         query="/ei-producer/v1/eiproducers/$2"
1396     res="$(__do_curl_to_api ECS PUT $query $file)"
1397     status=${res:${#res}-3}
1398
1399         if [ $status -ne $1 ]; then
1400                 __log_test_fail_status_code $1 $status
1401                 return 1
1402         fi
1403
1404         __log_test_pass
1405         return 0
1406 }
1407
1408 # API Test function: GET /ei-producer/v1/eiproducers/{eiProducerId}/eijobs
1409 # args: (V1-1) <response-code> <producer-id> (EMPTY | [<job-id> <type-id> <target-url> <job-owner> <template-job-file>]+)
1410 # (Function for test scripts)
1411 ecs_api_edp_get_producer_jobs() {
1412         __log_test_start $@
1413
1414         #Valid number of parameter 2,3,7,11
1415         paramError=1
1416         if [ $# -eq 2 ]; then
1417                 paramError=0
1418         fi
1419         if [ $# -eq 3 ] && [ "$3" == "EMPTY" ]; then
1420                 paramError=0
1421         fi
1422         variablecount=$(($#-2))
1423         if [ $# -gt 3 ] && [ $(($variablecount%5)) -eq 0 ]; then
1424                 paramError=0
1425         fi
1426         if [ $paramError -eq 1 ]; then
1427                 __print_err "<response-code> <producer-id> (EMPTY | [<job-id> <type-id> <target-url> <job-owner> <template-job-file>]+)" $@
1428                 return 1
1429         fi
1430
1431         query="/ei-producer/v1/eiproducers/$2/eijobs"
1432     res="$(__do_curl_to_api ECS GET $query)"
1433     status=${res:${#res}-3}
1434         if [ $status -ne $1 ]; then
1435                 __log_test_fail_status_code $1 $status
1436                 return 1
1437         fi
1438         if [ $# -gt 2 ]; then
1439                 body=${res:0:${#res}-3}
1440                 targetJson="["
1441                 if [ $# -gt 3 ]; then
1442                         arr=(${@:3})
1443                         for ((i=0; i<$(($#-3)); i=i+5)); do
1444                                 if [ "$targetJson" != "[" ]; then
1445                                         targetJson=$targetJson","
1446                                 fi
1447                                 if [ -f ${arr[$i+4]} ]; then
1448                                         jobfile=$(cat ${arr[$i+4]})
1449                                         jobfile=$(echo "$jobfile" | sed "s/XXXX/${arr[$i]}/g")
1450                                 else
1451                                         _log_test_fail_general "Job template file "${arr[$i+4]}", does not exist"
1452                                         return 1
1453                                 fi
1454                                 targetJson=$targetJson"{\"ei_job_identity\":\"${arr[$i]}\",\"ei_type_identity\":\"${arr[$i+1]}\",\"target_uri\":\"${arr[$i+2]}\",\"owner\":\"${arr[$i+3]}\",\"ei_job_data\":$jobfile}"
1455                         done
1456                 fi
1457                 targetJson=$targetJson"]"
1458
1459                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1460                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1461
1462                 if [ $res -ne 0 ]; then
1463                         __log_test_fail_body
1464                         return 1
1465                 fi
1466         fi
1467
1468         __log_test_pass
1469         return 0
1470 }
1471
1472 # API Test function: GET /ei-producer/v1/eiproducers/{eiProducerId}/eijobs
1473 # args: (V1-2) <response-code> <producer-id> (EMPTY | [<job-id> <type-id> <target-url> <job-owner> <template-job-file>]+)
1474 # (Function for test scripts)
1475 ecs_api_edp_get_producer_jobs_2() {
1476         __log_test_start $@
1477
1478         #Valid number of parameter 2,3,7,11
1479         paramError=1
1480         if [ $# -eq 2 ]; then
1481                 paramError=0
1482         fi
1483         if [ $# -eq 3 ] && [ "$3" == "EMPTY" ]; then
1484                 paramError=0
1485         fi
1486         variablecount=$(($#-2))
1487         if [ $# -gt 3 ] && [ $(($variablecount%5)) -eq 0 ]; then
1488                 paramError=0
1489         fi
1490         if [ $paramError -eq 1 ]; then
1491                 __print_err "<response-code> <producer-id> (EMPTY | [<job-id> <type-id> <target-url> <job-owner> <template-job-file>]+)" $@
1492                 return 1
1493         fi
1494
1495         query="/ei-producer/v1/eiproducers/$2/eijobs"
1496     res="$(__do_curl_to_api ECS GET $query)"
1497     status=${res:${#res}-3}
1498         if [ $status -ne $1 ]; then
1499                 __log_test_fail_status_code $1 $status
1500                 return 1
1501         fi
1502         if [ $# -gt 2 ]; then
1503                 body=${res:0:${#res}-3}
1504                 targetJson="["
1505                 if [ $# -gt 3 ]; then
1506                         arr=(${@:3})
1507                         for ((i=0; i<$(($#-3)); i=i+5)); do
1508                                 if [ "$targetJson" != "[" ]; then
1509                                         targetJson=$targetJson","
1510                                 fi
1511                                 if [ -f ${arr[$i+4]} ]; then
1512                                         jobfile=$(cat ${arr[$i+4]})
1513                                         jobfile=$(echo "$jobfile" | sed "s/XXXX/${arr[$i]}/g")
1514                                 else
1515                                         _log_test_fail_general "Job template file "${arr[$i+4]}", does not exist"
1516                                         return 1
1517                                 fi
1518                                 targetJson=$targetJson"{\"ei_job_identity\":\"${arr[$i]}\",\"ei_type_identity\":\"${arr[$i+1]}\",\"target_uri\":\"${arr[$i+2]}\",\"owner\":\"${arr[$i+3]}\",\"ei_job_data\":$jobfile, \"last_updated\":\"????\"}"
1519                         done
1520                 fi
1521                 targetJson=$targetJson"]"
1522
1523                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1524                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1525
1526                 if [ $res -ne 0 ]; then
1527                         __log_test_fail_body
1528                         return 1
1529                 fi
1530         fi
1531
1532         __log_test_pass
1533         return 0
1534 }
1535
1536 ##########################################
1537 ####          Service status          ####
1538 ##########################################
1539 # Function prefix: ecs_api_service
1540
1541 # API Test function: GET ​/status
1542 # args: <response-code>
1543 # (Function for test scripts)
1544 ecs_api_service_status() {
1545         __log_test_start $@
1546
1547     if [ $# -lt 1 ]; then
1548                 __print_err "<response-code> [<producer-id>]*|NOID" $@
1549                 return 1
1550         fi
1551         res="$(__do_curl_to_api ECS GET /status)"
1552     status=${res:${#res}-3}
1553         if [ $status -ne $1 ]; then
1554                 __log_test_fail_status_code $1 $status
1555                 return 1
1556         fi
1557         __log_test_pass
1558         return 0
1559 }
1560
1561
1562 ##########################################
1563 ####          Reset jobs              ####
1564 ##########################################
1565 # Function prefix: ecs_api_admin
1566
1567 # Admin to remove all jobs
1568 # args: <response-code> [ <type> ]
1569 # (Function for test scripts)
1570
1571 ecs_api_admin_reset() {
1572         __log_test_start $@
1573
1574         if [  -z "$FLAT_A1_EI" ]; then
1575                 query="/A1-EI/v1/eitypes/$2/eijobs"
1576         else
1577                 query="/A1-EI/v1/eijobs"
1578         fi
1579     res="$(__do_curl_to_api ECS GET $query)"
1580     status=${res:${#res}-3}
1581
1582         if [ $status -ne 200 ]; then
1583                 __log_test_fail_status_code $1 $status
1584                 return 1
1585         fi
1586
1587         #Remove brackets and response code
1588         body=${res:1:${#res}-4}
1589         list=$(echo ${body//,/ })
1590         list=$(echo ${list//[/})
1591         list=$(echo ${list//]/})
1592         list=$(echo ${list//\"/})
1593         list=$list" "
1594         for job in $list; do
1595                 if [  -z "$FLAT_A1_EI" ]; then
1596                         echo "Not supported for non-flat EI api"
1597                 else
1598                         query="/A1-EI/v1/eijobs/$job"
1599                         res="$(__do_curl_to_api ECS DELETE $query)"
1600                         status=${res:${#res}-3}
1601                         if [ $status -ne 204 ]; then
1602                                 __log_test_fail_status_code $1 $status
1603                                 return 1
1604                         fi
1605                         echo " Deleted job: "$job
1606                 fi
1607         done
1608
1609         __log_test_pass
1610         return 0
1611 }