public void GetBatchTaskTest() { // Setup cmdlet to get a task by id BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); cmdlet.BatchContext = context; cmdlet.JobId = "job-1"; cmdlet.Id = "task1"; cmdlet.Filter = null; // Build a CloudTask instead of querying the service on a Get CloudTask call CloudTaskGetResponse response = BatchTestHelpers.CreateCloudTaskGetResponse(cmdlet.Id); RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor <CloudTaskGetParameters, CloudTaskGetResponse>(response); cmdlet.AdditionalBehaviors = new List <BatchClientBehavior>() { interceptor }; // Setup the cmdlet to write pipeline output to a list that can be examined later List <PSCloudTask> pipeline = new List <PSCloudTask>(); commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny <PSCloudTask>())).Callback <object>(t => pipeline.Add((PSCloudTask)t)); cmdlet.ExecuteCmdlet(); // Verify that the cmdlet wrote the task returned from the OM to the pipeline Assert.Equal(1, pipeline.Count); Assert.Equal(cmdlet.Id, pipeline[0].Id); }
// TO DO: Since we have to fetch the task, the interceptor needs to handle that case too. Once // the cmdlet can directly call the List Subtasks method by itself, update these test cases to // use the generic interceptor creation helper. private RequestInterceptor CreateFakeListSubtasksInterceptor(string taskId, CloudTaskListSubtasksResponse listSubtasksResponse) { RequestInterceptor interceptor = new RequestInterceptor((baseRequest) => { BatchRequest <CloudTaskListSubtasksParameters, CloudTaskListSubtasksResponse> listSubtaskRequest = baseRequest as BatchRequest <CloudTaskListSubtasksParameters, CloudTaskListSubtasksResponse>; if (listSubtaskRequest != null) { listSubtaskRequest.ServiceRequestFunc = (cancellationToken) => { Task <CloudTaskListSubtasksResponse> task = Task.FromResult(listSubtasksResponse); return(task); }; } else { BatchRequest <CloudTaskGetParameters, CloudTaskGetResponse> getTaskRequest = (BatchRequest <CloudTaskGetParameters, CloudTaskGetResponse>)baseRequest; getTaskRequest.ServiceRequestFunc = (cancellationToken) => { CloudTaskGetResponse response = BatchTestHelpers.CreateCloudTaskGetResponse(taskId); Task <CloudTaskGetResponse> task = Task.FromResult(response); return(task); }; } }); return(interceptor); }
public void GetBatchTaskODataTest() { // Setup cmdlet to get a single task BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); cmdlet.BatchContext = context; cmdlet.JobId = "testJob"; cmdlet.Id = "testTask1"; cmdlet.Select = "id,state"; cmdlet.Expand = "stats"; string requestSelect = null; string requestExpand = null; // Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used. CloudTaskGetResponse getResponse = BatchTestHelpers.CreateCloudTaskGetResponse(cmdlet.Id); RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor <CloudTaskGetParameters, CloudTaskGetResponse>(getResponse); ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) => { requestSelect = request.Parameters.DetailLevel.SelectClause; requestExpand = request.Parameters.DetailLevel.ExpandClause; return(Task.FromResult(response)); }); cmdlet.AdditionalBehaviors = new List <BatchClientBehavior>() { requestInterceptor, responseInterceptor }; cmdlet.ExecuteCmdlet(); Assert.Equal(cmdlet.Select, requestSelect); Assert.Equal(cmdlet.Expand, requestExpand); }
// TO DO: Since we have to fetch the task, the interceptor needs to handle that case too. Once // the cmdlet can directly call the List Subtasks method by itself, update these test cases to // use the generic interceptor creation helper. private RequestInterceptor CreateFakeListSubtasksInterceptor( string taskId, AzureOperationResponse <ProxyModels.CloudTaskListSubtasksResult, ProxyModels.TaskListSubtasksHeaders> listSubtasksResponse) { RequestInterceptor interceptor = new RequestInterceptor((baseRequest) => { TaskListSubtasksBatchRequest listSubtaskRequest = baseRequest as TaskListSubtasksBatchRequest; if (listSubtaskRequest != null) { listSubtaskRequest.ServiceRequestFunc = (cancellationToken) => { Task <AzureOperationResponse <ProxyModels.CloudTaskListSubtasksResult, ProxyModels.TaskListSubtasksHeaders> > task = Task.FromResult(listSubtasksResponse); return(task); }; } else { TaskGetBatchRequest getTaskRequest = (TaskGetBatchRequest)baseRequest; getTaskRequest.ServiceRequestFunc = (cancellationToken) => { AzureOperationResponse <ProxyModels.CloudTask, ProxyModels.TaskGetHeaders> response = BatchTestHelpers.CreateCloudTaskGetResponse(taskId); Task <AzureOperationResponse <ProxyModels.CloudTask, ProxyModels.TaskGetHeaders> > task = Task.FromResult(response); return(task); }; } }); return(interceptor); }
public void GetBatchTaskODataTest() { // Setup cmdlet to get a single task BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); cmdlet.BatchContext = context; cmdlet.JobId = "testJob"; cmdlet.Id = "testTask1"; cmdlet.Select = "id,state"; cmdlet.Expand = "stats"; string requestSelect = null; string requestExpand = null; // Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used. AzureOperationResponse <ProxyModels.CloudTask, ProxyModels.TaskGetHeaders> getResponse = BatchTestHelpers.CreateCloudTaskGetResponse(cmdlet.Id); RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor < ProxyModels.TaskGetOptions, AzureOperationResponse <ProxyModels.CloudTask, ProxyModels.TaskGetHeaders> >(getResponse); ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) => { ProxyModels.TaskGetOptions options = (ProxyModels.TaskGetOptions)request.Options; requestSelect = options.Select; requestExpand = options.Expand; return(Task.FromResult(response)); }); cmdlet.AdditionalBehaviors = new List <BatchClientBehavior>() { requestInterceptor, responseInterceptor }; cmdlet.ExecuteCmdlet(); Assert.Equal(cmdlet.Select, requestSelect); Assert.Equal(cmdlet.Expand, requestExpand); }
public void WhenGettingATaskFromTheService_ApplicationPackageReferencesAreMapped() { BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); cmdlet.BatchContext = context; cmdlet.Id = "task-1"; cmdlet.JobId = "job-1"; cmdlet.Filter = null; // Build a CloudTask instead of querying the service on a Get CloudTask call string applicationId = "foo"; string applicationVersion = "beta"; ProxyModels.CloudTask cloudTask = new ProxyModels.CloudTask { Id = "task-1", ApplicationPackageReferences = new[] { new ProxyModels.ApplicationPackageReference(applicationId, applicationVersion) } }; AzureOperationResponse <ProxyModels.CloudTask, ProxyModels.TaskGetHeaders> response = BatchTestHelpers.CreateCloudTaskGetResponse(cloudTask); RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor <ProxyModels.TaskGetOptions, AzureOperationResponse <ProxyModels.CloudTask, ProxyModels.TaskGetHeaders> >(response); cmdlet.AdditionalBehaviors = new List <BatchClientBehavior> { interceptor }; // Setup the cmdlet to write pipeline output to a list that can be examined later var pipeline = new List <PSCloudTask>(); commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny <PSCloudTask>())).Callback <object>(t => pipeline.Add((PSCloudTask)t)); cmdlet.ExecuteCmdlet(); // Verify that the cmdlet wrote the task returned from the OM to the pipeline Assert.Single(pipeline); Assert.Equal(cmdlet.Id, pipeline[0].Id); var psApplicationPackageReference = pipeline[0].ApplicationPackageReferences.First(); Assert.Equal(applicationId, psApplicationPackageReference.ApplicationId); Assert.Equal(applicationVersion, psApplicationPackageReference.Version); }
public void WhenGettingATaskFromTheService_ExitConditionsAreMapped() { BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); cmdlet.BatchContext = context; cmdlet.Id = "task-1"; cmdlet.JobId = "job-1"; cmdlet.Filter = null; // Build a CloudTask instead of querying the service on a Get CloudTask call ProxyModels.ExitOptions none = new ProxyModels.ExitOptions { JobAction = ProxyModels.JobAction.None }; ProxyModels.ExitOptions terminate = new ProxyModels.ExitOptions { JobAction = ProxyModels.JobAction.Terminate }; ProxyModels.CloudTask cloudTask = new ProxyModels.CloudTask { Id = "task-1", ExitConditions = new ProxyModels.ExitConditions { ExitCodeRanges = new[] { new ProxyModels.ExitCodeRangeMapping(2, 5, none) }, ExitCodes = new[] { new ProxyModels.ExitCodeMapping(4, terminate) }, PreProcessingError = terminate, FileUploadError = none, DefaultProperty = none, } }; AzureOperationResponse <ProxyModels.CloudTask, ProxyModels.TaskGetHeaders> response = BatchTestHelpers.CreateCloudTaskGetResponse(cloudTask); RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor <ProxyModels.TaskGetOptions, AzureOperationResponse <ProxyModels.CloudTask, ProxyModels.TaskGetHeaders> >(response); cmdlet.AdditionalBehaviors = new List <BatchClientBehavior>() { interceptor }; // Setup the cmdlet to write pipeline output to a list that can be examined later var pipeline = new List <PSCloudTask>(); commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny <PSCloudTask>())).Callback <object>(t => pipeline.Add((PSCloudTask)t)); cmdlet.ExecuteCmdlet(); // Verify that the cmdlet wrote the task returned from the OM to the pipeline Assert.Single(pipeline); Assert.Equal(cmdlet.Id, pipeline[0].Id); PSExitConditions psExitConditions = pipeline[0].ExitConditions; Assert.Equal(JobAction.None, psExitConditions.Default.JobAction); Assert.Equal(JobAction.None, psExitConditions.ExitCodeRanges.First().ExitOptions.JobAction); Assert.Equal(JobAction.Terminate, psExitConditions.PreProcessingError.JobAction); Assert.Equal(JobAction.None, psExitConditions.FileUploadError.JobAction); Assert.Equal(4, psExitConditions.ExitCodes.First().Code); Assert.Equal(JobAction.Terminate, psExitConditions.ExitCodes.First().ExitOptions.JobAction); Assert.Equal(2, psExitConditions.ExitCodeRanges.First().Start); Assert.Equal(5, psExitConditions.ExitCodeRanges.First().End); Assert.Equal(JobAction.None, psExitConditions.ExitCodeRanges.First().ExitOptions.JobAction); }