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