Netconf RPC test
[integration/test.git] / csit / libraries / TemplatedRequests.robot
1 *** Settings ***
2 Documentation     Resource for supporting http Requests based on data stored in files.
3 ...
4 ...               Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
5 ...
6 ...               This program and the accompanying materials are made available under the
7 ...               terms of the Eclipse Public License v1.0 which accompanies this distribution,
8 ...               and is available at http://www.eclipse.org/legal/epl-v10.html
9 ...
10 ...
11 ...               The main strength of this library are *_As_*_Templated keywords
12 ...               User gives a path to directory where files with templates for URI
13 ...               and XML (or JSON) data are present, and a mapping with substitution to make;
14 ...               the keywords will take it from there.
15 ...               Mapping can be given as a dict object, or as its json text representation.
16 ...               Simple example (tidy insists on single space where 4 spaces should be):
17 ...               TemplatedRequests.Put_As_Json_Templated folder=${VAR_BASE}/person mapping={"NAME":"joe"}
18 ...               TemplatedRequests.Get_As_Json_Templated folder=${VAR_BASE}/person mapping={"NAME":"joe"} verify=True
19 ...
20 ...               In that example, we are PUTting "person" data with specified value for "NAME" placeholder.
21 ...               We are not verifying PUT response (probably empty string which is not a valid JSON),
22 ...               but we are issuing GET (same URI) and verifying the repsonse matches the same data.
23 ...               Both lines are returning text response, but in the example we are not saving it into variable.
24 ...
25 ...               Optionally, *_As_*_Templated keywords call verification of response.
26 ...               There are separate Verify_* keywords, for users who use intermediate processing.
27 ...               For JSON responses, there is a support for normalizing.
28 ...               *_Templated keywords without As allow more customization at cost of more arguments.
29 ...               *_Uri keywords do not use templates, but may be useful in general,
30 ...               perhaps for users who call Resolve_Text_* keywords.
31 ...               *_As_*_Uri are the less flexible but less argument-heavy versions of *_Uri keywords.
32 ...
33 ...               This resource supports generating data with simple lists.
34 ...               ${iterations} argument control number of items, "$i" will be substituted
35 ...               automatically (not by the provided mapping) with integers starting with ${iter_start} (default 1).
36 ...               For example "iterations=2 iter_start=3" will create items with i=3 and i=4.
37 ...
38 ...               This implementation relies on file names to distinguish data.
39 ...               Each file is expected to end in newline, compiled data has final newline removed.
40 ...               Here is a table so that users can create their own templates:
41 ...               location.uri: Template with URI.
42 ...               data.xml: Template with XML data to send, or GET data to expect.
43 ...               data.json: Template with JSON data to send, or GET data to expect.
44 ...               post_data.xml: Template with XML data to POST, (different from GET response).
45 ...               post_data.json: Template with JSON data to POST, (different from GET response).
46 ...               response.xml: Template with PUT or POST XML response to expect.
47 ...               response.json: Template with PUT or POST JSON response to expect.
48 ...               *.prolog.*: Temlate with data before iterated items.
49 ...               *.item.*: Template with data piece corresponding to one item.
50 ...               *.epilog.*: Temlate with data after iterated items.
51 ...
52 ...               One typical use of this Resource is to make runtime changes to ODL configuration.
53 ...               Different ODL parts have varying ways of configuration,
54 ...               this library affects only the Config Subsystem way.
55 ...               Config Subsystem has (except for Java APIs mostly available only from inside of ODL)
56 ...               a NETCONF server as its publicly available entry point.
57 ...               Netconf-connector feature makes this netconf server available for RESTCONF calls.
58 ...               Be careful to use appropriate feature, odl-netconf-connector* does not work in cluster.
59 ...
60 ...               This Resource currently calls RequestsLibrary directly,
61 ...               so it does not work with AuthStandalone or similar.
62 ...               This Resource does not maintain any internal Sessions.
63 ...               If caller does not provide any, session with alias "default" is used.
64 ...               There is a helper Keyword to create the "default" session.
65 ...               The session used is assumed to have most things pre-configured appropriately,
66 ...               which includes auth, host, port and (lack of) base URI.
67 ...               It is recommended to have everything past port (for example /restconf) be defined
68 ...               not in the session, but in URI data of individual templates.
69 ...               Headers are set in Keywords appropriately. Http session's timout is configurable
70 ...               both on session level (where it becomes a default value for requests) and on request
71 ...               level (when present, it overrides the session value). To override the default
72 ...               value keywords' http_timeout parameter may be used.
73 ...
74 ...               These Keywords contain frequent BuiltIn.Log invocations,
75 ...               so they are not suited for scale or performance suites.
76 ...               And as usual, performance tests should use specialized utilities,
77 ...               as Robot in general and this Resource specifically will be too slow.
78 ...
79 ...               As this Resource makes assumptions about intended headers,
80 ...               it is not flexible enough for suites specifically testing Restconf corner cases.
81 ...               Also, list of allowed http status codes is quite rigid and broad.
82 ...
83 ...               Rules for ordering Keywords within this Resource:
84 ...               1. User friendlier Keywords first.
85 ...               2. Get, Put, Post, Delete, Verify.
86 ...               3. Within class of equally usable, use order in which a suite would call them.
87 ...               4. Higher-level Keywords first.
88 ...               5. Json before Xml.
89 ...               Motivation: Users read from the start, so it is important
90 ...               to offer them the better-to-use Keywords first.
91 ...               https://wiki.opendaylight.org/view/Integration/Test/Test_Code_Guidelines#Keyword_ordering
92 ...               In this case, templates are nicer that raw data,
93 ...               *_As_* keywords are better than messing wth explicit header dicts,
94 ...               Json is less prone to element ordering issues.
95 ...               PUT does not fail on existing element, also it does not allow
96 ...               shortened URIs (container instead keyed list element) as Post does.
97 ...
98 ...               TODO: Add ability to override allowed status codes,
99 ...               so that negative tests do not need to parse the failure message.
100 ...
101 ...               TODO: Migrate suites to this Resource and remove *ViaRestconf Resources.
102 ...
103 ...               TODO: Currently the verification step is only in *_As_*_Templated keywords.
104 ...               It could be moved to "non-as" *_Templated ones,
105 ...               but that would take even more horizontal space. Is it worth doing?
106 ...
107 ...               TODO: Should iterations=0 be supported for JSON (remove [])?
108 ...
109 ...               TODO: Currently, ${ACCEPT_EMPTY} is used for JSON-expecting requests.
110 ...               perhaps explicit ${ACCEPT_JSON} will be better, even if it sends few bytes more?
111 Library           Collections
112 Library           OperatingSystem
113 Library           RequestsLibrary
114 Library           ${CURDIR}/norm_json.py
115 Variables         ${CURDIR}/../variables/Variables.py
116
117 *** Variables ***
118 # TODO: Make the following list more narrow when streams without Bug 2594 fix (up to beryllium) are no longer used.
119 @{ALLOWED_DELETE_STATUS_CODES}    ${200}    ${201}    ${204}    ${404}    # List of integers, not strings. Used by DELETE if the resource may be not present.
120 @{ALLOWED_STATUS_CODES}    ${200}    ${201}    ${204}    # List of integers, not strings. Used by both PUT and DELETE (if the resource should have been present).
121 @{DATA_VALIDATION_ERROR}    ${400}    # For testing mildly negative scenarios where ODL reports user error.
122 @{DELETED_STATUS_CODE}    ${404}    # List of integers, not strings. Used by DELETE if the resource may be not present.
123 # TODO: Add option for delete to require 404.
124 @{INTERNAL_SERVER_ERROR}    ${500}    # Only for testing severely negative scenarios where ODL cannot recover.
125 @{KEYS_WITH_BITS}    op    # the default list with keys to be sorted when norm_json libray is used
126 @{NO_STATUS_CODES}
127 @{UNAUTHORIZED_STATUS_CODES}    ${401}    # List of integers, not strings. Used in Keystone Authentication when the user is not authorized to use the requested resource.
128
129 *** Keywords ***
130 Create_Default_Session
131     [Arguments]    ${url}=http://${ODL_SYSTEM_IP}:${RESTCONFPORT}    ${auth}=${AUTH}    ${timeout}=1    ${max_retries}=0
132     [Documentation]    Create "default" session to ${url} with authentication and connection parameters.
133     ...    This Keyword is in this Resource only so that user do not need to call RequestsLibrary directly.
134     RequestsLibrary.Create_Session    alias=default    url=${url}    auth=${auth}    timeout=${timeout}    max_retries=${max_retries}
135
136 Get_As_Json_Templated
137     [Arguments]    ${folder}    ${mapping}={}    ${session}=default    ${verify}=False    ${iterations}=${EMPTY}    ${iter_start}=1
138     ...    ${http_timeout}=${EMPTY}
139     [Documentation]    Add arguments sensible for JSON data, return Get_Templated response text.
140     ...    Optionally, verification against JSON data (may be iterated) is called.
141     ...    Only subset of JSON data is verified and returned if JMES path is specified in
142     ...    file ${folder}${/}jmespath.expr.
143     ${response_text} =    Get_Templated    folder=${folder}    mapping=${mapping}    accept=${ACCEPT_EMPTY}    session=${session}    normalize_json=True
144     ...    http_timeout=${http_timeout}
145     BuiltIn.Run_Keyword_If    ${verify}    Verify_Response_As_Json_Templated    response=${response_text}    folder=${folder}    base_name=data    mapping=${mapping}
146     ...    iterations=${iterations}    iter_start=${iter_start}
147     [Return]    ${response_text}
148
149 Get_As_Xml_Templated
150     [Arguments]    ${folder}    ${mapping}={}    ${session}=default    ${verify}=False    ${iterations}=${EMPTY}    ${iter_start}=1
151     ...    ${http_timeout}=${EMPTY}
152     [Documentation]    Add arguments sensible for XML data, return Get_Templated response text.
153     ...    Optionally, verification against XML data (may be iterated) is called.
154     ${response_text} =    Get_Templated    folder=${folder}    mapping=${mapping}    accept=${ACCEPT_XML}    session=${session}    normalize_json=False
155     ...    http_timeout=${http_timeout}
156     BuiltIn.Run_Keyword_If    ${verify}    Verify_Response_As_Xml_Templated    response=${response_text}    folder=${folder}    base_name=data    mapping=${mapping}
157     ...    iterations=${iterations}    iter_start=${iter_start}
158     [Return]    ${response_text}
159
160 Put_As_Json_Templated
161     [Arguments]    ${folder}    ${mapping}={}    ${session}=default    ${verify}=False    ${iterations}=${EMPTY}    ${iter_start}=1
162     ...    ${http_timeout}=${EMPTY}
163     [Documentation]    Add arguments sensible for JSON data, return Put_Templated response text.
164     ...    Optionally, verification against response.json (no iteration) is called.
165     ...    Only subset of JSON data is verified and returned if JMES path is specified in
166     ...    file ${folder}${/}jmespath.expr.
167     ${response_text} =    Put_Templated    folder=${folder}    base_name=data    extension=json    accept=${ACCEPT_EMPTY}    content_type=${HEADERS_YANG_JSON}
168     ...    mapping=${mapping}    session=${session}    normalize_json=True    endline=${\n}    iterations=${iterations}    iter_start=${iter_start}
169     ...    http_timeout=${http_timeout}
170     BuiltIn.Run_Keyword_If    ${verify}    Verify_Response_As_Json_Templated    response=${response_text}    folder=${folder}    base_name=response    mapping=${mapping}
171     [Return]    ${response_text}
172
173 Put_As_Xml_Templated
174     [Arguments]    ${folder}    ${mapping}={}    ${session}=default    ${verify}=False    ${iterations}=${EMPTY}    ${iter_start}=1
175     ...    ${http_timeout}=${EMPTY}
176     [Documentation]    Add arguments sensible for XML data, return Put_Templated response text.
177     ...    Optionally, verification against response.xml (no iteration) is called.
178     # In case of iterations, we use endlines in data to send, as it should not matter and it is more readable.
179     ${response_text} =    Put_Templated    folder=${folder}    base_name=data    extension=xml    accept=${ACCEPT_XML}    content_type=${HEADERS_XML}
180     ...    mapping=${mapping}    session=${session}    normalize_json=False    endline=${\n}    iterations=${iterations}    iter_start=${iter_start}
181     ...    http_timeout=${http_timeout}
182     BuiltIn.Run_Keyword_If    ${verify}    Verify_Response_As_Xml_Templated    response=${response_text}    folder=${folder}    base_name=response    mapping=${mapping}
183     [Return]    ${response_text}
184
185 Post_As_Json_Templated
186     [Arguments]    ${folder}    ${mapping}={}    ${session}=default    ${verify}=False    ${iterations}=${EMPTY}    ${iter_start}=1
187     ...    ${additional_allowed_status_codes}=${NO_STATUS_CODES}    ${explicit_status_codes}=${NO_STATUS_CODES}    ${http_timeout}=${EMPTY}
188     [Documentation]    Add arguments sensible for JSON data, return Post_Templated response text.
189     ...    Optionally, verification against response.json (no iteration) is called.
190     ...    Only subset of JSON data is verified and returned if JMES path is specified in
191     ...    file ${folder}${/}jmespath.expr.
192     ...    Response status code must be one of values from ${explicit_status_codes} if specified or one of set
193     ...    created from all positive HTTP status codes together with ${additional_allowed_status_codes}.
194     ${response_text} =    Post_Templated    folder=${folder}    base_name=data    extension=json    accept=${ACCEPT_EMPTY}    content_type=${HEADERS_YANG_JSON}
195     ...    mapping=${mapping}    session=${session}    normalize_json=True    endline=${\n}    iterations=${iterations}    iter_start=${iter_start}
196     ...    additional_allowed_status_codes=${additional_allowed_status_codes}    explicit_status_codes=${explicit_status_codes}    http_timeout=${http_timeout}
197     BuiltIn.Run_Keyword_If    ${verify}    Verify_Response_As_Json_Templated    response=${response_text}    folder=${folder}    base_name=response    mapping=${mapping}
198     [Return]    ${response_text}
199
200 Post_As_Xml_Templated
201     [Arguments]    ${folder}    ${mapping}={}    ${session}=default    ${verify}=False    ${iterations}=${EMPTY}    ${iter_start}=1
202     ...    ${additional_allowed_status_codes}=${NO_STATUS_CODES}    ${explicit_status_codes}=${NO_STATUS_CODES}    ${http_timeout}=${EMPTY}
203     [Documentation]    Add arguments sensible for XML data, return Post_Templated response text.
204     ...    Optionally, verification against response.xml (no iteration) is called.
205     # In case of iterations, we use endlines in data to send, as it should not matter and it is more readable.
206     ${response_text} =    Post_Templated    folder=${folder}    base_name=data    extension=xml    accept=${ACCEPT_XML}    content_type=${HEADERS_XML}
207     ...    mapping=${mapping}    session=${session}    normalize_json=False    endline=${\n}    iterations=${iterations}    iter_start=${iter_start}
208     ...    additional_allowed_status_codes=${additional_allowed_status_codes}    explicit_status_codes=${explicit_status_codes}    http_timeout=${http_timeout}
209     BuiltIn.Run_Keyword_If    ${verify}    Verify_Response_As_Xml_Templated    response=${response_text}    folder=${folder}    base_name=response    mapping=${mapping}
210     [Return]    ${response_text}
211
212 Delete_Templated
213     [Arguments]    ${folder}    ${mapping}={}    ${session}=default    ${additional_allowed_status_codes}=${NO_STATUS_CODES}    ${http_timeout}=${EMPTY}    ${location}=location
214     [Documentation]    Resolve URI from folder, issue DELETE request.
215     ${uri} =    Resolve_Text_From_Template_Folder    folder=${folder}    base_name=${location}    extension=uri    mapping=${mapping}
216     ${response_text} =    Delete_From_Uri    uri=${uri}    session=${session}    additional_allowed_status_codes=${additional_allowed_status_codes}    http_timeout=${http_timeout}
217     [Return]    ${response_text}
218
219 Verify_Response_As_Json_Templated
220     [Arguments]    ${response}    ${folder}    ${base_name}=response    ${mapping}={}    ${iterations}=${EMPTY}    ${iter_start}=1
221     [Documentation]    Resolve expected JSON data, should be equal to provided \${response}.
222     ...    JSON normalization is used, endlines enabled for readability.
223     Verify_Response_Templated    response=${response}    folder=${folder}    base_name=${base_name}    extension=json    mapping=${mapping}    normalize_json=True
224     ...    endline=${\n}    iterations=${iterations}    iter_start=${iter_start}
225
226 Verify_Response_As_Xml_Templated
227     [Arguments]    ${response}    ${folder}    ${base_name}=response    ${mapping}={}    ${iterations}=${EMPTY}    ${iter_start}=1
228     [Documentation]    Resolve expected XML data, should be equal to provided \${response}.
229     ...    Endline set to empty, as this Resource does not support indented XML comparison.
230     Verify_Response_Templated    response=${response}    folder=${folder}    base_name=${base_name}    extension=xml    mapping=${mapping}    normalize_json=False
231     ...    endline=${EMPTY}    iterations=${iterations}    iter_start=${iter_start}
232
233 Get_As_Json_From_Uri
234     [Arguments]    ${uri}    ${session}=default    ${http_timeout}=${EMPTY}
235     [Documentation]    Specify JSON headers and return Get_From_Uri normalized response text.
236     ${response_text} =    Get_From_Uri    uri=${uri}    accept=${ACCEPT_EMPTY}    session=${session}    normalize_json=True    http_timeout=${http_timeout}
237     [Return]    ${response_text}
238
239 Get_As_Xml_From_Uri
240     [Arguments]    ${uri}    ${session}=default    ${http_timeout}=${EMPTY}
241     [Documentation]    Specify XML headers and return Get_From_Uri response text.
242     ${response_text} =    Get_From_Uri    uri=${uri}    accept=${ACCEPT_XML}    session=${session}    normalize_json=False    http_timeout=${http_timeout}
243     [Return]    ${response_text}
244
245 Put_As_Json_To_Uri
246     [Arguments]    ${uri}    ${data}    ${session}=default    ${http_timeout}=${EMPTY}
247     [Documentation]    Specify JSON headers and return Put_To_Uri normalized response text.
248     ...    Yang json content type is used as a workaround to RequestsLibrary json conversion eagerness.
249     ${response_text} =    Put_To_Uri    uri=${uri}    data=${data}    accept=${ACCEPT_EMPTY}    content_type=${HEADERS_YANG_JSON}    session=${session}
250     ...    normalize_json=True    http_timeout=${http_timeout}
251     [Return]    ${response_text}
252
253 Put_As_Xml_To_Uri
254     [Arguments]    ${uri}    ${data}    ${session}=default    ${http_timeout}=${EMPTY}
255     [Documentation]    Specify XML headers and return Put_To_Uri response text.
256     ${response_text} =    Put_To_Uri    uri=${uri}    data=${data}    accept=${ACCEPT_XML}    content_type=${HEADERS_XML}    session=${session}
257     ...    normalize_json=False    http_timeout=${http_timeout}
258     [Return]    ${response_text}
259
260 Post_As_Json_To_Uri
261     [Arguments]    ${uri}    ${data}    ${session}=default    ${additional_allowed_status_codes}=${NO_STATUS_CODES}    ${explicit_status_codes}=${NO_STATUS_CODES}    ${http_timeout}=${EMPTY}
262     [Documentation]    Specify JSON headers and return Post_To_Uri normalized response text.
263     ...    Yang json content type is used as a workaround to RequestsLibrary json conversion eagerness.
264     ...    Response status code must be one of values from ${explicit_status_codes} if specified or one of set
265     ...    created from all positive HTTP status codes together with ${additional_allowed_status_codes}.
266     ${response_text} =    Post_To_Uri    uri=${uri}    data=${data}    accept=${ACCEPT_EMPTY}    content_type=${HEADERS_YANG_JSON}    session=${session}
267     ...    normalize_json=True    additional_allowed_status_codes=${additional_allowed_status_codes}    explicit_status_codes=${explicit_status_codes}    http_timeout=${http_timeout}
268     [Return]    ${response_text}
269
270 Post_As_Xml_To_Uri
271     [Arguments]    ${uri}    ${data}    ${session}=default    ${http_timeout}=${EMPTY}
272     [Documentation]    Specify XML headers and return Post_To_Uri response text.
273     ${response_text} =    Post_To_Uri    uri=${uri}    data=${data}    accept=${ACCEPT_XML}    content_type=${HEADERS_XML}    session=${session}
274     ...    normalize_json=False    http_timeout=${http_timeout}
275     [Return]    ${response_text}
276
277 Delete_From_Uri
278     [Arguments]    ${uri}    ${session}=default    ${additional_allowed_status_codes}=${NO_STATUS_CODES}    ${http_timeout}=${EMPTY}
279     [Documentation]    DELETE resource at URI, check status_code and return response text..
280     BuiltIn.Log    ${uri}
281     ${response} =    BuiltIn.Run_Keyword_If    """${http_timeout}""" == """${EMPTY}"""    RequestsLibrary.Delete_Request    alias=${session}    uri=${uri}
282     ...    ELSE    RequestsLibrary.Delete_Request    alias=${session}    uri=${uri}    timeout=${http_timeout}
283     Check_Status_Code    ${response}    additional_allowed_status_codes=${additional_allowed_status_codes}
284     [Return]    ${response.text}
285
286 Resolve_Jmes_Path
287     [Arguments]    ${folder}
288     [Documentation]    Reads JMES path from file ${folder}${/}jmespath.expr if the file exists and
289     ...    returns the JMES path. Empty string is returned otherwise.
290     ${read_jmes_file} =    BuiltIn.Run Keyword And Return Status    OperatingSystem.File Should Exist    ${folder}${/}jmespath.expr
291     ${jmes_expression} =    Run Keyword If    ${read_jmes_file} == ${true}    OperatingSystem.Get_File    ${folder}${/}jmespath.expr
292     ${expression} =    BuiltIn.Set Variable If    ${read_jmes_file} == ${true}    ${jmes_expression}    ${EMPTY}
293     [Return]    ${expression}
294
295 Get_Templated
296     [Arguments]    ${folder}    ${accept}    ${mapping}={}    ${session}=default    ${normalize_json}=False    ${http_timeout}=${EMPTY}
297     [Documentation]    Resolve URI from folder, call Get_From_Uri, return response text.
298     ${uri} =    Resolve_Text_From_Template_Folder    folder=${folder}    base_name=location    extension=uri    mapping=${mapping}
299     ${jmes_expression} =    Resolve_Jmes_Path    ${folder}
300     ${response_text} =    Get_From_Uri    uri=${uri}    accept=${accept}    session=${session}    normalize_json=${normalize_json}    jmes_path=${jmes_expression}
301     ...    http_timeout=${http_timeout}
302     [Return]    ${response_text}
303
304 Put_Templated
305     [Arguments]    ${folder}    ${base_name}    ${extension}    ${content_type}    ${accept}    ${mapping}={}
306     ...    ${session}=default    ${normalize_json}=False    ${endline}=${\n}    ${iterations}=${EMPTY}    ${iter_start}=1    ${http_timeout}=${EMPTY}
307     [Documentation]    Resolve URI and data from folder, call Put_To_Uri, return response text.
308     ${uri} =    Resolve_Text_From_Template_Folder    folder=${folder}    base_name=location    extension=uri    mapping=${mapping}
309     ${data} =    Resolve_Text_From_Template_Folder    folder=${folder}    base_name=${base_name}    extension=${extension}    mapping=${mapping}    endline=${endline}
310     ...    iterations=${iterations}    iter_start=${iter_start}
311     ${jmes_expression} =    Resolve_Jmes_Path    ${folder}
312     ${response_text} =    Put_To_Uri    uri=${uri}    data=${data}    content_type=${content_type}    accept=${accept}    session=${session}
313     ...    http_timeout=${http_timeout}    normalize_json=${normalize_json}    jmes_path=${jmes_expression}
314     [Return]    ${response_text}
315
316 Post_Templated
317     [Arguments]    ${folder}    ${base_name}    ${extension}    ${content_type}    ${accept}    ${mapping}={}
318     ...    ${session}=default    ${normalize_json}=False    ${endline}=${\n}    ${iterations}=${EMPTY}    ${iter_start}=1    ${additional_allowed_status_codes}=${NO_STATUS_CODES}
319     ...    ${explicit_status_codes}=${NO_STATUS_CODES}    ${http_timeout}=${EMPTY}
320     [Documentation]    Resolve URI and data from folder, call Post_To_Uri, return response text.
321     ${uri} =    Resolve_Text_From_Template_Folder    folder=${folder}    base_name=location    extension=uri    mapping=${mapping}
322     ${data} =    Resolve_Text_From_Template_Folder    folder=${folder}    name_prefix=post_    base_name=${base_name}    extension=${extension}    mapping=${mapping}
323     ...    endline=${endline}    iterations=${iterations}    iter_start=${iter_start}
324     ${jmes_expression} =    Resolve_Jmes_Path    ${folder}
325     ${response_text} =    Post_To_Uri    uri=${uri}    data=${data}    content_type=${content_type}    accept=${accept}    session=${session}
326     ...    jmes_path=${jmes_expression}    normalize_json=${normalize_json}    additional_allowed_status_codes=${additional_allowed_status_codes}    explicit_status_codes=${explicit_status_codes}    http_timeout=${http_timeout}
327     [Return]    ${response_text}
328
329 Verify_Response_Templated
330     [Arguments]    ${response}    ${folder}    ${base_name}    ${extension}    ${mapping}={}    ${normalize_json}=False
331     ...    ${endline}=${\n}    ${iterations}=${EMPTY}    ${iter_start}=1
332     [Documentation]    Resolve expected text from template, provided response shuld be equal.
333     ...    If \${normalize_json}, perform normalization before comparison.
334     # TODO: Support for XML-aware comparison could be added, but there are issues with namespaces and similar.
335     ${expected_text} =    Resolve_Text_From_Template_Folder    folder=${folder}    base_name=${base_name}    extension=${extension}    mapping=${mapping}    endline=${endline}
336     ...    iterations=${iterations}    iter_start=${iter_start}
337     BuiltIn.Run_Keyword_And_Return_If    """${expected_text}""" == """${EMPTY}"""    BuiltIn.Should_Be_Equal    ${EMPTY}    ${response}
338     BuiltIn.Run_Keyword_If    ${normalize_json}    Normalize_Jsons_And_Compare    expected_raw=${expected_text}    actual_raw=${response}
339     ...    ELSE    BuiltIn.Should_Be_Equal    ${expected_text}    ${response}
340
341 Get_From_Uri
342     [Arguments]    ${uri}    ${accept}=${ACCEPT_EMPTY}    ${session}=default    ${normalize_json}=False    ${jmes_path}=${EMPTY}    ${http_timeout}=${EMPTY}
343     [Documentation]    GET data from given URI, check status code and return response text.
344     ...    \${accept} is a Python object with headers to use.
345     ...    If \${normalize_json}, normalize as JSON text before returning.
346     BuiltIn.Log    ${uri}
347     BuiltIn.Log    ${accept}
348     ${response} =    BuiltIn.Run_Keyword_If    """${http_timeout}""" == """${EMPTY}"""    RequestsLibrary.Get_Request    alias=${session}    uri=${uri}    headers=${accept}
349     ...    ELSE    RequestsLibrary.Get_Request    alias=${session}    uri=${uri}    headers=${accept}    timeout=${http_timeout}
350     Check_Status_Code    ${response}
351     BuiltIn.Run_Keyword_Unless    ${normalize_json}    BuiltIn.Return_From_Keyword    ${response.text}
352     ${text_normalized} =    norm_json.normalize_json_text    ${response.text}    jmes_path=${jmes_path}
353     [Return]    ${text_normalized}
354
355 Put_To_Uri
356     [Arguments]    ${uri}    ${data}    ${content_type}    ${accept}    ${session}=default    ${normalize_json}=False
357     ...    ${jmes_path}=${EMPTY}    ${http_timeout}=${EMPTY}
358     [Documentation]    PUT data to given URI, check status code and return response text.
359     ...    \${content_type} and \${accept} are mandatory Python objects with headers to use.
360     ...    If \${normalize_json}, normalize text before returning.
361     BuiltIn.Log    ${uri}
362     BuiltIn.Log    ${data}
363     BuiltIn.Log    ${content_type}
364     BuiltIn.Log    ${accept}
365     ${headers} =    Join_Two_Headers    first=${content_type}    second=${accept}
366     ${response} =    BuiltIn.Run_Keyword_If    """${http_timeout}""" == """${EMPTY}"""    RequestsLibrary.Put_Request    alias=${session}    uri=${uri}    data=${data}
367     ...    headers=${headers}
368     ...    ELSE    RequestsLibrary.Put_Request    alias=${session}    uri=${uri}    data=${data}    headers=${headers}
369     ...    timeout=${http_timeout}
370     Check_Status_Code    ${response}
371     BuiltIn.Run_Keyword_Unless    ${normalize_json}    BuiltIn.Return_From_Keyword    ${response.text}
372     ${text_normalized} =    norm_json.normalize_json_text    ${response.text}    jmes_path=${jmes_path}
373     [Return]    ${text_normalized}
374
375 Post_To_Uri
376     [Arguments]    ${uri}    ${data}    ${content_type}    ${accept}    ${session}=default    ${normalize_json}=False
377     ...    ${jmes_path}=${EMPTY}    ${additional_allowed_status_codes}=${NO_STATUS_CODES}    ${explicit_status_codes}=${NO_STATUS_CODES}    ${http_timeout}=${EMPTY}
378     [Documentation]    POST data to given URI, check status code and return response text.
379     ...    \${content_type} and \${accept} are mandatory Python objects with headers to use.
380     ...    If \${normalize_json}, normalize text before returning.
381     BuiltIn.Log    ${uri}
382     BuiltIn.Log    ${data}
383     BuiltIn.Log    ${content_type}
384     BuiltIn.Log    ${accept}
385     ${headers} =    Join_Two_Headers    first=${content_type}    second=${accept}
386     ${response} =    BuiltIn.Run_Keyword_If    """${http_timeout}""" == """${EMPTY}"""    RequestsLibrary.Post_Request    alias=${session}    uri=${uri}    data=${data}
387     ...    headers=${headers}
388     ...    ELSE    RequestsLibrary.Post_Request    alias=${session}    uri=${uri}    data=${data}    headers=${headers}
389     ...    timeout=${http_timeout}
390     Check_Status_Code    ${response}    additional_allowed_status_codes=${additional_allowed_status_codes}    explicit_status_codes=${explicit_status_codes}
391     BuiltIn.Run_Keyword_Unless    ${normalize_json}    BuiltIn.Return_From_Keyword    ${response.text}
392     ${text_normalized} =    norm_json.normalize_json_text    ${response.text}    jmes_path=${jmes_path}
393     [Return]    ${text_normalized}
394
395 Check_Status_Code
396     [Arguments]    ${response}    ${additional_allowed_status_codes}=${NO_STATUS_CODES}    ${explicit_status_codes}=${NO_STATUS_CODES}
397     [Documentation]    Log response text, check status_code is one of allowed ones.
398     # TODO: Remove overlap with keywords from Utils.robot
399     BuiltIn.Log    ${response.text}
400     BuiltIn.Log    ${response.status_code}
401     BuiltIn.Run_Keyword_And_Return_If    """${explicit_status_codes}""" != """${NO_STATUS_CODES}"""    Collections.List_Should_Contain_Value    ${explicit_status_codes}    ${response.status_code}
402     ${final_allowd_list} =    Collections.Combine_Lists    ${ALLOWED_STATUS_CODES}    ${additional_allowed_status_codes}
403     Collections.List_Should_Contain_Value    ${final_allowd_list}    ${response.status_code}
404
405 Join_Two_Headers
406     [Arguments]    ${first}    ${second}
407     [Documentation]    Take two dicts, join them, return result. Second argument values take precedence.
408     ${accumulator} =    Collections.Copy_Dictionary    ${first}
409     ${items_to_add} =    Collections.Get_Dictionary_Items    ${second}
410     Collections.Set_To_Dictionary    ${accumulator}    @{items_to_add}
411     BuiltIn.Log    ${accumulator}
412     [Return]    ${accumulator}
413
414 Resolve_Text_From_Template_Folder
415     [Arguments]    ${folder}    ${name_prefix}=${EMPTY}    ${base_name}=data    ${extension}=json    ${mapping}={}    ${iterations}=${EMPTY}
416     ...    ${iter_start}=1    ${endline}=${\n}
417     [Documentation]    Read a template from folder, strip endline, make changes according to mapping, return the result.
418     ...    If \${iterations} value is present, put text together from "prolog", "item" and "epilog" parts,
419     ...    where additional template variable ${i} goes from ${iter_start}, by one ${iterations} times.
420     ...    POST (as opposed to PUT) needs slightly different data, \${name_prefix} may be used to distinguish.
421     ...    (Actually, it is GET who formats data differently when URI is a top-level container.)
422     BuiltIn.Run_Keyword_And_Return_If    not "${iterations}"    Resolve_Text_From_Template_File    folder=${folder}    file_name=${name_prefix}${base_name}.${extension}    mapping=${mapping}
423     ${prolog} =    Resolve_Text_From_Template_File    folder=${folder}    file_name=${name_prefix}${base_name}.prolog.${extension}    mapping=${mapping}
424     ${epilog} =    Resolve_Text_From_Template_File    folder=${folder}    file_name=${name_prefix}${base_name}.epilog.${extension}    mapping=${mapping}
425     # Even POST uses the same item template (except indentation), so name prefix is ignored.
426     ${item_template} =    Resolve_Text_From_Template_File    folder=${folder}    file_name=${base_name}.item.${extension}    mapping=${mapping}
427     ${items} =    BuiltIn.Create_List
428     ${separator} =    BuiltIn.Set_Variable_If    '${extension}' != 'json'    ${endline}    ,${endline}
429     : FOR    ${iteration}    IN RANGE    ${iter_start}    ${iterations}+${iter_start}
430     \    BuiltIn.Run_Keyword_If    ${iteration} > ${iter_start}    Collections.Append_To_List    ${items}    ${separator}
431     \    ${item} =    BuiltIn.Evaluate    string.Template('''${item_template}''').substitute({"i":"${iteration}"})    modules=string
432     \    Collections.Append_To_List    ${items}    ${item}
433     # TODO: The following makes ugly result for iterations=0. Should we fix that?
434     ${final_text} =    BuiltIn.Catenate    SEPARATOR=    ${prolog}    ${endline}    @{items}    ${endline}
435     ...    ${epilog}
436     [Return]    ${final_text}
437
438 Resolve_Text_From_Template_File
439     [Arguments]    ${folder}    ${file_name}    ${mapping}={}
440     [Documentation]    Check if ${folder}.${ODL_STREAM}/${file_name} exists. If yes read and Log contents of file ${folder}.${ODL_STREAM}/${file_name},
441     ...    remove endline, perform safe substitution, return result.
442     ...    If no do it with the default ${folder}/${file_name}.
443     ${file_path_stream}=    BuiltIn.Set Variable    ${folder}.${ODL_STREAM}${/}${file_name}
444     ${file_stream_exists}=    BuiltIn.Run Keyword And Return Status    OperatingSystem.File Should Exist    ${file_path_stream}
445     ${file_path}=    BuiltIn.Set Variable If    ${file_stream_exists}    ${file_path_stream}    ${folder}${/}${file_name}
446     ${template} =    OperatingSystem.Get_File    ${file_path}
447     BuiltIn.Log    ${template}
448     ${final_text} =    BuiltIn.Evaluate    string.Template('''${template}'''.rstrip()).safe_substitute(${mapping})    modules=string
449     # Final text is logged where used.
450     [Return]    ${final_text}
451
452 Normalize_Jsons_And_Compare
453     [Arguments]    ${expected_raw}    ${actual_raw}
454     [Documentation]    Use norm_json to normalize both JSON arguments, call Should_Be_Equal.
455     ${expected_normalized} =    norm_json.normalize_json_text    ${expected_raw}
456     ${actual_normalized} =    norm_json.normalize_json_text    ${actual_raw}
457     # Should_Be_Equal shall print nice diff-style line comparison.
458     BuiltIn.Should_Be_Equal    ${expected_normalized}    ${actual_normalized}
459     # TODO: Add garbage collection? Check whether the temporary data accumulates.
460
461 Normalize_Jsons_With_Bits_And_Compare
462     [Arguments]    ${expected_raw}    ${actual_raw}    ${keys_with_bits}=${KEYS_WITH_BITS}
463     [Documentation]    Use norm_json to normalize both JSON arguments, call Should_Be_Equal.
464     ${expected_normalized} =    norm_json.normalize_json_text    ${expected_raw}    keys_with_bits=${keys_with_bits}
465     ${actual_normalized} =    norm_json.normalize_json_text    ${actual_raw}    keys_with_bits=${keys_with_bits}
466     BuiltIn.Should_Be_Equal    ${expected_normalized}    ${actual_normalized}