Added r-app catalogue to demo test case
[nonrtric.git] / test / common / rapp_catalogue_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 specific test functions for RAPP Catalogue API
21
22 . ../common/api_curl.sh
23
24 # Tests if a variable value in the RAPP Catalogue is equal to a target value and and optional timeout.
25 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
26 # equal to the target or not.
27 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
28 # before setting pass or fail depending on if the variable value becomes equal to the target
29 # value or not.
30 # (Function for test scripts)
31 rc_equal() {
32         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
33                 __var_test RC "$LOCALHOST$RC_EXTERNAL_PORT/" $1 "=" $2 $3
34         else
35                 __print_err "Wrong args to ecs_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
36         fi
37 }
38
39
40 ##########################################
41 #########  RAPP Catalogue API   ##########
42 ##########################################
43 #Function prefix: rapp_cat_api
44
45 # API Test function: GET /services
46 # args: <response-code> [(<service-id> <version> <display-name> <description>)+ | EMPTY ]
47 # (Function for test scripts)
48 rapp_cat_api_get_services() {
49         __log_test_start $@
50
51         if [ $# -lt 1 ]; then
52                 __print_err "<response-code> [(<service-id> <version> <display-name> <description>)+ | EMPTY ]" $@
53                 return 1
54         fi
55         query="/services"
56     res="$(__do_curl_to_api RC GET $query)"
57     status=${res:${#res}-3}
58
59         if [ $status -ne $1 ]; then
60                 __log_test_fail_status_code $1 $status
61                 return 1
62         fi
63
64         if [ $# -gt 1 ]; then
65                 body=${res:0:${#res}-3}
66                 targetJson="["
67                 arr=(${@:2})
68
69                 if [ $# -eq 2 ]; then
70                         targetJson="[]"
71                 else
72                         for ((i=0; i<$(($#-1)); i=i+4)); do
73                                 if [ "$targetJson" != "[" ]; then
74                                         targetJson=$targetJson","
75                                 fi
76                                 targetJson=$targetJson"{\"name\": \"${arr[$i]}\",\"version\": \"${arr[$i+1]}\",\"display_name\": \"${arr[$i+2]}\",\"description\": \"${arr[$i+3]}\",\"registrationDate\": \"????\"}"
77                         done
78                         targetJson=$targetJson"]"
79                 fi
80                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
81                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
82
83                 if [ $res -ne 0 ]; then
84                         __log_test_fail_body
85                         return 1
86                 fi
87         fi
88
89         __log_test_pass
90         return 0
91 }
92
93 # API Test function: PUT ​/services/{service-id}
94 # args: <response-code> <service-id> <version> <display-name> <description>
95 # (Function for test scripts)
96 rapp_cat_api_put_service() {
97         __log_test_start $@
98
99         if [ $# -ne 5 ]; then
100                 __print_err "<response-code> <service-id> <version> <display-name> <description>" $@
101                 return 1
102         fi
103
104         inputJson="{\"version\": \"$3\",\"display_name\": \"$4\",\"description\": \"$5\"}"
105         file="./tmp/.p.json"
106         echo "$inputJson" > $file
107         query="/services/$2"
108         res="$(__do_curl_to_api RC PUT $query $file)"
109     status=${res:${#res}-3}
110
111         if [ $status -ne $1 ]; then
112                 __log_test_fail_status_code $1 $status
113                 return 1
114         fi
115
116         __log_test_pass
117         return 0
118 }
119
120 # API Test function: GET ​/services/{service-id}
121 # args: <response-code> <service-id>
122 # (Function for test scripts)
123 rapp_cat_api_get_service() {
124         __log_test_start $@
125
126         if [ $# -lt 2 ] || [ $# -gt 5 ]; then
127                 __print_err "<response-code> <service-id> <version> <display-name> <description>" $@
128                 return 1
129         fi
130
131         query="/services/$2"
132     res="$(__do_curl_to_api RC GET $query)"
133     status=${res:${#res}-3}
134
135         if [ $status -ne $1 ]; then
136                 __log_test_fail_status_code $1 $status
137                 return 1
138         fi
139
140         if [ $# -gt 2 ]; then
141                 body=${res:0:${#res}-3}
142                 targetJson="{\"name\": \"$2\",\"version\": \"$3\",\"display_name\": \"$4\",\"description\": \"$5\",\"registrationDate\": \"????\"}"
143                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
144                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
145
146                 if [ $res -ne 0 ]; then
147                         __log_test_fail_body
148                         return 1
149                 fi
150         fi
151
152         __log_test_pass
153         return 0
154 }
155
156 # API Test function: DELETE ​/services/{service-id}
157 # args: <response-code> <service-id>
158 # (Function for test scripts)
159 rapp_cat_api_delete_service() {
160         __log_test_start $@
161
162         if [ $# -ne 2 ]; then
163                 __print_err "<response-code> <service-id>" $@
164                 return 1
165         fi
166
167         query="/services/$2"
168         res="$(__do_curl_to_api RC DELETE $query)"
169     status=${res:${#res}-3}
170
171         if [ $status -ne $1 ]; then
172                 __log_test_fail_status_code $1 $status
173                 return 1
174         fi
175
176         __log_test_pass
177         return 0
178 }