Skip to content

Commit

Permalink
Added tests for unknown entity state trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
leosperry committed Aug 10, 2024
1 parent 8ef5014 commit 3b0ffdb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,83 @@ public async Task WhenPostStartupOrPreStartupPostCacheSpecified_AndEventPreStart
await Task.Delay(DELAY); //sometimes the verification can run before the task is scheduled

// Then
auto.Verify(a => a.Execute(It.IsAny<HaEntityStateChange>(), default), Times.Never);
auto.Verify(a => a.Execute(It.IsAny<HaEntityStateChange>(), default), Times.Never);
}

[Fact]
public async Task WhenEntityBadState_ShouldNotRun()
{
// arrange
Mock<IDistributedCache> cache= new();
cache.Setup(c => c.GetAsync(It.IsAny<string>(), default)).ReturnsAsync(default(byte[]?));

Mock<IAutomationWrapper> auto = new();
auto.Setup(a => a.TriggerEntityIds()).Returns(["enterprise"]);
auto.Setup(a => a.EventTimings).Returns(EventTiming.PostStartup);
auto.Setup(a => a.GetMetaData()).Returns(new AutomationMetaData(){Name=""});

Mock<IInternalRegistrar> registrar = new();
registrar.Setup(r => r.Registered).Returns([auto.Object]);

AutomationManager collector = new(
Enumerable.Empty<IAutomationRegistry>(), registrar.Object);

Mock<IMessageContext> fakeContext = new();
Mock<IConsumerContext> consumerContext = new();
fakeContext.Setup(c => c.ConsumerContext).Returns(consumerContext.Object);

Mock<ISystemObserver> observer = new();
Mock<ILogger<HaStateHandler>> logger = new();

HaEntityState fakeState = TestHelpers.GetState(lastUpdated: DateTime.Now.AddDays(1), state:"unknown");

// act
HaStateHandler sut = new HaStateHandler(cache.Object, collector, observer.Object, logger.Object);

await sut.Handle(fakeContext.Object, fakeState);
await Task.Delay(DELAY); //sometimes the verification can run before the task is scheduled

// assert
auto.Verify(a => a.Execute(It.IsAny<HaEntityStateChange>(), default), Times.Never);
}

[Fact]
public async Task WhenEntityBadState_andMetaTriggerBad_ShouldRun()
{
// arrange
Mock<IDistributedCache> cache= new();
cache.Setup(c => c.GetAsync(It.IsAny<string>(), default)).ReturnsAsync(default(byte[]?));

Mock<IAutomationWrapper> auto = new();
auto.Setup(a => a.TriggerEntityIds()).Returns(["enterprise"]);
auto.Setup(a => a.EventTimings).Returns(EventTiming.PostStartup);
auto.Setup(a => a.GetMetaData()).Returns(new AutomationMetaData(){
Name="", TriggerOnBadState = true
});

Mock<IInternalRegistrar> registrar = new();
registrar.Setup(r => r.Registered).Returns([auto.Object]);

AutomationManager collector = new(
Enumerable.Empty<IAutomationRegistry>(), registrar.Object);

Mock<IMessageContext> fakeContext = new();
Mock<IConsumerContext> consumerContext = new();
fakeContext.Setup(c => c.ConsumerContext).Returns(consumerContext.Object);

Mock<ISystemObserver> observer = new();
Mock<ILogger<HaStateHandler>> logger = new();

HaEntityState fakeState = TestHelpers.GetState(lastUpdated: DateTime.Now.AddDays(1), state:"unknown");

// act
HaStateHandler sut = new HaStateHandler(cache.Object, collector, observer.Object, logger.Object);

await sut.Handle(fakeContext.Object, fakeState);
await Task.Delay(DELAY); //sometimes the verification can run before the task is scheduled

// assert
auto.Verify(a => a.Execute(It.IsAny<HaEntityStateChange>(), default), Times.Once);
}

byte[]? getBytes<T>(T o)
Expand Down
2 changes: 1 addition & 1 deletion src/HaKafkaNet/Implementations/Core/TraceLogProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public async Task Trace(TraceEvent evt, AutomationMetaData meta, Func<Task> trac
task.Wait();
await task;
}
catch (System.Exception ex)
catch (Exception ex)
{
act?.AddEvent(new ActivityEvent(automation_fault));
_logger.LogError(ex, automation_fault);
Expand Down

0 comments on commit 3b0ffdb

Please sign in to comment.