Add retry mechanism instead of sleep
[cps.git] / csit / tests / cps-data-operations / cps-data-operations.robot
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2024 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 *** Settings ***
22 Documentation         NCMP
23
24 Library               Collections
25 Library               OperatingSystem
26 Library               RequestsLibrary
27 Library               BuiltIn
28 Library               ConfluentKafkaLibrary
29
30 Suite Setup           Create Session      CPS_URL    http://${CPS_CORE_HOST}:${CPS_CORE_PORT}
31
32 *** Variables ***
33
34 ${auth}                                 Basic Y3BzdXNlcjpjcHNyMGNrcyE=
35 ${topic}                                data-operation-client-topic
36 ${ncmpBasePath}                         /ncmp
37 ${expectedRequestId}                    ${EMPTY}
38 ${dmipluginCsitStubUrl}                 http://${DMI_CSIT_STUB_HOST}:${DMI_CSIT_STUB_PORT}
39 ${newCmHandleRequestBody}               {"dmiPlugin":"${dmipluginCsitStubUrl}","createdCmHandles":[{"cmHandle":"CMHandle1"}]}
40
41 *** Test Cases ***
42
43 NCMP Data Operation, forwarded to DMI, response on Client Topic
44         ${uri}=                          Set Variable        ${ncmpBasePath}/v1/data
45         ${dataOperationReqBody}=         Get Binary File     ${DATADIR_CPS_CORE}${/}dataOperationRequest.json
46         ${params}=                       Create Dictionary   topic=${topic}
47         ${headers}=                      Create Dictionary   Content-Type=application/json         Authorization=${auth}
48                                          POST On Session     CPS_URL   ncmpInventory/v1/ch         headers=${headers}     data=${newCmHandleRequestBody}
49         ${getCmHandleUri}=               Set Variable        ${ncmpBasePath}/v1/ch/CMHandle1
50         ${getCmHandleHeaders}=           Create Dictionary   Authorization=${auth}
51         Wait Until Keyword Succeeds      8sec    100ms       Is CM Handle READY    ${getCmHandleUri}    ${getCmHandleHeaders}    CMHandle1
52         ${response}=                     POST On Session     CPS_URL   ${uri}   params=${params}   headers=${headers}     data=${dataOperationReqBody}
53         Set Global Variable              ${expectedRequestId}       ${response.json()}[requestId]
54         Should Be Equal As Strings       ${response.status_code}   200
55
56 Consume cloud event from client topic
57     ${group_id}=         Create Consumer     auto_offset_reset=earliest
58     Subscribe Topic      topics=${topic}     group_id=${group_id}
59     ${messages}=         Poll                group_id=${group_id}     only_value=false
60     ${event}             Set Variable        ${messages}[0]
61     ${headers}           Set Variable        ${event.headers()}
62     FOR   ${header_key_value_pair}   IN  @{headers}
63         Compare Header Values       ${header_key_value_pair[0]}   ${header_key_value_pair[1]}      "ce_specversion"      "1.0"
64         Compare Header Values       ${header_key_value_pair[0]}   ${header_key_value_pair[1]}      "ce_type"             "org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent"
65         Compare Header Values       ${header_key_value_pair[0]}   ${header_key_value_pair[1]}      "ce_correlationid"    "${expectedRequestId}"
66                 Compare Header Values       ${header_key_value_pair[0]}   ${header_key_value_pair[1]}      "ce_source"           "DMI"
67     END
68     [Teardown]                      Basic Teardown                    ${group_id}
69
70 *** Keywords ***
71 Compare Header Values
72     [Arguments]    ${header_key}    ${header_value}    ${header_to_check}    ${expected_header_value}
73     IF   "${header_key}" == ${header_to_check}
74         Should Be Equal As Strings    "${header_value}"    ${expected_header_value}
75     END
76
77 Is CM Handle READY
78     [Arguments]    ${uri}    ${headers}    ${cmHandle}
79     ${response}=    GET On Session    CPS_URL    ${uri}    headers=${headers}
80     Should Be Equal As Strings    ${response.status_code}    200
81     FOR  ${item}  IN  ${response.json()}
82             IF  "${item['cmHandle']}" == "${cmHandle}"
83                 Should Be Equal As Strings    ${item['state']['cmHandleState']}    READY
84             END
85     END
86
87 Basic Teardown
88     [Arguments]     ${group_id}
89     Unsubscribe     ${group_id}
90     Close Consumer  ${group_id}
91