Skip to content

Commit

Permalink
refactor(digital-twin-client): Make the DigitalTwin client mockable. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vinagesh authored Apr 19, 2021
1 parent 9e68dcb commit af7b805
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 33 deletions.
67 changes: 37 additions & 30 deletions iothub/service/src/DigitalTwin/DigitalTwinClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public class DigitalTwinClient : IDisposable
private readonly IotHubGatewayServiceAPIs _client;
private readonly DigitalTwin _protocolLayer;

/// <summary>
/// Creates an instance of <see cref="DigitalTwinClient"/>, provided for unit testing purposes only.
/// Use the CreateFromConnectionString method to create an instance to use the client.
/// </summary>
public DigitalTwinClient()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DigitalTwinClient"/> class.</summary>
/// <param name="connectionString">The IoT hub's connection string.</param>
Expand All @@ -34,7 +42,6 @@ public static DigitalTwinClient CreateFromConnectionString(string connectionStri
return new DigitalTwinClient(iotHubConnectionString.HttpsEndpoint, sharedAccessKeyCredential, handlers);
}


private DigitalTwinClient(Uri uri, IotServiceClientCredentials credentials, params DelegatingHandler[] handlers)
{
var httpMessageHandler = HttpClientHelper.CreateDefaultHttpMessageHandler(null, uri, ServicePointHelpers.DefaultConnectionLeaseTimeout);
Expand Down Expand Up @@ -96,7 +103,7 @@ private static HttpMessageHandler CreateHttpHandlerPipeline(HttpMessageHandler h
/// <param name="digitalTwinId">The Id of the digital twin.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The application/json digital twin and the http response.</returns>
public async Task<HttpOperationResponse<T, DigitalTwinGetHeaders>> GetDigitalTwinAsync<T>(string digitalTwinId, CancellationToken cancellationToken = default)
public virtual async Task<HttpOperationResponse<T, DigitalTwinGetHeaders>> GetDigitalTwinAsync<T>(string digitalTwinId, CancellationToken cancellationToken = default)
{
using HttpOperationResponse<string, DigitalTwinGetHeaders> response = await _protocolLayer.GetDigitalTwinWithHttpMessagesAsync(digitalTwinId, null, cancellationToken)
.ConfigureAwait(false);
Expand All @@ -118,10 +125,10 @@ public async Task<HttpOperationResponse<T, DigitalTwinGetHeaders>> GetDigitalTwi
/// <param name="requestOptions">The optional settings for this request.</param>
/// <param name="cancellationToken">The cancellationToken.</param>
/// <returns>The http response.</returns>
public Task<HttpOperationHeaderResponse<DigitalTwinUpdateHeaders>> UpdateDigitalTwinAsync(
string digitalTwinId,
string digitalTwinUpdateOperations,
DigitalTwinUpdateRequestOptions requestOptions = default,
public virtual Task<HttpOperationHeaderResponse<DigitalTwinUpdateHeaders>> UpdateDigitalTwinAsync(
string digitalTwinId,
string digitalTwinUpdateOperations,
DigitalTwinUpdateRequestOptions requestOptions = default,
CancellationToken cancellationToken = default)
{
return _protocolLayer.UpdateDigitalTwinWithHttpMessagesAsync(digitalTwinId, digitalTwinUpdateOperations, requestOptions?.IfMatch, null, cancellationToken);
Expand All @@ -136,20 +143,20 @@ public Task<HttpOperationHeaderResponse<DigitalTwinUpdateHeaders>> UpdateDigital
/// <param name="requestOptions">The optional settings for this request.</param>
/// <param name="cancellationToken">The cancellationToken.</param>
/// <returns>The application/json command invocation response and the http response. </returns>
public async Task<HttpOperationResponse<DigitalTwinCommandResponse, DigitalTwinInvokeCommandHeaders>> InvokeCommandAsync(
string digitalTwinId,
string commandName,
string payload = default,
DigitalTwinInvokeCommandRequestOptions requestOptions = default,
public virtual async Task<HttpOperationResponse<DigitalTwinCommandResponse, DigitalTwinInvokeCommandHeaders>> InvokeCommandAsync(
string digitalTwinId,
string commandName,
string payload = default,
DigitalTwinInvokeCommandRequestOptions requestOptions = default,
CancellationToken cancellationToken = default)
{
using HttpOperationResponse<string, DigitalTwinInvokeRootLevelCommandHeaders> response = await _protocolLayer.InvokeRootLevelCommandWithHttpMessagesAsync(
digitalTwinId,
commandName,
payload,
requestOptions?.ConnectTimeoutInSeconds,
requestOptions?.ResponseTimeoutInSeconds,
null,
digitalTwinId,
commandName,
payload,
requestOptions?.ConnectTimeoutInSeconds,
requestOptions?.ResponseTimeoutInSeconds,
null,
cancellationToken)
.ConfigureAwait(false);
return new HttpOperationResponse<DigitalTwinCommandResponse, DigitalTwinInvokeCommandHeaders>
Expand All @@ -171,22 +178,22 @@ public async Task<HttpOperationResponse<DigitalTwinCommandResponse, DigitalTwinI
/// <param name="requestOptions">The optional settings for this request.</param>
/// <param name="cancellationToken">The cancellationToken.</param>
/// <returns>The application/json command invocation response and the http response. </returns>
public async Task<HttpOperationResponse<DigitalTwinCommandResponse, DigitalTwinInvokeCommandHeaders>> InvokeComponentCommandAsync(
string digitalTwinId,
string componentName,
string commandName,
string payload = default,
DigitalTwinInvokeCommandRequestOptions requestOptions = default,
public virtual async Task<HttpOperationResponse<DigitalTwinCommandResponse, DigitalTwinInvokeCommandHeaders>> InvokeComponentCommandAsync(
string digitalTwinId,
string componentName,
string commandName,
string payload = default,
DigitalTwinInvokeCommandRequestOptions requestOptions = default,
CancellationToken cancellationToken = default)
{
using HttpOperationResponse<string, DigitalTwinInvokeComponentCommandHeaders> response = await _protocolLayer.InvokeComponentCommandWithHttpMessagesAsync(
digitalTwinId,
componentName,
commandName,
payload,
requestOptions?.ConnectTimeoutInSeconds,
requestOptions?.ResponseTimeoutInSeconds,
null,
digitalTwinId,
componentName,
commandName,
payload,
requestOptions?.ConnectTimeoutInSeconds,
requestOptions?.ResponseTimeoutInSeconds,
null,
cancellationToken)
.ConfigureAwait(false);
return new HttpOperationResponse<DigitalTwinCommandResponse, DigitalTwinInvokeCommandHeaders>
Expand Down
3 changes: 2 additions & 1 deletion iothub/service/src/JobClient/JobClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class JobClient : IDisposable
private IHttpClientHelper _httpClientHelper;

/// <summary>
/// Creates an instance of <see cref="JobClient"/>.
/// Creates an instance of <see cref="JobClient"/>, provided for unit testing purposes only.
/// Use the CreateFromConnectionString method to create an instance to use the client.
/// </summary>
public JobClient()
{
Expand Down
3 changes: 2 additions & 1 deletion iothub/service/src/RegistryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public class RegistryManager : IDisposable
private IHttpClientHelper _httpClientHelper;

/// <summary>
/// Creates an instance of <see cref="RegistryManager"/>.
/// Creates an instance of <see cref="RegistryManager"/>, provided for unit testing purposes only.
/// Use the CreateFromConnectionString method to create an instance to use the client.
/// </summary>
public RegistryManager()
{
Expand Down
3 changes: 2 additions & 1 deletion iothub/service/src/ServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public class ServiceClient : IDisposable
internal readonly IotHubConnection Connection;

/// <summary>
/// Creates an instance of <see cref="ServiceClient"/>.
/// Creates an instance of <see cref="ServiceClient"/>, provided for unit testing purposes only.
/// Use the CreateFromConnectionString method to create an instance to use the client.
/// </summary>
public ServiceClient()
{
Expand Down

0 comments on commit af7b805

Please sign in to comment.