private AmqpServiceClient CreateAmpqServiceClient()
        {
            var client = new AmqpServiceClient(this, AmqpClientConstants.ManagementAddress);

            Fx.Assert(string.Equals(client.Address, AmqpClientConstants.ManagementAddress, StringComparison.OrdinalIgnoreCase),
                      "The address should match the address of managementServiceClient");
            return(client);
        }
        public async Task PurgeMessageQueueWithCancellationTokenTest()
        {
            // Arrange Moq
            Tuple <Mock <IHttpClientHelper>, AmqpServiceClient, PurgeMessageQueueResult> setupParameters = this.SetupPurgeMessageQueueTests();
            Mock <IHttpClientHelper> restOpMock     = setupParameters.Item1;
            AmqpServiceClient        serviceClient  = setupParameters.Item2;
            PurgeMessageQueueResult  expectedResult = setupParameters.Item3;

            // Execute method under test
            PurgeMessageQueueResult result = await serviceClient.PurgeMessageQueueAsync("TestDevice", CancellationToken.None).ConfigureAwait(false);

            // Verify expected result
            Assert.AreSame(expectedResult, result);
            restOpMock.VerifyAll();
        }
        public async Task PurgeMessageQueueDeviceNotFoundTest()
        {
            // Arrange Moq
            var restOpMock = new Mock <IHttpClientHelper>();

            restOpMock.Setup(restOp => restOp.DeleteAsync <PurgeMessageQueueResult>(
                                 It.IsAny <Uri>(), It.IsAny <IDictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > > >(), null, It.IsAny <CancellationToken>())
                             ).Throws(new DeviceNotFoundException("device-id"));

            // Instantiate AmqpServiceClient with Mock IHttpClientHelper
            var authMethod    = new ServiceAuthenticationWithSharedAccessPolicyKey("test", "dGVzdFN0cmluZzE=");
            var builder       = IotHubConnectionStringBuilder.Create("acme.azure-devices.net", authMethod);
            var serviceClient = new AmqpServiceClient(restOpMock.Object);

            // Execute method under test
            PurgeMessageQueueResult result = await serviceClient.PurgeMessageQueueAsync("TestDevice", CancellationToken.None).ConfigureAwait(false);
        }
        internal AmqpServiceClient <IAmqpEntityManagement> GetManagementServiceClient(string address)
        {
            if (this.managementServiceClient == null)
            {
                lock (ThisLock)
                {
                    if (this.managementServiceClient == null)
                    {
                        this.managementServiceClient = new AmqpServiceClient <IAmqpEntityManagement>(this, address);
                    }

                    Fx.Assert(string.Equals(this.managementServiceClient.Address, address, StringComparison.OrdinalIgnoreCase), "The address should match the address of managementServiceClient");
                }
            }

            return(this.managementServiceClient);
        }
        public async Task DisposeTest()
        {
            var restOpMock = new Mock <IHttpClientHelper>();

            restOpMock.Setup(restOp => restOp.Dispose());
            var connectionClosed = false;
            Func <TimeSpan, Task <AmqpSession> > onCreate = _ => Task.FromResult(new AmqpSession(null, new AmqpSessionSettings(), null));
            Action <AmqpSession> onClose = _ => { connectionClosed = true; };
            // Instantiate AmqpServiceClient with Mock IHttpClientHelper and IotHubConnection
            var connection    = new IotHubConnection(onCreate, onClose);
            var serviceClient = new AmqpServiceClient(connection, restOpMock.Object);
            // This is required to cause onClose callback invocation.
            await connection.OpenAsync(TimeSpan.FromSeconds(1)).ConfigureAwait(false);

            serviceClient.Dispose();
            restOpMock.Verify(restOp => restOp.Dispose(), Times.Once());
            Assert.IsTrue(connectionClosed);
        }
        Tuple <Mock <IHttpClientHelper>, AmqpServiceClient, PurgeMessageQueueResult> SetupPurgeMessageQueueTests()
        {
            // Create expected return object
            var deviceId       = "TestDevice";
            var expectedResult = new PurgeMessageQueueResult()
            {
                DeviceId            = deviceId,
                TotalMessagesPurged = 1
            };

            // Mock IHttpClientHelper to return expected object on DeleteAsync
            var restOpMock = new Mock <IHttpClientHelper>();

            restOpMock.Setup(restOp => restOp.DeleteAsync <PurgeMessageQueueResult>(
                                 It.IsAny <Uri>(), It.IsAny <IDictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > > >(), null, It.IsAny <CancellationToken>())
                             ).ReturnsAsync(expectedResult);

            // Instantiate AmqpServiceClient with Mock IHttpClientHelper
            var authMethod    = new ServiceAuthenticationWithSharedAccessPolicyKey("test", "dGVzdFN0cmluZzE=");
            var builder       = IotHubConnectionStringBuilder.Create("acme.azure-devices.net", authMethod);
            var serviceClient = new AmqpServiceClient(restOpMock.Object);

            return(Tuple.Create(restOpMock, serviceClient, expectedResult));
        }