Merge "Change to use checked exceptions in rAPP Catalogue"
[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 . ../common/api_curl.sh
21
22 ### Admin API functions for the Callback Reciver
23
24
25 # Excute a curl cmd towards a Callback Reciver admin interface and check the response code.
26 # args: <expected-response-code> <curl-cmd-string>
27 __execute_curl_to_cr() {
28         echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
29         echo " CMD: $2" >> $HTTPLOG
30         res="$($2)"
31         echo " RESP: $res" >> $HTTPLOG
32         retcode=$?
33     if [ $retcode -ne 0 ]; then
34                 ((RES_CONF_FAIL++))
35                 echo " RETCODE: "$retcode
36         echo -e $RED" FAIL - fatal error when executing curl."$ERED
37         return 1
38     fi
39     status=${res:${#res}-3}
40     if [ $status -eq $1 ]; then
41         echo -e $GREEN" OK"$EGREEN
42         return 0
43     fi
44     echo -e $RED" FAIL - expected http response: "$1" but got http response: "$status $ERED
45         ((RES_CONF_FAIL++))
46     return 1
47 }
48
49 # Tests if a variable value in the CR is equal to a target value and and optional timeout.
50 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
51 # equal to the target or not.
52 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
53 # before setting pass or fail depending on if the variable value becomes equal to the target
54 # value or not.
55 # (Function for test scripts)
56 cr_equal() {
57         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
58                 __var_test "CR" "$LOCALHOST$CR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
59         else
60                 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
61         fi
62 }
63
64 # CR API: Check the contents of all current ric sync events for one id from PMS
65 # <response-code> <id> [ EMPTY | ( <ric-id> )+ ]
66 # (Function for test scripts)
67 cr_api_check_all_sync_events() {
68         __log_test_start $@
69
70         if [ "$PMS_VERSION" != "V2" ]; then
71                 __log_test_fail_not_supported
72                 return 1
73         fi
74
75     if [ $# -lt 2 ]; then
76         __print_err "<response-code> <id> [ EMPTY | ( <ric-id> )+ ]" $@
77         return 1
78     fi
79
80         query="/get-all-events/"$2
81         res="$(__do_curl_to_api CR GET $query)"
82         status=${res:${#res}-3}
83
84         if [ $status -ne $1 ]; then
85                 __log_test_fail_status_code $1 $status
86                 return 1
87         fi
88
89         if [ $# -gt 2 ]; then
90                 body=${res:0:${#res}-3}
91                 if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then
92                         targetJson="["
93                 else
94                         targetJson="["
95                         arr=(${@:3})
96
97                         for ((i=0; i<$(($#-2)); i=i+1)); do
98
99                                 if [ "$targetJson" != "[" ]; then
100                                         targetJson=$targetJson","
101                                 fi
102                                 targetJson=$targetJson"{\"ric_id\":\"${arr[$i]}\",\"event_type\":\"AVAILABLE\"}"
103                         done
104                 fi
105
106                 targetJson=$targetJson"]"
107                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
108                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
109
110                 if [ $res -ne 0 ]; then
111                         __log_test_fail_body
112                         return 1
113                 fi
114         fi
115         __log_test_pass
116         return 0
117 }
118
119 # CR API: Check the contents of all current status events for one id from ECS
120 # <response-code> <id> [ EMPTY | ( <status> )+ ]
121 # (Function for test scripts)
122 cr_api_check_all_ecs_events() {
123         __log_test_start $@
124
125     if [ $# -lt 2 ]; then
126         __print_err "<response-code> <id> [ EMPTY | ( <status> )+ ]" $@
127         return 1
128     fi
129
130         query="/get-all-events/"$2
131         res="$(__do_curl_to_api CR GET $query)"
132         status=${res:${#res}-3}
133
134         if [ $status -ne $1 ]; then
135                 __log_test_fail_status_code $1 $status
136                 return 1
137         fi
138
139         if [ $# -gt 2 ]; then
140                 body=${res:0:${#res}-3}
141                 if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then
142                         targetJson="["
143                 else
144                         targetJson="["
145                         arr=(${@:3})
146
147                         for ((i=0; i<$(($#-2)); i=i+1)); do
148
149                                 if [ "$targetJson" != "[" ]; then
150                                         targetJson=$targetJson","
151                                 fi
152                                 targetJson=$targetJson"{\"eiJobStatus\":\"${arr[$i]}\"}"
153                         done
154                 fi
155
156                 targetJson=$targetJson"]"
157                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
158                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
159
160                 if [ $res -ne 0 ]; then
161                         __log_test_fail_body
162                         return 1
163                 fi
164         fi
165         __log_test_pass
166         return 0
167 }