Add retry mechanism instead of sleep

- replaced sleeps with retry mechanism
- removed unnecessary sleeps before polling
- changed formatting

Issue-ID: CPS-1093
Signed-off-by: JvD_Ericsson <jeff.van.dam@est.tech>
Change-Id: I7e26570eeedea09fd36f9c9bce87c4cd45906aa9
diff --git a/csit/tests/cps-data-operations/cps-data-operations.robot b/csit/tests/cps-data-operations/cps-data-operations.robot
index 1f66119..17dce16 100644
--- a/csit/tests/cps-data-operations/cps-data-operations.robot
+++ b/csit/tests/cps-data-operations/cps-data-operations.robot
@@ -46,18 +46,19 @@
         ${params}=                       Create Dictionary   topic=${topic}
         ${headers}=                      Create Dictionary   Content-Type=application/json         Authorization=${auth}
                                          POST On Session     CPS_URL   ncmpInventory/v1/ch         headers=${headers}     data=${newCmHandleRequestBody}
-        Sleep                            8                   wait some time to get updated the cm handle state to READY
+        ${getCmHandleUri}=               Set Variable        ${ncmpBasePath}/v1/ch/CMHandle1
+        ${getCmHandleHeaders}=           Create Dictionary   Authorization=${auth}
+        Wait Until Keyword Succeeds      8sec    100ms       Is CM Handle READY    ${getCmHandleUri}    ${getCmHandleHeaders}    CMHandle1
         ${response}=                     POST On Session     CPS_URL   ${uri}   params=${params}   headers=${headers}     data=${dataOperationReqBody}
         Set Global Variable              ${expectedRequestId}       ${response.json()}[requestId]
         Should Be Equal As Strings       ${response.status_code}   200
-        Sleep                            5                         wait some time to get published a message to the client topic
 
 Consume cloud event from client topic
     ${group_id}=         Create Consumer     auto_offset_reset=earliest
     Subscribe Topic      topics=${topic}     group_id=${group_id}
     ${messages}=         Poll                group_id=${group_id}     only_value=false
-    ${event}                        Set Variable                      ${messages}[0]
-    ${headers}                      Set Variable                      ${event.headers()}
+    ${event}             Set Variable        ${messages}[0]
+    ${headers}           Set Variable        ${event.headers()}
     FOR   ${header_key_value_pair}   IN  @{headers}
         Compare Header Values       ${header_key_value_pair[0]}   ${header_key_value_pair[1]}      "ce_specversion"      "1.0"
         Compare Header Values       ${header_key_value_pair[0]}   ${header_key_value_pair[1]}      "ce_type"             "org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent"
@@ -68,9 +69,19 @@
 
 *** Keywords ***
 Compare Header Values
-    [Arguments]                    ${header_key}        ${header_value}     ${header_to_check}       ${expected_header_value}
+    [Arguments]    ${header_key}    ${header_value}    ${header_to_check}    ${expected_header_value}
     IF   "${header_key}" == ${header_to_check}
-        Should Be Equal As Strings              "${header_value}"    ${expected_header_value}
+        Should Be Equal As Strings    "${header_value}"    ${expected_header_value}
+    END
+
+Is CM Handle READY
+    [Arguments]    ${uri}    ${headers}    ${cmHandle}
+    ${response}=    GET On Session    CPS_URL    ${uri}    headers=${headers}
+    Should Be Equal As Strings    ${response.status_code}    200
+    FOR  ${item}  IN  ${response.json()}
+            IF  "${item['cmHandle']}" == "${cmHandle}"
+                Should Be Equal As Strings    ${item['state']['cmHandleState']}    READY
+            END
     END
 
 Basic Teardown
diff --git a/csit/tests/cps-data-sync/cps-data-sync.robot b/csit/tests/cps-data-sync/cps-data-sync.robot
index 71de4be..c0ee4da 100644
--- a/csit/tests/cps-data-sync/cps-data-sync.robot
+++ b/csit/tests/cps-data-sync/cps-data-sync.robot
@@ -34,20 +34,40 @@
 ${ncmpBasePath}           /ncmp
 
 *** Test Cases ***
+
+Check if ietfYang-PNFDemo is READY
+    ${uri}=        Set Variable       ${ncmpBasePath}/v1/ch/ietfYang-PNFDemo
+    ${headers}=    Create Dictionary  Authorization=${auth}
+    Wait Until Keyword Succeeds       10sec    100ms    Is CM Handle READY    ${uri}    ${headers}    ietfYang-PNFDemo
+
 Operational state goes to UNSYNCHRONIZED when data sync (flag) is enabled
     ${uri}=              Set Variable       ${ncmpBasePath}/v1/ch/ietfYang-PNFDemo/data-sync
     ${params}=           Create Dictionary  dataSyncEnabled=true
     ${headers}=          Create Dictionary  Authorization=${auth}
     ${response}=         PUT On Session     CPS_URL   ${uri}   params=${params}   headers=${headers}
     Should Be Equal As Strings              ${response.status_code}   200
