public async Task AssertDeviceConnectionOpenAsyncDps()

in DeviceBridgeTests/Services/ConnectionManagerTests.cs [181:219]


        public async Task AssertDeviceConnectionOpenAsyncDps()
        {
            using (ShimsContext.Create())
            {
                var connectionManager = CreateConnectionManager();
                _storageProviderMock.Invocations.Clear();

                // Checks that it attempts to connect to the hub returned by DPS.
                string connStr = null;
                ShimDps("test-hub.azure.devices.net");
                ShimDeviceClientAndCaptureConnectionString(capturedConnStr => connStr = capturedConnStr);
                await connectionManager.AssertDeviceConnectionOpenAsync("dps-test-device-id");
                Assert.True(connStr.Contains("test-hub.azure.devices.net"));

                // Check that the hub returned by DPS was cached in the DB.
                _storageProviderMock.Verify(p => p.AddOrUpdateHubCacheEntry(It.IsAny<Logger>(), "dps-test-device-id", "test-hub.azure.devices.net"), Times.Once);

                // Checks that failure to save hub in DB cache doesn't fail the open operation.
                connectionManager = CreateConnectionManager();
                _storageProviderMock.Setup(p => p.AddOrUpdateHubCacheEntry(It.IsAny<Logger>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new Exception());
                await connectionManager.AssertDeviceConnectionOpenAsync("dps-test-device-id");
                _storageProviderMock.Setup(p => p.AddOrUpdateHubCacheEntry(It.IsAny<Logger>(), It.IsAny<string>(), It.IsAny<string>())).Verifiable();

                // Check that the device client is cached and not reopened in subsequent calls.
                bool openAttempted = false;
                ShimDeviceClientAndCaptureOpen(() => openAttempted = true);
                await connectionManager.AssertDeviceConnectionOpenAsync("dps-test-device-id");
                Assert.False(openAttempted);

                // Operation fails if DPS registration fails.
                connectionManager = CreateConnectionManager();
                ShimDpsToFail();
                await ExpectToThrow(() => connectionManager.AssertDeviceConnectionOpenAsync("dps-test-device-id"));

                // Fails with DpsRegistrationFailedWithUnknownStatusException if DPS returns unknown response.
                ShimDps("test-hub.azure.devices.net", ProvisioningRegistrationStatusType.Failed);
                await ExpectToThrow(() => connectionManager.AssertDeviceConnectionOpenAsync("dps-test-device-id"), e => e is DpsRegistrationFailedWithUnknownStatusException);
            }
        }