1
- using System ;
2
1
using System . Threading . Tasks ;
2
+ using Microsoft . Extensions . DependencyInjection ;
3
3
using Microsoft . Extensions . Logging ;
4
4
using Moq ;
5
5
using WorkflowCore . Interface ;
6
+ using WorkflowCore . Models ;
6
7
using WorkflowCore . Models . LifeCycleEvents ;
7
8
using WorkflowCore . Services ;
8
9
using Xunit ;
@@ -17,17 +18,25 @@ public async Task PublishNotification_WhenStarted_PublishesNotification()
17
18
// Arrange
18
19
var wasCalled = new TaskCompletionSource < bool > ( ) ;
19
20
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
+
20
28
eventHubMock
21
29
. Setup ( hub => hub . PublishNotification ( It . IsAny < StepCompleted > ( ) ) )
22
30
. Callback ( ( ) => wasCalled . SetResult ( true ) ) ;
23
- LifeCycleEventPublisher publisher = new LifeCycleEventPublisher ( eventHubMock . Object , new LoggerFactory ( ) ) ;
31
+ LifeCycleEventPublisher publisher = new LifeCycleEventPublisher ( eventHubMock . Object , workflowOptions , new LoggerFactory ( ) ) ;
24
32
25
33
// Act
26
34
publisher . Start ( ) ;
27
35
publisher . PublishNotification ( new StepCompleted ( ) ) ;
28
36
29
37
// Assert
30
38
await wasCalled . Task ;
39
+ eventHubMock . Verify ( hub => hub . PublishNotification ( It . IsAny < StepCompleted > ( ) ) , Times . Once ( ) ) ;
31
40
}
32
41
33
42
[ Fact ( DisplayName = "Notifications should be published when the publisher is running" ) ]
@@ -36,10 +45,17 @@ public async Task PublishNotification_WhenRestarted_PublishesNotification()
36
45
// Arrange
37
46
var wasCalled = new TaskCompletionSource < bool > ( ) ;
38
47
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
+
39
55
eventHubMock
40
56
. Setup ( hub => hub . PublishNotification ( It . IsAny < StepCompleted > ( ) ) )
41
57
. Callback ( ( ) => wasCalled . SetResult ( true ) ) ;
42
- LifeCycleEventPublisher publisher = new LifeCycleEventPublisher ( eventHubMock . Object , new LoggerFactory ( ) ) ;
58
+ LifeCycleEventPublisher publisher = new LifeCycleEventPublisher ( eventHubMock . Object , workflowOptions , new LoggerFactory ( ) ) ;
43
59
44
60
// Act
45
61
publisher . Start ( ) ;
@@ -49,6 +65,33 @@ public async Task PublishNotification_WhenRestarted_PublishesNotification()
49
65
50
66
// Assert
51
67
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 ( ) ) ;
52
95
}
53
96
}
54
97
}
0 commit comments