Correcting Get Value from JSON
[integration/test.git] / csit / libraries / TemplatedRequests.robot
index a484e26ae76877576872818f51e7ffa27bd35ed2..eece6288df2fe802a64f30230ea666ecafba003e 100644 (file)
@@ -1,13 +1,13 @@
 *** Settings ***
 Documentation     Resource for supporting http Requests based on data stored in files.
-...
+...           
 ...               Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
-...
+...           
 ...               This program and the accompanying materials are made available under the
 ...               terms of the Eclipse Public License v1.0 which accompanies this distribution,
 ...               and is available at http://www.eclipse.org/legal/epl-v10.html
-...
-...
+...           
+...           
 ...               The main strength of this library are *_As_*_Templated keywords
 ...               User gives a path to directory where files with templates for URI
 ...               and XML (or JSON) data are present, and a mapping with substitution to make;
@@ -16,12 +16,12 @@ Documentation     Resource for supporting http Requests based on data stored in
 ...               Simple example (tidy insists on single space where 4 spaces should be):
 ...               TemplatedRequests.Put_As_Json_Templated folder=${VAR_BASE}/person mapping={"NAME":"joe"}
 ...               TemplatedRequests.Get_As_Json_Templated folder=${VAR_BASE}/person mapping={"NAME":"joe"} verify=True
-...
+...           
 ...               In that example, we are PUTting "person" data with specified value for "NAME" placeholder.
 ...               We are not verifying PUT response (probably empty string which is not a valid JSON),
 ...               but we are issuing GET (same URI) and verifying the repsonse matches the same data.
 ...               Both lines are returning text response, but in the example we are not saving it into variable.
-...
+...           
 ...               Optionally, *_As_*_Templated keywords call verification of response.
 ...               There are separate Verify_* keywords, for users who use intermediate processing.
 ...               For JSON responses, there is a support for normalizing.
@@ -29,12 +29,12 @@ Documentation     Resource for supporting http Requests based on data stored in
 ...               *_Uri keywords do not use templates, but may be useful in general,
 ...               perhaps for users who call Resolve_Text_* keywords.
 ...               *_As_*_Uri are the less flexible but less argument-heavy versions of *_Uri keywords.
-...
+...           
 ...               This resource supports generating data with simple lists.
 ...               ${iterations} argument control number of items, "$i" will be substituted
 ...               automatically (not by the provided mapping) with integers starting with ${iter_start} (default 1).
 ...               For example "iterations=2 iter_start=3" will create items with i=3 and i=4.
-...
+...           
 ...               This implementation relies on file names to distinguish data.
 ...               Each file is expected to end in newline, compiled data has final newline removed.
 ...               Here is a table so that users can create their own templates:
@@ -48,7 +48,7 @@ Documentation     Resource for supporting http Requests based on data stored in
 ...               *.prolog.*: Temlate with data before iterated items.
 ...               *.item.*: Template with data piece corresponding to one item.
 ...               *.epilog.*: Temlate with data after iterated items.
-...
+...           
 ...               One typical use of this Resource is to make runtime changes to ODL configuration.
 ...               Different ODL parts have varying ways of configuration,
 ...               this library affects only the Config Subsystem way.
@@ -56,7 +56,7 @@ Documentation     Resource for supporting http Requests based on data stored in
 ...               a NETCONF server as its publicly available entry point.
 ...               Netconf-connector feature makes this netconf server available for RESTCONF calls.
 ...               Be careful to use appropriate feature, odl-netconf-connector* does not work in cluster.
-...
+...           
 ...               This Resource currently calls RequestsLibrary directly,
 ...               so it does not work with AuthStandalone or similar.
 ...               This Resource does not maintain any internal Sessions.
@@ -70,16 +70,16 @@ Documentation     Resource for supporting http Requests based on data stored in
 ...               both on session level (where it becomes a default value for requests) and on request
 ...               level (when present, it overrides the session value). To override the default
 ...               value keywords' http_timeout parameter may be used.
-...
+...           
 ...               These Keywords contain frequent BuiltIn.Log invocations,
 ...               so they are not suited for scale or performance suites.
 ...               And as usual, performance tests should use specialized utilities,
 ...               as Robot in general and this Resource specifically will be too slow.
-...
+...           
 ...               As this Resource makes assumptions about intended headers,
 ...               it is not flexible enough for suites specifically testing Restconf corner cases.
 ...               Also, list of allowed http status codes is quite rigid and broad.
-...
+...           
 ...               Rules for ordering Keywords within this Resource:
 ...               1. User friendlier Keywords first.
 ...               2. Get, Put, Post, Delete, Verify.
@@ -94,18 +94,18 @@ Documentation     Resource for supporting http Requests based on data stored in
 ...               Json is less prone to element ordering issues.
 ...               PUT does not fail on existing element, also it does not allow
 ...               shortened URIs (container instead keyed list element) as Post does.
-...
+...           
 ...               TODO: Add ability to override allowed status codes,
 ...               so that negative tests do not need to parse the failure message.
-...
+...           
 ...               TODO: Migrate suites to this Resource and remove *ViaRestconf Resources.
-...
+...           
 ...               TODO: Currently the verification step is only in *_As_*_Templated keywords.
 ...               It could be moved to "non-as" *_Templated ones,
 ...               but that would take even more horizontal space. Is it worth doing?
-...
+...           
 ...               TODO: Should iterations=0 be supported for JSON (remove [])?
-...
+...           
 ...               TODO: Currently, ${ACCEPT_EMPTY} is used for JSON-expecting requests.
 ...               perhaps explicit ${ACCEPT_JSON} will be better, even if it sends few bytes more?
 Library           Collections
@@ -120,7 +120,7 @@ Resource          ${CURDIR}/../variables/Variables.robot
 @{ALLOWED_DELETE_STATUS_CODES}    ${200}    ${201}    ${204}    ${404}    # List of integers, not strings. Used by DELETE if the resource may be not present.
 @{ALLOWED_STATUS_CODES}    ${200}    ${201}    ${204}    # List of integers, not strings. Used by both PUT and DELETE (if the resource should have been present).
 @{DATA_VALIDATION_ERROR}    ${400}    # For testing mildly negative scenarios where ODL reports user error.
-@{DELETED_STATUS_CODE}    ${404}    # List of integers, not strings. Used by DELETE if the resource may be not present.
+@{DELETED_STATUS_CODES}    ${404}    ${409}    # List of integers, not strings. Used by DELETE if the resource may be not present.
 # TODO: Add option for delete to require 404.
 @{INTERNAL_SERVER_ERROR}    ${500}    # Only for testing severely negative scenarios where ODL cannot recover.
 @{KEYS_WITH_BITS}    op    # the default list with keys to be sorted when norm_json libray is used
@@ -136,13 +136,13 @@ Create_Default_Session
 
 Get_As_Json_Templated
     [Arguments]    ${folder}    ${mapping}={}    ${session}=default    ${verify}=False    ${iterations}=${EMPTY}    ${iter_start}=1
-    ...    ${http_timeout}=${EMPTY}
+    ...    ${http_timeout}=${EMPTY}    ${log_response}=True
     [Documentation]    Add arguments sensible for JSON data, return Get_Templated response text.
     ...    Optionally, verification against JSON data (may be iterated) is called.
     ...    Only subset of JSON data is verified and returned if JMES path is specified in
     ...    file ${folder}${/}jmespath.expr.
     ${response_text} =    Get_Templated    folder=${folder}    mapping=${mapping}    accept=${ACCEPT_EMPTY}    session=${session}    normalize_json=True
-    ...    http_timeout=${http_timeout}
+    ...    http_timeout=${http_timeout}    log_response=${log_response}
     BuiltIn.Run_Keyword_If    ${verify}    Verify_Response_As_Json_Templated    response=${response_text}    folder=${folder}    base_name=data    mapping=${mapping}
     ...    iterations=${iterations}    iter_start=${iter_start}
     [Return]    ${response_text}
@@ -249,15 +249,15 @@ Verify_Response_As_Xml_Templated
     ...    endline=${EMPTY}    iterations=${iterations}    iter_start=${iter_start}
 
 Get_As_Json_From_Uri
-    [Arguments]    ${uri}    ${session}=default    ${http_timeout}=${EMPTY}
+    [Arguments]    ${uri}    ${session}=default    ${http_timeout}=${EMPTY}    ${log_response}=True
     [Documentation]    Specify JSON headers and return Get_From_Uri normalized response text.
-    ${response_text} =    Get_From_Uri    uri=${uri}    accept=${ACCEPT_EMPTY}    session=${session}    normalize_json=True    http_timeout=${http_timeout}
+    ${response_text} =    Get_From_Uri    uri=${uri}    accept=${ACCEPT_EMPTY}    session=${session}    normalize_json=True    http_timeout=${http_timeout}    log_response=${log_response}
     [Return]    ${response_text}
 
 Get_As_Xml_From_Uri
-    [Arguments]    ${uri}    ${session}=default    ${http_timeout}=${EMPTY}
+    [Arguments]    ${uri}    ${session}=default    ${http_timeout}=${EMPTY}    ${log_response}=True
     [Documentation]    Specify XML headers and return Get_From_Uri response text.
