You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

141 lines
4.7 KiB

  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using MQTTnet.Client.Publishing;
  3. using MQTTnet.Client.Receiving;
  4. using MQTTnet.Server;
  5. using MQTTnet.Server.Status;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MQTTnet.Tests.Mockups
  11. {
  12. public sealed class TestServerWrapper : IMqttServer
  13. {
  14. public TestServerWrapper(IMqttServer implementation, TestContext testContext, TestEnvironment testEnvironment)
  15. {
  16. Implementation = implementation;
  17. TestContext = testContext;
  18. TestEnvironment = testEnvironment;
  19. }
  20. public IMqttServer Implementation { get; }
  21. public TestContext TestContext { get; }
  22. public TestEnvironment TestEnvironment { get; }
  23. public bool IsStarted { get; }
  24. public IMqttServerStartedHandler StartedHandler
  25. {
  26. get => Implementation.StartedHandler;
  27. set => Implementation.StartedHandler = value;
  28. }
  29. public IMqttServerStoppedHandler StoppedHandler
  30. {
  31. get => Implementation.StoppedHandler;
  32. set => Implementation.StoppedHandler = value;
  33. }
  34. public IMqttServerClientConnectedHandler ClientConnectedHandler
  35. {
  36. get => Implementation.ClientConnectedHandler;
  37. set => Implementation.ClientConnectedHandler = value;
  38. }
  39. public IMqttServerClientDisconnectedHandler ClientDisconnectedHandler
  40. {
  41. get => Implementation.ClientDisconnectedHandler;
  42. set => Implementation.ClientDisconnectedHandler = value;
  43. }
  44. public IMqttServerClientSubscribedTopicHandler ClientSubscribedTopicHandler
  45. {
  46. get => Implementation.ClientSubscribedTopicHandler;
  47. set => Implementation.ClientSubscribedTopicHandler = value;
  48. }
  49. public IMqttServerClientUnsubscribedTopicHandler ClientUnsubscribedTopicHandler
  50. {
  51. get => Implementation.ClientUnsubscribedTopicHandler;
  52. set => Implementation.ClientUnsubscribedTopicHandler = value;
  53. }
  54. public IMqttServerOptions Options => Implementation.Options;
  55. public IMqttApplicationMessageReceivedHandler ApplicationMessageReceivedHandler
  56. {
  57. get => Implementation.ApplicationMessageReceivedHandler;
  58. set => Implementation.ApplicationMessageReceivedHandler = value;
  59. }
  60. public Task ClearRetainedApplicationMessagesAsync()
  61. {
  62. return Implementation.ClearRetainedApplicationMessagesAsync();
  63. }
  64. public Task<IList<IMqttClientStatus>> GetClientStatusAsync()
  65. {
  66. return Implementation.GetClientStatusAsync();
  67. }
  68. public Task<IList<MqttApplicationMessage>> GetRetainedApplicationMessagesAsync()
  69. {
  70. return Implementation.GetRetainedApplicationMessagesAsync();
  71. }
  72. public Task<IList<IMqttSessionStatus>> GetSessionStatusAsync()
  73. {
  74. return Implementation.GetSessionStatusAsync();
  75. }
  76. public Task<MqttClientPublishResult> PublishAsync(MqttApplicationMessage applicationMessage, CancellationToken cancellationToken)
  77. {
  78. return Implementation.PublishAsync(applicationMessage, cancellationToken);
  79. }
  80. public Task StartAsync(IMqttServerOptions options)
  81. {
  82. if (TestContext != null)
  83. {
  84. var serverOptions = (MqttServerOptions)options;
  85. if (serverOptions.ConnectionValidator == null)
  86. {
  87. serverOptions.ConnectionValidator = new MqttServerConnectionValidatorDelegate(ConnectionValidator);
  88. }
  89. }
  90. return Implementation.StartAsync(options);
  91. }
  92. public Task StopAsync()
  93. {
  94. return Implementation.StopAsync();
  95. }
  96. public Task SubscribeAsync(string clientId, ICollection<MqttTopicFilter> topicFilters)
  97. {
  98. return Implementation.SubscribeAsync(clientId, topicFilters);
  99. }
  100. public Task UnsubscribeAsync(string clientId, ICollection<string> topicFilters)
  101. {
  102. return Implementation.UnsubscribeAsync(clientId, topicFilters);
  103. }
  104. public void Dispose()
  105. {
  106. Implementation.Dispose();
  107. }
  108. void ConnectionValidator(MqttConnectionValidatorContext ctx)
  109. {
  110. if (!ctx.ClientId.StartsWith(TestContext.TestName))
  111. {
  112. TestEnvironment.TrackException(new InvalidOperationException($"Invalid client ID used ({ctx.ClientId}). It must start with UnitTest name."));
  113. ctx.ReasonCode = Protocol.MqttConnectReasonCode.ClientIdentifierNotValid;
  114. }
  115. }
  116. }
  117. }