Skip to content

Commit 7ab5585

Browse files
committed
Updating unit tests to work with xunit, mocking rdfe registering, minor fixes
1 parent 879346e commit 7ab5585

File tree

11 files changed

+251
-116
lines changed

11 files changed

+251
-116
lines changed

src/ServiceManagement/RemoteApp/Commands.RemoteApp.Tests/Collection/RemoteAppCollection.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ public void RemoveCollection()
285285
public void GetRegionList()
286286
{
287287
List<Region> regionList = null;
288-
List<string> regions = null;
289288
GetAzureRemoteAppLocation mockCmdlet = SetUpTestCommon<GetAzureRemoteAppLocation>();
290289

291290
// Setup the environment for testing this cmdlet
@@ -307,7 +306,7 @@ public void GetRegionList()
307306
regionList = MockObject.ConvertList<Region>(mockCmdlet.runTime().OutputPipeline);
308307
Assert.NotNull(regionList);
309308

310-
Assert.True(MockObject.HasExpectedResults<string>(regions, MockObject.ContainsExpectedRegion), // This is expecting a List<string> instead of LocalModels.RegionList
309+
Assert.True(MockObject.HasExpectedResults<Region>(regionList, MockObject.ContainsExpectedRegion),
311310
"The actual result does not match the expected."
312311
);
313312

src/ServiceManagement/RemoteApp/Commands.RemoteApp.Tests/Commands.RemoteApp.Test.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<ItemGroup>
4141
<Compile Include="Collection\RemoteAppCollection.cs" />
4242
<Compile Include="Common\CollectionObjects.cs" />
43+
<Compile Include="Common\MockCommandRuntime.cs" />
4344
<Compile Include="Common\MockObject.cs" />
4445
<Compile Include="Common\RemoteApp.cs" />
4546
<Compile Include="Common\RemoteAppClient.cs" />
@@ -109,6 +110,10 @@
109110
<!--<Reference Include="Microsoft.WindowsAzure.Common">
110111
<HintPath>..\..\..\packages\Microsoft.WindowsAzure.Common.1.4.0\lib\net45\Microsoft.WindowsAzure.Common.dll</HintPath>
111112
</Reference>-->
113+
<Reference Include="Microsoft.WindowsAzure.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
114+
<SpecificVersion>False</SpecificVersion>
115+
<HintPath>..\..\..\packages\Microsoft.WindowsAzure.Management.4.1.0\lib\net40\Microsoft.WindowsAzure.Management.dll</HintPath>
116+
</Reference>
112117
<Reference Include="Moq, Version=4.2.1402.2112, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
113118
<HintPath>..\..\..\packages\Moq.4.2.1402.2112\lib\net40\Moq.dll</HintPath>
114119
<SpecificVersion>False</SpecificVersion>

src/ServiceManagement/RemoteApp/Commands.RemoteApp.Tests/Common/CollectionObjects.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,13 @@ public static bool ContainsExpectedCollection(List<Collection> expectedResult, C
261261
return isIdentical;
262262
}
263263

264-
public static bool ContainsExpectedRegion(List<string> expectedResult, string actual)
264+
public static bool ContainsExpectedRegion(List<Region> expectedResult, Region actual)
265265
{
266266
bool isIdentical = false;
267-
foreach (string expected in expectedResult)
267+
foreach (Region expected in expectedResult)
268268
{
269-
isIdentical = expected == actual;
269+
isIdentical = expected.DisplayName == actual.DisplayName;
270+
isIdentical &= expected.Name == actual.Name;
270271
if (isIdentical)
271272
{
272273
break;
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
16+
namespace Microsoft.Azure.Commands.Test.RemoteApp.Common
17+
{
18+
using System.Collections.Generic;
19+
using System.Diagnostics.CodeAnalysis;
20+
using System.Management.Automation;
21+
22+
public class MockCommandRuntime : ICommandRuntime
23+
{
24+
public List<ErrorRecord> ErrorStream = new List<ErrorRecord>();
25+
public List<object> OutputPipeline = new List<object>();
26+
public List<string> WarningStream = new List<string>();
27+
public List<string> VerboseStream = new List<string>();
28+
public List<string> DebugStream = new List<string>();
29+
30+
public override string ToString()
31+
{
32+
return "MockCommand";
33+
}
34+
35+
[SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
36+
Justification = "Tests should not access this property")]
37+
public PSTransactionContext CurrentPSTransaction
38+
{
39+
get { throw new System.NotImplementedException(); }
40+
}
41+
42+
[SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
43+
Justification = "Tests should not access this property")]
44+
public System.Management.Automation.Host.PSHost Host
45+
{
46+
get { throw new System.NotImplementedException(); }
47+
}
48+
49+
public bool ShouldContinue(string query, string caption, ref bool yesToAll, ref bool noToAll)
50+
{
51+
return true;
52+
}
53+
54+
public bool ShouldContinue(string query, string caption)
55+
{
56+
return true;
57+
}
58+
59+
public bool ShouldProcess(string verboseDescription, string verboseWarning, string caption, out ShouldProcessReason shouldProcessReason)
60+
{
61+
throw new System.NotImplementedException();
62+
}
63+
64+
public bool ShouldProcess(string verboseDescription, string verboseWarning, string caption)
65+
{
66+
return true;
67+
}
68+
69+
public bool ShouldProcess(string target, string action)
70+
{
71+
return true;
72+
}
73+
74+
public bool ShouldProcess(string target)
75+
{
76+
return true;
77+
}
78+
79+
public void ThrowTerminatingError(ErrorRecord errorRecord)
80+
{
81+
throw new System.NotImplementedException();
82+
}
83+
84+
public bool TransactionAvailable()
85+
{
86+
throw new System.NotImplementedException();
87+
}
88+
89+
public void WriteCommandDetail(string text)
90+
{
91+
throw new System.NotImplementedException();
92+
}
93+
94+
public void WriteDebug(string text)
95+
{
96+
DebugStream.Add(text);
97+
}
98+
99+
public void WriteError(ErrorRecord errorRecord)
100+
{
101+
ErrorStream.Add(errorRecord);
102+
}
103+
104+
public void WriteObject(object sendToPipeline, bool enumerateCollection)
105+
{
106+
System.Collections.IEnumerable enumerable = LanguagePrimitives.GetEnumerable(sendToPipeline);
107+
if (enumerable != null)
108+
{
109+
foreach (object o in enumerable)
110+
{
111+
OutputPipeline.Add(o);
112+
}
113+
}
114+
else
115+
{
116+
OutputPipeline.Add(sendToPipeline);
117+
}
118+
119+
}
120+
121+
public void WriteObject(object sendToPipeline)
122+
{
123+
OutputPipeline.Add(sendToPipeline);
124+
}
125+
126+
public void WriteProgress(long sourceId, ProgressRecord progressRecord)
127+
{
128+
// Do nothing
129+
}
130+
131+
public void WriteProgress(ProgressRecord progressRecord)
132+
{
133+
// Do nothing
134+
}
135+
136+
public void WriteVerbose(string text)
137+
{
138+
VerboseStream.Add(text);
139+
}
140+
141+
public void WriteWarning(string text)
142+
{
143+
this.WarningStream.Add(text);
144+
}
145+
146+
/// <summary>
147+
/// Clears all command runtime pipelines.
148+
/// </summary>
149+
public void ResetPipelines()
150+
{
151+
ErrorStream.Clear();
152+
OutputPipeline.Clear();
153+
WarningStream.Clear();
154+
VerboseStream.Clear();
155+
}
156+
}
157+
}

src/ServiceManagement/RemoteApp/Commands.RemoteApp.Tests/Common/RemoteApp.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public static int SetUpDefaultRemoteAppStartMenu(Mock<IRemoteAppManagementClient
149149
StartMenuApplication mockApp = new StartMenuApplication()
150150
{
151151
Name = app.Name,
152-
StartMenuAppId = null, // Yadav has a fix for this it should be the friendly name AppAlias
152+
StartMenuAppId = app.StartMenuAppId,
153153
VirtualPath = app.VirtualPath,
154154
CommandLineArguments = app.CommandLineArguments,
155155
IconUri = app.IconUri
@@ -163,7 +163,7 @@ public static int SetUpDefaultRemoteAppStartMenu(Mock<IRemoteAppManagementClient
163163
return mockStartMenuList.Count;
164164
}
165165

166-
public static int SetUpDefaultRemoteAppStartMenuByName(Mock<IRemoteAppManagementClient> clientMock, string collectionName, string alias)
166+
public static int SetUpDefaultRemoteAppStartMenuByName(Mock<IRemoteAppManagementClient> clientMock, string collectionName, string programName)
167167
{
168168
ISetup<IRemoteAppManagementClient, Task<GetStartMenuApplicationResult>> setup = null;
169169

@@ -175,11 +175,11 @@ public static int SetUpDefaultRemoteAppStartMenuByName(Mock<IRemoteAppManagement
175175

176176
response.Result = new StartMenuApplication()
177177
{
178-
Name = "Mohoro RemoteApp1",
179-
StartMenuAppId = null, // Yadav has a fix for this it should be the friendly name AppAlias
180-
VirtualPath = @"C:\Application\RemoteApp3.exe",
178+
Name = programName,
179+
StartMenuAppId = "123456",
180+
VirtualPath = @"C:\Application\" + programName,
181181
CommandLineArguments = "Arg1, Arg2, Arg3",
182-
IconUri = @"C:\Application\RemoteApp3.exe",
182+
IconUri = @"C:\Application\" + programName,
183183
};
184184

185185
mockStartMenuList = new List<StartMenuApplication>()
@@ -194,7 +194,7 @@ public static int SetUpDefaultRemoteAppStartMenuByName(Mock<IRemoteAppManagement
194194
}
195195
};
196196

197-
setup = clientMock.Setup(c => c.Publishing.StartMenuApplicationAsync(collectionName, alias, It.IsAny<CancellationToken>()));
197+
setup = clientMock.Setup(c => c.Publishing.StartMenuApplicationAsync(collectionName, programName, It.IsAny<CancellationToken>()));
198198
setup.Returns(Task.Factory.StartNew(() => response));
199199

200200
return mockStartMenuList.Count;

src/ServiceManagement/RemoteApp/Commands.RemoteApp.Tests/Common/RemoteAppClient.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ namespace Microsoft.Azure.Commands.Test.RemoteApp
1717
using Microsoft.Azure.Management.RemoteApp;
1818
using Microsoft.Azure.Management.RemoteApp.Cmdlets;
1919
using Microsoft.WindowsAzure;
20-
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
20+
using Microsoft.Azure.Commands.Test.RemoteApp.Common;
2121
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
2222
using Moq;
23+
using Moq.Language.Flow;
2324
using System;
25+
using System.Threading;
26+
using System.Threading.Tasks;
27+
2428

2529
public class RemoteAppClientCredentials : SubscriptionCloudCredentials
2630
{
@@ -59,23 +63,38 @@ public abstract class RemoteAppClientTest : TestBase
5963

6064
public MockCommandRuntime mockCommandRuntime { get; private set; }
6165

62-
protected Mock<IRemoteAppManagementClient> remoteAppManagementClientMock { get; private set; }
66+
protected Mock<IRemoteAppManagementClient> remoteAppManagementClientMock { get; set; }
67+
68+
protected Mock<Microsoft.WindowsAzure.Management.ManagementClient> mgmtClient {get; set; }
6369

6470
protected RemoteAppClientTest()
6571
{
6672
mockCommandRuntime = new MockCommandRuntime();
6773
remoteAppManagementClientMock = new Mock<IRemoteAppManagementClient>();
74+
remoteAppManagementClientMock.SetupProperty(c => c.RdfeNamespace, "remoteapp");
6875
}
6976

7077
protected T SetUpTestCommon<T>() where T : RdsCmdlet, new()
7178
{
7279
T RemoteAppCmdlet = null;
80+
ISetup<Microsoft.WindowsAzure.Management.ManagementClient, Task<AzureOperationResponse>> setup = null;
81+
82+
AzureOperationResponse response = new AzureOperationResponse()
83+
{
84+
RequestId = "1",
85+
StatusCode = System.Net.HttpStatusCode.OK
86+
};
87+
mgmtClient = new Mock<WindowsAzure.Management.ManagementClient>();
88+
setup = mgmtClient.Setup(c => c.Subscriptions.RegisterResourceAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()));
89+
setup.Returns(Task.Factory.StartNew(() => response));
7390

7491
RemoteAppCmdlet = new T()
7592
{
7693
CommandRuntime = mockCommandRuntime,
77-
Client = remoteAppManagementClientMock.Object
94+
Client = remoteAppManagementClientMock.Object,
95+
MgmtClient = mgmtClient.Object
7896
};
97+
7998
return RemoteAppCmdlet;
8099
}
81100
}

src/ServiceManagement/RemoteApp/Commands.RemoteApp.Tests/RemoteProgram/RemoteAppProgram.cs

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ public void GetRemoteAppByName()
7979

8080
// Required parameters for this test
8181
mockCmdlet.CollectionName = collectionName;
82-
mockCmdlet.RemoteAppProgram = remoteApplication;
82+
mockCmdlet.Alias = "1340fc";
8383

8484
// Setup the environment for testing this cmdlet
8585
MockObject.SetUpDefaultRemoteAppCollectionByName(remoteAppManagementClientMock, mockCmdlet.CollectionName);
86-
countOfExpectedApps = MockObject.SetUpDefaultRemoteAppApplicationsByName(remoteAppManagementClientMock, mockCmdlet.CollectionName, mockCmdlet.RemoteAppProgram);
86+
countOfExpectedApps = MockObject.SetUpDefaultRemoteAppApplicationsByName(remoteAppManagementClientMock, mockCmdlet.CollectionName, mockCmdlet.Alias);
8787

8888
mockCmdlet.ResetPipelines();
8989

@@ -160,52 +160,5 @@ public void GetAllStartMenuApplication()
160160

161161
Log("The test for Get-AzureRemoteAppCollection with {0} collections completed successfully", countOfExpectedApps);
162162
}
163-
164-
[Fact]
165-
public void GetStartMenuApplicationByName()
166-
{
167-
List<StartMenuApplication> remoteApps = null;
168-
int countOfExpectedApps = 1;
169-
GetStartMenuProgram mockCmdlet = SetUpTestCommon<GetStartMenuProgram>();
170-
171-
// Required parameters for this test
172-
mockCmdlet.CollectionName = collectionName;
173-
mockCmdlet.ProgramName = "notepad";
174-
175-
// Setup the environment for testing this cmdlet
176-
MockObject.SetUpDefaultRemoteAppCollectionByName(remoteAppManagementClientMock, mockCmdlet.CollectionName);
177-
MockObject.SetUpDefaultRemoteAppStartMenu(remoteAppManagementClientMock, mockCmdlet.CollectionName);
178-
countOfExpectedApps = MockObject.SetUpDefaultRemoteAppStartMenuByName(remoteAppManagementClientMock, mockCmdlet.CollectionName, mockCmdlet.ProgramName);
179-
180-
mockCmdlet.ResetPipelines();
181-
182-
Log("Calling Get-AzureRemoteAppCollection which should have {0} collections.", countOfExpectedApps);
183-
184-
mockCmdlet.ExecuteCmdlet();
185-
if (mockCmdlet.runTime().ErrorStream.Count != 0)
186-
{
187-
Assert.True(false,
188-
String.Format("Get-AzureRemoteAppCollection returned the following error {0}.",
189-
mockCmdlet.runTime().ErrorStream[0].Exception.Message
190-
)
191-
);
192-
}
193-
194-
remoteApps = MockObject.ConvertList<StartMenuApplication>(mockCmdlet.runTime().OutputPipeline);
195-
Assert.NotNull(remoteApps);
196-
197-
Assert.True(remoteApps.Count == countOfExpectedApps,
198-
String.Format("The expected number of collections returned {0} does not match the actual {1}.",
199-
countOfExpectedApps,
200-
remoteApps.Count
201-
)
202-
);
203-
204-
Assert.True(MockObject.HasExpectedResults<StartMenuApplication>(remoteApps, MockObject.ContainsExpectedStartMenu),
205-
"The actual result does not match the expected."
206-
);
207-
208-
Log("The test for Get-AzureRemoteAppCollection with {0} collections completed successfully", countOfExpectedApps);
209-
}
210163
}
211164
}

0 commit comments

Comments
 (0)