-    ${response_text} =    Get_From_Uri    uri=${uri}    accept=${ACCEPT_XML}    session=${session}    normalize_json=False    http_timeout=${http_timeout}
+    ${response_text} =    Get_From_Uri    uri=${uri}    accept=${ACCEPT_XML}    session=${session}    normalize_json=False    http_timeout=${http_timeout}    log_response=${log_response}
     [Return]    ${response_text}
 
 Put_As_Json_To_Uri
@@ -321,13 +321,13 @@ Resolve_Volatiles_Path
     [Return]    ${volatiles_list}
 
 Get_Templated
-    [Arguments]    ${folder}    ${accept}    ${mapping}={}    ${session}=default    ${normalize_json}=False    ${http_timeout}=${EMPTY}
+    [Arguments]    ${folder}    ${accept}    ${mapping}={}    ${session}=default    ${normalize_json}=False    ${http_timeout}=${EMPTY}    ${log_response}=True
     [Documentation]    Resolve URI from folder, call Get_From_Uri, return response text.
     ${uri} =    Resolve_Text_From_Template_Folder    folder=${folder}    base_name=location    extension=uri    mapping=${mapping}
     ${jmes_expression} =    Resolve_Jmes_Path    ${folder}
     ${volatiles_list}=    Resolve_Volatiles_Path    ${folder}
     ${response_text} =    Get_From_Uri    uri=${uri}    accept=${accept}    session=${session}    normalize_json=${normalize_json}    jmes_path=${jmes_expression}
-    ...    http_timeout=${http_timeout}    keys_with_volatiles=${volatiles_list}
+    ...    http_timeout=${http_timeout}    keys_with_volatiles=${volatiles_list}    log_response=${log_response}
     [Return]    ${response_text}
 
 Put_Templated
@@ -369,7 +369,7 @@ Verify_Response_Templated
 
 Get_From_Uri
     [Arguments]    ${uri}    ${accept}=${ACCEPT_EMPTY}    ${session}=default    ${normalize_json}=False    ${jmes_path}=${EMPTY}    ${http_timeout}=${EMPTY}
-    ...    ${keys_with_volatiles}=${EMPTY}
+    ...    ${keys_with_volatiles}=${EMPTY}    ${log_response}=True
     [Documentation]    GET data from given URI, check status code and return response text.
     ...    \${accept} is a Python object with headers to use.
     ...    If \${normalize_json}, normalize as JSON text before returning.
@@ -377,7 +377,7 @@ Get_From_Uri
     BuiltIn.Log    ${accept}
     ${response} =    BuiltIn.Run_Keyword_If    """${http_timeout}""" == """${EMPTY}"""    RequestsLibrary.Get_Request    alias=${session}    uri=${uri}    headers=${accept}
     ...    ELSE    RequestsLibrary.Get_Request    alias=${session}    uri=${uri}    headers=${accept}    timeout=${http_timeout}
-    Check_Status_Code    ${response}
+    Check_Status_Code    ${response}    log_response=${log_response}
     BuiltIn.Run_Keyword_Unless    ${normalize_json}    BuiltIn.Return_From_Keyword    ${response.text}
     ${text_normalized} =    norm_json.normalize_json_text    ${response.text}    jmes_path=${jmes_path}    keys_with_volatiles=${keys_with_volatiles}
     [Return]    ${text_normalized}
@@ -423,11 +423,13 @@ Post_To_Uri
     [Return]    ${text_normalized}
 
 Check_Status_Code
-    [Arguments]    ${response}    ${additional_allowed_status_codes}=${NO_STATUS_CODES}    ${explicit_status_codes}=${NO_STATUS_CODES}
-    [Documentation]    Log response text, check status_code is one of allowed ones.
+    [Arguments]    ${response}    ${additional_allowed_status_codes}=${NO_STATUS_CODES}    ${explicit_status_codes}=${NO_STATUS_CODES}    ${log_response}=True
+    [Documentation]    Log response text, check status_code is one of allowed ones. In cases where this keyword is
+    ...    called in a WUKS it could end up logging tons of data and it may be desired to skip the logging by passing
+    ...    log_response=False, but by default it remains True.
     # TODO: Remove overlap with keywords from Utils.robot
-    BuiltIn.Log    ${response.text}
-    BuiltIn.Log    ${response.status_code}
+    Run Keyword If    "${log_response}" == "True"    BuiltIn.Log    ${response.text}
+    Run Keyword If    "${log_response}" == "True"    BuiltIn.Log    ${response.status_code}
     # In order to allow other existing keywords to consume this keyword by passing a single non-list status code, we need to
     # check the type of the argument passed and convert those single non-list codes in to a one item list
     ${status_codes_type} =    Evaluate    type($additional_allowed_status_codes).__name__