Skip to content

Commit

Permalink
Merge fb58559 into 8e4b437
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Dec 13, 2020
2 parents 8e4b437 + fb58559 commit a673a5a
Show file tree
Hide file tree
Showing 24 changed files with 1,362 additions and 1,021 deletions.
224 changes: 171 additions & 53 deletions src/Fakes/NetDaemon.Fakes/DaemonHostTestBase.cs

Large diffs are not rendered by default.

87 changes: 80 additions & 7 deletions src/Fakes/NetDaemon.Fakes/HassClientMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,31 @@ namespace NetDaemon.Daemon.Fakes
/// </summary>
public class HassClientMock : Mock<IHassClient>
{
internal HassAreas Areas = new HassAreas();
internal HassDevices Devices = new HassDevices();
internal HassEntities Entities = new HassEntities();
internal ConcurrentQueue<HassEvent> FakeEvents = new();
internal ConcurrentDictionary<string, HassState> FakeStates = new();
/// <summary>
/// Fake areas in HassClient
/// </summary>
/// <returns></returns>
public HassAreas Areas = new HassAreas();

/// <summary>
/// Fake devices in HassClient
/// </summary>
/// <returns></returns>
public HassDevices Devices = new HassDevices();

/// <summary>
/// Fake entities in HassClient
/// </summary>
public HassEntities Entities = new HassEntities();

/// <summary>
/// Fake events in HassClient
/// </summary>
public ConcurrentQueue<HassEvent> FakeEvents = new();
/// <summary>
/// All current fake entities and it's states
/// </summary>
public ConcurrentDictionary<string, HassState> FakeStates = new();

/// <summary>
/// Default constructor
Expand Down Expand Up @@ -104,7 +124,7 @@ public static HassClientMock MockConnectFalse
/// <param name="domain">Domain of service</param>
/// <param name="service">Service to fake</param>
/// <param name="data">Data sent by service</param>
public void AddCallServiceEvent(string domain, string service, dynamic data)
public void AddCallServiceEvent(string domain, string service, dynamic? data = null)
{
// Todo: Refactor to smth smarter
FakeEvents.Enqueue(new HassEvent
Expand Down Expand Up @@ -196,6 +216,25 @@ public void AddChangedEvent(string entityId, object fromState, object toState)
});
}

/// <summary>
/// Adds a changed event for entity
/// </summary>
/// <param name="fromState">The from state advanced details</param>
/// <param name="toState">The to state advanced details</param>
public void AddChangeEventFull(HassState? fromState, HassState? toState)
{
FakeEvents.Enqueue(new HassEvent
{
EventType = "state_changed",
Data = new HassStateChangedEventData
{
EntityId = toState?.EntityId ?? fromState?.EntityId ?? "",
NewState = toState,
OldState = fromState
},
});
}

/// <summary>
/// Adds a custom event
/// </summary>
Expand Down Expand Up @@ -254,7 +293,7 @@ public CancellationTokenSource GetSourceWithTimeout(int milliSeconds = 100)
/// <param name="domain">Service domain</param>
/// <param name="service">Service to verify</param>
/// <param name="attributesTuples">Attributes sent by service</param>
public void VerifyCallService(string domain, string service,
public void VerifyCallServiceTuple(string domain, string service,
params (string attribute, object value)[] attributesTuples)
{
var attributes = new FluentExpandoObject();
Expand All @@ -264,6 +303,38 @@ public CancellationTokenSource GetSourceWithTimeout(int milliSeconds = 100)
Verify(n => n.CallService(domain, service, attributes, It.IsAny<bool>()), Times.AtLeastOnce);
}

/// <summary>
/// Verifies that call_service is called
/// </summary>
/// <param name="domain">Service domain</param>
/// <param name="service">Service to verify</param>
/// <param name="data">Data sent by service</param>
/// <param name="waitForResponse">If service was waiting for response</param>
/// <param name="times">Number of times called</param>
public void VerifyCallService(string domain, string service, object? data = null, bool waitForResponse = false, Moq.Times? times = null)
{
if (times is not null)
Verify(n => n.CallService(domain, service, data!, waitForResponse), times.Value);
else
Verify(n => n.CallService(domain, service, data!, waitForResponse), Times.AtLeastOnce);

}

/// <summary>
/// Verifies that call_service is called
/// </summary>
/// <param name="domain">Service domain</param>
/// <param name="service">Service to verify</param>
/// <param name="waitForResponse">If service was waiting for response</param>
/// <param name="times">Number of times called</param>
public void VerifyCallService(string domain, string service, bool waitForResponse = false, Moq.Times? times = null)
{
if (times is not null)
Verify(n => n.CallService(domain, service, It.IsAny<object>(), waitForResponse), times.Value);
else
Verify(n => n.CallService(domain, service, It.IsAny<object>(), waitForResponse), Times.AtLeastOnce);
}

/// <summary>
/// Verifies service being sent "times" times
/// </summary>
Expand All @@ -274,6 +345,8 @@ public void VerifyCallServiceTimes(string service, Times times)
Verify(n => n.CallService(It.IsAny<string>(), service, It.IsAny<FluentExpandoObject>(), It.IsAny<bool>()), times);
}



