PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ResourceManager/AzureBatch/Commands.Batch.Test/JobSchedules/GetBatchJobScheduleCommandTests.cs

https://gitlab.com/jslee1/azure-powershell
C# | 228 lines | 159 code | 39 blank | 30 comment | 0 complexity | 7af00f4430e4db48f291c79aea9dea90 MD5 | raw file
  1. // ----------------------------------------------------------------------------------
  2. //
  3. // Copyright Microsoft Corporation
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // ----------------------------------------------------------------------------------
  14. using Microsoft.Azure.Batch;
  15. using Microsoft.Azure.Batch.Protocol;
  16. using Microsoft.Azure.Commands.Batch.Models;
  17. using Microsoft.Rest.Azure;
  18. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  19. using Moq;
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Management.Automation;
  24. using System.Threading.Tasks;
  25. using Xunit;
  26. using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
  27. using ProxyModels = Microsoft.Azure.Batch.Protocol.Models;
  28. namespace Microsoft.Azure.Commands.Batch.Test.JobSchedules
  29. {
  30. public class GetBatchJobScheduleCommandTests : WindowsAzure.Commands.Test.Utilities.Common.RMTestBase
  31. {
  32. private GetBatchJobScheduleCommand cmdlet;
  33. private Mock<BatchClient> batchClientMock;
  34. private Mock<ICommandRuntime> commandRuntimeMock;
  35. public GetBatchJobScheduleCommandTests(Xunit.Abstractions.ITestOutputHelper output)
  36. {
  37. ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output));
  38. batchClientMock = new Mock<BatchClient>();
  39. commandRuntimeMock = new Mock<ICommandRuntime>();
  40. cmdlet = new GetBatchJobScheduleCommand()
  41. {
  42. CommandRuntime = commandRuntimeMock.Object,
  43. BatchClient = batchClientMock.Object,
  44. };
  45. }
  46. [Fact]
  47. [Trait(Category.AcceptanceType, Category.CheckIn)]
  48. public void GetBatchJobScheduleTest()
  49. {
  50. // Setup cmdlet to get a job schedule by id
  51. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  52. cmdlet.BatchContext = context;
  53. cmdlet.Id = "testJobSchedule";
  54. cmdlet.Filter = null;
  55. // Build a CloudJobSchedule instead of querying the service on a Get CloudJobSchedule call
  56. AzureOperationResponse<ProxyModels.CloudJobSchedule, ProxyModels.JobScheduleGetHeaders> response = BatchTestHelpers.CreateCloudJobScheduleGetResponse(cmdlet.Id);
  57. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  58. ProxyModels.JobScheduleGetOptions,
  59. AzureOperationResponse<ProxyModels.CloudJobSchedule, ProxyModels.JobScheduleGetHeaders>>(response);
  60. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  61. // Setup the cmdlet to write pipeline output to a list that can be examined later
  62. List<PSCloudJobSchedule> pipeline = new List<PSCloudJobSchedule>();
  63. commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSCloudJobSchedule>())).Callback<object>(j => pipeline.Add((PSCloudJobSchedule)j));
  64. cmdlet.ExecuteCmdlet();
  65. // Verify that the cmdlet wrote the job schedule returned from the OM to the pipeline
  66. Assert.Equal(1, pipeline.Count);
  67. Assert.Equal(cmdlet.Id, pipeline[0].Id);
  68. }
  69. [Fact]
  70. [Trait(Category.AcceptanceType, Category.CheckIn)]
  71. public void GetBatchJobScheduleODataTest()
  72. {
  73. // Setup cmdlet to get a single job schedule
  74. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  75. cmdlet.BatchContext = context;
  76. cmdlet.Id = "testJobSchedule";
  77. cmdlet.Select = "id,state";
  78. cmdlet.Expand = "stats";
  79. string requestSelect = null;
  80. string requestExpand = null;
  81. // Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used.
  82. AzureOperationResponse<ProxyModels.CloudJobSchedule, ProxyModels.JobScheduleGetHeaders> getResponse = BatchTestHelpers.CreateCloudJobScheduleGetResponse(cmdlet.Id);
  83. RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<ProxyModels.JobScheduleGetOptions, AzureOperationResponse<ProxyModels.CloudJobSchedule, ProxyModels.JobScheduleGetHeaders>>(getResponse);
  84. ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) =>
  85. {
  86. ProxyModels.JobScheduleGetOptions options = (ProxyModels.JobScheduleGetOptions)request.Options;
  87. requestSelect = options.Select;
  88. requestExpand = options.Expand;
  89. return Task.FromResult(response);
  90. });
  91. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor, responseInterceptor };
  92. cmdlet.ExecuteCmdlet();
  93. Assert.Equal(cmdlet.Select, requestSelect);
  94. Assert.Equal(cmdlet.Expand, requestExpand);
  95. }
  96. [Fact]
  97. [Trait(Category.AcceptanceType, Category.CheckIn)]
  98. public void ListBatchJobSchedulesODataTest()
  99. {
  100. // Setup cmdlet to list job schedules using an OData filter
  101. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  102. cmdlet.BatchContext = context;
  103. cmdlet.Id = null;
  104. cmdlet.Filter = "startswith(id,'test')";
  105. cmdlet.Select = "id,state";
  106. cmdlet.Expand = "stats";
  107. string requestFilter = null;
  108. string requestSelect = null;
  109. string requestExpand = null;
  110. AzureOperationResponse<IPage<ProxyModels.CloudJobSchedule>, ProxyModels.JobScheduleListHeaders> response = BatchTestHelpers.CreateGenericAzureOperationListResponse<ProxyModels.CloudJobSchedule, ProxyModels.JobScheduleListHeaders>();
  111. Action<BatchRequest<ProxyModels.JobScheduleListOptions, AzureOperationResponse<IPage<ProxyModels.CloudJobSchedule>, ProxyModels.JobScheduleListHeaders>>> listJobScheduleAction =
  112. (request) =>
  113. {
  114. ProxyModels.JobScheduleListOptions options = request.Options;
  115. requestFilter = options.Filter;
  116. requestSelect = options.Select;
  117. requestExpand = options.Expand;
  118. };
  119. RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<ProxyModels.JobScheduleListOptions, AzureOperationResponse<IPage<ProxyModels.CloudJobSchedule>, ProxyModels.JobScheduleListHeaders>>(responseToUse: response, requestAction: listJobScheduleAction);
  120. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor };
  121. cmdlet.ExecuteCmdlet();
  122. Assert.Equal(cmdlet.Filter, requestFilter);
  123. Assert.Equal(cmdlet.Select, requestSelect);
  124. Assert.Equal(cmdlet.Expand, requestExpand);
  125. }
  126. [Fact]
  127. [Trait(Category.AcceptanceType, Category.CheckIn)]
  128. public void ListBatchJobSchedulesWithoutFiltersTest()
  129. {
  130. // Setup cmdlet to list job schedules without filters
  131. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  132. cmdlet.BatchContext = context;
  133. cmdlet.Id = null;
  134. cmdlet.Filter = null;
  135. string[] idsOfConstructedJobSchedules = new[] { "id1", "id2", "id3" };
  136. // Build some CloudJobSchedules instead of querying the service on a List CloudJobSchedules call
  137. AzureOperationResponse<IPage<ProxyModels.CloudJobSchedule>, ProxyModels.JobScheduleListHeaders> response = BatchTestHelpers.CreateCloudJobScheduleListResponse(idsOfConstructedJobSchedules);
  138. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<ProxyModels.JobScheduleListOptions, AzureOperationResponse<IPage<ProxyModels.CloudJobSchedule>, ProxyModels.JobScheduleListHeaders>>(response);
  139. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  140. // Setup the cmdlet to write pipeline output to a list that can be examined later
  141. List<PSCloudJobSchedule> pipeline = new List<PSCloudJobSchedule>();
  142. commandRuntimeMock.Setup(r =>
  143. r.WriteObject(It.IsAny<PSCloudJobSchedule>()))
  144. .Callback<object>(j => pipeline.Add((PSCloudJobSchedule)j));
  145. cmdlet.ExecuteCmdlet();
  146. // Verify that the cmdlet wrote the constructed job schedules to the pipeline
  147. Assert.Equal(3, pipeline.Count);
  148. int jobScheduleCount = 0;
  149. foreach (PSCloudJobSchedule j in pipeline)
  150. {
  151. Assert.True(idsOfConstructedJobSchedules.Contains(j.Id));
  152. jobScheduleCount++;
  153. }
  154. Assert.Equal(idsOfConstructedJobSchedules.Length, jobScheduleCount);
  155. }
  156. [Fact]
  157. [Trait(Category.AcceptanceType, Category.CheckIn)]
  158. public void ListJobSchedulesMaxCountTest()
  159. {
  160. // Verify default max count
  161. Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);
  162. // Setup cmdlet to list job schedules without filters and a max count
  163. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  164. cmdlet.BatchContext = context;
  165. cmdlet.Id = null;
  166. cmdlet.Filter = null;
  167. int maxCount = 2;
  168. cmdlet.MaxCount = maxCount;
  169. string[] idsOfConstructedJobSchedules = new[] { "id1", "id2", "id3" };
  170. // Build some CloudJobSchedules instead of querying the service on a List CloudJobSchedules call
  171. AzureOperationResponse<IPage<ProxyModels.CloudJobSchedule>, ProxyModels.JobScheduleListHeaders> response = BatchTestHelpers.CreateCloudJobScheduleListResponse(idsOfConstructedJobSchedules);
  172. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<ProxyModels.JobScheduleListOptions, AzureOperationResponse<IPage<ProxyModels.CloudJobSchedule>, ProxyModels.JobScheduleListHeaders>>(response);
  173. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  174. // Setup the cmdlet to write pipeline output to a list that can be examined later
  175. List<PSCloudJobSchedule> pipeline = new List<PSCloudJobSchedule>();
  176. commandRuntimeMock.Setup(r =>
  177. r.WriteObject(It.IsAny<PSCloudJobSchedule>()))
  178. .Callback<object>(j => pipeline.Add((PSCloudJobSchedule)j));
  179. cmdlet.ExecuteCmdlet();
  180. // Verify that the max count was respected
  181. Assert.Equal(maxCount, pipeline.Count);
  182. // Verify setting max count <= 0 doesn't return nothing
  183. cmdlet.MaxCount = -5;
  184. pipeline.Clear();
  185. cmdlet.ExecuteCmdlet();
  186. Assert.Equal(idsOfConstructedJobSchedules.Length, pipeline.Count);
  187. }
  188. }
  189. }