Update test env to remove '-' in ric hostnames
[nonrtric.git] / test / common / cr_api_functions.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2020 Nordix Foundation. All rights reserved.
5 #  ========================================================================
6 #  Licensed under the Apache License, Version 2.0 (the "License");
7 #  you may not use this file except in compliance with the License.
8 #  You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #  Unless required by applicable law or agreed to in writing, software
13 #  distributed under the License is distributed on an "AS IS" BASIS,
14 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #  See the License for the specific language governing permissions and
16 #  limitations under the License.
17 #  ============LICENSE_END=================================================
18 #
19
20 # This is a script that contains container/service management functions test functions for the Callback Receiver
21
22
23 ################ Test engine functions ################
24
25 # Create the image var used during the test
26 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
27 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
28 __CR_imagesetup() {
29         __check_and_create_image_var CR "CR_IMAGE" "CR_IMAGE_BASE" "CR_IMAGE_TAG" LOCAL "$CR_DISPLAY_NAME" $IMAGE_TARGET_PLATFORM_IMG_TAG
30 }
31
32 # Pull image from remote repo or use locally built image
33 # arg: <pull-policy-override> <pull-policy-original>
34 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
35 # <pull-policy-original> Shall be used for images that does not allow overriding
36 # Both var may contain: 'remote', 'remote-remove' or 'local'
37 __CR_imagepull() {
38         echo -e $RED" Image for app CR shall never be pulled from remote repo"$ERED
39 }
40
41 # Build image (only for simulator or interfaces stubs owned by the test environment)
42 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
43 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
44 __CR_imagebuild() {
45         cd ../cr
46         echo " Building CR - $CR_DISPLAY_NAME - image: $CR_IMAGE"
47         docker build $IMAGE_TARGET_PLATFORM_CMD_PARAM --build-arg NEXUS_PROXY_REPO=$NEXUS_PROXY_REPO -t $CR_IMAGE . &> .dockererr
48         if [ $? -eq 0 ]; then
49                 echo -e  $GREEN"  Build Ok"$EGREEN
50                 __retag_and_push_image CR_IMAGE
51                 if [ $? -ne 0 ]; then
52                         exit 1
53                 fi
54         else
55                 echo -e $RED"  Build Failed"$ERED
56                 ((RES_CONF_FAIL++))
57                 cat .dockererr
58                 echo -e $RED"Exiting...."$ERED
59                 exit 1
60         fi
61 }
62
63 # Generate a string for each included image using the app display name and a docker images format string
64 # If a custom image repo is used then also the source image from the local repo is listed
65 # arg: <docker-images-format-string> <file-to-append>
66 __CR_image_data() {
67         echo -e "$CR_DISPLAY_NAME\t$(docker images --format $1 $CR_IMAGE)" >>   $2
68         if [ ! -z "$CR_IMAGE_SOURCE" ]; then
69                 echo -e "-- source image --\t$(docker images --format $1 $CR_IMAGE_SOURCE)" >>   $2
70         fi
71 }
72
73 # Scale kubernetes resources to zero
74 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
75 # This function is called for apps fully managed by the test script
76 __CR_kube_scale_zero() {
77         __kube_scale_all_resources $KUBE_SIM_NAMESPACE autotest CR
78 }
79
80 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
81 # This function is called for pre-started apps not managed by the test script.
82 __CR_kube_scale_zero_and_wait() {
83         echo -e $RED" CR app is not scaled in this state"$ERED
84 }
85
86 # Delete all kube resources for the app
87 # This function is called for apps managed by the test script.
88 __CR_kube_delete_all() {
89         __kube_delete_all_resources $KUBE_SIM_NAMESPACE autotest CR
90 }
91
92 # Store docker logs
93 # This function is called for apps managed by the test script.
94 # args: <log-dir> <file-prefix>
95 __CR_store_docker_logs() {
96         if [ $RUNMODE == "KUBE" ]; then
97                 for podname in $(kubectl $KUBECONF get pods -n $KUBE_SIM_NAMESPACE -l "autotest=CR" -o custom-columns=":metadata.name"); do
98                         kubectl $KUBECONF logs -n $KUBE_SIM_NAMESPACE $podname --tail=-1 > $1$2_$podname.log 2>&1
99                 done
100         else
101                 crs=$(docker ps --filter "name=$CR_APP_NAME" --filter "network=$DOCKER_SIM_NWNAME" --filter "status=running" --format {{.Names}})
102                 for crid in $crs; do
103                         docker logs $crid > $1$2_$crid.log 2>&1
104                 done
105         fi
106 }
107
108 # Initial setup of protocol, host and ports
109 # This function is called for apps managed by the test script.
110 # args: -
111 __CR_initial_setup() {
112         use_cr_http
113 }
114
115 # Set app short-name, app name and namespace for logging runtime statistics of kubernetes pods or docker containers
116 # For docker, the namespace shall be excluded
117 # This function is called for apps managed by the test script as well as for pre-started apps.
118 # args: -
119 __CR_statistics_setup() {
120         for ((CR_INSTANCE=MAX_CR_APP_COUNT; CR_INSTANCE>0; CR_INSTANCE-- )); do
121                 if [ $RUNMODE == "KUBE" ]; then
122                         CR_INSTANCE_KUBE=$(($CR_INSTANCE-1))
123                         echo -n " CR-$CR_INSTANCE_KUBE $CR_APP_NAME-$CR_INSTANCE_KUBE $KUBE_SIM_NAMESPACE "
124                 else
125                         echo -n " CR_$CR_INSTANCE ${CR_APP_NAME}-cr-$CR_INSTANCE "
126                 fi
127         done
128 }
129
130 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
131 # args: -
132 __CR_test_requirements() {
133         :
134 }
135
136 #######################################################
137
138 ################
139 ### CR functions
140 ################
141
142 #Var to hold the current number of CR instances
143 CR_APP_COUNT=1
144 MAX_CR_APP_COUNT=10
145
146 # Set http as the protocol to use for all communication to the Dmaap adapter
147 # args: -
148 # (Function for test scripts)
149 use_cr_http() {
150         __cr_set_protocoll "http" $CR_INTERNAL_PORT $CR_EXTERNAL_PORT
151 }
152
153 # Set https as the protocol to use for all communication to the Dmaap adapter
154 # args: -
155 # (Function for test scripts)
156 use_cr_https() {
157         __cr_set_protocoll "https" $CR_INTERNAL_SECURE_PORT $CR_EXTERNAL_SECURE_PORT
158 }
159
160 # Setup paths to svc/container for internal and external access
161 # args: <protocol> <internal-port> <external-port>
162 __cr_set_protocoll() {
163
164         echo -e $BOLD"$CR_DISPLAY_NAME protocol setting"$EBOLD
165         echo -e " Using $BOLD $1 $EBOLD towards $CR_DISPLAY_NAME"
166         ## Access to Dmaap adapter
167         for ((CR_INSTANCE=0; CR_INSTANCE<$MAX_CR_APP_COUNT; CR_INSTANCE++ )); do
168                 CR_DOCKER_INSTANCE=$(($CR_INSTANCE+1))
169                 # CR_SERVICE_PATH is the base path to cr
170                 __CR_SERVICE_PATH=$1"://"$CR_APP_NAME"-cr-"${CR_DOCKER_INSTANCE}":"$2  # docker access, container->container and script->container via proxy
171                 if [ $RUNMODE == "KUBE" ]; then
172                         __CR_SERVICE_PATH=$1"://"$CR_APP_NAME"-"$CR_INSTANCE.$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
173                 fi
174                 export CR_SERVICE_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH
175                 # Service paths are used in test script to provide callbacck urls to app
176                 export CR_SERVICE_MR_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK_MR  #Only for messages from dmaap adapter/mediator
177                 export CR_SERVICE_TEXT_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK_TEXT  #Callbacks for text payload
178                 export CR_SERVICE_APP_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK    #For general callbacks from apps
179
180                 if [ $CR_INSTANCE -eq 0 ]; then
181                         # CR_ADAPTER used for switching between REST and DMAAP (only REST supported currently)
182                         # CR_ADDAPTER need to be set before each call to CR....only set for instance 0 here
183                         CR_ADAPTER_TYPE="REST"
184                         CR_ADAPTER=$__CR_SERVICE_PATH
185                 fi
186         done
187         echo ""
188 }
189
190 # Export env vars for config files, docker compose and kube resources
191 # args: <proxy-flag>
192 __cr_export_vars() {
193         export CR_APP_NAME
194         export CR_DISPLAY_NAME
195
196         export KUBE_SIM_NAMESPACE
197         export DOCKER_SIM_NWNAME
198
199         export CR_IMAGE
200
201         export CR_INTERNAL_PORT
202         export CR_INTERNAL_SECURE_PORT
203         export CR_EXTERNAL_PORT
204         export CR_EXTERNAL_SECURE_PORT
205
206         export CR_APP_COUNT
207 }
208
209 # Start the Callback receiver in the simulator group
210 # args: <app-count>
211 # (Function for test scripts)
212 start_cr() {
213
214         echo -e $BOLD"Starting $CR_DISPLAY_NAME"$EBOLD
215
216         if [ $# -ne 1 ]; then
217                 echo -e $RED" Number of CR instances missing, usage: start_cr <app-count>"$ERED
218                 exit 1
219         fi
220         if [ $1 -lt 1 ] || [ $1 -gt 10 ]; then
221                 echo -e $RED" Number of CR shall be 1...10, usage: start_cr <app-count>"$ERED
222                 exit 1
223         fi
224         export CR_APP_COUNT=$1
225
226         if [ $RUNMODE == "KUBE" ]; then
227
228                 # Check if app shall be fully managed by the test script
229                 __check_included_image "CR"
230                 retcode_i=$?
231
232                 # Check if app shall only be used by the test script
233                 __check_prestarted_image "CR"
234                 retcode_p=$?
235
236                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
237                         echo -e $RED"The $CR_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
238                         echo -e $RED"The $CR_APP_NAME will not be started"$ERED
239                         exit
240                 fi
241                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
242                         echo -e $RED"The $CR_APP_NAME app is included both as managed and prestarted in this test script"$ERED
243                         echo -e $RED"The $CR_APP_NAME will not be started"$ERED
244                         exit
245                 fi
246
247                 # Check if app shall be used - not managed - by the test script
248                 if [ $retcode_p -eq 0 ]; then
249                         echo -e " Using existing $CR_APP_NAME deployment and service"
250                         echo " Setting CR replicas=1"
251                         __kube_scale deployment $CR_APP_NAME $KUBE_SIM_NAMESPACE 1
252                 fi
253
254                 if [ $retcode_i -eq 0 ]; then
255                         echo -e " Creating $CR_APP_NAME deployment and service"
256
257                         __cr_export_vars
258
259                         __kube_create_namespace $KUBE_SIM_NAMESPACE
260
261                         # Create service
262                         input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"svc.yaml
263                         output_yaml=$PWD/tmp/cr_svc.yaml
264                         __kube_create_instance service $CR_APP_NAME $input_yaml $output_yaml
265
266                         # Create app
267                         input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"app.yaml
268                         output_yaml=$PWD/tmp/cr_app.yaml
269                         __kube_create_instance app $CR_APP_NAME $input_yaml $output_yaml
270
271                 fi
272
273                 for ((CR_INSTANCE=0; CR_INSTANCE<$CR_APP_COUNT; CR_INSTANCE++ )); do
274                         __dynvar="CR_SERVICE_PATH_"$CR_INSTANCE
275                         __cr_app_name=$CR_APP_NAME"-"$CR_INSTANCE
276                         __check_service_start $__cr_app_name ${!__dynvar}$CR_ALIVE_URL
277                         result=$(__do_curl ${!__dynvar}/reset)
278                 done
279
280         else
281                 # Check if docker app shall be fully managed by the test script
282                 __check_included_image 'CR'
283                 if [ $? -eq 1 ]; then
284                         echo -e $RED"The Callback Receiver app is not included in this test script"$ERED
285                         echo -e $RED"The Callback Receiver will not be started"$ERED
286                         exit
287                 fi
288
289                 __cr_export_vars
290
291                 app_data=""
292                 cntr=1
293                 while [ $cntr -le $CR_APP_COUNT ]; do
294                         app=$CR_APP_NAME"-cr-"$cntr
295                         app_data="$app_data $app"
296                         let cntr=cntr+1
297                 done
298
299                 echo "COMPOSE_PROJECT_NAME="$CR_APP_NAME > $SIM_GROUP/$CR_COMPOSE_DIR/.env
300
301                 __start_container $CR_COMPOSE_DIR "" NODOCKERARGS $CR_APP_COUNT $app_data
302
303                 cntr=1   #Counter for docker instance, starts on 1
304                 cntr2=0  #Couter for env var name, starts with 0 to be compablible with kube
305                 while [ $cntr -le $CR_APP_COUNT ]; do
306                         app=$CR_APP_NAME"-cr-"$cntr
307                         __dynvar="CR_SERVICE_PATH_"$cntr2
308                         __check_service_start $app ${!__dynvar}$CR_ALIVE_URL
309                         let cntr=cntr+1
310                         let cntr2=cntr2+1
311                 done
312         fi
313         echo ""
314 }
315
316 #Convert a cr path id to the value of the environment var holding the url
317 # arg: <cr-path-id>
318 # returns: <base-url-to-the-app>
319 __cr_get_service_path(){
320         if [ $# -ne 1 ]; then
321                 echo "DUMMY"
322                 return 1
323         fi
324         if [ $1 -lt 0 ] || [ $1 -ge $MAX_CR_APP_COUNT ]; then
325                 echo "DUMMY"
326                 return 1
327         fi
328         __dynvar="CR_SERVICE_PATH_"$1
329         echo ${!__dynvar}
330         return 0
331 }
332
333 # Tests if a variable value in the CR is equal to a target value and and optional timeout.
334 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
335 # equal to the target or not.
336 # Arg: <cr-path-id> <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
337 # before setting pass or fail depending on if the variable value becomes equal to the target
338 # value or not.
339 # (Function for test scripts)
340 cr_equal() {
341         if [ $# -eq 3 ] || [ $# -eq 4 ]; then
342                 CR_SERVICE_PATH=$(__cr_get_service_path $1)
343                 CR_ADAPTER=$CR_SERVICE_PATH
344                 if [ $? -ne 0 ]; then
345                         __print_err "<cr-path-id> missing or incorrect" $@
346                         return 1
347                 fi
348                 __var_test "CR" "$CR_SERVICE_PATH/counter/" $2 "=" $3 $4
349         else
350                 __print_err "Wrong args to cr_equal, needs three or four args: <cr-path-id>  <variable-name> <target-value> [ timeout ]" $@
351         fi
352 }
353
354 # Tests if a variable value in the CR is equal to or greater than the target value and and optional timeout.
355 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
356 # equal to the target or not.
357 # Arg: <cr-path-id> <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
358 # before setting pass or fail depending on if the variable value becomes equal to or greater than the target
359 # value or not.
360 # (Function for test scripts)
361 cr_greater_or_equal() {
362         if [ $# -eq 3 ] || [ $# -eq 4 ]; then
363                 CR_SERVICE_PATH=$(__cr_get_service_path $1)
364                 CR_ADAPTER=$CR_SERVICE_PATH
365                 if [ $? -ne 0 ]; then
366                         __print_err "<cr-path-id> missing or incorrect" $@
367                         return 1
368                 fi
369                 __var_test "CR" "$CR_SERVICE_PATH/counter/" $2 ">=" $3 $4
370         else
371                 __print_err "Wrong args to cr_equal, needs three or four args: <cr-path-id>  <variable-name> <target-value> [ timeout ]" $@
372         fi
373 }
374
375 # Tests if a variable value in the CR contains the target string and and optional timeout
376 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
377 # the target or not.
378 # Arg: <cr-path-id> <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
379 # before setting pass or fail depending on if the variable value contains the target
380 # value or not.
381 # (Function for test scripts)
382 cr_contains_str() {
383
384         if [ $# -eq 3 ] || [ $# -eq 4 ]; then
385                 CR_SERVICE_PATH=$(__cr_get_service_path $1)
386                 CR_ADAPTER=$CR_SERVICE_PATH
387                 if [ $? -ne 0 ]; then
388                         __print_err "<cr-path-id> missing or incorrect" $@
389                         return 1
390                 fi
391                 __var_test "CR" "$CR_SERVICE_PATH/counter/" $2 "contain_str" $3 $4
392                 return 0
393         else
394                 __print_err "needs two or three args: <cr-path-id> <variable-name> <target-value> [ timeout ]"
395                 return 1
396         fi
397 }
398
399 # Read a variable value from CR sim and send to stdout. Arg: <cr-path-id> <variable-name>
400 cr_read() {
401         CR_SERVICE_PATH=$(__cr_get_service_path $1)
402         CR_ADAPTER=$CR_SERVICE_PATH
403         if [ $? -ne 0 ]; then
404                 __print_err "<cr-path-id> missing or incorrect" $@
405                 return  1
406         fi
407         echo "$(__do_curl $CR_SERVICE_PATH/counter/$2)"
408 }
409
410 # Function to configure write delay on callbacks
411 # Delay given in seconds.
412 # arg <response-code> <cr-path-id>  <delay-in-sec>
413 # (Function for test scripts)
414 cr_delay_callback() {
415         __log_conf_start $@
416
417         if [ $# -ne 3 ]; then
418         __print_err "<response-code> <cr-path-id> <delay-in-sec>]" $@
419         return 1
420         fi
421
422         CR_SERVICE_PATH=$(__cr_get_service_path $2)
423         CR_ADAPTER=$CR_SERVICE_PATH
424         if [ $? -ne 0 ]; then
425                 __print_err "<cr-path-id> missing or incorrect" $@
426                 return 1
427         fi
428
429         res="$(__do_curl_to_api CR POST /forcedelay?delay=$3)"
430         status=${res:${#res}-3}
431
432         if [ $status -ne 200 ]; then
433                 __log_conf_fail_status_code $1 $status
434                 return 1
435         fi
436
437         __log_conf_ok
438         return 0
439 }
440
441 # CR API: Check the contents of all current ric sync events for one id from A1PMS
442 # <response-code> <cr-path-id> <id> [ EMPTY | ( <ric-id> )+ ]
443 # (Function for test scripts)
444 cr_api_check_all_sync_events() {
445         __log_test_start $@
446
447         if [ "$A1PMS_VERSION" != "V2" ]; then
448                 __log_test_fail_not_supported
449                 return 1
450         fi
451
452     if [ $# -lt 3 ]; then
453         __print_err "<response-code> <cr-path-id> <id> [ EMPTY | ( <ric-id> )+ ]" $@
454         return 1
455     fi
456
457         CR_SERVICE_PATH=$(__cr_get_service_path $2)
458         CR_ADAPTER=$CR_SERVICE_PATH
459         if [ $? -ne 0 ]; then
460                 __print_err "<cr-path-id> missing or incorrect" $@
461                 return 1
462         fi
463
464         query="/get-all-events/"$3
465         res="$(__do_curl_to_api CR GET $query)"
466         status=${res:${#res}-3}
467
468         if [ $status -ne $1 ]; then
469                 __log_test_fail_status_code $1 $status
470                 return 1
471         fi
472
473         if [ $# -gt 3 ]; then
474                 body=${res:0:${#res}-3}
475                 if [ $# -eq 4 ] && [ $4 == "EMPTY" ]; then
476                         targetJson="["
477                 else
478                         targetJson="["
479                         arr=(${@:4})
480
481                         for ((i=0; i<$(($#-3)); i=i+1)); do
482
483                                 if [ "$targetJson" != "[" ]; then
484                                         targetJson=$targetJson","
485                                 fi
486                                 targetJson=$targetJson"{\"ric_id\":\"${arr[$i]}\",\"event_type\":\"AVAILABLE\"}"
487                         done
488                 fi
489
490                 targetJson=$targetJson"]"
491                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
492                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
493
494                 if [ $res -ne 0 ]; then
495                         __log_test_fail_body
496                         return 1
497                 fi
498         fi
499         __log_test_pass
500         return 0
501 }
502
503 # CR API: Check the contents of all current status events for one id from ICS
504 # <response-code> <cr-path-id> <id> [ EMPTY | ( <status> )+ ]
505 # (Function for test scripts)
506 cr_api_check_all_ics_events() {
507         __log_test_start $@
508
509     if [ $# -lt 3 ]; then
510         __print_err "<response-code> <cr-path-id> <id> [ EMPTY | ( <status> )+ ]" $@
511         return 1
512     fi
513
514         CR_SERVICE_PATH=$(__cr_get_service_path $2)
515         CR_ADAPTER=$CR_SERVICE_PATH
516         if [ $? -ne 0 ]; then
517                 __print_err "<cr-path-id> missing or incorrect" $@
518                 return 1
519         fi
520
521         query="/get-all-events/"$3
522         res="$(__do_curl_to_api CR GET $query)"
523         status=${res:${#res}-3}
524
525         if [ $status -ne $1 ]; then
526                 __log_test_fail_status_code $1 $status
527                 return 1
528         fi
529
530         if [ $# -gt 3 ]; then
531                 body=${res:0:${#res}-3}
532                 if [ $# -eq 4 ] && [ $4 == "EMPTY" ]; then
533                         targetJson="["
534                 else
535                         targetJson="["
536                         arr=(${@:4})
537
538                         for ((i=0; i<$(($#-3)); i=i+1)); do
539
540                                 if [ "$targetJson" != "[" ]; then
541                                         targetJson=$targetJson","
542                                 fi
543                                 targetJson=$targetJson"{\"eiJobStatus\":\"${arr[$i]}\"}"
544                         done
545                 fi
546
547                 targetJson=$targetJson"]"
548                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
549                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
550
551                 if [ $res -ne 0 ]; then
552                         __log_test_fail_body
553                         return 1
554                 fi
555         fi
556         __log_test_pass
557         return 0
558 }
559
560 # CR API: Check the contents of all current type subscription events for one id from ICS
561 # <response-code> <cr-path-id> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]
562 # (Function for test scripts)
563 cr_api_check_all_ics_subscription_events() {
564         __log_test_start $@
565
566         #Valid number of parameter 3,4,8,12
567         paramError=1
568         if [ $# -eq 3 ]; then
569                 paramError=0
570         fi
571         if [ $# -eq 4 ] && [ "$4" == "EMPTY" ]; then
572                 paramError=0
573         fi
574         variablecount=$(($#-3))
575         if [ $# -gt 4 ] && [ $(($variablecount%3)) -eq 0 ]; then
576                 paramError=0
577         fi
578         if [ $paramError -eq 1 ]; then
579                 __print_err "<response-code> <cr-path-id> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]" $@
580                 return 1
581         fi
582
583         CR_SERVICE_PATH=$(__cr_get_service_path $2)
584         CR_ADAPTER=$CR_SERVICE_PATH
585         if [ $? -ne 0 ]; then
586                 __print_err "<cr-path-id> missing or incorrect" $@
587                 return 1
588         fi
589
590         query="/get-all-events/"$3
591         res="$(__do_curl_to_api CR GET $query)"
592         status=${res:${#res}-3}
593
594         if [ $status -ne $1 ]; then
595                 __log_test_fail_status_code $1 $status
596                 return 1
597         fi
598
599         if [ $# -gt 3 ]; then
600                 body=${res:0:${#res}-3}
601                 targetJson="["
602                 if [ $# -gt 4 ]; then
603                         arr=(${@:4})
604                         for ((i=0; i<$(($#-4)); i=i+3)); do
605                                 if [ "$targetJson" != "[" ]; then
606                                         targetJson=$targetJson","
607                                 fi
608                                 if [ -f ${arr[$i+1]} ]; then
609                                         jobfile=$(cat ${arr[$i+1]})
610                                 else
611                                         __log_test_fail_general "Job schema file "${arr[$i+1]}", does not exist"
612                                         return 1
613                                 fi
614                                 targetJson=$targetJson"{\"info_type_id\":\"${arr[$i]}\",\"job_data_schema\":$jobfile,\"status\":\"${arr[$i+2]}\"}"
615                         done
616                 fi
617                 targetJson=$targetJson"]"
618
619                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
620                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
621
622                 if [ $res -ne 0 ]; then
623                         __log_test_fail_body
624                         return 1
625                 fi
626         fi
627
628         __log_test_pass
629         return 0
630 }
631
632
633 # CR API: Reset all events and counters
634 # Arg: <cr-path-id>
635 # (Function for test scripts)
636 cr_api_reset() {
637         __log_conf_start $@
638
639         if [ $# -ne 1 ]; then
640                 __print_err "<cr-path-id>" $@
641                 return 1
642         fi
643
644         CR_SERVICE_PATH=$(__cr_get_service_path $1)
645         CR_ADAPTER=$CR_SERVICE_PATH
646         if [ $? -ne 0 ]; then
647                 __print_err "<cr-path-id> missing or incorrect" $@
648                 return 1
649         fi
650
651         res="$(__do_curl_to_api CR GET /reset)"
652         status=${res:${#res}-3}
653
654         if [ $status -ne 200 ]; then
655                 __log_conf_fail_status_code $1 $status
656                 return 1
657         fi
658
659         __log_conf_ok
660         return 0
661 }
662
663
664 # CR API: Check the contents of all json events for path
665 # <response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg>+ )
666 # (Function for test scripts)
667 cr_api_check_all_generic_json_events() {
668         __log_test_start $@
669
670         if [ $# -lt 4 ]; then
671                 __print_err "<response-code> <cr-path-id>  <topic-url> (EMPTY | <json-msg>+ )" $@
672                 return 1
673         fi
674
675         CR_SERVICE_PATH=$(__cr_get_service_path $2)
676         CR_ADAPTER=$CR_SERVICE_PATH
677         if [ $? -ne 0 ]; then
678                 __print_err "<cr-path-id> missing or incorrect" $@
679                 return 1
680         fi
681
682         query="/get-all-events/"$3
683         res="$(__do_curl_to_api CR GET $query)"
684         status=${res:${#res}-3}
685
686         if [ $status -ne $1 ]; then
687                 __log_test_fail_status_code $1 $status
688                 return 1
689         fi
690         body=${res:0:${#res}-3}
691         targetJson="["
692
693         if [ $4 != "EMPTY" ]; then
694                 shift
695                 shift
696                 shift
697                 while [ $# -gt 0 ]; do
698                         if [ "$targetJson" != "[" ]; then
699                                 targetJson=$targetJson","
700                         fi
701                         targetJson=$targetJson$1
702                         shift
703                 done
704         fi
705         targetJson=$targetJson"]"
706
707         echo " TARGET JSON: $targetJson" >> $HTTPLOG
708         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
709
710         if [ $res -ne 0 ]; then
711                 __log_test_fail_body
712                 return 1
713         fi
714
715         __log_test_pass
716         return 0
717 }
718
719
720 # CR API: Check a single (oldest) json event (or none if empty) for path
721 # <response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg> )
722 # (Function for test scripts)
723 cr_api_check_single_generic_json_event() {
724         __log_test_start $@
725
726         if [ $# -ne 4 ]; then
727                 __print_err "<response-code> <cr-path-id>  <topic-url> (EMPTY | <json-msg> )" $@
728                 return 1
729         fi
730
731         CR_SERVICE_PATH=$(__cr_get_service_path $2)
732         CR_ADAPTER=$CR_SERVICE_PATH
733         if [ $? -ne 0 ]; then
734                 __print_err "<cr-path-id> missing or incorrect" $@
735                 return 1
736         fi
737
738         query="/get-event/"$3
739         res="$(__do_curl_to_api CR GET $query)"
740         status=${res:${#res}-3}
741
742         if [ $status -ne $1 ]; then
743                 __log_test_fail_status_code $1 $status
744                 return 1
745         fi
746         body=${res:0:${#res}-3}
747         targetJson=$4
748
749         if [ $targetJson == "EMPTY" ] && [ ${#body} -ne 0 ]; then
750                 __log_test_fail_body
751                 return 1
752         fi
753         echo " TARGET JSON: $targetJson" >> $HTTPLOG
754         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
755
756         if [ $res -ne 0 ]; then
757                 __log_test_fail_body
758                 return 1
759         fi
760
761         __log_test_pass
762         return 0
763 }
764
765 # CR API: Check a single (oldest) json in md5 format (or none if empty) for path.
766 # Note that if a json message is given, it shall be compact, no ws except inside string.
767 # The MD5 will generate different hash if ws is present or not in otherwise equivalent json
768 # arg: <response-code> <cr-path-id> <topic-url> (EMPTY | <data-msg> )
769 # (Function for test scripts)
770 cr_api_check_single_generic_event_md5() {
771         __log_test_start $@
772
773         if [ $# -ne 4 ]; then
774                 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <data-msg> )" $@
775                 return 1
776         fi
777
778         CR_SERVICE_PATH=$(__cr_get_service_path $2)
779         CR_ADAPTER=$CR_SERVICE_PATH
780         if [ $? -ne 0 ]; then
781                 __print_err "<cr-path-id> missing or incorrect" $@
782                 return 1
783         fi
784
785         query="/get-event/"$3
786         res="$(__do_curl_to_api CR GET $query)"
787         status=${res:${#res}-3}
788
789         if [ $status -ne $1 ]; then
790                 __log_test_fail_status_code $1 $status
791                 return 1
792         fi
793         body=${res:0:${#res}-3}
794         if [ $4 == "EMPTY" ]; then
795                 if [ ${#body} -ne 0 ]; then
796                         __log_test_fail_body
797                         return 1
798                 else
799                         __log_test_pass
800                         return 0
801                 fi
802         fi
803         command -v md5 > /dev/null # Mac
804         if [ $? -eq 0 ]; then
805                 targetMd5=$(echo -n "$4" | md5)
806         else
807                 command -v md5sum > /dev/null # Linux
808                 if [ $? -eq 0 ]; then
809                         targetMd5=$(echo -n "$4" | md5sum | cut -d' ' -f 1)  # Need to cut additional info printed by cmd
810                 else
811                         __log_test_fail_general "Command md5 nor md5sum is available"
812                         return 1
813                 fi
814         fi
815         targetMd5="\""$targetMd5"\"" #Quotes needed
816
817         echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG
818
819         if [ "$body" != "$targetMd5" ]; then
820                 __log_test_fail_body
821                 return 1
822         fi
823
824         __log_test_pass
825         return 0
826 }
827
828 # CR API: Check a single (oldest) event in md5 format (or none if empty) for path.
829 # Note that if a file with json message is given, the json shall be compact, no ws except inside string and not newlines.
830 # The MD5 will generate different hash if ws/newlines is present or not in otherwise equivalent json
831 # arg: <response-code> <cr-path-id> <topic-url> (EMPTY | <data-file> )
832 # (Function for test scripts)
833 cr_api_check_single_generic_event_md5_file() {
834         __log_test_start $@
835
836         if [ $# -ne 4 ]; then
837                 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <data-file> )" $@
838                 return 1
839         fi
840
841         CR_SERVICE_PATH=$(__cr_get_service_path $2)
842         CR_ADAPTER=$CR_SERVICE_PATH
843         if [ $? -ne 0 ]; then
844                 __print_err "<cr-path-id> missing or incorrect" $@
845                 return 1
846         fi
847
848         query="/get-event/"$3
849         res="$(__do_curl_to_api CR GET $query)"
850         status=${res:${#res}-3}
851
852         if [ $status -ne $1 ]; then
853                 __log_test_fail_status_code $1 $status
854                 return 1
855         fi
856         body=${res:0:${#res}-3}
857         if [ $4 == "EMPTY" ]; then
858                 if [ ${#body} -ne 0 ]; then
859                         __log_test_fail_body
860                         return 1
861                 else
862                         __log_test_pass
863                         return 0
864                 fi
865         fi
866
867         if [ ! -f $4 ]; then
868                 __log_test_fail_general "File $3 does not exist"
869                 return 1
870         fi
871
872         filedata=$(cat $4)
873
874         command -v md5 > /dev/null # Mac
875         if [ $? -eq 0 ]; then
876                 targetMd5=$(echo -n "$filedata" | md5)
877         else
878                 command -v md5sum > /dev/null # Linux
879                 if [ $? -eq 0 ]; then
880                         targetMd5=$(echo -n "$filedata" | md5sum | cut -d' ' -f 1)  # Need to cut additional info printed by cmd
881                 else
882                         __log_test_fail_general "Command md5 nor md5sum is available"
883                         return 1
884                 fi
885         fi
886         targetMd5="\""$targetMd5"\""   #Quotes needed
887
888         echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG
889
890         if [ "$body" != "$targetMd5" ]; then
891                 __log_test_fail_body
892                 return 1
893         fi
894
895         __log_test_pass
896         return 0
897 }