// 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 ListBatchSubtasksMaxCountTest()
        {
            // Verify default max count
            Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);

            // Setup cmdlet to list Subtasks with a max count
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();

            cmdlet.BatchContext = context;
            cmdlet.JobId        = "job-1";
            cmdlet.TaskId       = "task1";
            int maxCount = 2;

            cmdlet.MaxCount = maxCount;

            int[] idsOfConstructedSubtasks = new[] { 1, 2, 3 };

            // Build some SubtaskInformation objects instead of querying the service on a List Subtasks call
            CloudTaskListSubtasksResponse response    = BatchTestHelpers.CreateCloudTaskListSubtasksResponse(idsOfConstructedSubtasks);
            RequestInterceptor            interceptor = CreateFakeListSubtasksInterceptor(cmdlet.TaskId, response);

            cmdlet.AdditionalBehaviors = new List <BatchClientBehavior>()
            {
                interceptor
            };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List <PSSubtaskInformation> pipeline = new List <PSSubtaskInformation>();

            commandRuntimeMock.Setup(r =>
                                     r.WriteObject(It.IsAny <PSSubtaskInformation>()))
            .Callback <object>(s => pipeline.Add((PSSubtaskInformation)s));

            cmdlet.ExecuteCmdlet();

            // Verify that the max count was respected
            Assert.Equal(maxCount, pipeline.Count);

            // Verify setting max count <= 0 doesn't return nothing
            cmdlet.MaxCount = -5;
            pipeline.Clear();
            cmdlet.ExecuteCmdlet();

            Assert.Equal(idsOfConstructedSubtasks.Length, pipeline.Count);
        }
        public void ListBatchSubtasksWithoutFiltersTest()
        {
            // Setup cmdlet to list Subtasks without filters. Use WorkItemName and JobName.
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();

            cmdlet.BatchContext = context;
            cmdlet.JobId        = "job-1";
            cmdlet.TaskId       = "task1";

            int[] idsOfConstructedSubtasks = new[] { 1, 2, 3 };

            // Build some SubtaskInformation objects instead of querying the service on a List Subtasks call
            CloudTaskListSubtasksResponse response    = BatchTestHelpers.CreateCloudTaskListSubtasksResponse(idsOfConstructedSubtasks);
            RequestInterceptor            interceptor = CreateFakeListSubtasksInterceptor(cmdlet.TaskId, response);

            cmdlet.AdditionalBehaviors = new List <BatchClientBehavior>()
            {
                interceptor
            };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List <PSSubtaskInformation> pipeline = new List <PSSubtaskInformation>();

            commandRuntimeMock.Setup(r =>
                                     r.WriteObject(It.IsAny <PSSubtaskInformation>()))
            .Callback <object>(s => pipeline.Add((PSSubtaskInformation)s));

            cmdlet.ExecuteCmdlet();

            // Verify that the cmdlet wrote the constructed Subtasks to the pipeline
            Assert.Equal(3, pipeline.Count);
            int SubtaskCount = 0;

            foreach (PSSubtaskInformation s in pipeline)
            {
                Assert.True(idsOfConstructedSubtasks.Contains(s.Id.Value));
                SubtaskCount++;
            }
            Assert.Equal(idsOfConstructedSubtasks.Length, SubtaskCount);
        }
        // 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;
        }