7541a52860515f2c2ce940eeb29ca3937addf5fb
[osm/tests.git] / robot-systest / lib / rest_lib.robot
1 #   Licensed under the Apache License, Version 2.0 (the "License");
2 #   you may not use this file except in compliance with the License.
3 #   You may obtain a copy of the License at
4 #
5 #       http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #   Unless required by applicable law or agreed to in writing, software
8 #   distributed under the License is distributed on an "AS IS" BASIS,
9 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 #   See the License for the specific language governing permissions and
11 #   limitations under the License
12
13 *** Settings ***
14 Documentation   Library with sol003 keywords and variables .
15
16 Library   RequestsLibrary
17 Library   JsonValidator
18 Library   yaml
19 Library   JSONLibrary
20 Library   String
21 Library   OperatingSystem
22 Resource   %{ROBOT_DEVOPS_FOLDER}/lib/vim_lib.robot
23 Variables   %{ROBOT_DEVOPS_FOLDER}/resources/sol003_01-vnf_lifecycle_management.py
24
25 *** Variables ***
26 @{success_status_code_list}   200   201   202   204
27 ${auth_token_uri}   /osm/admin/v1/tokens
28 ${HOST}   ${EMPTY}
29
30 *** Keywords ***
31 Get Auth Token
32     [Tags]   auth_token
33     Get Hostname
34     Create Session   osmhit   ${HOST}   disable_warnings=1
35     &{headers}   Create Dictionary   Content-Type=application/json   Accept=application/json
36     &{data}   Create Dictionary   username=${osm_user}   password=${osm_password}   project-id=${osm_project}
37     ${resp}=   Post On Session   osmhit   ${auth_token_uri}   json=${data}   headers=${headers}
38     Log   ${resp}
39     Pass Execution If   ${resp.status_code} in ${success_status_code_list}   Get Auth Token completed
40     Set Suite Variable   ${token_status_code}   ${resp.status_code}
41     ${access_token}=   Get Value From Json   ${resp.json()}   $..id
42     Set Test Variable   ${access_token}   ${access_token[0]}
43     Set Test Variable   ${token_response}   ${resp}
44     sleep   2s
45
46 Set Dockerized Host
47     [Arguments]   ${env_host}
48     Set Suite Variable   ${HOST}   https://${env_host}
49
50 Set Standalone Host
51     [Arguments]   ${env_host}
52     Set Suite Variable   ${HOST}   https://${env_host}:9999
53
54 Read Directory
55     [Documentation]   To Read Current Direct ory
56     ${Directory}=   Replace String   ${CURDIR}   lib   resources/
57     ${json_path}=   Set Variable   ${Directory}
58     [Return]   ${JsonPath}
59
60 Post Api Request
61     [Arguments]   ${PostApi}
62     [Documentation]   Keword to Post API Request
63     Create Session   APISession   ${HOST}
64     &{headers}   Create Dictionary   Content-Type=application/json   Accept=application/json   Authorization=Bearer ${access_token}
65     ${request_response}=   Post On Session   APISession   ${PostApi}   json=${updated_json_obj}   headers=${headers}
66     Set Suite Variable   ${request_response}
67     ${response_statuscode}   Convert To String   ${request_response.status_code}
68     Set Suite Variable   ${response_statuscode}
69
70 Get Hostname
71     [Documentation]   Keyword to get the default osm Hostname
72     ${nbi_host}=   Get Environment Variable   OSM_HOSTNAME
73     ${passed}=   Run Keyword And Return Status   Should Contain   ${nbi_host}   :
74     Run Keyword If   ${passed}   Set Dockerized Host   ${nbi_host}
75     ...    ELSE   Set Standalone Host   ${nbi_host}
76
77 Get ID
78     [Arguments]   ${Key}
79     Pass Execution If   ${request_response.status_code} in ${success_status_code_list}   Get Auth Token completed
80     ${id}=   Get Value From Json   ${request_response.json()}   $..${Key}
81     Set Suite Variable   ${value}   ${id[0]}
82     [Return]   ${value}
83
84 Update Json Value
85     [Arguments]   ${JsonInput}   ${key}
86     ${json_path}=   Read Directory
87     ${json_obj}=   Load JSON From File   ${json_path}/${JsonInput}
88     ${updated_json_obj}=   JSONLibrary.Update Value To Json   ${json_obj}   $..${key}   ${created_vim_account_id}
89     Set Suite Variable   ${updated_json_obj}
90
91 Get Api Request
92     [Arguments]   ${uri}
93     [Documentation]   Keyword to Get API Request
94     Create Session   Session   ${HOST}
95     &{headers}   Create Dictionary   Content-Type=application/json   Accept=application/json   Authorization=Bearer ${access_token}
96     ${request_response}=   Get On Session   Session   ${uri}   headers=${Headers}
97     Set Suite Variable   ${request_response}
98     ${response_statuscode}   Convert To String   ${request_response.status_code}
99     Set Suite Variable   ${response_statuscode}