| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using MQTTnet.Client;
- using MQTTnet.Client.Connecting;
- using MQTTnet.Client.Disconnecting;
- using MQTTnet.Client.ExtendedAuthenticationExchange;
- using MQTTnet.Client.Options;
- using MQTTnet.Client.Publishing;
- using MQTTnet.Client.Receiving;
- using MQTTnet.Client.Subscribing;
- using MQTTnet.Client.Unsubscribing;
- using System.Threading;
- using System.Threading.Tasks;
- namespace MQTTnet.Tests.Mockups
- {
- public sealed class TestClientWrapper : IMqttClient
- {
- public TestClientWrapper(IMqttClient implementation, TestContext testContext)
- {
- Implementation = implementation;
- TestContext = testContext;
- }
- public IMqttClient Implementation { get; }
- public TestContext TestContext { get; }
- public bool IsConnected => Implementation.IsConnected;
- public IMqttClientOptions Options => Implementation.Options;
- public IMqttClientConnectedHandler ConnectedHandler
- {
- get => Implementation.ConnectedHandler;
- set => Implementation.ConnectedHandler = value;
- }
- public IMqttClientDisconnectedHandler DisconnectedHandler
- {
- get => Implementation.DisconnectedHandler;
- set => Implementation.DisconnectedHandler = value;
- }
- public IMqttApplicationMessageReceivedHandler ApplicationMessageReceivedHandler
- {
- get => Implementation.ApplicationMessageReceivedHandler;
- set => Implementation.ApplicationMessageReceivedHandler = value;
- }
- public Task<MqttClientAuthenticateResult> ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken)
- {
- if (TestContext != null)
- {
- var clientOptions = (MqttClientOptions)options;
- var existingClientId = clientOptions.ClientId;
- if (existingClientId != null && !existingClientId.StartsWith(TestContext.TestName))
- {
- clientOptions.ClientId = TestContext.TestName + existingClientId;
- }
- }
- return Implementation.ConnectAsync(options, cancellationToken);
- }
- public Task DisconnectAsync(MqttClientDisconnectOptions options, CancellationToken cancellationToken)
- {
- return Implementation.DisconnectAsync(options, cancellationToken);
- }
- public void Dispose()
- {
- Implementation.Dispose();
- }
- public Task PingAsync(CancellationToken cancellationToken)
- {
- return Implementation.PingAsync(cancellationToken);
- }
- public Task<MqttClientPublishResult> PublishAsync(MqttApplicationMessage applicationMessage, CancellationToken cancellationToken)
- {
- return Implementation.PublishAsync(applicationMessage, cancellationToken);
- }
- public Task SendExtendedAuthenticationExchangeDataAsync(MqttExtendedAuthenticationExchangeData data, CancellationToken cancellationToken)
- {
- return Implementation.SendExtendedAuthenticationExchangeDataAsync(data, cancellationToken);
- }
- public Task<MqttClientSubscribeResult> SubscribeAsync(MqttClientSubscribeOptions options, CancellationToken cancellationToken)
- {
- return Implementation.SubscribeAsync(options, cancellationToken);
- }
- public Task<MqttClientUnsubscribeResult> UnsubscribeAsync(MqttClientUnsubscribeOptions options, CancellationToken cancellationToken)
- {
- return Implementation.UnsubscribeAsync(options, cancellationToken);
- }
- }
- }
|