Upgrade RF syntax for v3.2 compatibility
[integration/test.git] / csit / libraries / DatastoreCRUD.robot
1 *** Settings ***
2 Library           Collections
3 Library           RequestsLibrary
4 Variables         ../variables/Variables.py
5
6 *** Keywords ***
7 Create Records
8     [Arguments]    ${controller_ip}    ${node}    ${first}    ${last}    ${prefix}    ${field bases}
9     ...    ${postfix}
10     [Documentation]    POSTs records to a controller's data store. First and last index numbers are specified
11     ...    as is a dictionary called field_bases containing the base name for the field contents
12     ...    onto which will be appended the index number. Prefix and postfix are used to complete
13     ...    the JSON payload. The keyword passes if return code is correct.
14     ${last}    Convert to Integer    ${last}
15     FOR    ${INDEX}    IN RANGE    ${first}    ${last+1}
16         ${payload}=    Assemble Payload    ${INDEX}    ${prefix}    ${field bases}    ${postfix}
17         Log    ${payload}
18         Create_Session    session    http://${controller_ip}:${RESTCONFPORT}${CONFIG_API}    headers=${HEADERS}    auth=${AUTH}
19         ${resp}    RequestsLibrary.Post Request    session    ${node}    ${payload}
20         Log    ${resp}
21         Should Be Equal As Strings    ${resp}    <Response [204]>
22     END
23
24 Read Records
25     [Arguments]    ${controller_ip}    ${node}
26     [Documentation]    GETs records from a shard on a controller's data store.
27     Create_Session    session    http://${controller_ip}:${RESTCONFPORT}${CONFIG_API}    headers=${HEADERS}    auth=${AUTH}
28     ${resp}=    RequestsLibrary.Get Request    session    ${node}
29     [Return]    ${resp.json()}
30
31 Update Records
32     [Arguments]    ${controller_ip}    ${node}    ${first}    ${last}    ${prefix}    ${field bases}
33     ...    ${postfix}
34     [Documentation]    PUTs records to shard on a controller's data store. First and last index numbers are specified
35     ...    as is a dictionary called field_bases containing the base name for the field contents
36     ...    onto which will be appended the index number. Prefix and postfix are used to complete
37     ...    the JSON payload. The keyword passes if return code is correct.
38     ${last}    Convert to Integer    ${last}
39     FOR    ${INDEX}    IN RANGE    ${first}    ${last+1}
40         ${payload}=    Assemble Payload    ${INDEX}    ${prefix}    ${field bases}    ${postfix}
41         Log    ${payload}
42         Create_Session    session    http://${controller_ip}:${RESTCONFPORT}${CONFIG_API}    headers=${HEADERS}    auth=${AUTH}
43         ${resp}=    RequestsLibrary.Put Request    session    ${node}/${INDEX}    ${payload}
44         Log    ${resp}
45         Should Be Equal As Strings    ${resp}    <Response [200]>
46     END
47
48 Delete Records
49     [Arguments]    ${controller_ip}    ${node}    ${first}    ${last}
50     [Documentation]    DELETEs specified range of records from a shard on a contrsoller's data store.
51     ${last}    Convert to Integer    ${last}
52     FOR    ${INDEX}    IN RANGE    ${first}    ${last+1}
53         Create_Session    session    http://${controller_ip}:${RESTCONFPORT}${CONFIG_API}    headers=${HEADERS}    auth=${AUTH}
54         ${resp}=    RequestsLibrary.Delete Request    session    ${node}/${INDEX}
55         Should Be Equal As Strings    ${resp}    <Response [200]>
56     END
57
58 Delete All Records
59     [Arguments]    ${controller_ip}    ${node}
60     [Documentation]    DELETEs all records from a shard on a controller's data store.
61     Create_Session    session    http://${controller_ip}:${RESTCONFPORT}${CONFIG_API}    headers=${HEADERS}    auth=${AUTH}
62     ${resp}=    RequestsLibrary.Delete Request    session    ${node}
63     Should Be Equal As Strings    ${resp}    <Response [200]>
64
65 Assemble Payload
66     [Arguments]    ${id}    ${prefix}    ${field bases}    ${postfix}
67     [Documentation]    Populates a payload for creating or updating a shard record.
68     ...    id: The record number and is also appended onto each field to uniquely identify it.
69     ...    prefix: The portion of the json payload before the records.
70     ...    field bases: A dictionary of records onto which the id is appended.
71     ...    prefix: The portion of the json payload after the records.
72     ${length}=    Get Length    ${field bases}
73     ${keys}=    Get Dictionary Keys    ${field bases}
74     ${payload}=    Set Variable    ${prefix}
75     FOR    ${key string}    IN    @{keys}
76         ${value string}=    Get From Dictionary    ${field bases}    ${key string}
77         ${payload}=    Catenate    ${payload}    "${key string}": "${value string}${id}",
78     END
79     ${payload}=    Get Substring    ${payload}    ${EMPTY}    -1
80     ${payload}=    Catenate    ${payload}    ${postfix}
81     [Return]    ${payload}