f463883315358bafbfeac58e26ac91e0567e2741
[testsuite/python-testing-utils.git] / robotframework-onap / ONAPLibrary / RequestSOKeywords.py
1 # Copyright 2019 AT&T Intellectual Property. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 from ONAPLibrary.BaseSOKeywords import BaseSOKeywords
15 from robot.api import logger
16 from robot.api.deco import keyword
17 from robot.libraries.BuiltIn import BuiltIn
18
19
20 class RequestSOKeywords(object):
21     """SO is an ONAP testing library for Robot Framework that provides functionality for interacting with the serivce
22     orchestrator. """
23
24     def __init__(self):
25         super(RequestSOKeywords, self).__init__()
26         self.application_id = "robot-ete"
27         self.builtin = BuiltIn()
28         self.base_keywords = BaseSOKeywords()
29
30     @keyword
31     def run_polling_get_request(self, endpoint, data_path, complete_states=None, fail_states=None, tries=20,
32                                 interval=15, auth=None):
33         """Runs an SO get request until a certain state is received."""
34         if fail_states is None:
35             fail_states = ["FAILED"]
36         if complete_states is None:
37             complete_states = ["COMPLETE"]
38         # do this until it is done
39         for i in range(tries):
40             resp = self.base_keywords.get_request(endpoint, data_path, auth=auth)
41             self.builtin.should_not_contain_any(resp.text, fail_states)
42             logger.info(resp.json()['request']['requestStatus']['requestState'])
43             if resp.json()['request']['requestStatus']['requestState'] in complete_states:
44                 logger.info("Received response from so " + resp.text)
45                 return True, resp
46             else:
47                 self.builtin.sleep(interval, "Response from SO is not in requested status")
48         return False, None
49
50     @keyword
51     def run_create_request(self, endpoint, data_path, data, auth=None):
52         """Runs an SO create request and returns the request id and instance id."""
53         response = self.base_keywords.post_request(endpoint, data_path, data, auth=auth)
54         logger.info("Creation request submitted to SO, got response")
55
56         req_id = response.get('requestReferences', {}).get('requestId', '')
57         instance_id = response.get('requestReferences', {}).get('instanceId', '')
58
59         return req_id, instance_id