12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
- using System . IO ;
16
15
using System . Linq ;
17
16
using System . Net ;
18
- using System . Net . Http ;
19
17
using System . Threading . Tasks ;
20
18
using Microsoft . Azure . Batch ;
21
- using Microsoft . Azure . Batch . Common ;
22
19
using Microsoft . Azure . Batch . Protocol ;
23
- using Microsoft . Azure . Commands . Batch . Models ;
24
- using Microsoft . Azure . Commands . Batch . Test . ScenarioTests ;
25
- using Microsoft . Azure . Management . Batch ;
26
20
using Microsoft . Azure . Management . Batch . Models ;
27
21
using System ;
28
22
using System . Collections ;
29
23
using System . Collections . Generic ;
30
24
using System . Reflection ;
31
- using Microsoft . Azure . Management . Resources ;
32
- using Microsoft . Azure . Management . Resources . Models ;
33
- using Microsoft . Azure . Test . HttpRecorder ;
34
- using Microsoft . WindowsAzure . Storage . Shared . Protocol ;
35
25
using Xunit ;
36
26
using ProxyModels = Microsoft . Azure . Batch . Protocol . Models ;
37
27
@@ -114,16 +104,22 @@ public static void AssertBatchAccountContextsAreEqual(BatchAccountContext contex
114
104
/// Creates a RequestInterceptor that does not contact the Batch Service but instead uses the supplied response body.
115
105
/// </summary>
116
106
/// <param name="responseToUse">The response the interceptor should return. If none is specified, then a new instance of the response type is instantiated.</param>
107
+ /// <param name="requestAction">An action to perform on the request.</param>
117
108
/// <typeparam name="TParameters">The type of the request parameters.</typeparam>
118
109
/// <typeparam name="TResponse">The type of the expected response.</typeparam>
119
- public static RequestInterceptor CreateNoOpInterceptor < TParameters , TResponse > ( TResponse responseToUse = null )
110
+ public static RequestInterceptor CreateFakeServiceResponseInterceptor < TParameters , TResponse > ( TResponse responseToUse = null ,
111
+ Action < BatchRequest < TParameters , TResponse > > requestAction = null )
120
112
where TParameters : ProxyModels . BatchParameters
121
113
where TResponse : ProxyModels . BatchOperationResponse , new ( )
122
114
{
123
115
RequestInterceptor interceptor = new RequestInterceptor ( ( baseRequest ) =>
124
116
{
125
- BatchRequest < TParameters , TResponse > request =
126
- ( BatchRequest < TParameters , TResponse > ) baseRequest ;
117
+ BatchRequest < TParameters , TResponse > request = ( BatchRequest < TParameters , TResponse > ) baseRequest ;
118
+
119
+ if ( requestAction != null )
120
+ {
121
+ requestAction ( request ) ;
122
+ }
127
123
128
124
request . ServiceRequestFunc = ( cancellationToken ) =>
129
125
{
@@ -140,12 +136,12 @@ public static RequestInterceptor CreateNoOpInterceptor<TParameters, TResponse>(T
140
136
/// The interceptor must handle both request types since it's possible for one OM node file method to perform both REST APIs.
141
137
/// </summary>
142
138
/// <param name="fileName">The name of the file to put in the response body.</param>
143
- public static RequestInterceptor CreateNoOpGetFileAndPropertiesInterceptor ( string fileName )
139
+ public static RequestInterceptor CreateFakGetFileAndPropertiesResponseInterceptor ( string fileName )
144
140
{
145
141
RequestInterceptor interceptor = new RequestInterceptor ( ( baseRequest ) =>
146
142
{
147
143
BatchRequest < ProxyModels . NodeFileGetParameters , ProxyModels . NodeFileGetResponse > fileRequest = baseRequest as
148
- BatchRequest < ProxyModels . NodeFileGetParameters , ProxyModels . NodeFileGetResponse > ;
144
+ BatchRequest < ProxyModels . NodeFileGetParameters , ProxyModels . NodeFileGetResponse > ;
149
145
150
146
if ( fileRequest != null )
151
147
{
@@ -401,6 +397,106 @@ public static ProxyModels.NodeFileListResponse CreateNodeFileListResponse(IEnume
401
397
return response ;
402
398
}
403
399
400
+ /// <summary>
401
+ /// Fabricates a CloudJob that's in the bound state
402
+ /// </summary>
403
+ public static CloudJob CreateFakeBoundJob ( BatchAccountContext context )
404
+ {
405
+ string jobId = "testJob" ;
406
+
407
+ RequestInterceptor interceptor = new RequestInterceptor ( ( baseRequest ) =>
408
+ {
409
+ BatchRequest < ProxyModels . CloudJobGetParameters , ProxyModels . CloudJobGetResponse > request =
410
+ ( BatchRequest < ProxyModels . CloudJobGetParameters , ProxyModels . CloudJobGetResponse > ) baseRequest ;
411
+
412
+ request . ServiceRequestFunc = ( cancellationToken ) =>
413
+ {
414
+ ProxyModels . CloudJobGetResponse response = new ProxyModels . CloudJobGetResponse ( ) ;
415
+ response . Job = new ProxyModels . CloudJob ( jobId , new ProxyModels . PoolInformation ( ) ) ;
416
+
417
+ Task < ProxyModels . CloudJobGetResponse > task = Task . FromResult ( response ) ;
418
+ return task ;
419
+ } ;
420
+ } ) ;
421
+
422
+ return context . BatchOMClient . JobOperations . GetJob ( jobId , additionalBehaviors : new BatchClientBehavior [ ] { interceptor } ) ;
423
+ }
424
+
425
+ /// <summary>
426
+ /// Fabricates a CloudJobSchedule that's in the bound state
427
+ /// </summary>
428
+ public static CloudJobSchedule CreateFakeBoundJobSchedule ( BatchAccountContext context )
429
+ {
430
+ string jobScheduleId = "testJobSchedule" ;
431
+
432
+ RequestInterceptor interceptor = new RequestInterceptor ( ( baseRequest ) =>
433
+ {
434
+ BatchRequest < ProxyModels . CloudJobScheduleGetParameters , ProxyModels . CloudJobScheduleGetResponse > request =
435
+ ( BatchRequest < ProxyModels . CloudJobScheduleGetParameters , ProxyModels . CloudJobScheduleGetResponse > ) baseRequest ;
436
+
437
+ request . ServiceRequestFunc = ( cancellationToken ) =>
438
+ {
439
+ ProxyModels . CloudJobScheduleGetResponse response = new ProxyModels . CloudJobScheduleGetResponse ( ) ;
440
+ response . JobSchedule = new ProxyModels . CloudJobSchedule ( jobScheduleId , new ProxyModels . Schedule ( ) , new ProxyModels . JobSpecification ( ) ) ;
441
+
442
+ Task < ProxyModels . CloudJobScheduleGetResponse > task = Task . FromResult ( response ) ;
443
+ return task ;
444
+ } ;
445
+ } ) ;
446
+
447
+ return context . BatchOMClient . JobScheduleOperations . GetJobSchedule ( jobScheduleId , additionalBehaviors : new BatchClientBehavior [ ] { interceptor } ) ;
448
+ }
449
+
450
+ /// <summary>
451
+ /// Fabricates a CloudPool that's in the bound state
452
+ /// </summary>
453
+ public static CloudPool CreateFakeBoundPool ( BatchAccountContext context )
454
+ {
455
+ string poolId = "testPool" ;
456
+
457
+ RequestInterceptor interceptor = new RequestInterceptor ( ( baseRequest ) =>
458
+ {
459
+ BatchRequest < ProxyModels . CloudPoolGetParameters , ProxyModels . CloudPoolGetResponse > request =
460
+ ( BatchRequest < ProxyModels . CloudPoolGetParameters , ProxyModels . CloudPoolGetResponse > ) baseRequest ;
461
+
462
+ request . ServiceRequestFunc = ( cancellationToken ) =>
463
+ {
464
+ ProxyModels . CloudPoolGetResponse response = new ProxyModels . CloudPoolGetResponse ( ) ;
465
+ response . Pool = new ProxyModels . CloudPool ( poolId , "small" , "4" ) ;
466
+
467
+ Task < ProxyModels . CloudPoolGetResponse > task = Task . FromResult ( response ) ;
468
+ return task ;
469
+ } ;
470
+ } ) ;
471
+
472
+ return context . BatchOMClient . PoolOperations . GetPool ( poolId , additionalBehaviors : new BatchClientBehavior [ ] { interceptor } ) ;
473
+ }
474
+
475
+ /// <summary>
476
+ /// Fabricates a CloudTask that's in the bound state
477
+ /// </summary>
478
+ public static CloudTask CreateFakeBoundTask ( BatchAccountContext context )
479
+ {
480
+ string taskId = "testTask" ;
481
+
482
+ RequestInterceptor interceptor = new RequestInterceptor ( ( baseRequest ) =>
483
+ {
484
+ BatchRequest < ProxyModels . CloudTaskGetParameters , ProxyModels . CloudTaskGetResponse > request =
485
+ ( BatchRequest < ProxyModels . CloudTaskGetParameters , ProxyModels . CloudTaskGetResponse > ) baseRequest ;
486
+
487
+ request . ServiceRequestFunc = ( cancellationToken ) =>
488
+ {
489
+ ProxyModels . CloudTaskGetResponse response = new ProxyModels . CloudTaskGetResponse ( ) ;
490
+ response . Task = new ProxyModels . CloudTask ( taskId , "cmd /c dir /s" ) ;
491
+
492
+ Task < ProxyModels . CloudTaskGetResponse > task = Task . FromResult ( response ) ;
493
+ return task ;
494
+ } ;
495
+ } ) ;
496
+
497
+ return context . BatchOMClient . JobOperations . GetTask ( "jobId" , taskId , additionalBehaviors : new BatchClientBehavior [ ] { interceptor } ) ;
498
+ }
499
+
404
500
/// <summary>
405
501
/// Uses Reflection to set a property value on an object. Can be used to bypass restricted set accessors.
406
502
/// </summary>
0 commit comments