/// <summary>
/// Verify state if entity
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions tests/NetDaemon.Daemon.Tests/Daemon/DataRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using NetDaemon.Common.Fluent;
using NetDaemon.Daemon.Storage;
using Xunit;
using NetDaemon.Daemon.Fakes;

namespace NetDaemon.Daemon.Tests.Daemon
{
Expand Down
120 changes: 60 additions & 60 deletions tests/NetDaemon.Daemon.Tests/Daemon/HttpHandlerMock.cs
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
using Moq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NetDaemon.Common;
// using Moq;
// using System.Net;
// using System.Net.Http;
// using System.Text;
// using System.Threading;
// using System.Threading.Tasks;
// using NetDaemon.Common;

namespace NetDaemon.Daemon.Tests.Daemon
{
public class HttpClientFactoryMock : Mock<IHttpClientFactory>
{
private HttpClient? _httpClient;
private MockHttpMessageHandler? _handler;
public MockHttpMessageHandler? MessageHandler => _handler;
// namespace NetDaemon.Daemon.Tests.Daemon
// {
// public class HttpClientFactoryMock : Mock<IHttpClientFactory>
// {
// private HttpClient? _httpClient;
// private MockHttpMessageHandler? _handler;
// public MockHttpMessageHandler? MessageHandler => _handler;

public HttpClientFactoryMock()
{
}
// public HttpClientFactoryMock()
// {
// }

public void SetResponse(string response, HttpStatusCode statusCode = HttpStatusCode.OK)
{
_handler = new MockHttpMessageHandler(response, statusCode);
_httpClient = new HttpClient(_handler);
Setup(x => x.CreateClient(It.IsAny<string>())).Returns(_httpClient!);
}
}
// public void SetResponse(string response, HttpStatusCode statusCode = HttpStatusCode.OK)
// {
// _handler = new MockHttpMessageHandler(response, statusCode);
// _httpClient = new HttpClient(_handler);
// Setup(x => x.CreateClient(It.IsAny<string>())).Returns(_httpClient!);
// }
// }

public class HttpHandlerMock : Mock<IHttpHandler>
{
private HttpClient? _httpClient;
private MockHttpMessageHandler? _handler;
// public class HttpHandlerMock : Mock<IHttpHandler>
// {
// private HttpClient? _httpClient;
// private MockHttpMessageHandler? _handler;

public MockHttpMessageHandler? MessageHandler => _handler;
// public MockHttpMessageHandler? MessageHandler => _handler;

public HttpHandlerMock()
{
}
// public HttpHandlerMock()
// {
// }

public void SetResponse(string response, HttpStatusCode statusCode = HttpStatusCode.OK)
{
_handler = new MockHttpMessageHandler(response, statusCode);
_httpClient = new HttpClient(_handler);
Setup(x => x.CreateHttpClient(It.IsAny<string>())).Returns(_httpClient!);
}
}
// public void SetResponse(string response, HttpStatusCode statusCode = HttpStatusCode.OK)
// {
// _handler = new MockHttpMessageHandler(response, statusCode);
// _httpClient = new HttpClient(_handler);
// Setup(x => x.CreateHttpClient(It.IsAny<string>())).Returns(_httpClient!);
// }
// }

public class MockHttpMessageHandler : HttpMessageHandler
{
private readonly string _response;
private readonly HttpStatusCode _StatusCode;
// public class MockHttpMessageHandler : HttpMessageHandler
// {
// private readonly string _response;
// private readonly HttpStatusCode _StatusCode;

private string? _requestContent;
// private string? _requestContent;

public string? RequestContent => _requestContent;
// public string? RequestContent => _requestContent;

public MockHttpMessageHandler(string response, HttpStatusCode statusCode = HttpStatusCode.OK)
{
_response = response;
_StatusCode = statusCode;
}
// public MockHttpMessageHandler(string response, HttpStatusCode statusCode = HttpStatusCode.OK)
// {
// _response = response;
// _StatusCode = statusCode;
// }

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var responseMessage = new HttpResponseMessage(_StatusCode);
// protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
// {
// var responseMessage = new HttpResponseMessage(_StatusCode);

if (request is not null && request.Content is not null)
_requestContent = await request.Content.ReadAsStringAsync().ConfigureAwait(false);
// if (request is not null && request.Content is not null)
// _requestContent = await request.Content.ReadAsStringAsync().ConfigureAwait(false);

responseMessage.Content = new ByteArrayContent(Encoding.ASCII.GetBytes(_response));
return responseMessage;
}
}
}
// responseMessage.Content = new ByteArrayContent(Encoding.ASCII.GetBytes(_response));
// return responseMessage;
// }
// }
// }
1 change: 1 addition & 0 deletions tests/NetDaemon.Daemon.Tests/Daemon/HttpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using NetDaemon.Daemon.Fakes;
using Xunit;

namespace NetDaemon.Daemon.Tests.Daemon
Expand Down
Loading

0 comments on commit a673a5a

Please sign in to comment.