< Summary

Class:Azure.Messaging.ServiceBus.ServiceBusRuleManager
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\RuleManager\ServiceBusRuleManager.cs
Covered lines:0
Uncovered lines:73
Coverable lines:73
Total lines:257
Line coverage:0% (0 of 73)
Covered branches:0
Total branches:0

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_SubscriptionPath()-0%100%
get_Identifier()-0%100%
get_IsDisposed()-0%100%
.ctor(...)-0%100%
AddRuleAsync()-0%100%
AddRuleAsync()-0%100%
RemoveRuleAsync()-0%100%
GetRulesAsync()-0%100%
DisposeAsync()-0%100%
Equals(...)-0%100%
GetHashCode()-0%100%
ToString()-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\RuleManager\ServiceBusRuleManager.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.ComponentModel;
 7using System.Threading;
 8using System.Threading.Tasks;
 9using Azure.Core;
 10using Azure.Messaging.ServiceBus.Core;
 11using Azure.Messaging.ServiceBus.Diagnostics;
 12using Azure.Messaging.ServiceBus.Management;
 13
 14namespace Azure.Messaging.ServiceBus
 15{
 16    /// <summary>
 17    /// Manages rules for subscriptions.
 18    /// </summary>
 19    internal class ServiceBusRuleManager : IAsyncDisposable
 20    {
 21        /// <summary>
 22        /// The path of the Service Bus subscription that the rule manager is connected to, specific to the
 23        /// Service Bus namespace that contains it.
 24        /// </summary>
 025        public string SubscriptionPath { get; private set; }
 26
 27        /// <summary>
 28        /// Gets the ID to identify this client. This can be used to correlate logs and exceptions.
 29        /// </summary>
 30        /// <remarks>Every new client has a unique ID.</remarks>
 031        internal string Identifier { get; private set; }
 32
 33        /// <summary>
 34        ///   Indicates whether or not this <see cref="ServiceBusRuleManager"/> has been disposed.
 35        /// </summary>
 36        ///
 37        /// <value>
 38        /// <c>true</c> if the rule manager is disposed; otherwise, <c>false</c>.
 39        /// </value>
 040        public bool IsDisposed { get; private set; } = false;
 41
 42        /// <summary>
 43        /// The active connection to the Azure Service Bus service, enabling client communications for metadata
 44        /// about the associated Service Bus entity and access to transport-aware rule manager.
 45        /// </summary>
 46        ///
 47        private readonly ServiceBusConnection _connection;
 48
 49        /// <summary>
 50        /// An abstracted Service Bus transport-specific rule manager that is associated with the
 51        /// Service Bus entity gateway; intended to perform delegated operations.
 52        /// </summary>
 53        internal readonly TransportRuleManager InnerRuleManager;
 54
 55        /// <summary>
 56        ///   Initializes a new instance of the <see cref="ServiceBusRuleManager"/> class.
 57        /// </summary>
 58        ///
 59        /// <param name="connection">The <see cref="ServiceBusConnection" /> connection to use for communication with th
 60        /// <param name="subscriptionPath">The path of the Service Bus subscription to which the rule manager is bound.<
 61        ///
 062        internal ServiceBusRuleManager(
 063            ServiceBusConnection connection,
 064            string subscriptionPath)
 65        {
 066            Argument.AssertNotNull(connection, nameof(connection));
 067            Argument.AssertNotNull(connection.RetryOptions, nameof(connection.RetryOptions));
 068            Argument.AssertNotNullOrWhiteSpace(subscriptionPath, nameof(subscriptionPath));
 069            connection.ThrowIfClosed();
 70
 071            Identifier = DiagnosticUtilities.GenerateIdentifier(subscriptionPath);
 072            _connection = connection;
 073            SubscriptionPath = subscriptionPath;
 074            InnerRuleManager = _connection.CreateTransportRuleManager(
 075                subscriptionPath: SubscriptionPath,
 076                retryPolicy: connection.RetryOptions.ToRetryPolicy(),
 077                identifier: Identifier);
 078        }
 79
 80        /// <summary>
 81        /// Adds a rule to the current subscription to filter the messages reaching from topic to the subscription.
 82        /// </summary>
 83        ///
 84        /// <param name="ruleName">Name of the rule</param>
 85        /// <param name="filter">The filter expression against which messages will be matched.</param>
 86        /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t
 87        ///
 88        /// <remarks>
 89        /// You can add rules to the subscription that decides which messages from the topic should reach the subscripti
 90        /// A default <see cref="TrueRuleFilter"/> rule named <see cref="RuleProperties.DefaultRuleName"/> is always add
 91        /// You can add multiple rules with distinct names to the same subscription.
 92        /// Multiple filters combine with each other using logical OR condition. i.e., If any filter succeeds, the messa
 93        /// </remarks>
 94        ///
 95        /// <returns>A task instance that represents the asynchronous add rule operation.</returns>
 96        public virtual async Task AddRuleAsync(
 97            string ruleName,
 98            RuleFilter filter,
 99            CancellationToken cancellationToken = default)
 100        {
 0101            await AddRuleAsync(new RuleProperties(name: ruleName, filter: filter), cancellationToken).ConfigureAwait(fal
 0102        }
 103
 104        /// <summary>
 105        /// Adds a rule to the current subscription to filter the messages reaching from topic to the subscription.
 106        /// </summary>
 107        ///
 108        /// <param name="description">The rule description that provides the rule to add.</param>
 109        /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t
 110        ///
 111        /// <remarks>
 112        /// You can add rules to the subscription that decides which messages from the topic should reach the subscripti
 113        /// A default <see cref="TrueRuleFilter"/> rule named <see cref="RuleProperties.DefaultRuleName"/> is always add
 114        /// You can add multiple rules with distinct names to the same subscription.
 115        /// Multiple filters combine with each other using logical OR condition. i.e., If any filter succeeds, the messa
 116        /// </remarks>
 117        ///
 118        /// <returns>A task instance that represents the asynchronous add rule operation.</returns>
 119        public virtual async Task AddRuleAsync(
 120            RuleProperties description,
 121            CancellationToken cancellationToken = default)
 122        {
 0123            Argument.AssertNotDisposed(IsDisposed, nameof(ServiceBusRuleManager));
 0124            Argument.AssertNotNull(description, nameof(description));
 0125            cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>();
 0126            EntityNameFormatter.CheckValidRuleName(description.Name);
 0127            ServiceBusEventSource.Log.AddRuleStart(Identifier, description.Name);
 128
 129            try
 130            {
 0131                await InnerRuleManager.AddRuleAsync(
 0132                    description,
 0133                    cancellationToken).ConfigureAwait(false);
 0134            }
 0135            catch (Exception exception)
 136            {
 0137                ServiceBusEventSource.Log.AddRuleException(Identifier, exception.ToString());
 0138                throw;
 139            }
 140
 0141            cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>();
 0142            ServiceBusEventSource.Log.AddRuleComplete(Identifier);
 0143        }
 144
 145        /// <summary>
 146        /// Removes the rule on the subscription identified by <paramref name="ruleName" />.
 147        /// </summary>
 148        ///
 149        /// <param name="ruleName">Name of the rule</param>
 150        /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t
 151        ///
 152        /// <returns>A task instance that represents the asynchronous remove rule operation.</returns>
 153        public virtual async Task RemoveRuleAsync(
 154            string ruleName,
 155            CancellationToken cancellationToken = default)
 156        {
 0157            Argument.AssertNotDisposed(IsDisposed, nameof(ServiceBusRuleManager));
 0158            Argument.AssertNotNullOrEmpty(ruleName, nameof(ruleName));
 0159            cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>();
 0160            ServiceBusEventSource.Log.RemoveRuleStart(Identifier, ruleName);
 161
 162            try
 163            {
 0164                await InnerRuleManager.RemoveRuleAsync(
 0165                    ruleName,
 0166                    cancellationToken).ConfigureAwait(false);
 0167            }
 0168            catch (Exception exception)
 169            {
 0170                ServiceBusEventSource.Log.RemoveRuleException(Identifier, exception.ToString());
 0171                throw;
 172            }
 173
 0174            cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>();
 0175            ServiceBusEventSource.Log.RemoveRuleComplete(Identifier);
 0176        }
 177
 178        /// <summary>
 179        /// Get all rules associated with the subscription.
 180        /// </summary>
 181        ///
 182        /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t
 183        ///
 184        /// <returns>Returns a list of rules description</returns>
 185        public virtual async Task<IList<RuleProperties>> GetRulesAsync(CancellationToken cancellationToken = default)
 186        {
 187
 0188            Argument.AssertNotDisposed(IsDisposed, nameof(ServiceBusRuleManager));
 0189            cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>();
 0190            ServiceBusEventSource.Log.GetRuleStart(Identifier);
 191            IList<RuleProperties> rulesDescription;
 192
 193            try
 194            {
 0195                rulesDescription = await InnerRuleManager.GetRulesAsync(cancellationToken).ConfigureAwait(false);
 0196            }
 0197            catch (Exception exception)
 198            {
 0199                ServiceBusEventSource.Log.GetRuleException(Identifier, exception.ToString());
 0200                throw;
 201            }
 202
 0203            cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>();
 0204            ServiceBusEventSource.Log.GetRuleComplete(Identifier);
 0205            return rulesDescription;
 0206        }
 207
 208        /// <summary>
 209        /// Performs the task needed to clean up resources used by the <see cref="ServiceBusRuleManager" />.
 210        /// </summary>
 211        ///
 212        /// <returns>A task to be resolved on when the operation has completed.</returns>
 213        public virtual async ValueTask DisposeAsync()
 214        {
 0215            IsDisposed = true;
 216
 0217            ServiceBusEventSource.Log.ClientDisposeStart(typeof(ServiceBusRuleManager), Identifier);
 218            try
 219            {
 0220                await InnerRuleManager.CloseAsync(CancellationToken.None).ConfigureAwait(false);
 0221            }
 0222            catch (Exception ex)
 223            {
 0224                ServiceBusEventSource.Log.ClientDisposeException(typeof(ServiceBusRuleManager), Identifier, ex);
 0225                throw;
 226            }
 227
 0228            ServiceBusEventSource.Log.ClientDisposeComplete(typeof(ServiceBusRuleManager), Identifier);
 0229        }
 230
 231        /// <summary>
 232        /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
 233        /// </summary>
 234        ///
 235        /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
 236        ///
 237        /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>
 238        [EditorBrowsable(EditorBrowsableState.Never)]
 0239        public override bool Equals(object obj) => base.Equals(obj);
 240
 241        /// <summary>
 242        /// Returns a hash code for this instance.
 243        /// </summary>
 244        ///
 245        /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a ha
 246        [EditorBrowsable(EditorBrowsableState.Never)]
 0247        public override int GetHashCode() => base.GetHashCode();
 248
 249        /// <summary>
 250        /// Converts the instance to string representation.
 251        /// </summary>
 252        ///
 253        /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
 254        [EditorBrowsable(EditorBrowsableState.Never)]
 0255        public override string ToString() => base.ToString();
 256    }
 257}