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