1
+ using Microsoft . Extensions . Configuration ;
2
+ using Microsoft . Extensions . Logging ;
1
3
using Moq ;
2
4
using Moq . Protected ;
3
5
using NUnit . Framework ;
8
10
using System . Net . Http ;
9
11
using System . Threading ;
10
12
using System . Threading . Tasks ;
11
- using Umbraco . Core . Cache ;
12
- using Umbraco . Core . Logging ;
13
- using Umbraco . Core . Services ;
13
+ using Umbraco . Cms . Core . Cache ;
14
+ using Umbraco . Cms . Core . Services ;
14
15
using Umbraco . Forms . Core ;
15
16
using Umbraco . Forms . Core . Persistence . Dtos ;
16
- using Umbraco . Forms . Core . Providers . Models ;
17
17
using Umbraco . Forms . Integrations . Crm . Hubspot . Models ;
18
18
using Umbraco . Forms . Integrations . Crm . Hubspot . Services ;
19
19
@@ -102,46 +102,59 @@ public class HubspotContactServiceTests
102
102
[ Test ]
103
103
public async Task GetContactProperties_WithoutApiKeyConfigured_ReturnsEmptyCollectionWithLoggedWarning ( )
104
104
{
105
- Mock < IFacadeConfiguration > mockedConfig = CreateMockedConfiguration ( withApiKey : false ) ;
106
- var mockedLogger = new Mock < ILogger > ( ) ;
107
- var mockedAppCaches = new Mock < AppCaches > ( ) ;
105
+ Mock < IConfiguration > mockedConfig = CreateMockedConfiguration ( withApiKey : false ) ;
106
+ var mockedLogger = new Mock < ILogger < HubspotContactService > > ( ) ;
108
107
var mockedKeyValueService = new Mock < IKeyValueService > ( ) ;
109
- var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , mockedAppCaches . Object , mockedKeyValueService . Object ) ;
108
+ var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
110
109
111
110
var result = await sut . GetContactPropertiesAsync ( ) ;
112
111
113
- mockedLogger
114
- . Verify ( x => x . Info ( It . Is < Type > ( y => y == typeof ( HubspotContactService ) ) , It . IsAny < string > ( ) ) , Times . Once ) ;
112
+ mockedLogger . Verify (
113
+ m => m . Log (
114
+ LogLevel . Information ,
115
+ It . IsAny < EventId > ( ) ,
116
+ It . IsAny < It . IsAnyType > ( ) ,
117
+ It . IsAny < Exception > ( ) ,
118
+ It . IsAny < Func < It . IsAnyType , Exception ? , string > > ( ) ) ,
119
+ Times . Once ,
120
+ It . IsAny < string > ( ) ) ;
121
+
115
122
Assert . IsEmpty ( result ) ;
116
123
}
117
124
118
125
[ Test ]
119
126
public async Task GetContactProperties_WithFailedRequest_ReturnsEmptyCollectionWithLoggedError ( )
120
127
{
121
- Mock < IFacadeConfiguration > mockedConfig = CreateMockedConfiguration ( ) ;
122
- var mockedLogger = new Mock < ILogger > ( ) ;
123
- var mockedAppCaches = new Mock < AppCaches > ( ) ;
128
+ Mock < IConfiguration > mockedConfig = CreateMockedConfiguration ( ) ;
129
+ var mockedLogger = new Mock < ILogger < HubspotContactService > > ( ) ;
124
130
var mockedKeyValueService = new Mock < IKeyValueService > ( ) ;
125
- var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , mockedAppCaches . Object , mockedKeyValueService . Object ) ;
131
+ var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
126
132
127
133
var httpClient = CreateMockedHttpClient ( HttpStatusCode . InternalServerError ) ;
128
134
HubspotContactService . ClientFactory = ( ) => httpClient ;
129
135
130
136
var result = await sut . GetContactPropertiesAsync ( ) ;
131
137
132
- mockedLogger
133
- . Verify ( x => x . Error ( It . Is < Type > ( y => y == typeof ( HubspotContactService ) ) , It . IsAny < string > ( ) , It . IsAny < object [ ] > ( ) ) , Times . Once ) ;
138
+ mockedLogger . Verify (
139
+ m => m . Log (
140
+ LogLevel . Error ,
141
+ It . IsAny < EventId > ( ) ,
142
+ It . IsAny < It . IsAnyType > ( ) ,
143
+ It . IsAny < Exception > ( ) ,
144
+ It . IsAny < Func < It . IsAnyType , Exception ? , string > > ( ) ) ,
145
+ Times . Once ,
146
+ It . IsAny < string > ( ) ) ;
147
+
134
148
Assert . IsEmpty ( result ) ;
135
149
}
136
150
137
151
[ Test ]
138
152
public async Task GetContactProperties_WithSuccessfulRequest_ReturnsMappedAndOrderedPropertyCollection ( )
139
153
{
140
- Mock < IFacadeConfiguration > mockedConfig = CreateMockedConfiguration ( ) ;
141
- var mockedLogger = new Mock < ILogger > ( ) ;
142
- var mockedAppCaches = new Mock < AppCaches > ( ) ;
154
+ Mock < IConfiguration > mockedConfig = CreateMockedConfiguration ( ) ;
155
+ var mockedLogger = new Mock < ILogger < HubspotContactService > > ( ) ;
143
156
var mockedKeyValueService = new Mock < IKeyValueService > ( ) ;
144
- var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , mockedAppCaches . Object , mockedKeyValueService . Object ) ;
157
+ var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
145
158
146
159
var httpClient = CreateMockedHttpClient ( HttpStatusCode . OK , s_contactPropertiesResponse ) ;
147
160
HubspotContactService . ClientFactory = ( ) => httpClient ;
@@ -156,29 +169,35 @@ public async Task GetContactProperties_WithSuccessfulRequest_ReturnsMappedAndOrd
156
169
[ Test ]
157
170
public async Task PostContact_WithoutApiKeyConfigured_ReturnsNotConfiguredWithLoggedWarning ( )
158
171
{
159
- Mock < IFacadeConfiguration > mockedConfig = CreateMockedConfiguration ( withApiKey : false ) ;
160
- var mockedLogger = new Mock < ILogger > ( ) ;
161
- var mockedAppCaches = new Mock < AppCaches > ( ) ;
172
+ Mock < IConfiguration > mockedConfig = CreateMockedConfiguration ( withApiKey : false ) ;
173
+ var mockedLogger = new Mock < ILogger < HubspotContactService > > ( ) ;
162
174
var mockedKeyValueService = new Mock < IKeyValueService > ( ) ;
163
- var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , mockedAppCaches . Object , mockedKeyValueService . Object ) ;
175
+ var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
164
176
165
177
var record = new Record ( ) ;
166
178
var fieldMappings = new List < MappedProperty > ( ) ;
167
179
var result = await sut . PostContactAsync ( record , fieldMappings ) ;
168
180
169
- mockedLogger
170
- . Verify ( x => x . Warn ( It . Is < Type > ( y => y == typeof ( HubspotContactService ) ) , It . IsAny < string > ( ) ) , Times . Once ) ;
181
+ mockedLogger . Verify (
182
+ m => m . Log (
183
+ LogLevel . Warning ,
184
+ It . IsAny < EventId > ( ) ,
185
+ It . IsAny < It . IsAnyType > ( ) ,
186
+ It . IsAny < Exception > ( ) ,
187
+ It . IsAny < Func < It . IsAnyType , Exception ? , string > > ( ) ) ,
188
+ Times . Once ,
189
+ It . IsAny < string > ( ) ) ;
190
+
171
191
Assert . AreEqual ( CommandResult . NotConfigured , result ) ;
172
192
}
173
193
174
194
[ Test ]
175
195
public async Task PostContact_WithFailedRequest_ReturnsFailedWithLoggedError ( )
176
196
{
177
- Mock < IFacadeConfiguration > mockedConfig = CreateMockedConfiguration ( ) ;
178
- var mockedLogger = new Mock < ILogger > ( ) ;
179
- var mockedAppCaches = new Mock < AppCaches > ( ) ;
197
+ Mock < IConfiguration > mockedConfig = CreateMockedConfiguration ( ) ;
198
+ var mockedLogger = new Mock < ILogger < HubspotContactService > > ( ) ;
180
199
var mockedKeyValueService = new Mock < IKeyValueService > ( ) ;
181
- var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , mockedAppCaches . Object , mockedKeyValueService . Object ) ;
200
+ var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
182
201
183
202
var httpClient = CreateMockedHttpClient ( HttpStatusCode . InternalServerError ) ;
184
203
HubspotContactService . ClientFactory = ( ) => httpClient ;
@@ -187,20 +206,26 @@ public async Task PostContact_WithFailedRequest_ReturnsFailedWithLoggedError()
187
206
var fieldMappings = new List < MappedProperty > ( ) ;
188
207
var result = await sut . PostContactAsync ( record , fieldMappings ) ;
189
208
190
- mockedLogger
191
- . Verify ( x => x . Error ( It . Is < Type > ( y => y == typeof ( HubspotContactService ) ) , It . IsAny < string > ( ) ) , Times . Once ) ;
209
+ mockedLogger . Verify (
210
+ m => m . Log (
211
+ LogLevel . Error ,
212
+ It . IsAny < EventId > ( ) ,
213
+ It . IsAny < It . IsAnyType > ( ) ,
214
+ It . IsAny < Exception > ( ) ,
215
+ It . IsAny < Func < It . IsAnyType , Exception ? , string > > ( ) ) ,
216
+ Times . Once ,
217
+ It . IsAny < string > ( ) ) ;
192
218
193
219
Assert . AreEqual ( CommandResult . Failed , result ) ;
194
220
}
195
221
196
222
[ Test ]
197
223
public async Task PostContact_WithSuccessfulRequest_ReturnSuccess ( )
198
224
{
199
- Mock < IFacadeConfiguration > mockedConfig = CreateMockedConfiguration ( ) ;
200
- var mockedLogger = new Mock < ILogger > ( ) ;
201
- var mockedAppCaches = new Mock < AppCaches > ( ) ;
225
+ Mock < IConfiguration > mockedConfig = CreateMockedConfiguration ( ) ;
226
+ var mockedLogger = new Mock < ILogger < HubspotContactService > > ( ) ;
202
227
var mockedKeyValueService = new Mock < IKeyValueService > ( ) ;
203
- var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , mockedAppCaches . Object , mockedKeyValueService . Object ) ;
228
+ var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
204
229
205
230
var httpClient = CreateMockedHttpClient ( HttpStatusCode . OK ) ;
206
231
HubspotContactService . ClientFactory = ( ) => httpClient ;
@@ -222,20 +247,26 @@ public async Task PostContact_WithSuccessfulRequest_ReturnSuccess()
222
247
} ;
223
248
var result = await sut . PostContactAsync ( record , fieldMappings ) ;
224
249
225
- mockedLogger
226
- . Verify ( x => x . Warn ( It . Is < Type > ( y => y == typeof ( HubspotContactService ) ) , It . IsAny < string > ( ) , It . IsAny < object [ ] > ( ) ) , Times . Never ) ;
250
+ mockedLogger . Verify (
251
+ m => m . Log (
252
+ LogLevel . Warning ,
253
+ It . IsAny < EventId > ( ) ,
254
+ It . IsAny < It . IsAnyType > ( ) ,
255
+ It . IsAny < Exception > ( ) ,
256
+ It . IsAny < Func < It . IsAnyType , Exception ? , string > > ( ) ) ,
257
+ Times . Never ,
258
+ It . IsAny < string > ( ) ) ;
227
259
228
260
Assert . AreEqual ( CommandResult . Success , result ) ;
229
261
}
230
262
231
263
[ Test ]
232
264
public async Task PostContact_WithSuccessfulRequestAndUnmappedField_ReturnSuccessWithLoggedWarning ( )
233
265
{
234
- Mock < IFacadeConfiguration > mockedConfig = CreateMockedConfiguration ( ) ;
235
- var mockedLogger = new Mock < ILogger > ( ) ;
236
- var mockedAppCaches = new Mock < AppCaches > ( ) ;
266
+ Mock < IConfiguration > mockedConfig = CreateMockedConfiguration ( ) ;
267
+ var mockedLogger = new Mock < ILogger < HubspotContactService > > ( ) ;
237
268
var mockedKeyValueService = new Mock < IKeyValueService > ( ) ;
238
- var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , mockedAppCaches . Object , mockedKeyValueService . Object ) ;
269
+ var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
239
270
240
271
var httpClient = CreateMockedHttpClient ( HttpStatusCode . OK ) ;
241
272
HubspotContactService . ClientFactory = ( ) => httpClient ;
@@ -252,19 +283,26 @@ public async Task PostContact_WithSuccessfulRequestAndUnmappedField_ReturnSucces
252
283
} ;
253
284
var result = await sut . PostContactAsync ( record , fieldMappings ) ;
254
285
255
- mockedLogger
256
- . Verify ( x => x . Warn ( It . Is < Type > ( y => y == typeof ( HubspotContactService ) ) , It . IsAny < string > ( ) , It . IsAny < object [ ] > ( ) ) , Times . Once ) ;
286
+ mockedLogger . Verify (
287
+ m => m . Log (
288
+ LogLevel . Warning ,
289
+ It . IsAny < EventId > ( ) ,
290
+ It . IsAny < It . IsAnyType > ( ) ,
291
+ It . IsAny < Exception > ( ) ,
292
+ It . IsAny < Func < It . IsAnyType , Exception ? , string > > ( ) ) ,
293
+ Times . Once ,
294
+ It . IsAny < string > ( ) ) ;
257
295
258
296
Assert . AreEqual ( CommandResult . Success , result ) ;
259
297
}
260
298
261
- private static Mock < IFacadeConfiguration > CreateMockedConfiguration ( bool withApiKey = true )
299
+ private static Mock < IConfiguration > CreateMockedConfiguration ( bool withApiKey = true )
262
300
{
263
- var mockedConfiguration = new Mock < IFacadeConfiguration > ( ) ;
301
+ var mockedConfiguration = new Mock < IConfiguration > ( ) ;
264
302
if ( withApiKey )
265
303
{
266
304
mockedConfiguration
267
- . Setup ( x => x . GetSetting ( It . Is < string > ( y => y == "HubSpotApiKey" ) ) )
305
+ . Setup ( x => x [ It . Is < string > ( y => y == HubspotWorkflow . HubspotApiKey ) ] )
268
306
. Returns ( ApiKey ) ;
269
307
}
270
308
0 commit comments