1
1
namespace NetEvolve . HealthChecks . Tests . Unit . Azure . ServiceBus ;
2
2
3
3
using System ;
4
+ using System . Diagnostics ;
5
+ using Microsoft . Extensions . DependencyInjection ;
4
6
using NetEvolve . Extensions . XUnit ;
5
7
using NetEvolve . HealthChecks . Azure . ServiceBus ;
6
8
using NSubstitute ;
@@ -9,14 +11,12 @@ namespace NetEvolve.HealthChecks.Tests.Unit.Azure.ServiceBus;
9
11
[ TestGroup ( $ "{ nameof ( Azure ) } .{ nameof ( ServiceBus ) } ") ]
10
12
public sealed class ClientCreationTests
11
13
{
12
- private const string TestName = "TestName" ;
13
-
14
14
[ Fact ]
15
15
public void GetClient_WhenModeIsConnectionString_ShouldCreateNewClient ( )
16
16
{
17
17
// Arrange
18
18
const string connectionString =
19
- "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=getclient-connectionstring ;SharedAccessKey=testkey" ;
19
+ "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=testname ;SharedAccessKey=testkey" ;
20
20
var options = new ServiceBusQueueOptions
21
21
{
22
22
Mode = ClientCreationMode . ConnectionString ,
@@ -25,7 +25,11 @@ public void GetClient_WhenModeIsConnectionString_ShouldCreateNewClient()
25
25
var serviceProvider = Substitute . For < IServiceProvider > ( ) ;
26
26
27
27
// Act
28
- var client = ClientCreation . GetClient ( TestName , options , serviceProvider ) ;
28
+ var client = ClientCreation . GetClient (
29
+ nameof ( GetClient_WhenModeIsDefaultAzureCredentials_ShouldCreateNewClient ) ,
30
+ options ,
31
+ serviceProvider
32
+ ) ;
29
33
30
34
// Assert
31
35
Assert . NotNull ( client ) ;
@@ -35,7 +39,7 @@ public void GetClient_WhenModeIsConnectionString_ShouldCreateNewClient()
35
39
public void GetClient_WhenModeIsDefaultAzureCredentials_ShouldCreateNewClient ( )
36
40
{
37
41
// Arrange
38
- const string fullyQualifiedNamespace = "getclient-default .servicebus.windows.net" ;
42
+ const string fullyQualifiedNamespace = "test .servicebus.windows.net" ;
39
43
var options = new ServiceBusQueueOptions
40
44
{
41
45
Mode = ClientCreationMode . DefaultAzureCredentials ,
@@ -44,7 +48,11 @@ public void GetClient_WhenModeIsDefaultAzureCredentials_ShouldCreateNewClient()
44
48
var serviceProvider = Substitute . For < IServiceProvider > ( ) ;
45
49
46
50
// Act
47
- var client = ClientCreation . GetClient ( TestName , options , serviceProvider ) ;
51
+ var client = ClientCreation . GetClient (
52
+ nameof ( GetClient_WhenModeIsDefaultAzureCredentials_ShouldCreateNewClient ) ,
53
+ options ,
54
+ serviceProvider
55
+ ) ;
48
56
49
57
// Assert
50
58
Assert . NotNull ( client ) ;
@@ -55,7 +63,7 @@ public void GetClient_CalledTwiceWithSameName_ShouldReturnSameInstance()
55
63
{
56
64
// Arrange
57
65
const string connectionString =
58
- "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=getclient-samename ;SharedAccessKey=testkey" ;
66
+ "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=testname ;SharedAccessKey=testkey" ;
59
67
var options = new ServiceBusQueueOptions
60
68
{
61
69
Mode = ClientCreationMode . ConnectionString ,
@@ -64,8 +72,16 @@ public void GetClient_CalledTwiceWithSameName_ShouldReturnSameInstance()
64
72
var serviceProvider = Substitute . For < IServiceProvider > ( ) ;
65
73
66
74
// Act
67
- var client1 = ClientCreation . GetClient ( TestName , options , serviceProvider ) ;
68
- var client2 = ClientCreation . GetClient ( TestName , options , serviceProvider ) ;
75
+ var client1 = ClientCreation . GetClient (
76
+ nameof ( GetClient_CalledTwiceWithSameName_ShouldReturnSameInstance ) ,
77
+ options ,
78
+ serviceProvider
79
+ ) ;
80
+ var client2 = ClientCreation . GetClient (
81
+ nameof ( GetClient_CalledTwiceWithSameName_ShouldReturnSameInstance ) ,
82
+ options ,
83
+ serviceProvider
84
+ ) ;
69
85
70
86
// Assert
71
87
Assert . Same ( client1 , client2 ) ;
@@ -76,7 +92,7 @@ public void GetAdministrationClient_WhenModeIsConnectionString_ShouldCreateNewCl
76
92
{
77
93
// Arrange
78
94
const string connectionString =
79
- "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=getadministrationclient-connectionstring ;SharedAccessKey=testkey" ;
95
+ "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=testname ;SharedAccessKey=testkey" ;
80
96
var options = new ServiceBusQueueOptions
81
97
{
82
98
Mode = ClientCreationMode . ConnectionString ,
@@ -85,7 +101,11 @@ public void GetAdministrationClient_WhenModeIsConnectionString_ShouldCreateNewCl
85
101
var serviceProvider = Substitute . For < IServiceProvider > ( ) ;
86
102
87
103
// Act
88
- var client = ClientCreation . GetAdministrationClient ( TestName , options , serviceProvider ) ;
104
+ var client = ClientCreation . GetAdministrationClient (
105
+ nameof ( GetAdministrationClient_WhenModeIsConnectionString_ShouldCreateNewClient ) ,
106
+ options ,
107
+ serviceProvider
108
+ ) ;
89
109
90
110
// Assert
91
111
Assert . NotNull ( client ) ;
@@ -95,7 +115,7 @@ public void GetAdministrationClient_WhenModeIsConnectionString_ShouldCreateNewCl
95
115
public void GetAdministrationClient_WhenModeIsDefaultAzureCredentials_ShouldCreateNewClient ( )
96
116
{
97
117
// Arrange
98
- const string fullyQualifiedNamespace = "getadministrationclient-default .servicebus.windows.net" ;
118
+ const string fullyQualifiedNamespace = "test .servicebus.windows.net" ;
99
119
var options = new ServiceBusQueueOptions
100
120
{
101
121
Mode = ClientCreationMode . DefaultAzureCredentials ,
@@ -104,7 +124,11 @@ public void GetAdministrationClient_WhenModeIsDefaultAzureCredentials_ShouldCrea
104
124
var serviceProvider = Substitute . For < IServiceProvider > ( ) ;
105
125
106
126
// Act
107
- var client = ClientCreation . GetAdministrationClient ( TestName , options , serviceProvider ) ;
127
+ var client = ClientCreation . GetAdministrationClient (
128
+ nameof ( GetAdministrationClient_WhenModeIsDefaultAzureCredentials_ShouldCreateNewClient ) ,
129
+ options ,
130
+ serviceProvider
131
+ ) ;
108
132
109
133
// Assert
110
134
Assert . NotNull ( client ) ;
@@ -115,7 +139,7 @@ public void GetAdministrationClient_CalledTwiceWithSameName_ShouldReturnSameInst
115
139
{
116
140
// Arrange
117
141
const string connectionString =
118
- "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=getadministrationclient-samename ;SharedAccessKey=testkey" ;
142
+ "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=testname ;SharedAccessKey=testkey" ;
119
143
var options = new ServiceBusQueueOptions
120
144
{
121
145
Mode = ClientCreationMode . ConnectionString ,
@@ -124,10 +148,94 @@ public void GetAdministrationClient_CalledTwiceWithSameName_ShouldReturnSameInst
124
148
var serviceProvider = Substitute . For < IServiceProvider > ( ) ;
125
149
126
150
// Act
127
- var client1 = ClientCreation . GetAdministrationClient ( TestName , options , serviceProvider ) ;
128
- var client2 = ClientCreation . GetAdministrationClient ( TestName , options , serviceProvider ) ;
151
+ var client1 = ClientCreation . GetAdministrationClient (
152
+ nameof ( GetAdministrationClient_CalledTwiceWithSameName_ShouldReturnSameInstance ) ,
153
+ options ,
154
+ serviceProvider
155
+ ) ;
156
+ var client2 = ClientCreation . GetAdministrationClient (
157
+ nameof ( GetAdministrationClient_CalledTwiceWithSameName_ShouldReturnSameInstance ) ,
158
+ options ,
159
+ serviceProvider
160
+ ) ;
129
161
130
162
// Assert
131
163
Assert . Same ( client1 , client2 ) ;
132
164
}
165
+
166
+ [ Fact ]
167
+ public void GetClient_WhenModeIsInvalid_ShouldThrowUnreachableException ( )
168
+ {
169
+ // Arrange
170
+ var options = new ServiceBusQueueOptions { Mode = ( ClientCreationMode ) int . MaxValue } ;
171
+ var serviceProvider = Substitute . For < IServiceProvider > ( ) ;
172
+
173
+ // Act
174
+ void Act ( ) =>
175
+ ClientCreation . GetClient (
176
+ nameof ( GetClient_WhenModeIsInvalid_ShouldThrowUnreachableException ) ,
177
+ options ,
178
+ serviceProvider
179
+ ) ;
180
+
181
+ // Assert
182
+ _ = Assert . Throws < UnreachableException > ( Act ) ;
183
+ }
184
+
185
+ [ Fact ]
186
+ public void GetAdministrationClient_WhenModeIsInvalid_ShouldThrowUnreachableException ( )
187
+ {
188
+ // Arrange
189
+ var options = new ServiceBusQueueOptions { Mode = ( ClientCreationMode ) int . MaxValue } ;
190
+ var serviceProvider = Substitute . For < IServiceProvider > ( ) ;
191
+
192
+ // Act
193
+ void Act ( ) =>
194
+ ClientCreation . GetAdministrationClient (
195
+ nameof ( GetAdministrationClient_WhenModeIsInvalid_ShouldThrowUnreachableException ) ,
196
+ options ,
197
+ serviceProvider
198
+ ) ;
199
+
200
+ // Assert
201
+ _ = Assert . Throws < UnreachableException > ( Act ) ;
202
+ }
203
+
204
+ [ Fact ]
205
+ public void GetClient_WhenServiceProviderIsNull_ShouldThrowInvalidOperationException ( )
206
+ {
207
+ // Arrange
208
+ var options = new ServiceBusQueueOptions { Mode = ClientCreationMode . ServiceProvider } ;
209
+ var serviceProvider = new ServiceCollection ( ) . BuildServiceProvider ( ) ;
210
+
211
+ // Act
212
+ void Act ( ) =>
213
+ ClientCreation . GetClient (
214
+ nameof ( GetClient_WhenServiceProviderIsNull_ShouldThrowInvalidOperationException ) ,
215
+ options ,
216
+ serviceProvider
217
+ ) ;
218
+
219
+ // Assert
220
+ _ = Assert . Throws < InvalidOperationException > ( Act ) ;
221
+ }
222
+
223
+ [ Fact ]
224
+ public void GetAdministrationClient_WhenServiceProviderIsNull_ShouldThrowInvalidOperationException ( )
225
+ {
226
+ // Arrange
227
+ var options = new ServiceBusQueueOptions { Mode = ClientCreationMode . ServiceProvider } ;
228
+ var serviceProvider = new ServiceCollection ( ) . BuildServiceProvider ( ) ;
229
+
230
+ // Act
231
+ void Act ( ) =>
232
+ ClientCreation . GetAdministrationClient (
233
+ nameof ( GetAdministrationClient_WhenServiceProviderIsNull_ShouldThrowInvalidOperationException ) ,
234
+ options ,
235
+ serviceProvider
236
+ ) ;
237
+
238
+ // Assert
239
+ _ = Assert . Throws < InvalidOperationException > ( Act ) ;
240
+ }
133
241
}
0 commit comments