Skip to content

Commit

Permalink
Merge 39d3c7d into cccb4e2
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Dec 29, 2020
2 parents cccb4e2 + 39d3c7d commit a289fd4
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: CI build
on: [push, pull_request]
jobs:
build:
name: 🔨 Build sources
name: 🔨 Build sources (CI)
runs-on: ubuntu-latest
steps:
- name: 📤 Checkout the repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
deploy:
name: Test image build
name: 👀 Test image build (CI)
runs-on: ubuntu-latest
steps:
- name: 📤 Checkout the repository
Expand Down
11 changes: 10 additions & 1 deletion src/App/NetDaemon.App/Common/INetDaemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ public interface INetDaemon : INetDaemonCommon
/// <param name="state">The state that being set, only primitives are supported</param>
/// <param name="attributes">Attributes, use anonomous types and lowercase letters</param>
void SetState(string entityId, dynamic state, dynamic? attributes = null);

/// <summary>
/// Access the underlying service provider for IOT access to services
/// </summary>
public IServiceProvider? ServiceProvider { get; }
}

/// <summary>
Expand Down Expand Up @@ -417,6 +422,11 @@ public interface INetDaemonAppBase :
/// Returns all entities (EntityId) that are currently registered
/// </summary>
public IEnumerable<string> EntityIds { get; }

/// <summary>
/// Returns the underlying ServiceProvider to use IOC instancing
/// </summary>
public IServiceProvider? ServiceProvider { get; }
}

/// <summary>
Expand Down Expand Up @@ -662,7 +672,6 @@ public interface INetDaemonInitialableApp
/// <summary>
/// Restores the app state
/// </summary>
/// <returns></returns>
/// <remarks>
/// <para>
/// Restores the state of the storage object.!--
Expand Down
3 changes: 3 additions & 0 deletions src/App/NetDaemon.App/Common/NetDaemonAppBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ public async Task RestoreAppStateAsync()
/// </summary>
protected INetDaemon? Daemon { get; set; }

/// <inheritdoc/>
public IServiceProvider? ServiceProvider => Daemon?.ServiceProvider;

/// <inheritdoc/>
public void SaveAppState()
{
Expand Down
6 changes: 5 additions & 1 deletion src/Daemon/NetDaemon.Daemon/Daemon/NetDaemonHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public class NetDaemonHost : INetDaemonHost, IAsyncDisposable

private CancellationTokenSource? _cancelTokenSource;

public IServiceProvider? ServiceProvider { get; }

/// <summary>
/// Constructor
/// </summary>
Expand All @@ -98,7 +100,8 @@ public class NetDaemonHost : INetDaemonHost, IAsyncDisposable
IHassClient? hassClient,
IDataRepository? repository,
ILoggerFactory? loggerFactory = null,
IHttpHandler? httpHandler = null
IHttpHandler? httpHandler = null,
IServiceProvider? serviceProvider = null
)
{
loggerFactory ??= DefaultLoggerFactory;
Expand All @@ -107,6 +110,7 @@ public class NetDaemonHost : INetDaemonHost, IAsyncDisposable
_hassClient = hassClient
?? throw new ArgumentNullException(nameof(hassClient));
_scheduler = new Scheduler(loggerFactory: loggerFactory);
ServiceProvider = serviceProvider;
_repository = repository;
_isDisposed = false;
Logger.LogTrace("Instance NetDaemonHost");
Expand Down
2 changes: 1 addition & 1 deletion src/DevelopmentApps/NetDaemon.DevelopmentApps.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
Expand Down
12 changes: 9 additions & 3 deletions src/Fakes/NetDaemon.Fakes/DaemonHostTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using NetDaemon.Common;
using NetDaemon.Common.Exceptions;
using NetDaemon.Common.Fluent;
using NetDaemon.Common.Reactive;
using NetDaemon.Daemon.Storage;
using Xunit;

Expand All @@ -31,13 +30,14 @@ public DaemonHostTestBase()
LoggerMock = new LoggerMock();
DefaultHassClientMock = HassClientMock.DefaultMock;
DefaultDataRepositoryMock = new Mock<IDataRepository>();

DefaultHttpHandlerMock = new HttpHandlerMock();
DefaultDaemonHost = new NetDaemonHost(
DefaultHassClientMock.Object,
DefaultDataRepositoryMock.Object,
LoggerMock.LoggerFactory,
DefaultHttpHandlerMock.Object)
DefaultHttpHandlerMock.Object,
DefaultServiceProviderMock.Object
)
{
InternalDelayTimeForTts = 0 // Allow no extra waittime
};
Expand All @@ -59,6 +59,12 @@ public DaemonHostTestBase()
/// Returns default HttpHandler mock
/// </summary>
public HttpHandlerMock DefaultHttpHandlerMock { get; }

