|
| 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 | + |
| 15 | +using System; |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Management.Automation; |
| 18 | +using System.Threading; |
| 19 | +using System.Threading.Tasks; |
| 20 | +using Microsoft.Azure.Management.Intune; |
| 21 | +using Microsoft.Azure.Management.Intune.Models; |
| 22 | +using Microsoft.Rest.Azure; |
| 23 | +using Microsoft.WindowsAzure.Commands.ScenarioTest; |
| 24 | +using Moq; |
| 25 | +using Newtonsoft.Json; |
| 26 | +using Xunit; |
| 27 | +using Microsoft.Azure.Commands.Intune; |
| 28 | + |
| 29 | +namespace Commands.Intune.Test.UnitTests.Operations |
| 30 | +{ |
| 31 | + public class GetIntuneOperationResultsCmdletTests |
| 32 | + { |
| 33 | + private Mock<IIntuneResourceManagementClient> intuneClientMock; |
| 34 | + private Mock<ICommandRuntime> commandRuntimeMock; |
| 35 | + private GetIntuneOperationResultsCmdlet cmdlet; |
| 36 | + private Location expectedLocation; |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// C'tor for GetIntuneApplicationCmdletTests class. |
| 40 | + /// </summary> |
| 41 | + public GetIntuneOperationResultsCmdletTests() |
| 42 | + { |
| 43 | + commandRuntimeMock = new Mock<ICommandRuntime>(); |
| 44 | + intuneClientMock = new Mock<IIntuneResourceManagementClient>(); |
| 45 | + |
| 46 | + cmdlet = new GetIntuneOperationResultsCmdlet(); |
| 47 | + this.cmdlet.CommandRuntime = commandRuntimeMock.Object; |
| 48 | + this.cmdlet.IntuneClient = intuneClientMock.Object; |
| 49 | + |
| 50 | + // Set-up mock Location and mock the underlying service API method |
| 51 | + AzureOperationResponse<Location> resLocation = new AzureOperationResponse<Location>(); |
| 52 | + expectedLocation = new Location("mockHostName"); |
| 53 | + resLocation.Body = expectedLocation; |
| 54 | + |
| 55 | + intuneClientMock.Setup(f => f.GetLocationByHostNameWithHttpMessagesAsync(It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>())) |
| 56 | + .Returns(Task.FromResult(resLocation)); |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Test to return valid item. |
| 61 | + /// </summary> |
| 62 | + [Fact] |
| 63 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 64 | + public void GetIntuneOperationResults_ReturnsValidItem_Test() |
| 65 | + { |
| 66 | + // Set up the expected App |
| 67 | + string expectedOperationResultsBody = "{\r\n \"value\": [\r\n {\r\n \"id\": \"/providers/Microsoft.Intune/locations/fef.msua06/operationResults/b2a254ae-ca91-4086-b6cd-575e5dbc6698\",\r\n \"name\": \"b2a254ae-ca91-4086-b6cd-575e5dbc6698\",\r\n \"type\": \"Microsoft.Intune/locations/operationResults\",\r\n \"properties\": {\r\n \"friendlyName\": \"Wipe\",\r\n \"category\": \"ApplicationManagement\",\r\n \"lastModifiedTime\": \"2015-12-03T00:15:48.4394665\",\r\n \"state\": \"pending\",\r\n \"operationMetadata\": [\r\n {\r\n \"name\": \"app\",\r\n \"value\": \"Word\"\r\n },\r\n {\r\n \"name\": \"userId\",\r\n \"value\": \"d8aeb100-6e17-4cf7-bbda-169000d03c1e\"\r\n },\r\n {\r\n \"name\": \"userName\",\r\n \"value\": \"Joe Admin\"\r\n },\r\n {\r\n \"name\": \"deviceType\",\r\n \"value\": \"iPad\"\r\n }\r\n ]\r\n }\r\n }\r\n ]\r\n}"; |
| 68 | + |
| 69 | + var expectedRespose = new AzureOperationResponse<IPage<OperationResult>>(); |
| 70 | + var expectedResultPage = new Page<OperationResult>(); |
| 71 | + |
| 72 | + expectedResultPage = JsonConvert.DeserializeObject<Page<OperationResult>>(expectedOperationResultsBody, intuneClientMock.Object.DeserializationSettings); |
| 73 | + |
| 74 | + expectedRespose.Body = expectedResultPage; |
| 75 | + |
| 76 | + // Set up the mock methods |
| 77 | + intuneClientMock.Setup(f => f.GetOperationResultsWithHttpMessagesAsync( |
| 78 | + expectedLocation.HostName, |
| 79 | + It.IsAny<string>(), |
| 80 | + It.IsAny<int?>(), |
| 81 | + It.IsAny<string>(), |
| 82 | + It.IsAny<Dictionary<string, List<string>>>(), |
| 83 | + It.IsAny<CancellationToken>())) |
| 84 | + .Returns(Task.FromResult(expectedRespose)); |
| 85 | + |
| 86 | + intuneClientMock.Setup(f => f.GetOperationResultsNextWithHttpMessagesAsync( |
| 87 | + It.IsAny<string>(), |
| 88 | + It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>())) |
| 89 | + .Returns(Task.FromResult(expectedRespose)); |
| 90 | + |
| 91 | + commandRuntimeMock.Setup(m => m.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())) |
| 92 | + .Returns(() => true); |
| 93 | + |
| 94 | + // Set cmdline args and execute the cmdlet |
| 95 | + this.cmdlet.ExecuteCmdlet(); |
| 96 | + |
| 97 | + // Verify the result |
| 98 | + commandRuntimeMock.Verify(f => f.WriteObject(expectedResultPage, true), Times.Once()); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments