locationclient_ut.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include "utils.h"
  4. #include "gtest/gtest.h"
  5. #include "gmock/gmock.h"
  6. #include "alibabacloud/core/AlibabaCloud.h"
  7. #include "alibabacloud/core/SimpleCredentialsProvider.h"
  8. #include "alibabacloud/core/location/LocationClient.h"
  9. using namespace std;
  10. using namespace AlibabaCloud;
  11. using namespace AlibabaCloud::Location;
  12. using ::testing::_;
  13. using ::testing::DefaultValue;
  14. namespace {
  15. class mockLocationClient : public LocationClient {
  16. public:
  17. mockLocationClient(const string& key, const string& secret, const ClientConfiguration& cfg):
  18. LocationClient(key, secret, cfg) {}
  19. mockLocationClient(const Credentials& credentials, const ClientConfiguration& cfg):
  20. LocationClient(credentials, cfg) {}
  21. mockLocationClient(const std::shared_ptr<CredentialsProvider> &credentialsProvider, const ClientConfiguration &configuration):
  22. LocationClient(credentialsProvider, configuration) {}
  23. MOCK_CONST_METHOD3(makeRequest, RpcServiceClient::JsonOutcome (const std::string &endpoint, const RpcServiceRequest &msg, HttpRequest::Method method));
  24. };
  25. TEST(LocationClient, sync_ak) {
  26. ClientConfiguration configuration("cn-hangzhou");
  27. Model::DescribeEndpointsRequest req;
  28. req.setId("cn-hangzhou");
  29. req.setServiceCode("ecs");
  30. req.setType("openAPI");
  31. mockLocationClient client("key", "secret", configuration);
  32. RpcServiceClient::JsonOutcome xout("{\"Endpoints\":{\"Endpoint\":[{\"Protocols\":{\"Protocols\":[\"HTTP\",\"HTTPS\"]},\"Type\":\"test-type\",\"Namespace\":\"test-namespace\",\"Id\":\"cn-hangzhou\",\"SerivceCode\":\"test-servicecode\",\"Endpoint\":\"test-endpoint\"}]},\"RequestId\":\"test-request-id\",\"Success\":true}");
  33. DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
  34. EXPECT_CALL(client, makeRequest(_, _, _));
  35. LocationClient::DescribeEndpointsOutcome out = client.describeEndpoints(req);
  36. EXPECT_TRUE(out.error().errorCode().empty());
  37. EXPECT_TRUE(out.result().success());
  38. EXPECT_TRUE(out.result().requestId() == "test-request-id");
  39. EXPECT_TRUE(out.result().endpoints().size() == 1);
  40. Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
  41. EXPECT_TRUE(ep.endpoint == "test-endpoint");
  42. EXPECT_TRUE(ep.id == "cn-hangzhou");
  43. EXPECT_TRUE(ep.namespace_ == "test-namespace");
  44. EXPECT_TRUE(ep.type == "test-type");
  45. EXPECT_TRUE(ep.protocols.size() == 2);
  46. std::vector<std::string>::iterator it;
  47. std::vector<std::string> protocols = ep.protocols;
  48. it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
  49. EXPECT_TRUE(it != protocols.end());
  50. it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
  51. EXPECT_TRUE(it != protocols.end());
  52. }
  53. //{"Endpoints":{"Endpoint":[{"Protocols":{"Protocols":["HTTP","HTTPS"]},"Type":"openAPI","Namespace":"","Id":"cn-hangzhou","SerivceCode":"ecs","Endpoint":"ecs-cn-hangzhou.aliyuncs.com"}]},"RequestId":"test-request-id","Success":true}
  54. TEST(LocationClient, sync_credentials) {
  55. Credentials credentials("key", "secret");
  56. ClientConfiguration configuration("cn-hangzhou");
  57. Model::DescribeEndpointsRequest req;
  58. req.setId("cn-hangzhou");
  59. req.setServiceCode("ecs");
  60. req.setType("openAPI");
  61. mockLocationClient client(credentials, configuration);
  62. RpcServiceClient::JsonOutcome xout("{\"Endpoints\":{\"Endpoint\":[{\"Protocols\":{\"Protocols\":[\"HTTP\",\"HTTPS\"]},\"Type\":\"test-type\",\"Namespace\":\"test-namespace\",\"Id\":\"cn-hangzhou\",\"SerivceCode\":\"test-servicecode\",\"Endpoint\":\"test-endpoint\"}]},\"RequestId\":\"test-request-id\",\"Success\":true}");
  63. DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
  64. EXPECT_CALL(client, makeRequest(_, _, _));
  65. LocationClient::DescribeEndpointsOutcome out = client.describeEndpoints(req);
  66. EXPECT_TRUE(out.error().errorCode().empty());
  67. EXPECT_TRUE(out.result().success());
  68. EXPECT_TRUE(out.result().requestId() == "test-request-id");
  69. EXPECT_TRUE(out.result().endpoints().size() == 1);
  70. Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
  71. EXPECT_TRUE(ep.endpoint == "test-endpoint");
  72. EXPECT_TRUE(ep.id == "cn-hangzhou");
  73. EXPECT_TRUE(ep.namespace_ == "test-namespace");
  74. EXPECT_TRUE(ep.type == "test-type");
  75. EXPECT_TRUE(ep.protocols.size() == 2);
  76. std::vector<std::string>::iterator it;
  77. std::vector<std::string> protocols = ep.protocols;
  78. it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
  79. EXPECT_TRUE(it != protocols.end());
  80. it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
  81. EXPECT_TRUE(it != protocols.end());
  82. }
  83. TEST(LocationClient, sync_credentials_ptr) {
  84. Credentials credentials("key", "secret");
  85. ClientConfiguration configuration("cn-hangzhou");
  86. Model::DescribeEndpointsRequest req;
  87. req.setId("cn-hangzhou");
  88. req.setServiceCode("ecs");
  89. req.setType("openAPI");
  90. mockLocationClient client(std::make_shared<SimpleCredentialsProvider>(credentials), configuration);
  91. RpcServiceClient::JsonOutcome xout("{\"Endpoints\":{\"Endpoint\":[{\"Protocols\":{\"Protocols\":[\"HTTP\",\"HTTPS\"]},\"Type\":\"test-type\",\"Namespace\":\"test-namespace\",\"Id\":\"cn-hangzhou\",\"SerivceCode\":\"test-servicecode\",\"Endpoint\":\"test-endpoint\"}]},\"RequestId\":\"test-request-id\",\"Success\":true}");
  92. DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
  93. EXPECT_CALL(client, makeRequest(_, _, _));
  94. LocationClient::DescribeEndpointsOutcome out = client.describeEndpoints(req);
  95. EXPECT_TRUE(out.error().errorCode().empty());
  96. EXPECT_TRUE(out.result().success());
  97. EXPECT_TRUE(out.result().requestId() == "test-request-id");
  98. EXPECT_TRUE(out.result().endpoints().size() == 1);
  99. Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
  100. EXPECT_TRUE(ep.endpoint == "test-endpoint");
  101. EXPECT_TRUE(ep.id == "cn-hangzhou");
  102. EXPECT_TRUE(ep.namespace_ == "test-namespace");
  103. EXPECT_TRUE(ep.type == "test-type");
  104. EXPECT_TRUE(ep.protocols.size() == 2);
  105. std::vector<std::string>::iterator it;
  106. std::vector<std::string> protocols = ep.protocols;
  107. it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
  108. EXPECT_TRUE(it != protocols.end());
  109. it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
  110. EXPECT_TRUE(it != protocols.end());
  111. }
  112. TEST(LocationClient, callable) {
  113. InitializeSdk();
  114. ClientConfiguration configuration("cn-hangzhou");
  115. Model::DescribeEndpointsRequest req;
  116. req.setId("cn-hangzhou");
  117. req.setServiceCode("ecs");
  118. req.setType("openAPI");
  119. mockLocationClient client("key", "secret", configuration);
  120. RpcServiceClient::JsonOutcome xout("{\"Endpoints\":{\"Endpoint\":[{\"Protocols\":{\"Protocols\":[\"HTTP\",\"HTTPS\"]},\"Type\":\"test-type\",\"Namespace\":\"test-namespace\",\"Id\":\"cn-hangzhou\",\"SerivceCode\":\"test-servicecode\",\"Endpoint\":\"test-endpoint\"}]},\"RequestId\":\"test-request-id\",\"Success\":true}");
  121. DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
  122. EXPECT_CALL(client, makeRequest(_, _, _));
  123. LocationClient::DescribeEndpointsOutcomeCallable cb = client.describeEndpointsCallable(req);
  124. LocationClient::DescribeEndpointsOutcome out = cb.get();
  125. EXPECT_TRUE(out.error().errorCode().empty());
  126. EXPECT_TRUE(out.result().success());
  127. EXPECT_TRUE(out.result().requestId() == "test-request-id");
  128. EXPECT_TRUE(out.result().endpoints().size() == 1);
  129. Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
  130. EXPECT_TRUE(ep.endpoint == "test-endpoint");
  131. EXPECT_TRUE(ep.id == "cn-hangzhou");
  132. EXPECT_TRUE(ep.namespace_ == "test-namespace");
  133. EXPECT_TRUE(ep.type == "test-type");
  134. EXPECT_TRUE(ep.protocols.size() == 2);
  135. std::vector<std::string>::iterator it;
  136. std::vector<std::string> protocols = ep.protocols;
  137. it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
  138. EXPECT_TRUE(it != protocols.end());
  139. it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
  140. EXPECT_TRUE(it != protocols.end());
  141. ShutdownSdk();
  142. }
  143. void cb(const LocationClient *client,
  144. const Model::DescribeEndpointsRequest &req,
  145. const LocationClient::DescribeEndpointsOutcome& out,
  146. const std::shared_ptr<const AsyncCallerContext>& contex) {
  147. EXPECT_FALSE(out.error().errorCode().empty());
  148. }
  149. TEST(LocationClient, async) {
  150. InitializeSdk();
  151. ClientConfiguration configuration("cn-hangzhou");
  152. Model::DescribeEndpointsRequest req;
  153. req.setId("cn-hangzhou");
  154. req.setServiceCode("ecs");
  155. req.setType("openAPI");
  156. LocationClient client("key", "secret", configuration);
  157. LocationClient::DescribeEndpointsOutcome out;
  158. const AsyncCallerContext context;
  159. LocationClient::DescribeEndpointsAsyncHandler handler(cb);
  160. client.describeEndpointsAsync(req, handler, std::make_shared<const AsyncCallerContext>(context));
  161. ShutdownSdk();
  162. }
  163. }