Rename /tests to /ckm to align with tizen branch
[platform/core/test/security-tests.git] / src / cynara-tests / common / cynara_test_client_async_request_monitor.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include <cynara_test_client_async_request_monitor.h>
18
19 #include <dpl/test/test_runner.h>
20
21 #include <exception>
22
23 namespace CynaraTestClientAsync {
24
25 RequestMonitor::~RequestMonitor() noexcept(false)
26 {
27     bool oops = std::uncaught_exception();
28     try {
29         for (auto ent : m_requests)
30         {
31             RUNNER_ERROR_MSG("There was no callback for request with:"
32                           << "id = " << ent.first << ","
33                           << "expectedResponse = " << ent.second.m_expectedResponse << ","
34                           << "expectedCause = " << ent.second.m_expectedCause << ".");
35         }
36         RUNNER_ASSERT_MSG(m_requests.empty(),
37                              m_requests.size() << "requests does not receive callback.");
38     } catch (...) {
39         if (!oops)
40             throw;
41         RUNNER_ERROR_MSG("Error: more exceptions thrown while releasing"
42                              " CynaraTestAsync::RequestMonitor.");
43     }
44 }
45
46 void RequestMonitor::registerRequest(cynara_check_id id, const RequestEntity &request)
47 {
48     auto p = m_requests.insert({id, request});
49     RUNNER_ASSERT_MSG(p.second,
50                           "Request with id = " << p.first->first << " already exists.");
51 }
52
53 void RequestMonitor::updateResponse(cynara_check_id checkId, cynara_async_call_cause cause,
54                                     int response, void *data)
55 {
56     RequestMonitor *monitor = reinterpret_cast<RequestMonitor*>(data);
57     if (!monitor) {
58         RUNNER_FAIL_MSG("Bad user data (nullptr) in response callback.");
59         return;
60     }
61
62     auto it = monitor->m_requests.find(checkId);
63     if (it == monitor->m_requests.end()) {
64         RUNNER_FAIL_MSG("Received unexpected callback for request:"
65                            << "id = " << checkId << ","
66                            << "response = " << response << ","
67                            << "cause = " << cause << ".");
68         return;
69     }
70
71     //save request data and remove request from monitored requests
72     auto expectedResponse = it->second.m_expectedResponse;
73     auto expectedCause = it->second.m_expectedCause;
74     auto userFunction = it->second.m_userFunction;
75     monitor->m_requests.erase(it);
76
77     RUNNER_ASSERT_MSG(cause == expectedCause,
78                          "Unexpected cause in response callback:"
79                             << "id = " << checkId << ","
80                             << "received response = " << response << ","
81                             << "expected response = " << expectedResponse << ","
82                             << "received cause = " << cause << ","
83                             << "expected cause = " << expectedCause << ".");
84
85     if (cause == CYNARA_CALL_CAUSE_ANSWER)
86     {
87         RUNNER_ASSERT_MSG(response == expectedResponse,
88                              "Unexpected response in response callback:"
89                                 << "id = " << checkId << ","
90                                 << "received response = " << response << ","
91                                 << "expected response = " << expectedResponse << ","
92                                 << "received cause = " << cause << ","
93                                 << "expected cause = " << expectedCause << ".");
94     }
95
96     if (userFunction)
97         userFunction();
98 }
99
100 }// namespace CynaraTestClientAsync