Temporary skip to push sampleapp images
[nonrtric.git] / test / common / ics_api_functions.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2020-2023 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 ICS
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 __ICS_imagesetup() {
28         __check_and_create_image_var ICS "ICS_IMAGE" "ICS_IMAGE_BASE" "ICS_IMAGE_TAG" $1 "$ICS_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 __ICS_imagepull() {
37         __check_and_pull_image $1 "$ICS_DISPLAY_NAME" $ICS_APP_NAME ICS_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 __ICS_imagebuild() {
44         echo -e $RED" Image for app ICS 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 # If a custom image repo is used then also the source image from the local repo is listed
49 # arg: <docker-images-format-string> <file-to-append>
50 __ICS_image_data() {
51         echo -e "$ICS_DISPLAY_NAME\t$(docker images --format $1 $ICS_IMAGE)" >>   $2
52         if [ ! -z "$ICS_IMAGE_SOURCE" ]; then
53                 echo -e "-- source image --\t$(docker images --format $1 $ICS_IMAGE_SOURCE)" >>   $2
54         fi
55 }
56
57 # Scale kubernetes resources to zero
58 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
59 # This function is called for apps fully managed by the test script
60 __ICS_kube_scale_zero() {
61         __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest ICS
62 }
63
64 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
65 # This function is called for pre-started apps not managed by the test script.
66 __ICS_kube_scale_zero_and_wait() {
67         __kube_scale_and_wait_all_resources $KUBE_NONRTRIC_NAMESPACE app "$KUBE_NONRTRIC_NAMESPACE"-informationservice
68 }
69
70 # Delete all kube resources for the app
71 # This function is called for apps managed by the test script.
72 __ICS_kube_delete_all() {
73         __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest ICS
74 }
75
76 # Store docker logs
77 # This function is called for apps managed by the test script.
78 # args: <log-dir> <file-prefix>
79 __ICS_store_docker_logs() {
80         if [ $RUNMODE == "KUBE" ]; then
81                 kubectl $KUBECONF  logs -l "autotest=ICS" -n $KUBE_NONRTRIC_NAMESPACE --tail=-1 > $1$2_ics.log 2>&1
82         else
83                 docker logs $ICS_APP_NAME > $1$2_ics.log 2>&1
84         fi
85 }
86
87 # Initial setup of protocol, host and ports
88 # This function is called for apps managed by the test script.
89 # args: -
90 __ICS_initial_setup() {
91         use_ics_rest_http
92         export ICS_SIDECAR_JWT_FILE=""
93 }
94
95 # Set app short-name, app name and namespace for logging runtime statistics of kubernetes pods or docker containers
96 # For docker, the namespace shall be excluded
97 # This function is called for apps managed by the test script as well as for pre-started apps.
98 # args: -
99 __ICS_statistics_setup() {
100         if [ $RUNMODE == "KUBE" ]; then
101                 echo "ICS $ICS_APP_NAME $KUBE_NONRTRIC_NAMESPACE"
102         else
103                 echo "ICS $ICS_APP_NAME"
104         fi
105 }
106
107 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
108 # args: -
109 __ICS_test_requirements() {
110         :
111 }
112
113 #######################################################
114
115
116 # Make curl retries towards ICS for http response codes set in this env var, space separated list of codes
117 ICS_RETRY_CODES=""
118
119 #Save first worker node the pod is started on
120 __ICS_WORKER_NODE=""
121
122 ###########################
123 ### ICS functions
124 ###########################
125
126 # All calls to ICS will be directed to the ICS REST interface from now on
127 # args: -
128 # (Function for test scripts)
129 use_ics_rest_http() {
130         __ics_set_protocoll "http" $ICS_INTERNAL_PORT $ICS_EXTERNAL_PORT
131 }
132
133 # All calls to ICS will be directed to the ICS REST interface from now on
134 # args: -
135 # (Function for test scripts)
136 use_ics_rest_https() {
137         __ics_set_protocoll "https" $ICS_INTERNAL_SECURE_PORT $ICS_EXTERNAL_SECURE_PORT
138 }
139
140 # All calls to ICS will be directed to the ICS dmaap interface over http from now on
141 # args: -
142 # (Function for test scripts)
143 use_ics_dmaap_http() {
144         echo -e $BOLD"ICS dmaap protocol setting"$EBOLD
145         echo -e $RED" - NOT SUPPORTED - "$ERED
146         echo -e " Using $BOLD http $EBOLD and $BOLD DMAAP $EBOLD towards ICS"
147         ICS_ADAPTER_TYPE="MR-HTTP"
148         echo ""
149 }
150
151 # Setup paths to svc/container for internal and external access
152 # args: <protocol> <internal-port> <external-port>
153 __ics_set_protocoll() {
154         echo -e $BOLD"$ICS_DISPLAY_NAME protocol setting"$EBOLD
155         echo -e " Using $BOLD $1 $EBOLD towards $ICS_DISPLAY_NAME"
156
157         ## Access to ICS
158
159         ICS_SERVICE_PATH=$1"://"$ICS_APP_NAME":"$2  # docker access, container->container and script->container via proxy
160         if [ $RUNMODE == "KUBE" ]; then
161                 ICS_SERVICE_PATH=$1"://"$ICS_APP_NAME.$KUBE_NONRTRIC_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
162         fi
163
164         # ICS_ADAPTER used for switching between REST and DMAAP (only REST supported currently)
165         ICS_ADAPTER_TYPE="REST"
166         ICS_ADAPTER=$ICS_SERVICE_PATH
167
168         echo ""
169 }
170
171 # Export env vars for config files, docker compose and kube resources
172 # args: PROXY|NOPROXY
173 __ics_export_vars() {
174                 export ICS_APP_NAME
175                 export ICS_APP_NAME_ALIAS
176                 export KUBE_NONRTRIC_NAMESPACE
177                 export ICS_IMAGE
178                 export ICS_INTERNAL_PORT
179                 export ICS_INTERNAL_SECURE_PORT
180                 export ICS_EXTERNAL_PORT
181                 export ICS_EXTERNAL_SECURE_PORT
182                 export ICS_CONFIG_MOUNT_PATH
183                 export ICS_CONFIG_CONFIGMAP_NAME=$ICS_APP_NAME"-config"
184                 export ICS_DATA_CONFIGMAP_NAME=$ICS_APP_NAME"-data"
185                 export ICS_CONTAINER_MNT_DIR
186                 export ICS_HOST_MNT_DIR
187                 export ICS_CONFIG_FILE
188                 export DOCKER_SIM_NWNAME
189                 export ICS_DISPLAY_NAME
190                 export ICS_LOGPATH
191
192                 export ICS_DATA_PV_NAME=$ICS_APP_NAME"-pv"
193                 export ICS_DATA_PVC_NAME=$ICS_APP_NAME"-pvc"
194                 #Create a unique path for the pv each time to prevent a previous volume to be reused
195                 export ICS_PV_PATH="icsdata-"$(date +%s)
196                 export HOST_PATH_BASE_DIR
197
198                 if [ $1 == "PROXY" ]; then
199                         export ICS_HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_CONFIG_PORT  #Set if proxy is started
200                         export ICS_HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_CONFIG_HOST_NAME #Set if proxy is started
201                         if [ $ICS_HTTP_PROXY_CONFIG_PORT -eq 0 ] || [ -z "$ICS_HTTP_PROXY_CONFIG_HOST_NAME" ]; then
202                                 echo -e $YELLOW" Warning: HTTP PROXY will not be configured, proxy app not started"$EYELLOW
203                         else
204                                 echo " Configured with http proxy"
205                         fi
206                 else
207                         export ICS_HTTP_PROXY_CONFIG_PORT=0
208                         export ICS_HTTP_PROXY_CONFIG_HOST_NAME=""
209                         echo " Configured without http proxy"
210                 fi
211 }
212
213
214 # Start the ICS
215 # args: PROXY|NOPROXY <config-file>
216 # (Function for test scripts)
217 start_ics() {
218
219         echo -e $BOLD"Starting $ICS_DISPLAY_NAME"$EBOLD
220
221         if [ $RUNMODE == "KUBE" ]; then
222
223                 # Check if app shall be fully managed by the test script
224                 __check_included_image "ICS"
225                 retcode_i=$?
226
227                 # Check if app shall only be used by the test script
228                 __check_prestarted_image "ICS"
229                 retcode_p=$?
230
231                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
232                         echo -e $RED"The $ICS_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
233                         echo -e $RED"The $ICS_APP_NAME will not be started"$ERED
234                         exit
235                 fi
236                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
237                         echo -e $RED"The $ICS_APP_NAME app is included both as managed and prestarted in this test script"$ERED
238                         echo -e $RED"The $ICS_APP_NAME will not be started"$ERED
239                         exit
240                 fi
241
242                 if [ $retcode_p -eq 0 ]; then
243                         echo -e " Using existing $ICS_APP_NAME deployment and service"
244                         echo " Setting ICS replicas=1"
245                         res_type=$(__kube_get_resource_type $ICS_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
246                         __kube_scale $res_type $ICS_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
247                 fi
248
249                 # Check if app shall be fully managed by the test script
250                 if [ $retcode_i -eq 0 ]; then
251                         echo -e " Creating $ICS_APP_NAME app and expose service"
252
253                         #Check if nonrtric namespace exists, if not create it
254                         __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
255
256                         __ics_export_vars $1
257
258                         # Create config map for config
259                         datafile=$PWD/tmp/$ICS_CONFIG_FILE
260                         cp $2 $datafile
261                         output_yaml=$PWD/tmp/ics_cfc.yaml
262                         __kube_create_configmap $ICS_CONFIG_CONFIGMAP_NAME $KUBE_NONRTRIC_NAMESPACE autotest ICS $datafile $output_yaml
263
264                         # Create pv
265                         input_yaml=$SIM_GROUP"/"$ICS_COMPOSE_DIR"/"pv.yaml
266                         output_yaml=$PWD/tmp/ics_pv.yaml
267                         __kube_create_instance pv $ICS_APP_NAME $input_yaml $output_yaml
268
269                         # Create pvc
270                         input_yaml=$SIM_GROUP"/"$ICS_COMPOSE_DIR"/"pvc.yaml
271                         output_yaml=$PWD/tmp/ics_pvc.yaml
272                         __kube_create_instance pvc $ICS_APP_NAME $input_yaml $output_yaml
273
274                         # Create service
275                         input_yaml=$SIM_GROUP"/"$ICS_COMPOSE_DIR"/"svc.yaml
276                         output_yaml=$PWD/tmp/ics_svc.yaml
277                         __kube_create_instance service $ICS_APP_NAME $input_yaml $output_yaml
278
279                         # Create app
280                         input_yaml=$SIM_GROUP"/"$ICS_COMPOSE_DIR"/"app.yaml
281                         output_yaml=$PWD/tmp/ics_app.yaml
282                         if [ -z "$ICS_SIDECAR_JWT_FILE" ]; then
283                                 cat $input_yaml | sed  '/#ICS_JWT_START/,/#ICS_JWT_STOP/d' > $PWD/tmp/ics_app_tmp.yaml
284                                 input_yaml=$PWD/tmp/ics_app_tmp.yaml
285                         fi
286                         __kube_create_instance app $ICS_APP_NAME $input_yaml $output_yaml
287                 fi
288
289                 # Tie the ICS to a worker node so that ICS will always be scheduled to the same worker node if the ICS pod is restarted
290                 # A PVC of type hostPath is mounted to ICS, for persistent storage, so the ICS must always be on the node which mounted the volume
291
292                 # Keep the initial worker node in case the pod need to be "restarted" - must be made to the same node due to a volume mounted on the host
293                 if [ $retcode_i -eq 0 ]; then
294                         __ICS_WORKER_NODE=$(kubectl $KUBECONF get pod -l "autotest=ICS" -n $KUBE_NONRTRIC_NAMESPACE -o jsonpath='{.items[*].spec.nodeName}')
295                         if [ -z "$__ICS_WORKER_NODE" ]; then
296                                 echo -e $YELLOW" Cannot find worker node for pod for $ICS_APP_NAME, persistency may not work"$EYELLOW
297                         fi
298                 else
299                         echo -e $YELLOW" Persistency may not work for app $ICS_APP_NAME in multi-worker node config when running it as a prestarted app"$EYELLOW
300                 fi
301
302
303                 __check_service_start $ICS_APP_NAME $ICS_SERVICE_PATH$ICS_ALIVE_URL
304
305         else
306                 __check_included_image 'ICS'
307                 if [ $? -eq 1 ]; then
308                         echo -e $RED"The ICS app is not included in this test script"$ERED
309                         echo -e $RED"ICS will not be started"$ERED
310                         exit 1
311                 fi
312
313                 curdir=$PWD
314                 cd $SIM_GROUP
315                 cd ics
316                 cd $ICS_HOST_MNT_DIR
317                 #cd ..
318                 if [ -d db ]; then
319                         if [ "$(ls -A $DIR)" ]; then
320                                 echo -e $BOLD" Cleaning files in mounted dir: $PWD/db"$EBOLD
321                                 rm -rf db/*  &> /dev/null
322                                 if [ $? -ne 0 ]; then
323                                         echo -e $RED" Cannot remove database files in: $PWD"$ERED
324                                         exit 1
325                                 fi
326                         fi
327                 else
328                         echo " No files in mounted dir or dir does not exists"
329                         mkdir db
330                 fi
331
332                 cd $curdir
333
334                 __ics_export_vars $1
335
336                 dest_file=$SIM_GROUP/$ICS_COMPOSE_DIR/$ICS_HOST_MNT_DIR/$ICS_CONFIG_FILE
337
338                 envsubst < $2 > $dest_file
339
340                 __start_container $ICS_COMPOSE_DIR "" NODOCKERARGS 1 $ICS_APP_NAME
341
342                 __check_service_start $ICS_APP_NAME $ICS_SERVICE_PATH$ICS_ALIVE_URL
343         fi
344         echo ""
345         return 0
346 }
347
348 # Stop the ics
349 # args: -
350 # args: -
351 # (Function for test scripts)
352 stop_ics() {
353         echo -e $BOLD"Stopping $ICS_DISPLAY_NAME"$EBOLD
354
355         if [ $RUNMODE == "KUBE" ]; then
356
357                 __check_prestarted_image "ICS"
358                 if [ $? -eq 0 ]; then
359                         echo -e $YELLOW" Persistency may not work for app $ICS_APP_NAME in multi-worker node config when running it as a prestarted app"$EYELLOW
360                         res_type=$(__kube_get_resource_type $ICS_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
361                         __kube_scale $res_type $ICS_APP_NAME $KUBE_NONRTRIC_NAMESPACE 0
362                         return 0
363                 fi
364
365                 __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest ICS
366                 echo "  Deleting the replica set - a new will be started when the app is started"
367                 tmp=$(kubectl $KUBECONF delete rs -n $KUBE_NONRTRIC_NAMESPACE -l "autotest=ICS")
368                 if [ $? -ne 0 ]; then
369                         echo -e $RED" Could not delete replica set "$RED
370                         ((RES_CONF_FAIL++))
371                         return 1
372                 fi
373         else
374                 docker stop $ICS_APP_NAME &> ./tmp/.dockererr
375                 if [ $? -ne 0 ]; then
376                         __print_err "Could not stop $ICS_APP_NAME" $@
377                         cat ./tmp/.dockererr
378                         ((RES_CONF_FAIL++))
379                         return 1
380                 fi
381         fi
382         echo -e $BOLD$GREEN"Stopped"$EGREEN$EBOLD
383         echo ""
384         return 0
385 }
386
387 # Start a previously stopped ics
388 # args: -
389 # (Function for test scripts)
390 start_stopped_ics() {
391         echo -e $BOLD"Starting (the previously stopped) $ICS_DISPLAY_NAME"$EBOLD
392
393         if [ $RUNMODE == "KUBE" ]; then
394
395                 __check_prestarted_image "ICS"
396                 if [ $? -eq 0 ]; then
397                         echo -e $YELLOW" Persistency may not work for app $ICS_APP_NAME in multi-worker node config when running it as a prestarted app"$EYELLOW
398                         res_type=$(__kube_get_resource_type $ICS_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
399                         __kube_scale $res_type $ICS_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
400                         __check_service_start $ICS_APP_NAME $ICS_SERVICE_PATH$ICS_ALIVE_URL
401                         return 0
402                 fi
403
404                 # Tie the ICS to the same worker node it was initially started on
405                 # A PVC of type hostPath is mounted to A1PMS, for persistent storage, so the A1PMS must always be on the node which mounted the volume
406                 if [ -z "$__ICS_WORKER_NODE" ]; then
407                         echo -e $RED" No initial worker node found for pod "$RED
408                         ((RES_CONF_FAIL++))
409                         return 1
410                 else
411                         echo -e $BOLD" Setting nodeSelector kubernetes.io/hostname=$__ICS_WORKER_NODE to deployment for $ICS_APP_NAME. Pod will always run on this worker node: $__ICS_WORKER_NODE"$BOLD
412                         echo -e $BOLD" The mounted volume is mounted as hostPath and only available on that worker node."$BOLD
413                         tmp=$(kubectl $KUBECONF patch deployment $ICS_APP_NAME -n $KUBE_NONRTRIC_NAMESPACE --patch '{"spec": {"template": {"spec": {"nodeSelector": {"kubernetes.io/hostname": "'$__ICS_WORKER_NODE'"}}}}}')
414                         if [ $? -ne 0 ]; then
415                                 echo -e $YELLOW" Cannot set nodeSelector to deployment for $ICS_APP_NAME, persistency may not work"$EYELLOW
416                         fi
417                         __kube_scale deployment $ICS_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
418                 fi
419         else
420                 docker start $ICS_APP_NAME &> ./tmp/.dockererr
421                 if [ $? -ne 0 ]; then
422                         __print_err "Could not start (the stopped) $ICS_APP_NAME" $@
423                         cat ./tmp/.dockererr
424                         ((RES_CONF_FAIL++))
425                         return 1
426                 fi
427         fi
428         __check_service_start $ICS_APP_NAME $ICS_SERVICE_PATH$ICS_ALIVE_URL
429         if [ $? -ne 0 ]; then
430                 return 1
431         fi
432         echo ""
433         return 0
434 }
435
436 # Turn on debug level tracing in ICS
437 # args: -
438 # (Function for test scripts)
439 set_ics_debug() {
440         echo -e $BOLD"Setting ics debug logging"$EBOLD
441         curlString="$ICS_SERVICE_PATH$ICS_ACTUATOR -X POST  -H Content-Type:application/json -d {\"configuredLevel\":\"debug\"}"
442         result=$(__do_curl "$curlString")
443         if [ $? -ne 0 ]; then
444                 __print_err "Could not set debug mode" $@
445                 ((RES_CONF_FAIL++))
446                 return 1
447         fi
448         echo ""
449         return 0
450 }
451
452 # Turn on trace level tracing in ICS
453 # args: -
454 # (Function for test scripts)
455 set_ics_trace() {
456         echo -e $BOLD"Setting ics trace logging"$EBOLD
457         curlString="$ICS_SERVICE_PATH$ICS_ACTUATOR -X POST  -H Content-Type:application/json -d {\"configuredLevel\":\"trace\"}"
458         result=$(__do_curl "$curlString")
459         if [ $? -ne 0 ]; then
460                 __print_err "Could not set trace mode" $@
461                 ((RES_CONF_FAIL++))
462                 return 1
463         fi
464         echo ""
465         return 0
466 }
467
468 # Perform curl retries when making direct call to ICS for the specified http response codes
469 # Speace separated list of http response codes
470 # args: [<response-code>]*
471 use_ics_retries() {
472         echo -e $BOLD"Do curl retries to the ICS REST inteface for these response codes:$@"$EBOLD
473         ICS_RETRY_CODES=$@
474         echo ""
475         return 0
476 }
477
478 # Check the ics logs for WARNINGs and ERRORs
479 # args: -
480 # (Function for test scripts)
481 check_ics_logs() {
482         __check_container_logs "ICS" $ICS_APP_NAME $ICS_LOGPATH WARN ERR
483 }
484
485
486 # Tests if a variable value in the ICS is equal to a target value and and optional timeout.
487 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
488 # equal to the target or not.
489 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
490 # before setting pass or fail depending on if the variable value becomes equal to the target
491 # value or not.
492 # (Function for test scripts)
493 ics_equal() {
494         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
495                 __var_test ICS "$ICS_SERVICE_PATH/" $1 "=" $2 $3
496         else
497                 __print_err "Wrong args to ics_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
498         fi
499 }
500
501
502 ##########################################
503 ######### A1-E information  API ##########
504 ##########################################
505 #Function prefix: ics_api_a1
506
507 # API Test function: GET /A1-EI​/v1​/eitypes​/{eiTypeId}​/eijobs
508 # args: <response-code> <type-id>|NOTYPE  <owner-id>|NOOWNER [ EMPTY | <job-id>+ ]
509 # (Function for test scripts)
510 ics_api_a1_get_job_ids() {
511         __log_test_start $@
512
513         # Valid number of parameters 4,5,6 etc
514         if [ $# -lt 3 ]; then
515                 __print_err "<response-code> <type-id>|NOTYPE  <owner-id>|NOOWNER [ EMPTY | <job-id>+ ]" $@
516                 return 1
517         fi
518         search=""
519         if [ $3 != "NOWNER" ]; then
520                 search="?owner="$3
521         fi
522
523         if [ $2 != "NOTYPE" ]; then
524                 if [ -z "$search" ]; then
525                         search="?eiTypeId="$2
526                 else
527                         search=$search"&eiTypeId="$2
528                 fi
529         fi
530         query="/A1-EI/v1/eijobs$search"
531     res="$(__do_curl_to_api ICS GET $query)"
532     status=${res:${#res}-3}
533
534         if [ $status -ne $1 ]; then
535                 __log_test_fail_status_code $1 $status
536                 return 1
537         fi
538
539         if [ $# -gt 3 ]; then
540                 body=${res:0:${#res}-3}
541                 targetJson="["
542
543                 for pid in ${@:4} ; do
544                         if [ "$targetJson" != "[" ]; then
545                                 targetJson=$targetJson","
546                         fi
547                         if [ $pid != "EMPTY" ]; then
548                                 targetJson=$targetJson"\"$pid\""
549                         fi
550                 done
551
552                 targetJson=$targetJson"]"
553                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
554                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
555
556                 if [ $res -ne 0 ]; then
557                         __log_test_fail_body
558                         return 1
559                 fi
560         fi
561
562         __log_test_pass
563         return 0
564 }
565
566 # API Test function: GET â€‹/A1-EI​/v1​/eitypes​/{eiTypeId}
567 # args: <response-code> <type-id> [<schema-file>]
568 # (Function for test scripts)
569 ics_api_a1_get_type() {
570         __log_test_start $@
571
572     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
573                 __print_err "<response-code> <type-id> [<schema-file>]" $@
574                 return 1
575         fi
576
577         query="/A1-EI/v1/eitypes/$2"
578     res="$(__do_curl_to_api ICS GET $query)"
579     status=${res:${#res}-3}
580
581         if [ $status -ne $1 ]; then
582                 __log_test_fail_status_code $1 $status
583                 return 1
584         fi
585
586         if [ $# -eq 3 ]; then
587                 body=${res:0:${#res}-3}
588                 if [ -f $3 ]; then
589                         schema=$(cat $3)
590                 else
591                         __log_test_fail_general "Schema file "$3", does not exist"
592                         return 1
593                 fi
594                 targetJson=$schema
595                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
596                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
597
598                 if [ $res -ne 0 ]; then
599                         __log_test_fail_body
600                         return 1
601                 fi
602         fi
603
604         __log_test_pass
605         return 0
606 }
607
608 # API Test function: GET /A1-EI/v1/eitypes
609 # args: <response-code> [ (EMPTY | [<type-id>]+) ]
610 # (Function for test scripts)
611 ics_api_a1_get_type_ids() {
612         __log_test_start $@
613
614     if [ $# -lt 1 ]; then
615                 __print_err "<response-code> [ (EMPTY | [<type-id>]+) ]" $@
616                 return 1
617         fi
618
619         query="/A1-EI/v1/eitypes"
620     res="$(__do_curl_to_api ICS GET $query)"
621     status=${res:${#res}-3}
622
623         if [ $status -ne $1 ]; then
624                 __log_test_fail_status_code $1 $status
625                 return 1
626         fi
627         if [ $# -gt 1 ]; then
628                 body=${res:0:${#res}-3}
629                 targetJson="["
630                 if [ $2 != "EMPTY" ]; then
631                         for pid in ${@:2} ; do
632                                 if [ "$targetJson" != "[" ]; then
633                                         targetJson=$targetJson","
634                                 fi
635                                 targetJson=$targetJson"\"$pid\""
636                         done
637                 fi
638                 targetJson=$targetJson"]"
639                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
640                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
641
642                 if [ $res -ne 0 ]; then
643                         __log_test_fail_body
644                         return 1
645                 fi
646         fi
647
648         __log_test_pass
649         return 0
650 }
651
652 # API Test function: GET â€‹/A1-EI​/v1​/eitypes​/{eiTypeId}​/eijobs​/{eiJobId}​/status
653 # args: <response-code> <job-id> [<status> [<timeout>]]
654 # (Function for test scripts)
655 ics_api_a1_get_job_status() {
656         __log_test_start $@
657
658         if [ $# -lt 2 ] && [ $# -gt 4 ]; then
659                 __print_err "<response-code> <job-id> [<status> [<timeout>]]" $@
660                 return 1
661         fi
662
663         query="/A1-EI/v1/eijobs/$2/status"
664
665         start=$SECONDS
666         for (( ; ; )); do
667                 res="$(__do_curl_to_api ICS GET $query)"
668                 status=${res:${#res}-3}
669
670                 if [ $# -eq 4 ]; then
671                         duration=$((SECONDS-start))
672                         echo -ne " Response=${status} after ${duration} seconds, waiting for ${3} ${SAMELINE}"
673                         if [ $duration -gt $4 ]; then
674                                 echo ""
675                                 duration=-1  #Last iteration
676                         fi
677                 else
678                         duration=-1 #single test, no wait
679                 fi
680
681                 if [ $status -ne $1 ]; then
682                         if [ $duration -eq -1 ]; then
683                                 __log_test_fail_status_code $1 $status
684                                 return 1
685                         fi
686                 fi
687                 if [ $# -ge 3 ] && [ $status -eq $1 ]; then
688                         body=${res:0:${#res}-3}
689                         targetJson="{\"eiJobStatus\": \"$3\"}"
690                         echo " TARGET JSON: $targetJson" >> $HTTPLOG
691                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
692
693                         if [ $res -ne 0 ]; then
694                                 if [ $duration -eq -1 ]; then
695                                         __log_test_fail_body
696                                         return 1
697                                 fi
698                         else
699                                 duration=-1  #Goto pass
700                         fi
701                 fi
702                 if [ $duration -eq -1 ]; then
703                         if [ $# -eq 4 ]; then
704                                 echo ""
705                         fi
706                         __log_test_pass
707                         return 0
708                 else
709                         sleep 1
710                 fi
711         done
712
713         __log_test_pass
714         return 0
715 }
716
717 # API Test function: GET â€‹/A1-EI​/v1​/eitypes​/{eiTypeId}​/eijobs​/{eiJobId}
718 # args: <response-code> <job-id> [<type-id> <target-url> <owner-id> <template-job-file>]
719 # (Function for test scripts)
720 ics_api_a1_get_job() {
721         __log_test_start $@
722
723         if [ $# -ne 2 ] && [ $# -ne 7 ]; then
724                 __print_err "<response-code> <job-id> [<type-id> <target-url> <owner-id> <notification-url> <template-job-file>]" $@
725                 return 1
726         fi
727         query="/A1-EI/v1/eijobs/$2"
728     res="$(__do_curl_to_api ICS GET $query)"
729     status=${res:${#res}-3}
730
731         if [ $status -ne $1 ]; then
732                 __log_test_fail_status_code $1 $status
733                 return 1
734         fi
735
736         if [ $# -eq 7 ]; then
737                 body=${res:0:${#res}-3}
738
739                 if [ -f $7 ]; then
740                         jobfile=$(cat $7)
741                         jobfile=$(echo "$jobfile" | sed "s/XXXX/$2/g")
742                 else
743                         __log_test_fail_general "Job template file "$6", does not exist"
744                         return 1
745                 fi
746                 targetJson="{\"eiTypeId\": \"$3\", \"jobResultUri\": \"$4\",\"jobOwner\": \"$5\",\"jobStatusNotificationUri\": \"$6\",\"jobDefinition\": $jobfile}"
747                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
748                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
749
750                 if [ $res -ne 0 ]; then
751                         __log_test_fail_body
752                         return 1
753                 fi
754         fi
755
756         __log_test_pass
757         return 0
758 }
759
760 # API Test function: DELETE â€‹/A1-EI​/v1​/eitypes​/{eiTypeId}​/eijobs​/{eiJobId}
761 # args: <response-code> <job-id>
762 # (Function for test scripts)
763 ics_api_a1_delete_job() {
764         __log_test_start $@
765
766         if [ $# -ne 2 ]; then
767                 __print_err "<response-code> <job-id>" $@
768                 return 1
769         fi
770         query="/A1-EI/v1/eijobs/$2"
771     res="$(__do_curl_to_api ICS DELETE $query)"
772     status=${res:${#res}-3}
773
774         if [ $status -ne $1 ]; then
775                 __log_test_fail_status_code $1 $status
776                 return 1
777         fi
778
779         __log_test_pass
780         return 0
781 }
782
783 # API Test function: PUT â€‹/A1-EI​/v1​/eitypes​/{eiTypeId}​/eijobs​/{eiJobId}
784 # args <response-code> <job-id> <type-id> <target-url> <owner-id> <notification-url> <template-job-file>
785 # (Function for test scripts)
786 ics_api_a1_put_job() {
787         __log_test_start $@
788
789         if [ $# -lt 7 ]; then
790                 __print_err "<response-code> <job-id> <type-id> <target-url> <owner-id> <notification-url> <template-job-file>" $@
791                 return 1
792         fi
793         if [ -f $7 ]; then
794                 jobfile=$(cat $7)
795                 jobfile=$(echo "$jobfile" | sed "s/XXXX/$2/g")
796         else
797                 __log_test_fail_general "Job template file "$7", does not exist"
798                 return 1
799         fi
800
801         inputJson="{\"eiTypeId\": \"$3\", \"jobResultUri\": \"$4\",\"jobOwner\": \"$5\",\"jobStatusNotificationUri\": \"$6\",\"jobDefinition\": $jobfile}"
802         file="./tmp/.p.json"
803         echo "$inputJson" > $file
804
805         query="/A1-EI/v1/eijobs/$2"
806
807     res="$(__do_curl_to_api ICS PUT $query $file)"
808     status=${res:${#res}-3}
809
810         if [ $status -ne $1 ]; then
811                 __log_test_fail_status_code $1 $status
812                 return 1
813         fi
814
815         __log_test_pass
816         return 0
817 }
818
819
820 ##########################################
821 ####   information Data Producer API   ####
822 ##########################################
823 # Function prefix: ics_api_edp
824
825 # API Test function: GET /ei-producer/v1/eitypes
826 # API Test function: GET /data-producer/v1/info-types
827 # args: <response-code> [ EMPTY | <type-id>+]
828 # (Function for test scripts)
829 ics_api_edp_get_type_ids() {
830         __log_test_start $@
831
832     if [ $# -lt 1 ]; then
833                 __print_err "<response-code> [ EMPTY | <type-id>+]" $@
834                 return 1
835         fi
836         query="/data-producer/v1/info-types"
837     res="$(__do_curl_to_api ICS GET $query)"
838     status=${res:${#res}-3}
839
840         if [ $status -ne $1 ]; then
841                 __log_test_fail_status_code $1 $status
842                 return 1
843         fi
844
845         if [ $# -gt 1 ]; then
846                 body=${res:0:${#res}-3}
847                 targetJson="["
848                 if [ $2 != "EMPTY" ]; then
849                         for pid in ${@:2} ; do
850                                 if [ "$targetJson" != "[" ]; then
851                                         targetJson=$targetJson","
852                                 fi
853                                 targetJson=$targetJson"\"$pid\""
854                         done
855                 fi
856                 targetJson=$targetJson"]"
857                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
858                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
859
860                 if [ $res -ne 0 ]; then
861                         __log_test_fail_body
862                         return 1
863                 fi
864         fi
865
866         __log_test_pass
867         return 0
868 }
869
870 # API Test function: GET /ei-producer/v1/eiproducers/{eiProducerId}/status
871 # API Test function: GET /data-producer/v1/info-producers/{infoProducerId}/status
872 # args: <response-code> <producer-id> [<status> [<timeout>]]
873 # (Function for test scripts)
874 ics_api_edp_get_producer_status() {
875         __log_test_start $@
876
877     if [ $# -lt 2 ] || [ $# -gt 4 ]; then
878                 __print_err "<response-code> <producer-id> [<status> [<timeout>]]" $@
879                 return 1
880         fi
881         query="/data-producer/v1/info-producers/$2/status"
882         start=$SECONDS
883         for (( ; ; )); do
884                 res="$(__do_curl_to_api ICS GET $query)"
885                 status=${res:${#res}-3}
886
887                 if [ $# -eq 4 ]; then
888                         duration=$((SECONDS-start))
889                         echo -ne " Response=${status} after ${duration} seconds, waiting for ${3} ${SAMELINE}"
890                         if [ $duration -gt $4 ]; then
891                                 echo ""
892                                 duration=-1  #Last iteration
893                         fi
894                 else
895                         duration=-1 #single test, no wait
896                 fi
897
898                 if [ $status -ne $1 ]; then
899                         if [ $duration -eq -1 ]; then
900                                 __log_test_fail_status_code $1 $status
901                                 return 1
902                         fi
903                 fi
904                 if [ $# -ge 3 ] && [ $status -eq $1 ]; then
905                         body=${res:0:${#res}-3}
906                         targetJson="{\"operational_state\": \"$3\"}"
907                         echo " TARGET JSON: $targetJson" >> $HTTPLOG
908                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
909
910                         if [ $res -ne 0 ]; then
911                                 if [ $duration -eq -1 ]; then
912                                         __log_test_fail_body
913                                         return 1
914                                 fi
915                         else
916                                 duration=-1  #Goto pass
917                         fi
918                 fi
919                 if [ $duration -eq -1 ]; then
920                         if [ $# -eq 4 ]; then
921                                 echo ""
922                         fi
923                         __log_test_pass
924                         return 0
925                 else
926                         sleep 1
927                 fi
928         done
929 }
930
931
932 # API Test function: GET /ei-producer/v1/eiproducers
933 # args (v1_1): <response-code> [ EMPTY | <producer-id>+]
934 # (Function for test scripts)
935 ics_api_edp_get_producer_ids() {
936         __log_test_start $@
937
938     if [ $# -lt 1 ]; then
939                 __print_err "<response-code> [ EMPTY | <producer-id>+]" $@
940                 return 1
941         fi
942
943         query="/ei-producer/v1/eiproducers"
944     res="$(__do_curl_to_api ICS GET $query)"
945     status=${res:${#res}-3}
946
947         if [ $status -ne $1 ]; then
948                 __log_test_fail_status_code $1 $status
949                 return 1
950         fi
951
952         if [ $# -gt 1 ]; then
953                 body=${res:0:${#res}-3}
954                 targetJson="["
955
956                 for pid in ${@:2} ; do
957                         if [ "$targetJson" != "[" ]; then
958                                 targetJson=$targetJson","
959                         fi
960                         if [ $pid != "EMPTY" ]; then
961                                 targetJson=$targetJson"\"$pid\""
962                         fi
963                 done
964
965                 targetJson=$targetJson"]"
966                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
967                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
968
969                 if [ $res -ne 0 ]; then
970                         __log_test_fail_body
971                         return 1
972                 fi
973         fi
974
975         __log_test_pass
976         return 0
977 }
978
979 # API Test function: GET /ei-producer/v1/eiproducers
980 # API Test function: GET /data-producer/v1/info-producers
981 # args (v1_2): <response-code> [ ( NOTYPE | <type-id> ) [ EMPTY | <producer-id>+] ]
982 # (Function for test scripts)
983 ics_api_edp_get_producer_ids_2() {
984         __log_test_start $@
985
986     if [ $# -lt 1 ]; then
987                 __print_err "<response-code> [ ( NOTYPE | <type-id> ) [ EMPTY | <producer-id>+] ]" $@
988                 return 1
989         fi
990         query="/data-producer/v1/info-producers"
991         if [ $# -gt 1 ] && [ $2 != "NOTYPE" ]; then
992                 query=$query"?info_type_id=$2&infoTypeId=$2"  #info_type_id changed to infoTypeId in F-release.
993                                                                                                                 #Remove info_type_id when F-release is no longer supported
994         fi
995     res="$(__do_curl_to_api ICS GET $query)"
996     status=${res:${#res}-3}
997
998         if [ $status -ne $1 ]; then
999                 __log_test_fail_status_code $1 $status
1000                 return 1
1001         fi
1002
1003         if [ $# -gt 2 ]; then
1004                 body=${res:0:${#res}-3}
1005                 targetJson="["
1006
1007                 for pid in ${@:3} ; do
1008                         if [ "$targetJson" != "[" ]; then
1009                                 targetJson=$targetJson","
1010                         fi
1011                         if [ $pid != "EMPTY" ]; then
1012                                 targetJson=$targetJson"\"$pid\""
1013                         fi
1014                 done
1015
1016                 targetJson=$targetJson"]"
1017                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1018                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1019
1020                 if [ $res -ne 0 ]; then
1021                         __log_test_fail_body
1022                         return 1
1023                 fi
1024         fi
1025
1026         __log_test_pass
1027         return 0
1028 }
1029
1030 # API Test function: GET /ei-producer/v1/eitypes/{eiTypeId}
1031 # args: (v1_1) <response-code> <type-id> [<job-schema-file> (EMPTY | [<producer-id>]+)]
1032 # (Function for test scripts)
1033 ics_api_edp_get_type() {
1034         __log_test_start $@
1035
1036         paramError=1
1037         if [ $# -eq 2 ]; then
1038                 paramError=0
1039         fi
1040         if [ $# -gt 3 ]; then
1041                 paramError=0
1042         fi
1043     if [ $paramError -ne 0 ]; then
1044                 __print_err "<response-code> <type-id> [<job-schema-file> 'EMPTY' | ([<producer-id>]+)]" $@
1045                 return 1
1046         fi
1047
1048         query="/ei-producer/v1/eitypes/$2"
1049     res="$(__do_curl_to_api ICS GET $query)"
1050     status=${res:${#res}-3}
1051
1052         if [ $status -ne $1 ]; then
1053                 __log_test_fail_status_code $1 $status
1054                 return 1
1055         fi
1056         if [ $# -gt 3 ]; then
1057                 body=${res:0:${#res}-3}
1058
1059                 if [ -f $3 ]; then
1060                         schema=$(cat $3)
1061                 else
1062                         __log_test_fail_general "Job template file "$3", does not exist"
1063                         return 1
1064                 fi
1065
1066                 targetJson=""
1067                 if [ $4 != "EMPTY" ]; then
1068                         for pid in ${@:4} ; do
1069                                 if [ "$targetJson" != "" ]; then
1070                                         targetJson=$targetJson","
1071                                 fi
1072                                 targetJson=$targetJson"\"$pid\""
1073                         done
1074                 fi
1075                 targetJson="{\"ei_job_data_schema\":$schema, \"ei_producer_ids\": [$targetJson]}"
1076
1077                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1078                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1079
1080                 if [ $res -ne 0 ]; then
1081                         __log_test_fail_body
1082                         return 1
1083                 fi
1084         fi
1085         __log_test_pass
1086         return 0
1087 }
1088
1089 # API Test function: GET /ei-producer/v1/eitypes/{eiTypeId}
1090 # API Test function: GET /data-producer/v1/info-types/{infoTypeId}
1091 # args: (v1_2) <response-code> <type-id> [<job-schema-file> [ <info-type-info> ]]
1092 # (Function for test scripts)
1093 ics_api_edp_get_type_2() {
1094         __log_test_start $@
1095
1096         paramError=1
1097         if [ $# -eq 2 ]; then
1098                 paramError=0
1099         fi
1100         if [ $# -eq 3 ]; then
1101                 paramError=0
1102         fi
1103         if [ $# -eq 4 ]; then
1104                 paramError=0
1105         fi
1106     if [ $paramError -ne 0 ]; then
1107                 __print_err "<response-code> <type-id> [<job-schema-file> [ <info-type-info> ]]" $@
1108                 return 1
1109         fi
1110         query="/data-producer/v1/info-types/$2"
1111
1112     res="$(__do_curl_to_api ICS GET $query)"
1113     status=${res:${#res}-3}
1114
1115         if [ $status -ne $1 ]; then
1116                 __log_test_fail_status_code $1 $status
1117                 return 1
1118         fi
1119         if [ $# -ge 3 ]; then
1120                 body=${res:0:${#res}-3}
1121
1122                 if [ -f $3 ]; then
1123                         schema=$(cat $3)
1124                 else
1125                         __log_test_fail_general "Job template file "$3", does not exist"
1126                         return 1
1127                 fi
1128                 info_data=""
1129                 if [ $# -gt 3 ]; then
1130                         if [ -f $4 ]; then
1131                                 info_data=$(cat $4)
1132                         else
1133                                 __log_test_fail_general "Info-data file "$4", does not exist"
1134                                 return 1
1135                         fi
1136                         info_data=",\"info_type_information\":$info_data"
1137                 fi
1138                 targetJson="{\"info_job_data_schema\":$schema $info_data}"
1139
1140                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1141                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1142
1143                 if [ $res -ne 0 ]; then
1144                         __log_test_fail_body
1145                         return 1
1146                 fi
1147         fi
1148         __log_test_pass
1149         return 0
1150 }
1151
1152 # API Test function: PUT /ei-producer/v1/eitypes/{eiTypeId}
1153 # API Test function: PUT /data-producer/v1/info-types/{infoTypeId}
1154 # args: (v1_2) <response-code> <type-id> <job-schema-file> [ <info-type-info> ]
1155 # (Function for test scripts)
1156 ics_api_edp_put_type_2() {
1157         __log_test_start $@
1158
1159         if [ $# -lt 3 ] || [ $# -gt 4 ]; then
1160                 __print_err "<response-code> <type-id> <job-schema-file> [ <info-type-info> ]" $@
1161                 return 1
1162         fi
1163
1164         if [ ! -f $3 ]; then
1165                 __log_test_fail_general "Job schema file "$3", does not exist"
1166                 return 1
1167         fi
1168
1169         info_data=""
1170         if [ $# -gt 3 ]; then
1171                 if [ -f $4 ]; then
1172                         info_data=$(cat $4)
1173                 else
1174                         __log_test_fail_general "Info-data file "$4", does not exist"
1175                         return 1
1176                 fi
1177                 info_data=",\"info_type_information\":$info_data"
1178         fi
1179
1180         schema=$(cat $3)
1181         input_json="{\"info_job_data_schema\":$schema $info_data}"
1182         file="./tmp/put_type.json"
1183         echo $input_json > $file
1184
1185         query="/data-producer/v1/info-types/$2"
1186     res="$(__do_curl_to_api ICS PUT $query $file)"
1187     status=${res:${#res}-3}
1188
1189         if [ $status -ne $1 ]; then
1190                 __log_test_fail_status_code $1 $status
1191                 return 1
1192         fi
1193
1194         __log_test_pass
1195         return 0
1196 }
1197
1198 # API Test function: DELETE /ei-producer/v1/eitypes/{eiTypeId}
1199 # API Test function: DELETE /data-producer/v1/info-types/{infoTypeId}
1200 # args: (v1_2) <response-code> <type-id>
1201 # (Function for test scripts)
1202 ics_api_edp_delete_type_2() {
1203         __log_test_start $@
1204
1205     if [ $# -ne 2 ]; then
1206                 __print_err "<response-code> <type-id>" $@
1207                 return 1
1208         fi
1209
1210         query="/data-producer/v1/info-types/$2"
1211     res="$(__do_curl_to_api ICS DELETE $query)"
1212     status=${res:${#res}-3}
1213
1214         if [ $status -ne $1 ]; then
1215                 __log_test_fail_status_code $1 $status
1216                 return 1
1217         fi
1218
1219         __log_test_pass
1220         return 0
1221 }
1222
1223 # API Test function: GET /ei-producer/v1/eiproducers/{eiProducerId}
1224 # args: (v1_1) <response-code> <producer-id> [<job-callback> <supervision-callback> (EMPTY | [<type-id> <schema-file>]+) ]
1225 # (Function for test scripts)
1226 ics_api_edp_get_producer() {
1227         __log_test_start $@
1228
1229         #Possible arg count: 2, 5 6, 8, 10 etc
1230         paramError=1
1231         if [ $# -eq 2 ]; then
1232                 paramError=0
1233         fi
1234         if [ $# -eq 5 ] && [ "$5" == "EMPTY" ]; then
1235                 paramError=0
1236         fi
1237         variablecount=$(($#-4))
1238         if [ $# -gt 5 ] && [ $(($variablecount%2)) -eq 0 ]; then
1239                 paramError=0
1240         fi
1241
1242     if [ $paramError -ne 0 ]; then
1243                 __print_err "<response-code> <producer-id> [<job-callback> <supervision-callback> (NOID | [<type-id> <schema-file>]+) ]" $@
1244                 return 1
1245         fi
1246
1247         query="/ei-producer/v1/eiproducers/$2"
1248     res="$(__do_curl_to_api ICS GET $query)"
1249     status=${res:${#res}-3}
1250
1251         if [ $status -ne $1 ]; then
1252                 __log_test_fail_status_code $1 $status
1253                 return 1
1254         fi
1255
1256         if [ $# -gt 2 ]; then
1257                 body=${res:0:${#res}-3}
1258                 targetJson="["
1259                 if [ $# -gt 5 ]; then
1260                         arr=(${@:5})
1261                         for ((i=0; i<$(($#-5)); i=i+2)); do
1262                                 if [ "$targetJson" != "[" ]; then
1263                                         targetJson=$targetJson","
1264                                 fi
1265                                 if [ -f ${arr[$i+1]} ]; then
1266                                         schema=$(cat ${arr[$i+1]})
1267                                 else
1268                                         __log_test_fail_general "Schema file "${arr[$i+1]}", does not exist"
1269                                         return 1
1270                                 fi
1271
1272                                 targetJson=$targetJson"{\"ei_type_identity\":\"${arr[$i]}\",\"ei_job_data_schema\":$schema}"
1273                         done
1274                 fi
1275                 targetJson=$targetJson"]"
1276                 if [ $# -gt 4 ]; then
1277                         targetJson="{\"supported_ei_types\":$targetJson,\"ei_job_callback_url\": \"$3\",\"ei_producer_supervision_callback_url\": \"$4\"}"
1278                 fi
1279                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1280                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1281
1282                 if [ $res -ne 0 ]; then
1283                         __log_test_fail_body
1284                         return 1
1285                 fi
1286         fi
1287
1288         __log_test_pass
1289         return 0
1290 }
1291
1292 # API Test function: GET /ei-producer/v1/eiproducers/{eiProducerId}
1293 # API Test function: GET /data-producer/v1/info-producers/{infoProducerId}
1294 # args (v1_2): <response-code> <producer-id> [<job-callback> <supervision-callback> (EMPTY | <type-id>+) ]
1295 # (Function for test scripts)
1296 ics_api_edp_get_producer_2() {
1297         __log_test_start $@
1298
1299         #Possible arg count: 2, 5, 6, 7, 8 etc
1300         paramError=1
1301         if [ $# -eq 2 ]; then
1302                 paramError=0
1303         fi
1304         if [ $# -eq 5 ] && [ "$5" == "EMPTY" ]; then
1305                 paramError=0
1306         fi
1307         if [ $# -ge 5 ]; then
1308                 paramError=0
1309         fi
1310
1311     if [ $paramError -ne 0 ]; then
1312                 __print_err "<response-code> <producer-id> [<job-callback> <supervision-callback> (EMPTY | <type-id>+) ]" $@
1313                 return 1
1314         fi
1315         query="/data-producer/v1/info-producers/$2"
1316     res="$(__do_curl_to_api ICS GET $query)"
1317     status=${res:${#res}-3}
1318
1319         if [ $status -ne $1 ]; then
1320                 __log_test_fail_status_code $1 $status
1321                 return 1
1322         fi
1323
1324         if [ $# -gt 2 ]; then
1325                 body=${res:0:${#res}-3}
1326                 targetJson="["
1327                 if [ $# -gt 4 ] && [ "$5" != "EMPTY" ]; then
1328                         arr=(${@:5})
1329                         for ((i=0; i<$(($#-4)); i=i+1)); do
1330                                 if [ "$targetJson" != "[" ]; then
1331                                         targetJson=$targetJson","
1332                                 fi
1333                                 targetJson=$targetJson"\"${arr[$i]}\""
1334                         done
1335                 fi
1336                 targetJson=$targetJson"]"
1337                 if [ $# -gt 4 ]; then
1338                         targetJson="{\"supported_info_types\":$targetJson,\"info_job_callback_url\": \"$3\",\"info_producer_supervision_callback_url\": \"$4\"}"
1339                 fi
1340                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1341                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1342
1343                 if [ $res -ne 0 ]; then
1344                         __log_test_fail_body
1345                         return 1
1346                 fi
1347         fi
1348
1349         __log_test_pass
1350         return 0
1351 }
1352
1353 # API Test function: DELETE /ei-producer/v1/eiproducers/{eiProducerId}
1354 # API Test function: DELETE /data-producer/v1/info-producers/{infoProducerId}
1355 # args: <response-code> <producer-id>
1356 # (Function for test scripts)
1357 ics_api_edp_delete_producer() {
1358         __log_test_start $@
1359
1360     if [ $# -lt 2 ]; then
1361                 __print_err "<response-code> <producer-id>" $@
1362                 return 1
1363         fi
1364         query="/data-producer/v1/info-producers/$2"
1365     res="$(__do_curl_to_api ICS DELETE $query)"
1366     status=${res:${#res}-3}
1367
1368         if [ $status -ne $1 ]; then
1369                 __log_test_fail_status_code $1 $status
1370                 return 1
1371         fi
1372
1373         __log_test_pass
1374         return 0
1375 }
1376
1377 # API Test function: PUT /ei-producer/v1/eiproducers/{eiProducerId}
1378 # args: (v1_1) <response-code> <producer-id> <job-callback> <supervision-callback> NOTYPE|[<type-id> <schema-file>]+
1379 # (Function for test scripts)
1380 ics_api_edp_put_producer() {
1381         __log_test_start $@
1382
1383         #Valid number of parametrer 5,6,8,10,
1384         paramError=1
1385         if  [ $# -eq 5 ] && [ "$5" == "NOTYPE" ]; then
1386                 paramError=0
1387         elif [ $# -gt 5 ] && [ $(($#%2)) -eq 0 ]; then
1388                 paramError=0
1389         fi
1390         if [ $paramError -ne 0 ]; then
1391                 __print_err "<response-code> <producer-id> <job-callback> <supervision-callback> NOTYPE|[<type-id> <schema-file>]+" $@
1392                 return 1
1393         fi
1394
1395         inputJson="["
1396         if [ $# -gt 5 ]; then
1397                 arr=(${@:5})
1398                 for ((i=0; i<$(($#-5)); i=i+2)); do
1399                         if [ "$inputJson" != "[" ]; then
1400                                 inputJson=$inputJson","
1401                         fi
1402                         if [ -f ${arr[$i+1]} ]; then
1403                                 schema=$(cat ${arr[$i+1]})
1404                         else
1405                                 __log_test_fail_general "Schema file "${arr[$i+1]}", does not exist"
1406                                 return 1
1407                         fi
1408                         inputJson=$inputJson"{\"ei_type_identity\":\"${arr[$i]}\",\"ei_job_data_schema\":$schema}"
1409                 done
1410         fi
1411         inputJson="\"supported_ei_types\":"$inputJson"]"
1412
1413         inputJson=$inputJson",\"ei_job_callback_url\": \"$3\",\"ei_producer_supervision_callback_url\": \"$4\""
1414
1415         inputJson="{"$inputJson"}"
1416
1417         file="./tmp/.p.json"
1418         echo "$inputJson" > $file
1419         query="/ei-producer/v1/eiproducers/$2"
1420     res="$(__do_curl_to_api ICS PUT $query $file)"
1421     status=${res:${#res}-3}
1422
1423         if [ $status -ne $1 ]; then
1424                 __log_test_fail_status_code $1 $status
1425                 return 1
1426         fi
1427
1428         __log_test_pass
1429         return 0
1430 }
1431
1432 # API Test function: PUT /ei-producer/v1/eiproducers/{eiProducerId}
1433 # API Test function: PUT /data-producer/v1/info-producers/{infoProducerId}
1434 # args: (v1_2) <response-code> <producer-id> <job-callback> <supervision-callback> NOTYPE|[<type-id>+]
1435 # (Function for test scripts)
1436 ics_api_edp_put_producer_2() {
1437         __log_test_start $@
1438
1439         #Valid number of parametrer 5,6,8,10,
1440         paramError=1
1441         if  [ $# -eq 5 ] && [ "$5" == "NOTYPE" ]; then
1442                 paramError=0
1443         elif [ $# -ge 5 ]; then
1444                 paramError=0
1445         fi
1446         if [ $paramError -ne 0 ]; then
1447                 __print_err "<response-code> <producer-id> <job-callback> <supervision-callback> NOTYPE|[<type-id>+]" $@
1448                 return 1
1449         fi
1450
1451         inputJson="["
1452         if [ $# -gt 4 ] && [ "$5" != "NOTYPE" ]; then
1453                 arr=(${@:5})
1454                 for ((i=0; i<$(($#-4)); i=i+1)); do
1455                         if [ "$inputJson" != "[" ]; then
1456                                 inputJson=$inputJson","
1457                         fi
1458                         inputJson=$inputJson"\""${arr[$i]}"\""
1459                 done
1460         fi
1461         inputJson="\"supported_info_types\":"$inputJson"]"
1462
1463         inputJson=$inputJson",\"info_job_callback_url\": \"$3\",\"info_producer_supervision_callback_url\": \"$4\""
1464
1465         inputJson="{"$inputJson"}"
1466
1467         file="./tmp/.p.json"
1468         echo "$inputJson" > $file
1469         query="/data-producer/v1/info-producers/$2"
1470     res="$(__do_curl_to_api ICS PUT $query $file)"
1471     status=${res:${#res}-3}
1472
1473         if [ $status -ne $1 ]; then
1474                 __log_test_fail_status_code $1 $status
1475                 return 1
1476         fi
1477
1478         __log_test_pass
1479         return 0
1480 }
1481
1482 # API Test function: GET /ei-producer/v1/eiproducers/{eiProducerId}/eijobs
1483 # args: (V1-1) <response-code> <producer-id> (EMPTY | [<job-id> <type-id> <target-url> <job-owner> <template-job-file>]+)
1484 # (Function for test scripts)
1485 ics_api_edp_get_producer_jobs() {
1486         __log_test_start $@
1487
1488         #Valid number of parameter 2,3,7,11
1489         paramError=1
1490         if [ $# -eq 2 ]; then
1491                 paramError=0
1492         fi
1493         if [ $# -eq 3 ] && [ "$3" == "EMPTY" ]; then
1494                 paramError=0
1495         fi
1496         variablecount=$(($#-2))
1497         if [ $# -gt 3 ] && [ $(($variablecount%5)) -eq 0 ]; then
1498                 paramError=0
1499         fi
1500         if [ $paramError -eq 1 ]; then
1501                 __print_err "<response-code> <producer-id> (EMPTY | [<job-id> <type-id> <target-url> <job-owner> <template-job-file>]+)" $@
1502                 return 1
1503         fi
1504
1505         query="/ei-producer/v1/eiproducers/$2/eijobs"
1506     res="$(__do_curl_to_api ICS GET $query)"
1507     status=${res:${#res}-3}
1508         if [ $status -ne $1 ]; then
1509                 __log_test_fail_status_code $1 $status
1510                 return 1
1511         fi
1512         if [ $# -gt 2 ]; then
1513                 body=${res:0:${#res}-3}
1514                 targetJson="["
1515                 if [ $# -gt 3 ]; then
1516                         arr=(${@:3})
1517                         for ((i=0; i<$(($#-3)); i=i+5)); do
1518                                 if [ "$targetJson" != "[" ]; then
1519                                         targetJson=$targetJson","
1520                                 fi
1521                                 if [ -f ${arr[$i+4]} ]; then
1522                                         jobfile=$(cat ${arr[$i+4]})
1523                                         jobfile=$(echo "$jobfile" | sed "s/XXXX/${arr[$i]}/g")
1524                                 else
1525                                         __log_test_fail_general "Job template file "${arr[$i+4]}", does not exist"
1526                                         return 1
1527                                 fi
1528                                 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}"
1529                         done
1530                 fi
1531                 targetJson=$targetJson"]"
1532
1533                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1534                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1535
1536                 if [ $res -ne 0 ]; then
1537                         __log_test_fail_body
1538                         return 1
1539                 fi
1540         fi
1541
1542         __log_test_pass
1543         return 0
1544 }
1545
1546 # API Test function: GET /ei-producer/v1/eiproducers/{eiProducerId}/eijobs
1547 # API Test function: GET /data-producer/v1/info-producers/{infoProducerId}/info-jobs
1548 # args: (V1-2) <response-code> <producer-id> (EMPTY | [<job-id> <type-id> <target-url> <job-owner> <template-job-file>]+)
1549 # (Function for test scripts)
1550 ics_api_edp_get_producer_jobs_2() {
1551         __log_test_start $@
1552
1553         #Valid number of parameter 2,3,7,11
1554         paramError=1
1555         if [ $# -eq 2 ]; then
1556                 paramError=0
1557         fi
1558         if [ $# -eq 3 ] && [ "$3" == "EMPTY" ]; then
1559                 paramError=0
1560         fi
1561         variablecount=$(($#-2))
1562         if [ $# -gt 3 ] && [ $(($variablecount%5)) -eq 0 ]; then
1563                 paramError=0
1564         fi
1565         if [ $paramError -eq 1 ]; then
1566                 __print_err "<response-code> <producer-id> (EMPTY | [<job-id> <type-id> <target-url> <job-owner> <template-job-file>]+)" $@
1567                 return 1
1568         fi
1569         query="/data-producer/v1/info-producers/$2/info-jobs"
1570     res="$(__do_curl_to_api ICS GET $query)"
1571     status=${res:${#res}-3}
1572         if [ $status -ne $1 ]; then
1573                 __log_test_fail_status_code $1 $status
1574                 return 1
1575         fi
1576         if [ $# -gt 2 ]; then
1577                 body=${res:0:${#res}-3}
1578                 targetJson="["
1579                 if [ $# -gt 3 ]; then
1580                         arr=(${@:3})
1581                         for ((i=0; i<$(($#-3)); i=i+5)); do
1582                                 if [ "$targetJson" != "[" ]; then
1583                                         targetJson=$targetJson","
1584                                 fi
1585                                 if [ -f ${arr[$i+4]} ]; then
1586                                         jobfile=$(cat ${arr[$i+4]})
1587                                         jobfile=$(echo "$jobfile" | sed "s/XXXX/${arr[$i]}/g")
1588                                 else
1589                                         __log_test_fail_general "Job template file "${arr[$i+4]}", does not exist"
1590                                         return 1
1591                                 fi
1592                                 targetJson=$targetJson"{\"info_job_identity\":\"${arr[$i]}\",\"info_type_identity\":\"${arr[$i+1]}\",\"target_uri\":\"${arr[$i+2]}\",\"owner\":\"${arr[$i+3]}\",\"info_job_data\":$jobfile, \"last_updated\":\"????\"}"
1593
1594                         done
1595                 fi
1596                 targetJson=$targetJson"]"
1597
1598                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1599                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1600
1601                 if [ $res -ne 0 ]; then
1602                         __log_test_fail_body
1603                         return 1
1604                 fi
1605         fi
1606
1607         __log_test_pass
1608         return 0
1609 }
1610
1611 ##########################################
1612 ####          Service status          ####
1613 ##########################################
1614 # Function prefix: ics_api_service
1615
1616 # API Test function: GET â€‹/status
1617 # args: <response-code>
1618 # (Function for test scripts)
1619 ics_api_service_status() {
1620         __log_test_start $@
1621
1622     if [ $# -lt 1 ]; then
1623                 __print_err "<response-code>" $@
1624                 return 1
1625         fi
1626         res="$(__do_curl_to_api ICS GET /status)"
1627     status=${res:${#res}-3}
1628         if [ $status -ne $1 ]; then
1629                 __log_test_fail_status_code $1 $status
1630                 return 1
1631         fi
1632         __log_test_pass
1633         return 0
1634 }
1635
1636 ###########################################
1637 ######### Info data consumer API ##########
1638 ###########################################
1639 #Function prefix: ics_api_idc
1640
1641
1642 # API Test function: GET /data-consumer/v1/info-types
1643 # args: <response-code> [ (EMPTY | [<type-id>]+) ]
1644 # (Function for test scripts)
1645 ics_api_idc_get_type_ids() {
1646         __log_test_start $@
1647
1648     if [ $# -lt 1 ]; then
1649                 __print_err "<response-code> [ (EMPTY | [<type-id>]+) ]" $@
1650                 return 1
1651         fi
1652
1653         query="/data-consumer/v1/info-types"
1654     res="$(__do_curl_to_api ICS GET $query)"
1655     status=${res:${#res}-3}
1656
1657         if [ $status -ne $1 ]; then
1658                 __log_test_fail_status_code $1 $status
1659                 return 1
1660         fi
1661         if [ $# -gt 1 ]; then
1662                 body=${res:0:${#res}-3}
1663                 targetJson="["
1664                 if [ $2 != "EMPTY" ]; then
1665                         for pid in ${@:2} ; do
1666                                 if [ "$targetJson" != "[" ]; then
1667                                         targetJson=$targetJson","
1668                                 fi
1669                                 targetJson=$targetJson"\"$pid\""
1670                         done
1671                 fi
1672                 targetJson=$targetJson"]"
1673                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1674                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1675
1676                 if [ $res -ne 0 ]; then
1677                         __log_test_fail_body
1678                         return 1
1679                 fi
1680         fi
1681
1682         __log_test_pass
1683         return 0
1684 }
1685
1686 # API Test function: GET /data-consumer/v1/info-jobs
1687 # args: <response-code> <type-id>|NOTYPE <owner-id>|NOOWNER [ EMPTY | <job-id>+ ]
1688 # (Function for test scripts)
1689 ics_api_idc_get_job_ids() {
1690         __log_test_start $@
1691
1692         # Valid number of parameters 4,5,6 etc
1693         if [ $# -lt 3 ]; then
1694                 __print_err "<response-code> <type-id>|NOTYPE  <owner-id>|NOOWNER [ EMPTY | <job-id>+ ]" $@
1695                 return 1
1696         fi
1697         search=""
1698         if [ $3 != "NOWNER" ]; then
1699                 search="?owner="$3
1700         fi
1701
1702         if [ $2 != "NOTYPE" ]; then
1703                 if [ -z "$search" ]; then
1704                         search="?infoTypeId="$2
1705                 else
1706                         search=$search"&infoTypeId="$2
1707                 fi
1708         fi
1709         query="/data-consumer/v1/info-jobs$search"
1710
1711     res="$(__do_curl_to_api ICS GET $query)"
1712     status=${res:${#res}-3}
1713
1714         if [ $status -ne $1 ]; then
1715                 __log_test_fail_status_code $1 $status
1716                 return 1
1717         fi
1718
1719         if [ $# -gt 3 ]; then
1720                 body=${res:0:${#res}-3}
1721                 targetJson="["
1722
1723                 for pid in ${@:4} ; do
1724                         if [ "$targetJson" != "[" ]; then
1725                                 targetJson=$targetJson","
1726                         fi
1727                         if [ $pid != "EMPTY" ]; then
1728                                 targetJson=$targetJson"\"$pid\""
1729                         fi
1730                 done
1731
1732                 targetJson=$targetJson"]"
1733                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1734                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1735
1736                 if [ $res -ne 0 ]; then
1737                         __log_test_fail_body
1738                         return 1
1739                 fi
1740         fi
1741
1742         __log_test_pass
1743         return 0
1744 }
1745
1746 # API Test function: GET /data-consumer/v1/info-jobs/{infoJobId}
1747 # args: <response-code> <job-id> [<type-id> <target-url> <owner-id> <template-job-file>]
1748 # (Function for test scripts)
1749 ics_api_idc_get_job() {
1750         __log_test_start $@
1751
1752         if [ $# -ne 2 ] && [ $# -ne 7 ]; then
1753                 __print_err "<response-code> <job-id> [<type-id> <target-url> <owner-id> <notification-url> <template-job-file>]" $@
1754                 return 1
1755         fi
1756         query="/data-consumer/v1/info-jobs/$2"
1757     res="$(__do_curl_to_api ICS GET $query)"
1758     status=${res:${#res}-3}
1759
1760         if [ $status -ne $1 ]; then
1761                 __log_test_fail_status_code $1 $status
1762                 return 1
1763         fi
1764
1765         if [ $# -eq 7 ]; then
1766                 body=${res:0:${#res}-3}
1767
1768                 if [ -f $7 ]; then
1769                         jobfile=$(cat $7)
1770                         jobfile=$(echo "$jobfile" | sed "s/XXXX/$2/g")
1771                 else
1772                         __log_test_fail_general "Job template file "$6", does not exist"
1773                         return 1
1774                 fi
1775                 targetJson="{\"info_type_id\": \"$3\", \"job_result_uri\": \"$4\",\"job_owner\": \"$5\",\"status_notification_uri\": \"$6\",\"job_definition\": $jobfile}"
1776                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1777                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1778
1779                 if [ $res -ne 0 ]; then
1780                         __log_test_fail_body
1781                         return 1
1782                 fi
1783         fi
1784
1785         __log_test_pass
1786         return 0
1787 }
1788
1789
1790 # API Test function: PUT â€‹/data-consumer/v1/info-jobs/{infoJobId}
1791 # args: <response-code> <job-id> <type-id> <target-url> <owner-id> <notification-url> <template-job-file> [ VALIDATE ]
1792 # (Function for test scripts)
1793 ics_api_idc_put_job() {
1794         __log_test_start $@
1795
1796         if [ $# -lt 7 ] || [ $# -gt 8 ]; then
1797                 __print_err "<response-code> <job-id> <type-id> <target-url> <owner-id> <notification-url> <template-job-file> [ VALIDATE ]" $@
1798                 return 1
1799         fi
1800         if [ -f $7 ]; then
1801                 jobfile=$(cat $7)
1802                 jobfile=$(echo "$jobfile" | sed "s/XXXX/$2/g")
1803         else
1804                 __log_test_fail_general "Job template file "$7", does not exist"
1805                 return 1
1806         fi
1807
1808         inputJson="{\"info_type_id\": \"$3\", \"job_result_uri\": \"$4\",\"job_owner\": \"$5\",\"status_notification_uri\": \"$6\",\"job_definition\": $jobfile}"
1809         file="./tmp/.p.json"
1810         echo "$inputJson" > $file
1811
1812         query="/data-consumer/v1/info-jobs/$2"
1813
1814         if [ $# -eq 8 ]; then
1815                 if [ $8 == "VALIDATE" ]; then
1816                         query=$query"?typeCheck=true"
1817                 fi
1818         fi
1819
1820     res="$(__do_curl_to_api ICS PUT $query $file)"
1821     status=${res:${#res}-3}
1822
1823         if [ $status -ne $1 ]; then
1824                 __log_test_fail_status_code $1 $status
1825                 return 1
1826         fi
1827
1828         __log_test_pass
1829         return 0
1830 }
1831
1832 # API Test function: DELETE â€‹/data-consumer/v1/info-jobs/{infoJobId}
1833 # args: <response-code> <job-id>
1834 # (Function for test scripts)
1835 ics_api_idc_delete_job() {
1836         __log_test_start $@
1837
1838         if [ $# -ne 2 ]; then
1839                 __print_err "<response-code> <job-id>" $@
1840                 return 1
1841         fi
1842         query="/data-consumer/v1/info-jobs/$2"
1843     res="$(__do_curl_to_api ICS DELETE $query)"
1844     status=${res:${#res}-3}
1845
1846         if [ $status -ne $1 ]; then
1847                 __log_test_fail_status_code $1 $status
1848                 return 1
1849         fi
1850
1851         __log_test_pass
1852         return 0
1853 }
1854
1855 # API Test function: GET â€‹/data-consumer/v1/info-types/{infoTypeId}
1856 # args: <response-code> <type-id> [<schema-file> [<type-status> <producers-count]]
1857 # (Function for test scripts)
1858 ics_api_idc_get_type() {
1859         __log_test_start $@
1860
1861     if [ $# -lt 2 ] || [ $# -gt 5 ]; then
1862                 __print_err "<response-code> <type-id> [<schema-file> [<type-status> <producers-count]]" $@
1863                 return 1
1864         fi
1865
1866         query="/data-consumer/v1/info-types/$2"
1867     res="$(__do_curl_to_api ICS GET $query)"
1868     status=${res:${#res}-3}
1869
1870         if [ $status -ne $1 ]; then
1871                 __log_test_fail_status_code $1 $status
1872                 return 1
1873         fi
1874
1875         if [ $# -gt 2 ]; then
1876                 body=${res:0:${#res}-3}
1877                 if [ -f $3 ]; then
1878                         schema=$(cat $3)
1879                 else
1880                         __log_test_fail_general "Schema file "$3", does not exist"
1881                         return 1
1882                 fi
1883                 if [ $# -eq 5 ]; then
1884                         targetJson="{\"job_data_schema\":$schema, \"type_status\":\"$4\", \"no_of_producers\":$5}"
1885                 else
1886                         targetJson="{\"job_data_schema\":$schema}"
1887                 fi
1888                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1889                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1890
1891                 if [ $res -ne 0 ]; then
1892                         __log_test_fail_body
1893                         return 1
1894                 fi
1895         fi
1896
1897         __log_test_pass
1898         return 0
1899 }
1900
1901 # API Test function: GET /data-consumer/v1/info-jobs/{infoJobId}/status
1902 # This test only status during an optional timeout. No test of the list of producers
1903 # args: <response-code> <job-id> [<status> [<timeout>]]
1904 # (Function for test scripts)
1905 ics_api_idc_get_job_status() {
1906         __log_test_start $@
1907
1908         if [ $# -lt 2 ] && [ $# -gt 4 ]; then
1909                 __print_err "<response-code> <job-id> [<status> [<timeout>]]" $@
1910                 return 1
1911         fi
1912
1913         query="/data-consumer/v1/info-jobs/$2/status"
1914
1915         start=$SECONDS
1916         for (( ; ; )); do
1917                 res="$(__do_curl_to_api ICS GET $query)"
1918                 status=${res:${#res}-3}
1919
1920                 if [ $# -eq 4 ]; then
1921                         duration=$((SECONDS-start))
1922                         echo -ne " Response=${status} after ${duration} seconds, waiting for ${3} ${SAMELINE}"
1923                         if [ $duration -gt $4 ]; then
1924                                 echo ""
1925                                 duration=-1  #Last iteration
1926                         fi
1927                 else
1928                         duration=-1 #single test, no wait
1929                 fi
1930
1931                 if [ $status -ne $1 ]; then
1932                         if [ $duration -eq -1 ]; then
1933                                 __log_test_fail_status_code $1 $status
1934                                 return 1
1935                         fi
1936                 fi
1937                 if [ $# -ge 3 ] && [ $status -eq $1 ]; then
1938                         body=${res:0:${#res}-3}
1939                         targetJson="{\"info_job_status\": \"$3\"}"
1940                         echo " TARGET JSON: $targetJson" >> $HTTPLOG
1941                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1942
1943                         if [ $res -ne 0 ]; then
1944                                 if [ $duration -eq -1 ]; then
1945                                         __log_test_fail_body
1946                                         return 1
1947                                 fi
1948                         else
1949                                 duration=-1  #Goto pass
1950                         fi
1951                 fi
1952                 if [ $duration -eq -1 ]; then
1953                         if [ $# -eq 4 ]; then
1954                                 echo ""
1955                         fi
1956                         __log_test_pass
1957                         return 0
1958                 else
1959                         sleep 1
1960                 fi
1961         done
1962
1963         __log_test_pass
1964         return 0
1965 }
1966
1967 # API Test function: GET /data-consumer/v1/info-jobs/{infoJobId}/status
1968 # This function test status and the list of producers with and optional timeout
1969 # args: <response-code> <job-id> [<status> EMPTYPROD|( <prod-count> <producer-id>+ ) [<timeout>]]
1970 # (Function for test scripts)
1971 ics_api_idc_get_job_status2() {
1972
1973         __log_test_start $@
1974         param_error=0
1975         if [ $# -lt 2 ]; then
1976                 param_error=1
1977         fi
1978         args=("$@")
1979         timeout=0
1980         if [ $# -gt 2 ]; then
1981                 if [ $# -lt 4 ]; then
1982                         param_error=1
1983                 fi
1984                 targetJson="{\"info_job_status\": \"$3\""
1985                 if [ "$4" == "EMPTYPROD" ]; then
1986                         targetJson=$targetJson",\"producers\": []}"
1987                         if [ $# -gt 4 ]; then
1988                                 timeout=$5
1989                         fi
1990                 else
1991                         targetJson=$targetJson",\"producers\": ["
1992                         if [ $# -eq $(($4+5)) ]; then
1993                                 idx=$(($4+4))
1994                                 timeout=${args[$idx]}
1995                         fi
1996                         for ((ics_i = 0 ; ics_i < $4 ; ics_i++)); do
1997                                 idx=$(($ics_i+4))
1998                                 if [ $ics_i -gt 0 ]; then
1999                                         targetJson=$targetJson","
2000                                 fi
2001                                 targetJson=$targetJson"\""${args[$idx]}"\""
2002                         done
2003                         targetJson=$targetJson"]}"
2004                 fi
2005         fi
2006
2007         if [ $param_error -ne 0 ]; then
2008                 __print_err "<response-code> <job-id> [<status> EMPTYPROD|( <prod-count> <producer-id>+ ) [<timeout>]]" $@
2009                 return 1
2010         fi
2011
2012         query="/data-consumer/v1/info-jobs/$2/status"
2013
2014         start=$SECONDS
2015         for (( ; ; )); do
2016                 res="$(__do_curl_to_api ICS GET $query)"
2017                 status=${res:${#res}-3}
2018
2019                 if [ $# -gt 2 ]; then
2020                         duration=$((SECONDS-start))
2021                         echo -ne " Response=${status} after ${duration} seconds, waiting for ${3} ${SAMELINE}"
2022                         if [ $duration -gt $timeout ]; then
2023                                 echo ""
2024                                 duration=-1  #Last iteration
2025                         fi
2026                 else
2027                         duration=-1 #single test, no wait
2028                 fi
2029
2030                 if [ $status -ne $1 ]; then
2031                         if [ $duration -eq -1 ]; then
2032                                 __log_test_fail_status_code $1 $status
2033                                 return 1
2034                         fi
2035                 fi
2036                 if [ $# -gt 2 ] && [ $status -eq $1 ]; then
2037                         body=${res:0:${#res}-3}
2038                         echo " TARGET JSON: $targetJson" >> $HTTPLOG
2039                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
2040
2041                         if [ $res -ne 0 ]; then
2042                                 if [ $duration -eq -1 ]; then
2043                                         __log_test_fail_body
2044                                         return 1
2045                                 fi
2046                         else
2047                                 duration=-1  #Goto pass
2048                         fi
2049                 fi
2050                 if [ $duration -eq -1 ]; then
2051                         if [ $# -eq 4 ]; then
2052                                 echo ""
2053                         fi
2054                         __log_test_pass
2055                         return 0
2056                 else
2057                         sleep 1
2058                 fi
2059         done
2060
2061         __log_test_pass
2062         return 0
2063 }
2064
2065 ##########################################
2066 ####     Type subscriptions           ####
2067 ##########################################
2068
2069 # API Test function: GET /data-consumer/v1/info-type-subscription
2070 # args: <response-code>  <owner-id>|NOOWNER [ EMPTY | <subscription-id>+]
2071 # (Function for test scripts)
2072 ics_api_idc_get_subscription_ids() {
2073         __log_test_start $@
2074
2075     if [ $# -lt 3 ]; then
2076                 __print_err "<response-code> <owner-id>|NOOWNER [ EMPTY | <subscription-id>+]" $@
2077                 return 1
2078         fi
2079
2080         query="/data-consumer/v1/info-type-subscription"
2081         search=""
2082         if [ $2 != "NOOWNER" ]; then
2083                 search="?owner="$2
2084         fi
2085
2086     res="$(__do_curl_to_api ICS GET $query$search)"
2087     status=${res:${#res}-3}
2088
2089         if [ $status -ne $1 ]; then
2090                 __log_test_fail_status_code $1 $status
2091                 return 1
2092         fi
2093
2094         if [ $# -gt 2 ]; then
2095                 body=${res:0:${#res}-3}
2096                 targetJson="["
2097                 if [ $3 != "EMPTY" ]; then
2098                         for pid in ${@:3} ; do
2099                                 if [ "$targetJson" != "[" ]; then
2100                                         targetJson=$targetJson","
2101                                 fi
2102                                 targetJson=$targetJson"\"$pid\""
2103                         done
2104                 fi
2105                 targetJson=$targetJson"]"
2106                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
2107                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
2108
2109                 if [ $res -ne 0 ]; then
2110                         __log_test_fail_body
2111                         return 1
2112                 fi
2113         fi
2114
2115         __log_test_pass
2116         return 0
2117 }
2118
2119 # API Test function: GET /data-consumer/v1/info-type-subscription/{subscriptionId}
2120 # args: <response-code>  <subscription-id> [ <owner-id> <status-uri> ]
2121 # (Function for test scripts)
2122 ics_api_idc_get_subscription() {
2123         __log_test_start $@
2124
2125     if [ $# -ne 2 ] && [ $# -ne 4 ]; then
2126                 __print_err "<response-code>  <subscription-id> [ <owner-id> <status-uri> ]" $@
2127                 return 1
2128         fi
2129
2130         query="/data-consumer/v1/info-type-subscription/$2"
2131     res="$(__do_curl_to_api ICS GET $query)"
2132     status=${res:${#res}-3}
2133
2134         if [ $status -ne $1 ]; then
2135                 __log_test_fail_status_code $1 $status
2136                 return 1
2137         fi
2138
2139         if [ $# -gt 2 ]; then
2140                 body=${res:0:${#res}-3}
2141                 targetJson="{\"owner\":\"$3\",\"status_result_uri\":\"$4\"}"
2142                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
2143                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
2144
2145                 if [ $res -ne 0 ]; then
2146                         __log_test_fail_body
2147                         return 1
2148                 fi
2149         fi
2150
2151         __log_test_pass
2152         return 0
2153 }
2154
2155 # API Test function: PUT /data-consumer/v1/info-type-subscription/{subscriptionId}
2156 # args: <response-code>  <subscription-id> <owner-id> <status-uri>
2157 # (Function for test scripts)
2158 ics_api_idc_put_subscription() {
2159         __log_test_start $@
2160
2161     if [ $# -ne 4 ]; then
2162                 __print_err "<response-code>  <subscription-id> <owner-id> <status-uri>" $@
2163                 return 1
2164         fi
2165
2166         inputJson="{\"owner\": \"$3\",\"status_result_uri\": \"$4\"}"
2167         file="./tmp/.p.json"
2168         echo "$inputJson" > $file
2169
2170         query="/data-consumer/v1/info-type-subscription/$2"
2171     res="$(__do_curl_to_api ICS PUT $query $file)"
2172     status=${res:${#res}-3}
2173
2174         if [ $status -ne $1 ]; then
2175                 __log_test_fail_status_code $1 $status
2176                 return 1
2177         fi
2178
2179         __log_test_pass
2180         return 0
2181 }
2182
2183 # API Test function: DELETE /data-consumer/v1/info-type-subscription/{subscriptionId}
2184 # args: <response-code>  <subscription-id>
2185 # (Function for test scripts)
2186 ics_api_idc_delete_subscription() {
2187         __log_test_start $@
2188
2189         if [ $# -ne 2 ]; then
2190                 __print_err "<response-code>  <subscription-id> " $@
2191                 return 1
2192         fi
2193
2194         query="/data-consumer/v1/info-type-subscription/$2"
2195     res="$(__do_curl_to_api ICS DELETE $query)"
2196     status=${res:${#res}-3}
2197
2198         if [ $status -ne $1 ]; then
2199                 __log_test_fail_status_code $1 $status
2200                 return 1
2201         fi
2202
2203         __log_test_pass
2204         return 0
2205 }
2206
2207 ##########################################
2208 ####          Reset jobs              ####
2209 ##########################################
2210 # Function prefix: ics_api_admin
2211
2212 # Admin to remove all jobs
2213 # args: <response-code> [ <type> ]
2214 # (Function for test scripts)
2215
2216 ics_api_admin_reset() {
2217         __log_test_start $@
2218
2219         query="/A1-EI/v1/eijobs"
2220     res="$(__do_curl_to_api ICS GET $query)"
2221     status=${res:${#res}-3}
2222
2223         if [ $status -ne 200 ]; then
2224                 __log_test_fail_status_code $1 $status
2225                 return 1
2226         fi
2227
2228         #Remove brackets and response code
2229         body=${res:1:${#res}-4}
2230         list=$(echo ${body//,/ })
2231         list=$(echo ${list//[/})
2232         list=$(echo ${list//]/})
2233         list=$(echo ${list//\"/})
2234         list=$list" "
2235         for job in $list; do
2236                 query="/A1-EI/v1/eijobs/$job"
2237                 res="$(__do_curl_to_api ICS DELETE $query)"
2238                 status=${res:${#res}-3}
2239                 if [ $status -ne 204 ]; then
2240                         __log_test_fail_status_code $1 $status
2241                         return 1
2242                 fi
2243                 echo " Deleted job: "$job
2244         done
2245
2246         __log_test_pass
2247         return 0
2248 }
2249
2250 ##########################################
2251 ####     Reset jobs and producers     ####
2252 ##########################################
2253
2254
2255 # Admin reset to remove all data in ics; jobs, producers etc
2256 # NOTE - only works in kubernetes and the pod should not be running
2257 # args: -
2258 # (Function for test scripts)
2259
2260 ics_kube_pvc_reset() {
2261         __log_test_start $@
2262
2263         pvc_name=$(kubectl $KUBECONF get pvc -n $KUBE_NONRTRIC_NAMESPACE  --no-headers -o custom-columns=":metadata.name" | grep information)
2264         if [ -z "$pvc_name" ]; then
2265                 pvc_name=informationservice-pvc
2266         fi
2267         echo " Trying to reset pvc: "$pvc_name
2268
2269         __kube_clean_pvc $ICS_APP_NAME $KUBE_NONRTRIC_NAMESPACE $pvc_name $ICS_CONTAINER_MNT_DIR
2270
2271         __log_test_pass
2272         return 0
2273 }
2274
2275 # args: <realm> <client-name> <client-secret>
2276 ics_configure_sec() {
2277         export ICS_CREDS_GRANT_TYPE="client_credentials"
2278         export ICS_CREDS_CLIENT_SECRET=$3
2279         export ICS_CREDS_CLIENT_ID=$2
2280         export ICS_AUTH_SERVICE_URL=$KEYCLOAK_SERVICE_PATH$KEYCLOAK_TOKEN_URL_PREFIX/$1/protocol/openid-connect/token
2281         export ICS_SIDECAR_MOUNT="/token-cache"
2282         export ICS_SIDECAR_JWT_FILE=$ICS_SIDECAR_MOUNT"/jwt.txt"
2283
2284         export AUTHSIDECAR_APP_NAME
2285         export AUTHSIDECAR_DISPLAY_NAME
2286 }