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