-    ${verifyUri}=              Set Variable       ${ncmpBasePath}/v1/ch/ietfYang-PNFDemo/state
-    ${verifyHeaders}=          Create Dictionary  Authorization=${auth}
-    ${verifyResponse}=         GET On Session     CPS_URL   ${verifyUri}   headers=${verifyHeaders}
-    Should Be Equal As Strings                    ${verifyResponse.json()['state']['dataSyncState']['operational']['syncState']}   UNSYNCHRONIZED
-    Sleep    5
+    ${verifyUri}=        Set Variable       ${ncmpBasePath}/v1/ch/ietfYang-PNFDemo/state
+    ${verifyHeaders}=    Create Dictionary  Authorization=${auth}
+    ${verifyResponse}=   GET On Session     CPS_URL   ${verifyUri}   headers=${verifyHeaders}
+    Should Be Equal As Strings    ${verifyResponse.json()['state']['dataSyncState']['operational']['syncState']}   UNSYNCHRONIZED
 
 Operational state goes to SYNCHRONIZED after sometime when data sync (flag) is enabled
-    ${uri}=              Set Variable       ${ncmpBasePath}/v1/ch/ietfYang-PNFDemo/state
-    ${headers}=          Create Dictionary  Authorization=${auth}
-    ${response}=         GET On Session     CPS_URL   ${uri}   headers=${headers}
-    Should Be Equal As Strings              ${response.json()['state']['dataSyncState']['operational']['syncState']}   SYNCHRONIZED
\ No newline at end of file
+    ${uri}=        Set Variable       ${ncmpBasePath}/v1/ch/ietfYang-PNFDemo/state
+    ${headers}=    Create Dictionary  Authorization=${auth}
+    Wait Until Keyword Succeeds    10sec    100ms    Is CM Handle State SYNCHRONIZED    ${uri}    ${headers}
+
+*** Keywords ***
+Is CM Handle READY
+    [Arguments]    ${uri}    ${headers}    ${cmHandle}
+    ${response}=    GET On Session    CPS_URL    ${uri}    headers=${headers}
+    Should Be Equal As Strings    ${response.status_code}    200
+    FOR  ${item}  IN  ${response.json()}
+            IF  "${item['cmHandle']}" == "${cmHandle}"
+                Should Be Equal As Strings    ${item['state']['cmHandleState']}    READY
+            END
+    END
+
+Is CM Handle State SYNCHRONIZED
+    [Arguments]    ${uri}    ${headers}
+    ${response}=   GET On Session     CPS_URL    ${uri}    headers=${headers}
+    Should Be Equal As Strings        ${response.json()['state']['dataSyncState']['operational']['syncState']}    SYNCHRONIZED
diff --git a/csit/tests/cps-model-sync/cps-model-sync.robot b/csit/tests/cps-model-sync/cps-model-sync.robot
index 704d02c..3e8551f 100644
--- a/csit/tests/cps-model-sync/cps-model-sync.robot
+++ b/csit/tests/cps-model-sync/cps-model-sync.robot
@@ -85,5 +85,4 @@
             IF   "${item['moduleName']}" == "stores"
                 Should Be Equal As Strings              "${item['revision']}"   "2020-09-15"
             END
-    END
-    Sleep    10
\ No newline at end of file
+    END
\ No newline at end of file
diff --git a/csit/tests/cps-trust-level/cps-trust-level.robot b/csit/tests/cps-trust-level/cps-trust-level.robot
index 7065900..e4deeff 100644
--- a/csit/tests/cps-trust-level/cps-trust-level.robot
+++ b/csit/tests/cps-trust-level/cps-trust-level.robot
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2023 Nordix Foundation
+ *  Copyright (C) 2023-2024 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -44,14 +44,13 @@
     ${headers}=     Create Dictionary    Content-Type=application/json    Authorization=${auth}
     ${response}=    POST On Session      CPS_URL   ${uri}    headers=${headers}    data=${jsonCreateCmHandles}
     Should Be Equal As Strings           ${response.status_code}    200
-    Sleep           5
 
 Verify notification
     ${group_id}=         Create Consumer     auto_offset_reset=earliest
-    Subscribe Topic      topics=cm-events     group_id=${group_id}
-    ${result}=      Poll                    group_id=${group_id}  only_value=False  poll_attempts=5
-    ${headers}                      Set Variable                ${result[0].headers()}
-    ${payload}                      Set Variable                ${result[0].value()}
+    Subscribe Topic      topics=cm-events    group_id=${group_id}
+    ${result}=           Poll                group_id=${group_id}  only_value=False  poll_attempts=5
+    ${headers}           Set Variable        ${result[0].headers()}
+    ${payload}           Set Variable        ${result[0].value()}
     FOR   ${header_key_value_pair}   IN  @{headers}
         Compare Header Values       ${header_key_value_pair[0]}   ${header_key_value_pair[1]}     "ce_specversion"      "1.0"
         Compare Header Values       ${header_key_value_pair[0]}   ${header_key_value_pair[1]}     "ce_source"           "NCMP"