1- using System ;
21using System . Threading . Tasks ;
2+ using Microsoft . Extensions . DependencyInjection ;
33using Microsoft . Extensions . Logging ;
44using Moq ;
55using WorkflowCore . Interface ;
6+ using WorkflowCore . Models ;
67using WorkflowCore . Models . LifeCycleEvents ;
78using WorkflowCore . Services ;
89using Xunit ;
@@ -17,17 +18,25 @@ public async Task PublishNotification_WhenStarted_PublishesNotification()
1718 // Arrange
1819 var wasCalled = new TaskCompletionSource < bool > ( ) ;
1920 var eventHubMock = new Mock < ILifeCycleEventHub > ( ) ;
21+ var serviceCollectionMock = new Mock < IServiceCollection > ( ) ;
22+
23+ var workflowOptions = new WorkflowOptions ( serviceCollectionMock . Object )
24+ {
25+ EnableLifeCycleEventsPublisher = true
26+ } ;
27+
2028 eventHubMock
2129 . Setup ( hub => hub . PublishNotification ( It . IsAny < StepCompleted > ( ) ) )
2230 . Callback ( ( ) => wasCalled . SetResult ( true ) ) ;
23- LifeCycleEventPublisher publisher = new LifeCycleEventPublisher ( eventHubMock . Object , new LoggerFactory ( ) ) ;
31+ LifeCycleEventPublisher publisher = new LifeCycleEventPublisher ( eventHubMock . Object , workflowOptions , new LoggerFactory ( ) ) ;
2432
2533 // Act
2634 publisher . Start ( ) ;
2735 publisher . PublishNotification ( new StepCompleted ( ) ) ;
2836
2937 // Assert
3038 await wasCalled . Task ;
39+ eventHubMock . Verify ( hub => hub . PublishNotification ( It . IsAny < StepCompleted > ( ) ) , Times . Once ( ) ) ;
3140 }
3241
3342 [ Fact ( DisplayName = "Notifications should be published when the publisher is running" ) ]
@@ -36,10 +45,17 @@ public async Task PublishNotification_WhenRestarted_PublishesNotification()
3645 // Arrange
3746 var wasCalled = new TaskCompletionSource < bool > ( ) ;
3847 var eventHubMock = new Mock < ILifeCycleEventHub > ( ) ;
48+ var serviceCollectionMock = new Mock < IServiceCollection > ( ) ;
49+
50+ var workflowOptions = new WorkflowOptions ( serviceCollectionMock . Object )
51+ {
52+ EnableLifeCycleEventsPublisher = true
53+ } ;
54+
3955 eventHubMock
4056 . Setup ( hub => hub . PublishNotification ( It . IsAny < StepCompleted > ( ) ) )
4157 . Callback ( ( ) => wasCalled . SetResult ( true ) ) ;
42- LifeCycleEventPublisher publisher = new LifeCycleEventPublisher ( eventHubMock . Object , new LoggerFactory ( ) ) ;
58+ LifeCycleEventPublisher publisher = new LifeCycleEventPublisher ( eventHubMock . Object , workflowOptions , new LoggerFactory ( ) ) ;
4359
4460 // Act
4561 publisher . Start ( ) ;
@@ -49,6 +65,33 @@ public async Task PublishNotification_WhenRestarted_PublishesNotification()
4965
5066 // Assert
5167 await wasCalled . Task ;
68+ eventHubMock . Verify ( hub => hub . PublishNotification ( It . IsAny < StepCompleted > ( ) ) , Times . Once ( ) ) ;
69+ }
70+
71+ [ Fact ( DisplayName = "Notifications should be disabled if option EnableLifeCycleEventsPublisher is disabled" ) ]
72+ public void PublishNotification_Disabled ( )
73+ {
74+ // Arrange
75+ var eventHubMock = new Mock < ILifeCycleEventHub > ( ) ;
76+ var serviceCollectionMock = new Mock < IServiceCollection > ( ) ;
77+
78+ var workflowOptions = new WorkflowOptions ( serviceCollectionMock . Object )
79+ {
80+ EnableLifeCycleEventsPublisher = false
81+ } ;
82+
83+ eventHubMock
84+ . Setup ( hub => hub . PublishNotification ( It . IsAny < StepCompleted > ( ) ) )
85+ . Returns ( Task . CompletedTask ) ;
86+ LifeCycleEventPublisher publisher = new LifeCycleEventPublisher ( eventHubMock . Object , workflowOptions , new LoggerFactory ( ) ) ;
87+
88+ // Act
89+ publisher . Start ( ) ;
90+ publisher . PublishNotification ( new StepCompleted ( ) ) ;
91+ publisher . Stop ( ) ;
92+
93+ // Assert
94+ eventHubMock . Verify ( hub => hub . PublishNotification ( It . IsAny < StepCompleted > ( ) ) , Times . Never ( ) ) ;
5295 }
5396 }
5497}
0 commit comments