Skip to content

Commit e34fd54

Browse files
committed
add tests for createProducerV2
1 parent c9cfaa8 commit e34fd54

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

tests/AuthTokenTest.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
#include <fstream>
2727
#include <streambuf>
2828
#include <string>
29+
#include <variant>
2930

31+
#include "VariantHelper.h"
3032
#include "lib/Future.h"
3133
#include "lib/LogUtils.h"
3234
#include "lib/Utils.h"
@@ -184,6 +186,13 @@ TEST(AuthPluginToken, testNoAuth) {
184186
Result result = client.createProducer(topicName, producer);
185187
ASSERT_EQ(ResultAuthorizationError, result);
186188

189+
std::visit(overloaded{[](Error&& error) {
190+
ASSERT_EQ(ResultAuthorizationError, error.result);
191+
ASSERT_EQ("Client is not authorized to Get Partition Metadata", error.message);
192+
},
193+
[](auto&&) { FAIL(); }},
194+
client.createProducerV2(topicName, {}));
195+
187196
Consumer consumer;
188197
result = client.subscribe(topicName, subName, consumer);
189198
ASSERT_EQ(ResultAuthorizationError, result);
@@ -200,6 +209,14 @@ TEST(AuthPluginToken, testNoAuthWithHttp) {
200209
Result result = client.createProducer(topicName, producer);
201210
ASSERT_EQ(ResultConnectError, result);
202211

212+
std::visit(overloaded{[](Error&& error) {
213+
ASSERT_EQ(ResultConnectError, error.result);
214+
ASSERT_TRUE(error.message.find("The requested URL returned error: 401") !=
215+
std::string::npos);
216+
},
217+
[](auto&&) { FAIL(); }},
218+
client.createProducerV2(topicName, {}));
219+
203220
Consumer consumer;
204221
result = client.subscribe(topicName, subName, consumer);
205222
ASSERT_EQ(ResultConnectError, result);
@@ -212,5 +229,16 @@ TEST(AuthPluginToken, testTokenSupplierException) {
212229
Client client(serviceUrl, config);
213230
Producer producer;
214231
ASSERT_EQ(ResultAuthenticationError, client.createProducer("topic", producer));
232+
233+
std::visit(
234+
overloaded{[](Error&& error) {
235+
ASSERT_EQ(ResultAuthenticationError, error.result);
236+
ASSERT_TRUE(error.message.find("failed to generate token") != std::string::npos);
237+
},
238+
[](auto&&) { FAIL(); }},
239+
client.createProducerV2("topic", {}));
240+
215241
ASSERT_EQ(ResultOk, client.close());
242+
243+
client.close();
216244
}

tests/VariantHelper.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
#pragma once
20+
21+
// See https://en.cppreference.com/cpp/utility/variant/visit2
22+
template <typename... Ts>
23+
struct overloaded : Ts... {
24+
using Ts::operator()...;
25+
};
26+
// explicit deduction guide (not needed as of C++20)
27+
template <typename... Ts>
28+
overloaded(Ts...) -> overloaded<Ts...>;

0 commit comments

Comments
 (0)