5ecee1af26a7de7d92962de2b6753429aa3cb376
[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.
70 ...
71 ...               These Keywords contain frequent BuiltIn.Log invocations,
72 ...               so they are not suited for scale or performance suites.
73 ...               And as usual, performance tests should use specialized utilities,
74 ...               as Robot in general and this Resource specifically will be too slow.
75 ...
76 ...               As this Resource makes assumptions about intended headers,
77 ...               it is not flexible enough for suites specifically testing Restconf corner cases.
78 ...               Also, list of allowed http status codes is quite rigid and broad.
79 ...
80 ...               Rules for ordering Keywords within this Resource:
81 ...               1. User friendlier Keywords first.
82 ...               2. Get, Put, Post, Delete, Verify.
83 ...               3. Within class of equally usable, use order in which a suite would call them.
84 ...               4. Higher-level Keywords first.
85 ...               5. Json before Xml.
86 ...               Motivation: Users read from the start, so it is important
87 ...               to offer them the better-to-use Keywords first.
88 ...               https://wiki.opendaylight.org/view/Integration/Test/Test_Code_Guidelines#Keyword_ordering
89 ...               In this case, templates are nicer that raw data,
90 ...               *_As_* keywords are better than messing wth explicit header dicts,
91 ...               Json is less prone to element ordering issues.
92 ...               PUT does not fail on existing element, also it does not allow
93 ...               shortened URIs (container instead keyed list element) as Post does.
94 ...
95 ...               TODO: Add ability to override allowed status codes,
96 ...               so that negative tests do not need to parse the failure message.
97 ...
98 ...               TODO: Migrate suites to this Resource and remove *ViaRestconf Resources.
99 ...
100 ...               TODO: Currently the verification step is only in *_As_*_Templated keywords.
101 ...               It could be moved to "non-as" *_Templated ones,
102 ...               but that would take even more horizontal space. Is it worth doing?
103 ...
104 ...               TODO: Should iterations=0 be supported for JSON (remove [])?
105 ...
106 ...               TODO: Currently, ${ACCEPT_EMPTY} is used for JSON-expecting requests.
107 ...               perhaps explicit ${ACCEPT_JSON} will be better, even if it sends few bytes more?
108 Library           Collections
109 Library           OperatingSystem
110 Library           RequestsLibrary
111 Library           ${CURDIR}/norm_json.py
112 Variables         ${CURDIR}/../variables/Variables.py
113
114 *** Variables ***
115 # TODO: Make the following list more narrow when streams without Bug 2594 fix (up to beryllium) are no longer used.
116 @{ALLOWED_STATUS_CODES}    ${200}    ${201}    ${204}    # List of integers, not strings. Used by both PUT and DELETE (if the resource should have been present).
117 @{DATA_VALIDATION_ERROR}    ${500}
118 @{DELETED_STATUS_CODE}    ${404}    # List of integers, not strings. Used by DELETE if the resource may be not present.
119 @{NO_STATUS_CODES}
120 @{ALLOWED_DELETE_STATUS_CODES}    ${200}    ${201}    ${204}    ${404}    # List of integers, not strings. Used by DELETE if the resource may be not present.
121 @{UNAUTHORIZED_STATUS_CODES}    ${401}    # List of integers, not strings. Used in Keystone Authentication when the user is not authorized to use the requested resource.
122 @{KEYS_WITH_BITS}    op    # the default list with keys to be sorted when norm_json libray is used
123 # TODO: Add option for delete to require 404.
124
125 *** Keywords ***
126 Create_Default_Session
127     [Arguments]    ${url}=http://${ODL_SYSTEM_IP}:${RESTCONFPORT}    ${auth}=${AUTH}
128     [Documentation]    Create "default" session to ${url} with default authentication.
129     ...    This Keyword is in this Resource only so that user do not need to call RequestsLibrary directly.
130     RequestsLibrary.Create_Session    alias=default    url=${url}    auth=${auth}
131
132 Get_As_Json_Templated
133     [Arguments]    ${folder}    ${mapping}={}    ${session}=default    ${verify}=False    ${iterations}=${EMPTY}    ${iter_start}=1
134     [Documentation]    Add arguments sensible for JSON data, return Get_Templated response text.
135     ...    Optionally, verification against JSON data (may be iterated) is called.
136     ${response_text} =    Get_Templated    folder=${folder}    mapping=${mapping}    accept=${ACCEPT_EMPTY}    session=${session}    normalize_json=True
137     BuiltIn.Run_Keyword_If    ${verify}    Verify_Response_As_Json_Templated    response=${response_text}    folder=${folder}    base_name=data    mapping=${mapping}
138     ...    iterations=${iterations}    iter_start=${iter_start}
139     [Return]    ${response_text}
140
141 Get_As_Xml_Templated
142     [Arguments]    ${folder}    ${mapping}={}    ${session}=default    ${verify}=False    ${iterations}=${EMPTY}    ${iter_start}=1
143     [Documentation]    Add arguments sensible for XML data, return Get_Templated response text.
144     ...    Optionally, verification against XML data (may be iterated) is called.
145     ${response_text} =    Get_Templated    folder=${folder}    mapping=${mapping}    accept=${ACCEPT_XML}    session=${session}    normalize_json=False
146     BuiltIn.Run_Keyword_If    ${verify}    Verify_Response_As_Xml_Templated    response=${response_text}    folder=${folder}    base_name=data    mapping=${mapping}
147     ...    iterations=${iterations}    iter_start=${iter_start}
148     [Return]    ${response_text}
149
150 Put_As_Json_Templated
151     [Arguments]    ${folder}    ${mapping}={}    ${session}=default    ${verify}=False    ${iterations}=${EMPTY}    ${iter_start}=1
152     [Documentation]    Add arguments sensible for JSON data, return Put_Templated response text.
153     ...    Optionally, verification against response.json (no iteration) is called.
154     ${response_text} =    Put_Templated    folder=${folder}    base_name=data    extension=json    accept=${ACCEPT_EMPTY}    content_type=${HEADERS_YANG_JSON}
155     ...    mapping=${mapping}    session=${session}    normalize_json=True    endline=${\n}    iterations=${iterations}    iter_start=${iter_start}
156     BuiltIn.Run_Keyword_If    ${verify}    Verify_Response_As_Json_Templated    response=${response_text}    folder=${folder}    base_name=response    mapping=${mapping}
157     [Return]    ${response_text}
158
159 Put_As_Xml_Templated
160     [Arguments]    ${folder}    ${mapping}={}    ${session}=default    ${verify}=False    ${iterations}=${EMPTY}    ${iter_start}=1
161     [Documentation]    Add arguments sensible for XML data, return Put_Templated response text.
162     ...    Optionally, verification against response.xml (no iteration) is called.
163     # In case of iterations, we use endlines in data to send, as it should not matter and it is more readable.
164     ${response_text} =    Put_Templated    folder=${folder}    base_name=data    extension=xml    accept=${ACCEPT_XML}    content_type=${HEADERS_XML}
165     ...    mapping=${mapping}    session=${session}    normalize_json=False    endline=${\n}    iterations=${iterations}    iter_start=${iter_start}
166     BuiltIn.Run_Keyword_If    ${verify}    Verify_Response_As_Xml_Templated    response=${response_text}    folder=${folder}    base_name=response    mapping=${mapping}
167     [Return]    ${response_text}
168
169 Post_As_Json_Templated
170     [Arguments]    ${folder}    ${mapping}={}    ${session}=default    ${verify}=False    ${iterations}=${EMPTY}    ${iter_start}=1
171     [Documentation]    Add arguments sensible for JSON data, return Post_Templated response text.
172     ...    Optionally, verification against response.json (no iteration) is called.
173     ${response_text} =    Post_Templated    folder=${folder}    base_name=data    extension=json    accept=${ACCEPT_EMPTY}    content_type=${HEADERS_YANG_JSON}
174     ...    mapping=${mapping}    session=${session}    normalize_json=True    endline=${\n}    iterations=${iterations}    iter_start=${iter_start}
175     BuiltIn.Run_Keyword_If    ${verify}    Verify_Response_As_Json_Templated    response=${response_text}    folder=${folder}    base_name=response    mapping=${mapping}
176     [Return]    ${response_text}
177
178 Post_As_Xml_Templated
179     [Arguments]    ${folder}    ${mapping}={}    ${session}=default    ${verify}=False    ${iterations}=${EMPTY}    ${iter_start}=1
180     ...    ${additional_allowed_status_codes}=${NO_STATUS_CODES}    ${explicit_status_codes}=${NO_STATUS_CODES}
181     [Documentation]    Add arguments sensible for XML data, return Post_Templated response text.
182     ...    Optionally, verification against response.xml (no iteration) is called.
183     # In case of iterations, we use endlines in data to send, as it should not matter and it is more readable.
184     ${response_text} =    Post_Templated    folder=${folder}    base_name=data    extension=xml    accept=${ACCEPT_XML}    content_type=${HEADERS_XML}
185     ...    mapping=${mapping}    session=${session}    normalize_json=False    endline=${\n}    iterations=${iterations}    iter_start=${iter_start}
186     ...    additional_allowed_status_codes=${additional_allowed_status_codes}    explicit_status_codes=${explicit_status_codes}
187     BuiltIn.Run_Keyword_If    ${verify}    Verify_Response_As_Xml_Templated    response=${response_text}    folder=${folder}    base_name=response    mapping=${mapping}
188     [Return]    ${response_text}
189
190 Delete_Templated
191     [Arguments]    ${folder}    ${mapping}={}    ${session}=default    ${additional_allowed_status_codes}=${NO_STATUS_CODES}
192     [Documentation]    Resolve URI from folder, issue DELETE request.
193     ${uri} =    Resolve_Text_From_Template_Folder    folder=${folder}    base_name=location    extension=uri    mapping=${mapping}
194     ${response_text} =    Delete_From_Uri    uri=${uri}    session=${session}    additional_allowed_status_codes=${additional_allowed_status_codes}
195     [Return]    ${response_text}
196
197 Verify_Response_As_Json_Templated
198     [Arguments]    ${response}    ${folder}    ${base_name}=response    ${mapping}={}    ${iterations}=${EMPTY}    ${iter_start}=1
199     [Documentation]    Resolve expected JSON data, should be equal to provided \${response}.
200     ...    JSON normalization is used, endlines enabled for readability.
201     Verify_Response_Templated    response=${response}    folder=${folder}    base_name=${base_name}    extension=json    mapping=${mapping}    normalize_json=True
202     ...    endline=${\n}    iterations=${iterations}    iter_start=${iter_start}
203
204 Verify_Response_As_Xml_Templated
205     [Arguments]    ${response}    ${folder}    ${base_name}=response    ${mapping}={}    ${iterations}=${EMPTY}    ${iter_start}=1
206     [Documentation]    Resolve expected XML data, should be equal to provided \${response}.
207     ...    Endline set to empty, as this Resource does not support indented XML comparison.
208     Verify_Response_Templated    response=${response}    folder=${folder}    base_name=${base_name}    extension=xml    mapping=${mapping}    normalize_json=False
209     ...    endline=${EMPTY}    iterations=${iterations}    iter_start=${iter_start}
210
211 Get_As_Json_From_Uri
212     [Arguments]    ${uri}    ${session}=default
213     [Documentation]    Specify JSON headers and return Get_From_Uri normalized response text.
214     ${response_text} =    Get_From_Uri    uri=${uri}    accept=${ACCEPT_EMPTY}    session=${session}    normalize_json=True
215     [Return]    ${response_text}
216
217 Get_As_Xml_From_Uri
218     [Arguments]    ${uri}    ${session}=default
219     [Documentation]    Specify XML headers and return Get_From_Uri response text.
220     ${response_text} =    Get_From_Uri    uri=${uri}    accept=${ACCEPT_XML}    session=${session}    normalize_json=False
221     [Return]    ${response_text}
222
223 Put_As_Json_To_Uri
224     [Arguments]    ${uri}    ${data}    ${session}=default
225     [Documentation]    Specify JSON headers and return Put_To_Uri normalized response text.
226     ...    Yang json content type is used as a workaround to RequestsLibrary json conversion eagerness.
227     ${response_text} =    Put_To_Uri    uri=${uri}    data=${data}    accept=${ACCEPT_EMPTY}    content_type=${HEADERS_YANG_JSON}    session=${session}
228     ...    normalize_json=True
229     [Return]    ${response_text}
230
231 Put_As_Xml_To_Uri
232     [Arguments]    ${uri}    ${data}    ${session}=default
233     [Documentation]    Specify XML headers and return Put_To_Uri response text.
234     ${response_text} =    Put_To_Uri    uri=${uri}    data=${data}    accept=${ACCEPT_XML}    content_type=${HEADERS_XML}    session=${session}
235     ...    normalize_json=False
236     [Return]    ${response_text}
237
238 Post_As_Json_To_Uri
239     [Arguments]    ${uri}    ${data}    ${session}=default
240     [Documentation]    Specify JSON headers and return Post_To_Uri normalized response text.
241     ...    Yang json content type is used as a workaround to RequestsLibrary json conversion eagerness.
242     ${response_text} =    Post_To_Uri    uri=${uri}    data=${data}    accept=${ACCEPT_EMPTY}    content_type=${HEADERS_YANG_JSON}    session=${session}
243     ...    normalize_json=True
244     [Return]    ${response_text}
245
246 Post_As_Xml_To_Uri
247     [Arguments]    ${uri}    ${data}    ${session}=default
248     [Documentation]    Specify XML headers and return Post_To_Uri response text.
249     ${response_text} =    Post_To_Uri    uri=${uri}    data=${data}    accept=${ACCEPT_XML}    content_type=${HEADERS_XML}    session=${session}
250     ...    normalize_json=False
251     [Return]    ${response_text}
252
253 Delete_From_Uri
254     [Arguments]    ${uri}    ${session}=default    ${additional_allowed_status_codes}=${NO_STATUS_CODES}
255     [Documentation]    DELETE resource at URI, check status_code and return response text..
256     BuiltIn.Log    ${uri}
257     ${response} =    RequestsLibrary.Delete_Request    alias=${session}    uri=${uri}
258     Check_Status_Code    ${response}    additional_allowed_status_codes=${additional_allowed_status_codes}
259     [Return]    ${response.text}
260
261 Get_Templated
262     [Arguments]    ${folder}    ${accept}    ${mapping}={}    ${session}=default    ${normalize_json}=False
263     [Documentation]    Resolve URI from folder, call Get_From_Uri, return response text.
264     ${uri} =    Resolve_Text_From_Template_Folder    folder=${folder}    base_name=location    extension=uri    mapping=${mapping}
265     ${response_text} =    Get_From_Uri    uri=${uri}    accept=${accept}    session=${session}    normalize_json=${normalize_json}
266     [Return]    ${response_text}
267
268 Put_Templated
269     [Arguments]    ${folder}    ${base_name}    ${extension}    ${content_type}    ${accept}    ${mapping}={}
270     ...    ${session}=default    ${normalize_json}=False    ${endline}=${\n}    ${iterations}=${EMPTY}    ${iter_start}=1
271     [Documentation]    Resolve URI and data from folder, call Put_To_Uri, return response text.
272     ${uri} =    Resolve_Text_From_Template_Folder    folder=${folder}    base_name=location    extension=uri    mapping=${mapping}
273     ${data} =    Resolve_Text_From_Template_Folder    folder=${folder}    base_name=${base_name}    extension=${extension}    mapping=${mapping}    endline=${endline}
274     ...    iterations=${iterations}    iter_start=${iter_start}
275     ${response_text} =    Put_To_Uri    uri=${uri}    data=${data}    content_type=${content_type}    accept=${accept}    session=${session}
276     ...    normalize_json=${normalize_json}
277     [Return]    ${response_text}
278
279 Post_Templated
280     [Arguments]    ${folder}    ${base_name}    ${extension}    ${content_type}    ${accept}    ${mapping}={}
281     ...    ${session}=default    ${normalize_json}=False    ${endline}=${\n}    ${iterations}=${EMPTY}    ${iter_start}=1    ${additional_allowed_status_codes}=${NO_STATUS_CODES}
282     ...    ${explicit_status_codes}=${NO_STATUS_CODES}
283     [Documentation]    Resolve URI and data from folder, call Post_To_Uri, return response text.
284     ${uri} =    Resolve_Text_From_Template_Folder    folder=${folder}    base_name=location    extension=uri    mapping=${mapping}
285     ${data} =    Resolve_Text_From_Template_Folder    folder=${folder}    name_prefix=post_    base_name=${base_name}    extension=${extension}    mapping=${mapping}
286     ...    endline=${endline}    iterations=${iterations}    iter_start=${iter_start}
287     ${response_text} =    Post_To_Uri    uri=${uri}    data=${data}    content_type=${content_type}    accept=${accept}    session=${session}
288     ...    normalize_json=${normalize_json}    additional_allowed_status_codes=${additional_allowed_status_codes}    explicit_status_codes=${explicit_status_codes}
289     [Return]    ${response_text}
290
291 Verify_Response_Templated
292     [Arguments]    ${response}    ${folder}    ${base_name}    ${extension}    ${mapping}={}    ${normalize_json}=False
293     ...    ${endline}=${\n}    ${iterations}=${EMPTY}    ${iter_start}=1
294     [Documentation]    Resolve expected text from template, provided response shuld be equal.
295     ...    If \${normalize_json}, perform normalization before comparison.
296     # TODO: Support for XML-aware comparison could be added, but there are issues with namespaces and similar.
297     ${expected_text} =    Resolve_Text_From_Template_Folder    folder=${folder}    base_name=${base_name}    extension=${extension}    mapping=${mapping}    endline=${endline}
298     ...    iterations=${iterations}    iter_start=${iter_start}
299     BuiltIn.Run_Keyword_If    ${normalize_json}    Normalize_Jsons_And_Compare    expected_raw=${expected_text}    actual_raw=${response}
300     ...    ELSE    BuiltIn.Should_Be_Equal    ${expected_text}    ${response}
301
302 Get_From_Uri
303     [Arguments]    ${uri}    ${accept}=${ACCEPT_EMPTY}    ${session}=default    ${normalize_json}=False
304     [Documentation]    GET data from given URI, check status code and return response text.
305     ...    \${accept} is a Python object with headers to use.
306     ...    If \${normalize_json}, normalize as JSON text before returning.
307     BuiltIn.Log    ${uri}
308     BuiltIn.Log    ${accept}
309     ${response} =    RequestsLibrary.Get_Request    alias=${session}    uri=${uri}    headers=${accept}
310     Check_Status_Code    ${response}
311     BuiltIn.Run_Keyword_Unless    ${normalize_json}    BuiltIn.Return_From_Keyword    ${response.text}
312     ${text_normalized} =    norm_json.normalize_json_text    ${response.text}
313     [Return]    ${text_normalized}
314
315 Put_To_Uri
316     [Arguments]    ${uri}    ${data}    ${content_type}    ${accept}    ${session}=default    ${normalize_json}=False
317     [Documentation]    PUT data to given URI, check status code and return response text.
318     ...    \${content_type} and \${accept} are mandatory Python objects with headers to use.
319     ...    If \${normalize_json}, normalize text before returning.
320     BuiltIn.Log    ${uri}
321     BuiltIn.Log    ${data}
322     BuiltIn.Log    ${content_type}
323     BuiltIn.Log    ${accept}
324     ${headers} =    Join_Two_Headers    first=${content_type}    second=${accept}
325     ${response} =    RequestsLibrary.Put_Request    alias=${session}    uri=${uri}    data=${data}    headers=${headers}
326     Check_Status_Code    ${response}
327     BuiltIn.Run_Keyword_Unless    ${normalize_json}    BuiltIn.Return_From_Keyword    ${response.text}
328     ${text_normalized} =    norm_json.normalize_json_text    ${response.text}
329     [Return]    ${text_normalized}
330
331 Post_To_Uri
332     [Arguments]    ${uri}    ${data}    ${content_type}    ${accept}    ${session}=default    ${normalize_json}=False
333     ...    ${additional_allowed_status_codes}=${NO_STATUS_CODES}    ${explicit_status_codes}=${NO_STATUS_CODES}
334     [Documentation]    POST data to given URI, check status code and return response text.
335     ...    \${content_type} and \${accept} are mandatory Python objects with headers to use.
336     ...    If \${normalize_json}, normalize text before returning.
337     BuiltIn.Log    ${uri}
338     BuiltIn.Log    ${data}
339     BuiltIn.Log    ${content_type}
340     BuiltIn.Log    ${accept}
341     ${headers} =    Join_Two_Headers    first=${content_type}    second=${accept}
342     ${response} =    RequestsLibrary.Post_Request    alias=${session}    uri=${uri}    data=${data}    headers=${headers}
343     Check_Status_Code    ${response}    additional_allowed_status_codes=${additional_allowed_status_codes}    explicit_status_codes=${explicit_status_codes}
344     BuiltIn.Run_Keyword_Unless    ${normalize_json}    BuiltIn.Return_From_Keyword    ${response.text}
345     ${text_normalized} =    norm_json.normalize_json_text    ${response.text}
346     [Return]    ${text_normalized}
347
348 Check_Status_Code
349     [Arguments]    ${response}    ${additional_allowed_status_codes}=${NO_STATUS_CODES}    ${explicit_status_codes}=${NO_STATUS_CODES}
350     [Documentation]    Log response text, check status_code is one of allowed ones.
351     # TODO: Remove overlap with keywords from Utils.robot
352     BuiltIn.Log    ${response.text}
353     BuiltIn.Log    ${response.status_code}
354     BuiltIn.Run_Keyword_And_Return_If    """${explicit_status_codes}""" != """${NO_STATUS_CODES}"""    Collections.List_Should_Contain_Value    ${explicit_status_codes}    ${response.status_code}
355     ${final_allowd_list} =    Collections.Combine_Lists    ${ALLOWED_STATUS_CODES}    ${additional_allowed_status_codes}
356     Collections.List_Should_Contain_Value    ${final_allowd_list}    ${response.status_code}
357
358 Join_Two_Headers
359     [Arguments]    ${first}    ${second}
360     [Documentation]    Take two dicts, join them, return result. Second argument values take precedence.
361     ${accumulator} =    Collections.Copy_Dictionary    ${first}
362     ${items_to_add} =    Collections.Get_Dictionary_Items    ${second}
363     Collections.Set_To_Dictionary    ${accumulator}    @{items_to_add}
364     BuiltIn.Log    ${accumulator}
365     [Return]    ${accumulator}
366
367 Resolve_Text_From_Template_Folder
368     [Arguments]    ${folder}    ${name_prefix}=${EMPTY}    ${base_name}=data    ${extension}=json    ${mapping}={}    ${iterations}=${EMPTY}
369     ...    ${iter_start}=1    ${endline}=${\n}
370     [Documentation]    Read a template from folder, strip endline, make changes according to mapping, return the result.
371     ...    If \${iterations} value is present, put text together from "prolog", "item" and "epilog" parts,
372     ...    where additional template variable ${i} goes from ${iter_start}, by one ${iterations} times.
373     ...    POST (as opposed to PUT) needs slightly different data, \${name_prefix} may be used to distinguish.
374     ...    (Actually, it is GET who formats data differently when URI is a top-level container.)
375     BuiltIn.Run_Keyword_And_Return_If    not "${iterations}"    Resolve_Text_From_Template_File    folder=${folder}    file_name=${name_prefix}${base_name}.${extension}    mapping=${mapping}
376     ${prolog} =    Resolve_Text_From_Template_File    folder=${folder}    file_name=${name_prefix}${base_name}.prolog.${extension}    mapping=${mapping}
377     ${epilog} =    Resolve_Text_From_Template_File    folder=${folder}    file_name=${name_prefix}${base_name}.epilog.${extension}    mapping=${mapping}
378     # Even POST uses the same item template (except indentation), so name prefix is ignored.
379     ${item_template} =    Resolve_Text_From_Template_File    folder=${folder}    file_name=${base_name}.item.${extension}    mapping=${mapping}
380     ${items} =    BuiltIn.Create_List
381     ${separator} =    BuiltIn.Set_Variable_If    '${extension}' != 'json'    ${endline}    ,${endline}
382     : FOR    ${iteration}    IN RANGE    ${iter_start}    ${iterations}+${iter_start}
383     \    BuiltIn.Run_Keyword_If    ${iteration} > ${iter_start}    Collections.Append_To_List    ${items}    ${separator}
384     \    ${item} =    BuiltIn.Evaluate    string.Template('''${item_template}''').substitute({"i":"${iteration}"})    modules=string
385     \    Collections.Append_To_List    ${items}    ${item}
386     # TODO: The following makes ugly result for iterations=0. Should we fix that?
387     ${final_text} =    BuiltIn.Catenate    SEPARATOR=    ${prolog}    ${endline}    @{items}    ${endline}
388     ...    ${epilog}
389     [Return]    ${final_text}
390
391 Resolve_Text_From_Template_File
392     [Arguments]    ${folder}    ${file_name}    ${mapping}={}
393     [Documentation]    Check if ${folder}.${ODL_STREAM}/${file_name} exists. If yes read and Log contents of file ${folder}.${ODL_STREAM}/${file_name},
394     ...    remove endline, perform safe substitution, return result.
395     ...    If no do it with the default ${folder}/${file_name}.
396     ${file_path_stream}=    BuiltIn.Set Variable    ${folder}.${ODL_STREAM}${/}${file_name}
397     ${file_stream_exists}=    BuiltIn.Run Keyword And Return Status    OperatingSystem.File Should Exist    ${file_path_stream}
398     ${file_path}=    BuiltIn.Set Variable If    ${file_stream_exists}    ${file_path_stream}    ${folder}${/}${file_name}
399     ${template} =    OperatingSystem.Get_File    ${file_path}
400     BuiltIn.Log    ${template}
401     ${final_text} =    BuiltIn.Evaluate    string.Template('''${template}'''.rstrip()).safe_substitute(${mapping})    modules=string
402     # Final text is logged where used.
403     [Return]    ${final_text}
404
405 Normalize_Jsons_And_Compare
406     [Arguments]    ${expected_raw}    ${actual_raw}
407     [Documentation]    Use norm_json to normalize both JSON arguments, call Should_Be_Equal.
408     ${expected_normalized} =    norm_json.normalize_json_text    ${expected_raw}
409     ${actual_normalized} =    norm_json.normalize_json_text    ${actual_raw}
410     # Should_Be_Equal shall print nice diff-style line comparison.
411     BuiltIn.Should_Be_Equal    ${expected_normalized}    ${actual_normalized}
412     # TODO: Add garbage collection? Check whether the temporary data accumulates.
413
414 Normalize_Jsons_With_Bits_And_Compare
415     [Arguments]    ${expected_raw}    ${actual_raw}    ${keys_with_bits}=${KEYS_WITH_BITS}
416     [Documentation]    Use norm_json to normalize both JSON arguments, call Should_Be_Equal.
417     ${expected_normalized} =    norm_json.normalize_json_text    ${expected_raw}    keys_with_bits=${keys_with_bits}
418     ${actual_normalized} =    norm_json.normalize_json_text    ${actual_raw}    keys_with_bits=${keys_with_bits}
419     BuiltIn.Should_Be_Equal    ${expected_normalized}    ${actual_normalized}