public void ListBatchComputeNodesByODataFilterTest()
        {
            // Setup cmdlet to list vms using an OData filter.
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.PoolId = "pool";
            cmdlet.Id = null;
            cmdlet.Filter = "state -eq 'idle'";

            string[] idsOfConstructedComputeNodes = new[] { "computeNode1", "computeNode2" };

            // Build some compute nodes instead of querying the service on a List ComputeNodes call
            ComputeNodeListResponse response = BatchTestHelpers.CreateComputeNodeListResponse(idsOfConstructedComputeNodes);
            RequestInterceptor interceptor = BatchTestHelpers.CreateNoOpInterceptor<ComputeNodeListParameters, ComputeNodeListResponse>(response);
            cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSComputeNode> pipeline = new List<PSComputeNode>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSComputeNode>()))
                .Callback<object>(c => pipeline.Add((PSComputeNode)c));

            cmdlet.ExecuteCmdlet();

            // Verify that the cmdlet wrote the constructed compute nodes to the pipeline
            Assert.Equal(2, pipeline.Count);
            int computeNodeCount = 0;
            foreach (PSComputeNode c in pipeline)
            {
                Assert.True(idsOfConstructedComputeNodes.Contains(c.Id));
                computeNodeCount++;
            }
            Assert.Equal(idsOfConstructedComputeNodes.Length, computeNodeCount);
        }
        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);
        }
        public void ListBatchPoolByODataFilterTest()
        {
            // Setup cmdlet to list pools using an OData filter
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.Id = null;
            cmdlet.Filter = "startswith(id,'test')";

            string[] idsOfConstructedPools = new[] { "test1", "test2" };

            // Build some CloudPools instead of querying the service on a List CloudPools call
            CloudPoolListResponse response = BatchTestHelpers.CreateCloudPoolListResponse(idsOfConstructedPools);
            RequestInterceptor interceptor = BatchTestHelpers.CreateNoOpInterceptor<CloudPoolListParameters, CloudPoolListResponse>(response);
            cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSCloudPool> pipeline = new List<PSCloudPool>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSCloudPool>()))
                .Callback<object>(p => pipeline.Add((PSCloudPool)p));

            cmdlet.ExecuteCmdlet();

            // Verify that the cmdlet wrote the constructed pools to the pipeline
            Assert.Equal(2, pipeline.Count);
            int poolCount = 0;
            foreach (PSCloudPool p in pipeline)
            {
                Assert.True(idsOfConstructedPools.Contains(p.Id));
                poolCount++;
            }
            Assert.Equal(idsOfConstructedPools.Length, poolCount);
        }
        public void ListBatchJobsByODataFilterTest()
        {
            // Setup cmdlet to list jobs using an OData filter. Use JobScheduleId input.
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.JobScheduleId = "jobSchedule";
            cmdlet.Id = null;
            cmdlet.Filter = "state -eq 'active'";

            string[] idsOfConstructedJobs = new[] { "job-1", "job-2" };

            // Build some CloudJobs instead of querying the service on a List CloudJobs call
            CloudJobListResponse response = BatchTestHelpers.CreateCloudJobListResponse(idsOfConstructedJobs);
            RequestInterceptor interceptor = BatchTestHelpers.CreateNoOpInterceptor<CloudJobListParameters, CloudJobListResponse>(response);
            cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSCloudJob> pipeline = new List<PSCloudJob>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSCloudJob>()))
                .Callback<object>(j => pipeline.Add((PSCloudJob)j));

            cmdlet.ExecuteCmdlet();

            // Verify that the cmdlet wrote the constructed jobs to the pipeline
            Assert.Equal(2, pipeline.Count);
            int jobCount = 0;
            foreach (PSCloudJob j in pipeline)
            {
                Assert.True(idsOfConstructedJobs.Contains(j.Id));
                jobCount++;
            }
            Assert.Equal(idsOfConstructedJobs.Length, jobCount);
        }
        public void ListBatchNodeAgentSkusParametersTest()
        {
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;

            string[] idsOfNodeAgentSkus = new[] { "batch.node.centos 7", "batch.node.debian 8", "batch.node.opensuse 13.2" };

            // Don't go to the service on an Get NodeAgentSkus call
            AzureOperationResponse<IPage<ProxyModels.NodeAgentSku>, ProxyModels.AccountListNodeAgentSkusHeaders> response =
                BatchTestHelpers.CreateNodeAgentSkuResponse(idsOfNodeAgentSkus);

            RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
                ProxyModels.AccountListNodeAgentSkusOptions,
                AzureOperationResponse<IPage<ProxyModels.NodeAgentSku>, ProxyModels.AccountListNodeAgentSkusHeaders>>(responseToUse: response);

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

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSNodeAgentSku> pipeline = new List<PSNodeAgentSku>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSNodeAgentSku>()))
                .Callback<object>(p => pipeline.Add((PSNodeAgentSku)p));

            // Verify no exceptions when required parameter is set
            cmdlet.ExecuteCmdlet();

            Assert.Equal(3, pipeline.Count);
            int nodeAgentCount = 0;
            foreach (PSNodeAgentSku p in pipeline)
            {
                Assert.True(idsOfNodeAgentSkus.Contains(p.Id));
                nodeAgentCount++;
            }
            Assert.Equal(idsOfNodeAgentSkus.Length, nodeAgentCount);
        }
        public void GetBatchPoolUsageODataTest()
        {
            int year = 2013;
            int month = 4;
            int day = 1;

            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.StartTime = new DateTime(year, month, day, 0, 0, 0);
            cmdlet.EndTime = new DateTime(year, month, day, 1, 0, 0);

            string[] poolIds = new[] { "p1", "p2" };
            DateTime[] startTimes = new[] { new DateTime(year, month, day, 0, 0, 0), new DateTime(year, month, day, 0, 30, 0) };
            DateTime[] endTimes = new[] { new DateTime(year, month, day, 0, 30, 0), new DateTime(year, month, day, 1, 0, 0) };

            AzureOperationResponse<IPage<ProxyModels.PoolUsageMetrics>, ProxyModels.PoolListPoolUsageMetricsHeaders> response =
                BatchTestHelpers.CreatePoolListUsageMetricsResponse(poolIds, startTimes, endTimes);

            RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
                ProxyModels.PoolListPoolUsageMetricsOptions,
                AzureOperationResponse<IPage<ProxyModels.PoolUsageMetrics>, ProxyModels.PoolListPoolUsageMetricsHeaders>>(responseToUse: response);

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

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSPoolUsageMetrics> pipeline = new List<PSPoolUsageMetrics>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSPoolUsageMetrics>()))
                .Callback<object>(p => pipeline.Add((PSPoolUsageMetrics)p));

            // Verify no exceptions when required parameter is set
            cmdlet.ExecuteCmdlet();

            Assert.Equal(2, pipeline.Count);
            int poolUsageCount = 0;
            foreach (PSPoolUsageMetrics p in pipeline)
            {
                Assert.True(poolIds.Contains(p.PoolId));
                poolUsageCount++;
            }

            Assert.Equal(poolIds.Length, poolUsageCount);
        }
        public void ListTasksMaxCountTest()
        {
            // Verify default max count
            Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);

            // Setup cmdlet to list tasks without filters and a max count
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.JobId = "job-1";
            cmdlet.Id = null;
            cmdlet.Filter = null;
            int maxCount = 2;
            cmdlet.MaxCount = maxCount;

            string[] idsOfConstructedTasks = new[] { "testTask1", "testTask2", "testTask3" };

            // Build some CloudTasks instead of querying the service on a List CloudTasks call
            AzureOperationResponse<IPage<ProxyModels.CloudTask>, ProxyModels.TaskListHeaders> response = BatchTestHelpers.CreateCloudTaskListResponse(idsOfConstructedTasks);
            RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
                ProxyModels.TaskListOptions,
                AzureOperationResponse<IPage<ProxyModels.CloudTask>, ProxyModels.TaskListHeaders>>(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 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(idsOfConstructedTasks.Length, pipeline.Count);
        }
        public void ListBatchTasksWithoutFiltersTest()
        {
            // Setup cmdlet to list tasks without filters. Use WorkItemName and JobName.
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.JobId = "job-1";
            cmdlet.Id = null;
            cmdlet.Filter = null;

            string[] idsOfConstructedTasks = new[] { "testTask1", "testTask2", "testTask3" };

            // Build some CloudTasks instead of querying the service on a List CloudTasks call
            AzureOperationResponse<IPage<ProxyModels.CloudTask>, ProxyModels.TaskListHeaders> response = BatchTestHelpers.CreateCloudTaskListResponse(idsOfConstructedTasks);
            RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
                ProxyModels.TaskListOptions,
                AzureOperationResponse<IPage<ProxyModels.CloudTask>, ProxyModels.TaskListHeaders>>(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 constructed tasks to the pipeline
            Assert.Equal(3, pipeline.Count);
            int taskCount = 0;
            foreach (PSCloudTask t in pipeline)
            {
                Assert.True(idsOfConstructedTasks.Contains(t.Id));
                taskCount++;
            }
            Assert.Equal(idsOfConstructedTasks.Length, taskCount);
        }
        public void ListBatchPoolWithoutFiltersTest()
        {
            // Setup cmdlet to list pools without filters
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.Id = null;
            cmdlet.Filter = null;

            string[] idsOfConstructedPools = new[] { "pool1", "pool2", "pool3" };

            // Build some CloudPools instead of querying the service on a List CloudPools call
            RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
            {
                BatchRequest<CloudPoolListParameters, CloudPoolListResponse> request =
                (BatchRequest<CloudPoolListParameters, CloudPoolListResponse>)baseRequest;

                request.ServiceRequestFunc = (cancellationToken) =>
                {
                    CloudPoolListResponse response = BatchTestHelpers.CreateCloudPoolListResponse(idsOfConstructedPools);
                    Task<CloudPoolListResponse> task = Task.FromResult(response);
                    return task;
                };
            });
            cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSCloudPool> pipeline = new List<PSCloudPool>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSCloudPool>()))
                .Callback<object>(p => pipeline.Add((PSCloudPool)p));

            cmdlet.ExecuteCmdlet();

            // Verify that the cmdlet wrote the constructed pools to the pipeline
            Assert.Equal(3, pipeline.Count);
            int poolCount = 0;
            foreach (PSCloudPool p in pipeline)
            {
                Assert.True(idsOfConstructedPools.Contains(p.Id));
                poolCount++;
            }
            Assert.Equal(idsOfConstructedPools.Length, poolCount);
        }
        public void ListBatchJobScheduleByODataFilterTest()
        {
            // Setup cmdlet to list job schedules using an OData filter
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.Id = null;
            cmdlet.Filter = "startswith(id,'test')";

            string[] idsOfConstructedJobSchedules = new[] { "test1", "test2" };

            // Build some CloudJobSchedules instead of querying the service on a List CloudJobSchedules call
            RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
            {
                BatchRequest<CloudJobScheduleListParameters, CloudJobScheduleListResponse> request =
                (BatchRequest<CloudJobScheduleListParameters, CloudJobScheduleListResponse>)baseRequest;

                request.ServiceRequestFunc = (cancellationToken) =>
                {
                    CloudJobScheduleListResponse response = BatchTestHelpers.CreateCloudJobScheduleListResponse(idsOfConstructedJobSchedules);
                    Task<CloudJobScheduleListResponse> task = Task.FromResult(response);
                    return task;
                };
            });
            cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSCloudJobSchedule> pipeline = new List<PSCloudJobSchedule>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSCloudJobSchedule>()))
                .Callback<object>(j => pipeline.Add((PSCloudJobSchedule)j));

            cmdlet.ExecuteCmdlet();

            // Verify that the cmdlet wrote the constructed job schedules to the pipeline
            Assert.Equal(2, pipeline.Count);
            int jobScheduleCount = 0;
            foreach (PSCloudJobSchedule j in pipeline)
            {
                Assert.True(idsOfConstructedJobSchedules.Contains(j.Id));
                jobScheduleCount++;
            }
            Assert.Equal(idsOfConstructedJobSchedules.Length, jobScheduleCount);
        }
        public void ListBatchComputeNodesWithoutFiltersTest()
        {
            // Setup cmdlet to list compute nodes without filters. 
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.PoolId = "testPool";
            cmdlet.Id = null;
            cmdlet.Filter = null;

            string[] idsOfConstructedComputeNodes = new[] { "computeNode1", "computeNode2", "computeNode3" };

            // Build some compute nodes instead of querying the service on a List ComputeNodes call
            AzureOperationResponse<IPage<ProxyModels.ComputeNode>, ProxyModels.ComputeNodeListHeaders> response =
                BatchTestHelpers.CreateComputeNodeListResponse(idsOfConstructedComputeNodes);
            RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
                ProxyModels.ComputeNodeListOptions,
                AzureOperationResponse<IPage<ProxyModels.ComputeNode>,
                ProxyModels.ComputeNodeListHeaders>>(response);

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

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSComputeNode> pipeline = new List<PSComputeNode>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSComputeNode>()))
                .Callback<object>(c => pipeline.Add((PSComputeNode)c));

            cmdlet.ExecuteCmdlet();

            // Verify that the cmdlet wrote the constructed compute nodes to the pipeline
            Assert.Equal(3, pipeline.Count);
            int computeNodeCount = 0;
            foreach (PSComputeNode c in pipeline)
            {
                Assert.True(idsOfConstructedComputeNodes.Contains(c.Id));
                computeNodeCount++;
            }
            Assert.Equal(idsOfConstructedComputeNodes.Length, computeNodeCount);
        }
        public void NewBatchTaskCollectionParametersTest()
        {
            string commandLine = "cmd /c dir /s";

            // Setup cmdlet without the required parameters
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;

            Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());

            cmdlet.JobId = "job-collection";

            Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());

            string[] taskIds = new[] {"simple1", "simple2"};
            PSCloudTask expected1 = new PSCloudTask(taskIds[0], commandLine);
            PSCloudTask expected2 = new PSCloudTask(taskIds[1], commandLine);

            cmdlet.Tasks = new PSCloudTask[] {expected1, expected2};

            IList<TaskAddParameter> requestCollection = null;

            Action<BatchRequest<
                IList<TaskAddParameter>,
                TaskAddCollectionOptions,
                AzureOperationResponse<TaskAddCollectionResult, TaskAddCollectionHeaders>>> extractCollection =
                (request) =>
                {
                    requestCollection = request.Parameters;
                };

            // Don't go to the service on an Add Task Collection call
            AzureOperationResponse<TaskAddCollectionResult, TaskAddCollectionHeaders> response =
                BatchTestHelpers.CreateTaskCollectionResponse(cmdlet.Tasks);

            RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor(responseToUse: response, requestAction: extractCollection);

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

            // Verify no exceptions when required parameters are set
            cmdlet.ExecuteCmdlet();

            Assert.Equal(2, requestCollection.Count);
            foreach (var task in requestCollection)
            {
                Assert.True(taskIds.Contains(task.Id));
            }
        }
        public void ListBatchJobsWithoutFiltersTest()
        {
            // Setup cmdlet to list Jobs without filters. Use WorkItem input.
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.WorkItem = BatchTestHelpers.CreatePSCloudWorkItem();
            cmdlet.Name = null;
            cmdlet.Filter = null;

            string[] namesOfConstructedJobs = new[] { "job-0000000001", "job-0000000002", "job-0000000003" };

            // Build some Jobs instead of querying the service on a ListJobs call
            YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) =>
            {
                if (request is ListJobsRequest)
                {
                    ListJobsResponse response = BatchTestHelpers.CreateListJobsResponse(namesOfConstructedJobs);
                    Task<object> task = Task<object>.Factory.StartNew(() => { return response; });
                    return task;
                }
                return null;
            });
            cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSCloudJob> pipeline = new List<PSCloudJob>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSCloudJob>()))
                .Callback<object>(j => pipeline.Add((PSCloudJob)j));

            cmdlet.ExecuteCmdlet();

            // Verify that the cmdlet wrote the constructed Jobs to the pipeline
            Assert.Equal(3, pipeline.Count);
            int jobCount = 0;
            foreach (PSCloudJob j in pipeline)
            {
                Assert.True(namesOfConstructedJobs.Contains(j.Name));
                jobCount++;
            }
            Assert.Equal(namesOfConstructedJobs.Length, jobCount);
        }
        public void ListNodeFilesByComputeNodeMaxCountTest()
        {
            // Verify default max count
            Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);

            // Setup cmdlet to list vm files and a max count. 
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.PoolId = "pool";
            cmdlet.ComputeNodeId = "computeNode1";
            cmdlet.Name = null;
            cmdlet.Filter = null;
            int maxCount = 2;
            cmdlet.MaxCount = maxCount;

            string[] namesOfConstructedNodeFiles = new[] { "startup", "workitems", "shared" };

            // Build some NodeFiles instead of querying the service on a List NodeFiles call
            AzureOperationResponse<IPage<ProxyModels.NodeFile>, ProxyModels.FileListFromComputeNodeHeaders> response =
                BatchTestHelpers.CreateNodeFileListByComputeNodeResponse(namesOfConstructedNodeFiles);
            RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
                bool?,
                ProxyModels.FileListFromComputeNodeOptions,
                AzureOperationResponse<IPage<ProxyModels.NodeFile>,
                ProxyModels.FileListFromComputeNodeHeaders>>(response);

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

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSNodeFile> pipeline = new List<PSNodeFile>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSNodeFile>()))
                .Callback<object>(f => pipeline.Add((PSNodeFile)f));

            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(namesOfConstructedNodeFiles.Length, pipeline.Count);
        }
        public void ListBatchNodeFilesByComputeNodeWithoutFiltersTest()
        {
            // Setup cmdlet to list vm files without filters. 
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.PoolId = "pool";
            cmdlet.ComputeNodeId = "computeNode1";
            cmdlet.Name = null;
            cmdlet.Filter = null;

            string[] namesOfConstructedNodeFiles = new[] { "startup", "workitems", "shared" };

            // Build some NodeFiles instead of querying the service on a List NodeFiles call
            AzureOperationResponse<IPage<ProxyModels.NodeFile>, ProxyModels.FileListFromComputeNodeHeaders> response =
                BatchTestHelpers.CreateNodeFileListByComputeNodeResponse(namesOfConstructedNodeFiles);
            RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
                bool?,
                ProxyModels.FileListFromComputeNodeOptions,
                AzureOperationResponse<IPage<ProxyModels.NodeFile>,
                ProxyModels.FileListFromComputeNodeHeaders>>(response);

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

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSNodeFile> pipeline = new List<PSNodeFile>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSNodeFile>()))
                .Callback<object>(f => pipeline.Add((PSNodeFile)f));

            cmdlet.ExecuteCmdlet();

            // Verify that the cmdlet wrote the constructed node files to the pipeline
            Assert.Equal(3, pipeline.Count);
            int taskCount = 0;
            foreach (PSNodeFile f in pipeline)
            {
                Assert.True(namesOfConstructedNodeFiles.Contains(f.Name));
                taskCount++;
            }
            Assert.Equal(namesOfConstructedNodeFiles.Length, taskCount);
        }
        public void GetBatchPoolUsageWithFilter()
        {
            int year = 2013;
            int month = 4;
            int day = 1;

            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.Filter = "poolId lt 'p2'";

            string requestFilter = null;
            string[] poolIds = new[] { "p1" };
            DateTime[] startTimes = new[] { new DateTime(year, month, day, 0, 0, 0) };
            DateTime[] endTimes = new[] { new DateTime(year, month, day, 0, 30, 0) };

            AzureOperationResponse<IPage<ProxyModels.PoolUsageMetrics>, ProxyModels.PoolListPoolUsageMetricsHeaders> response =
                BatchTestHelpers.CreatePoolListUsageMetricsResponse(poolIds, startTimes, endTimes);

            // Don't go to the service on an Get PoolUsageMetrics call
            RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
                ProxyModels.PoolListPoolUsageMetricsOptions,
                AzureOperationResponse<IPage<ProxyModels.PoolUsageMetrics>, ProxyModels.PoolListPoolUsageMetricsHeaders>>(responseToUse: response);

            ResponseInterceptor responseInterceptor = new ResponseInterceptor((responseToUse, request) =>
            {
                ProxyModels.PoolListPoolUsageMetricsOptions options = (ProxyModels.PoolListPoolUsageMetricsOptions)request.Options;
                requestFilter = options.Filter;

                return Task.FromResult(responseToUse);
            });

            cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor, responseInterceptor };

            // Verify no exceptions when required parameter is set
            cmdlet.ExecuteCmdlet();

            Assert.Equal(cmdlet.Filter, requestFilter);
        }
        public void ListBatchSubtasksMaxCountTest()
        {
            // Verify default max count
            Assert.Equal(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
            AzureOperationResponse<ProxyModels.CloudTaskListSubtasksResult, ProxyModels.TaskListSubtasksHeaders> response = BatchTestHelpers.CreateCloudTaskListSubtasksResponse(idsOfConstructedSubtasks);
            RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
                ProxyModels.TaskListSubtasksOptions,
                AzureOperationResponse<ProxyModels.CloudTaskListSubtasksResult,
                ProxyModels.TaskListSubtasksHeaders>>(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 ListBatchTasksByODataFilterTest()
        {
            // Setup cmdlet to list Tasks using an OData filter. Use WorkItemName and JobName input.
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.WorkItemName = "workItem";
            cmdlet.JobName = "job-0000000001";
            cmdlet.Name = null;
            cmdlet.Filter = "startswith(name,'test')";

            string[] namesOfConstructedTasks = new[] { "testTask1", "testTask2" };

            // Build some Tasks instead of querying the service on a ListTasks call
            YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) =>
            {
                if (request is ListTasksRequest)
                {
                    ListTasksResponse response = BatchTestHelpers.CreateListTasksResponse(namesOfConstructedTasks);
                    Task<object> task = Task<object>.Factory.StartNew(() => { return response; });
                    return task;
                }
                return null;
            });
            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 constructed Tasks to the pipeline
            Assert.Equal(2, pipeline.Count);
            int taskCount = 0;
            foreach (PSCloudTask t in pipeline)
            {
                Assert.True(namesOfConstructedTasks.Contains(t.Name));
                taskCount++;
            }
            Assert.Equal(namesOfConstructedTasks.Length, taskCount);
        }
        public void ListJobSchedulesMaxCountTest()
        {
            // Verify default max count
            Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);

            // Setup cmdlet to list job schedules without filters and a max count
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.Id = null;
            cmdlet.Filter = null;
            int maxCount = 2;
            cmdlet.MaxCount = maxCount;

            string[] idsOfConstructedJobSchedules = new[] { "id1", "id2", "id3" };

            // Build some CloudJobSchedules instead of querying the service on a List CloudJobSchedules call
            RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
            {
                BatchRequest<CloudJobScheduleListParameters, CloudJobScheduleListResponse> request =
                (BatchRequest<CloudJobScheduleListParameters, CloudJobScheduleListResponse>)baseRequest;

                request.ServiceRequestFunc = (cancellationToken) =>
                {
                    CloudJobScheduleListResponse response = BatchTestHelpers.CreateCloudJobScheduleListResponse(idsOfConstructedJobSchedules);
                    Task<CloudJobScheduleListResponse> task = Task.FromResult(response);
                    return task;
                };
            });
            cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSCloudJobSchedule> pipeline = new List<PSCloudJobSchedule>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSCloudJobSchedule>()))
                .Callback<object>(j => pipeline.Add((PSCloudJobSchedule)j));

            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(idsOfConstructedJobSchedules.Length, pipeline.Count);
        }
        public void ListVMsMaxCountTest()
        {
            // Verify default max count
            Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);

            // Setup cmdlet to list vms without filters and a max count
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.Pool = BatchTestHelpers.CreatePSCloudPool();
            cmdlet.Name = null;
            cmdlet.Filter = null;
            int maxCount = 2;
            cmdlet.MaxCount = maxCount;

            string[] namesOfConstructedVMs = new[] { "vm1", "vm2", "vm3" };

            // Build some vms instead of querying the service on a ListTVMs call
            YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) =>
            {
                if (request is ListTVMsRequest)
                {
                    ListTVMsResponse response = BatchTestHelpers.CreateListTVMsResponse(namesOfConstructedVMs);
                    Task<object> task = Task<object>.Factory.StartNew(() => { return response; });
                    return task;
                }
                return null;
            });
            cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSVM> pipeline = new List<PSVM>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSVM>()))
                .Callback<object>(v => pipeline.Add((PSVM)v));

            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(namesOfConstructedVMs.Length, pipeline.Count);
        }
        public void ListComputeNodesMaxCountTest()
        {
            // Verify default max count
            Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);

            // Setup cmdlet to list compute nodes without filters and a max count
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.PoolId = "testPool";
            cmdlet.Id = null;
            cmdlet.Filter = null;
            int maxCount = 2;
            cmdlet.MaxCount = maxCount;

            string[] idsOfConstructedComputeNodes = new[] { "computeNode1", "computeNode2", "computeNode3" };

            // Build some compute nodes instead of querying the service on a List ComputeNodes call
            ComputeNodeListResponse response = BatchTestHelpers.CreateComputeNodeListResponse(idsOfConstructedComputeNodes);
            RequestInterceptor interceptor = BatchTestHelpers.CreateNoOpInterceptor<ComputeNodeListParameters, ComputeNodeListResponse>(response);
            cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSComputeNode> pipeline = new List<PSComputeNode>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSComputeNode>()))
                .Callback<object>(c => pipeline.Add((PSComputeNode)c));

            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(idsOfConstructedComputeNodes.Length, pipeline.Count);
        }
        public void ListBatchVMsByODataFilterTest()
        {
            // Setup cmdlet to list vms using an OData filter.
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.PoolName = "pool";
            cmdlet.Name = null;
            cmdlet.Filter = "state -eq 'idle'";

            string[] namesOfConstructedVMs = new[] { "vm1", "vm2" };

            // Build some vms instead of querying the service on a ListTVMs call
            YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) =>
            {
                if (request is ListTVMsRequest)
                {
                    ListTVMsResponse response = BatchTestHelpers.CreateListTVMsResponse(namesOfConstructedVMs);
                    Task<object> task = Task<object>.Factory.StartNew(() => { return response; });
                    return task;
                }
                return null;
            });
            cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSVM> pipeline = new List<PSVM>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSVM>()))
                .Callback<object>(v => pipeline.Add((PSVM)v));

            cmdlet.ExecuteCmdlet();

            // Verify that the cmdlet wrote the constructed vms to the pipeline
            Assert.Equal(2, pipeline.Count);
            int vmCount = 0;
            foreach (PSVM v in pipeline)
            {
                Assert.True(namesOfConstructedVMs.Contains(v.Name));
                vmCount++;
            }
            Assert.Equal(namesOfConstructedVMs.Length, vmCount);
        }
        public void ListBatchJobSchedulesWithoutFiltersTest()
        {
            // Setup cmdlet to list job schedules without filters
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.Id = null;
            cmdlet.Filter = null;

            string[] idsOfConstructedJobSchedules = new[] { "id1", "id2", "id3" };

            // Build some CloudJobSchedules instead of querying the service on a List CloudJobSchedules call
            CloudJobScheduleListResponse response = BatchTestHelpers.CreateCloudJobScheduleListResponse(idsOfConstructedJobSchedules);
            RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CloudJobScheduleListParameters, CloudJobScheduleListResponse>(response);
            cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSCloudJobSchedule> pipeline = new List<PSCloudJobSchedule>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSCloudJobSchedule>()))
                .Callback<object>(j => pipeline.Add((PSCloudJobSchedule)j));

            cmdlet.ExecuteCmdlet();

            // Verify that the cmdlet wrote the constructed job schedules to the pipeline
            Assert.Equal(3, pipeline.Count);
            int jobScheduleCount = 0;
            foreach (PSCloudJobSchedule j in pipeline)
            {
                Assert.True(idsOfConstructedJobSchedules.Contains(j.Id));
                jobScheduleCount++;
            }
            Assert.Equal(idsOfConstructedJobSchedules.Length, jobScheduleCount);
        }
        public void ListBatchCertificatesWithoutFiltersTest()
        {
            // Setup cmdlet to list certs without filters
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
            cmdlet.BatchContext = context;
            cmdlet.ThumbprintAlgorithm = null;
            cmdlet.Thumbprint = null;
            cmdlet.Filter = null;

            string[] thumbprintsOfConstructedCerts = new[] { "12345", "67890", "ABCDE" };

            // Build some Certificates instead of querying the service on a List Certificates call
            CertificateListResponse response = BatchTestHelpers.CreateCertificateListResponse(thumbprintsOfConstructedCerts);
            RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CertificateListParameters, CertificateListResponse>(response);
            cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List<PSCertificate> pipeline = new List<PSCertificate>();
            commandRuntimeMock.Setup(r =>
                r.WriteObject(It.IsAny<PSCertificate>()))
                .Callback<object>(c => pipeline.Add((PSCertificate)c));

            cmdlet.ExecuteCmdlet();

            // Verify that the cmdlet wrote the constructed certs to the pipeline
            Assert.Equal(3, pipeline.Count);
            int poolCount = 0;
            foreach (PSCertificate c in pipeline)
            {
                Assert.True(thumbprintsOfConstructedCerts.Contains(c.Thumbprint));
                poolCount++;
            }
            Assert.Equal(thumbprintsOfConstructedCerts.Length, poolCount);
        }