/// <summary>
/// Default mock for the IServiceProvider
/// </summary>
public ServiceProviderMock DefaultServiceProviderMock { get; } = new();

/// <summary>
/// Returns default logger mock
/// </summary>
Expand Down
24 changes: 24 additions & 0 deletions src/Fakes/NetDaemon.Fakes/ServiceProviderMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Moq;
using System;
using System.Collections.Generic;

namespace NetDaemon.Daemon.Fakes
{
/// <summary>
/// Mock of IServiceProvider to test your own service
/// </summary>
public class ServiceProviderMock : Mock<IServiceProvider>
{
/// <summary>
/// Fake services returned by the GetServiceMethod
/// </summary>
public IDictionary<Type, object?> Services { get; } = new Dictionary<Type, object?>();
/// <summary>
/// Default constructor
/// </summary>
public ServiceProviderMock()
{
Setup(x => x.GetService(It.IsAny<Type>())).Returns<Type>(t => Services.TryGetValue(t, out var obj) ? obj : null);
}
}
}
21 changes: 21 additions & 0 deletions tests/NetDaemon.Daemon.Tests/Daemon/NetDaemonHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,27 @@ public async Task ClearShouldReturnNullGetApp()
Assert.Null(theApp);
}

private interface ITestGetService
{
string TestString { get; }
}
private class TestGetService : ITestGetService
{
public string TestString => "Test";
}

[Fact]
public void ServiceProviderShouldReturnCorrectService()
{
// ARRANGE
DefaultServiceProviderMock.Services[typeof(ITestGetService)] = new TestGetService();
// ACT
var service = DefaultDaemonHost.ServiceProvider?.GetService(typeof(ITestGetService)) as TestGetService;
// ASSERT
Assert.NotNull(service);
Assert.Equal("Test", service?.TestString);
}

[Fact]
public void EntityShouldReturnCorrectValueForArea()
{
Expand Down
29 changes: 23 additions & 6 deletions tests/NetDaemon.Daemon.Tests/Reactive/RxAppTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ namespace NetDaemon.Daemon.Tests.Reactive
/// </remarks>
public class RxAppTest : CoreDaemonHostTestBase
{
public RxAppTest() : base()
{
}

[Fact]
public async Task CallServiceShouldCallCorrectFunction()
Expand Down Expand Up @@ -90,8 +87,7 @@ public async Task NewEventMissingDataAttributeShouldReturnNull()
DefaultDaemonRxApp.EventChanges
.Subscribe(s => missingAttribute = s.Data?.missing_data);

var expandoObj = new ExpandoObject();
dynamic dynExpObject = expandoObj;
dynamic dynExpObject = new ExpandoObject();
dynExpObject.a_parameter = "hello";

DefaultHassClientMock.AddCustomEvent("AN_EVENT", dynExpObject);
Expand Down Expand Up @@ -401,7 +397,28 @@ public async Task GetDataShouldReturnCachedValue()

// ASSERT
DefaultDataRepositoryMock.Verify(n => n.Get<string>(It.IsAny<string>()), Times.Never);
DefaultDataRepositoryMock.Verify(n => n.Save<string>(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
DefaultDataRepositoryMock.Verify(n => n.Save(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}

private interface ITestGetService
{
string TestString { get; }
}
private class TestGetService : ITestGetService
{
public string TestString => "Test";
}

[Fact]
public void ServiceProviderShouldReturnCorrectService()
{
// ARRANGE
DefaultServiceProviderMock.Services[typeof(ITestGetService)] = new TestGetService();
// ACT
var service = DefaultDaemonRxApp.ServiceProvider?.GetService(typeof(ITestGetService)) as TestGetService;
// ASSERT
Assert.NotNull(service);
Assert.Equal("Test", service?.TestString);
}
}
}

0 comments on commit a289fd4

Please sign in to comment.