PageRenderTime 55ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/jslee1/azure-powershell
C# | 584 lines | 455 code | 97 blank | 32 comment | 37 complexity | ddba82d6ff1cb18435556337784fab4e 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.WindowsAzure.Commands.ScenarioTest;
  15. namespace Microsoft.WindowsAzure.Commands.RemoteApp.Test
  16. {
  17. using Common;
  18. using Microsoft.WindowsAzure.Management.RemoteApp;
  19. using Microsoft.WindowsAzure.Management.RemoteApp.Cmdlets;
  20. using Microsoft.WindowsAzure.Management.RemoteApp.Models;
  21. using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
  22. using Moq;
  23. using Moq.Language.Flow;
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Linq;
  27. using System.Management.Automation;
  28. using System.Threading;
  29. using System.Threading.Tasks;
  30. using Xunit;
  31. // Get-AzureRemoteAppCollectionUsageDetails, Get-AzureRemoteAppCollectionUsageSummary,
  32. public class RemoteAppCollectionTest : RemoteAppClientTest
  33. {
  34. [Fact]
  35. [Trait(Category.AcceptanceType, Category.CheckIn)]
  36. public void GetAllCollections()
  37. {
  38. int countOfExpectedCollections = 0;
  39. GetAzureRemoteAppCollection mockCmdlet = SetUpTestCommon<GetAzureRemoteAppCollection>();
  40. // Setup the environment for testing this cmdlet
  41. countOfExpectedCollections = MockObject.SetUpDefaultRemoteAppCollection(remoteAppManagementClientMock, collectionName);
  42. mockCmdlet.ResetPipelines();
  43. Log("Calling Get-AzureRemoteAppCollection which should have {0} collections.", countOfExpectedCollections);
  44. mockCmdlet.ExecuteCmdlet();
  45. if (mockCmdlet.runTime().ErrorStream.Count != 0)
  46. {
  47. Assert.True(false,
  48. String.Format("Get-AzureRemoteAppCollection returned the following error {0}.",
  49. mockCmdlet.runTime().ErrorStream[0].Exception.Message
  50. )
  51. );
  52. }
  53. List<Collection> collections = MockObject.ConvertList<Collection>(mockCmdlet.runTime().OutputPipeline);
  54. Assert.NotNull(collections);
  55. Assert.True(collections.Count == countOfExpectedCollections,
  56. String.Format("The expected number of collections returned {0} does not match the actual {1}.",
  57. countOfExpectedCollections,
  58. collections.Count
  59. )
  60. );
  61. Assert.True(MockObject.HasExpectedResults<Collection>(collections, MockObject.ContainsExpectedCollection),
  62. "The actual result does not match the expected."
  63. );
  64. Log("The test for Get-AzureRemoteAppCollection with {0} collections completed successfully", countOfExpectedCollections);
  65. }
  66. [Fact]
  67. [Trait(Category.AcceptanceType, Category.CheckIn)]
  68. public void GetCollectionsByName()
  69. {
  70. int countOfExpectedCollections = 1;
  71. GetAzureRemoteAppCollection mockCmdlet = SetUpTestCommon<GetAzureRemoteAppCollection>();
  72. // Required parameters for this test
  73. mockCmdlet.CollectionName = collectionName;
  74. // Setup the environment for testing this cmdlet
  75. MockObject.SetUpDefaultRemoteAppCollectionByName(remoteAppManagementClientMock, collectionName);
  76. mockCmdlet.ResetPipelines();
  77. Log("Calling Get-AzureRemoteAppCollection to get this collection {0}.", mockCmdlet.CollectionName);
  78. mockCmdlet.ExecuteCmdlet();
  79. if (mockCmdlet.runTime().ErrorStream.Count != 0)
  80. {
  81. Assert.True(false,
  82. String.Format("Get-AzureRemoteAppUser returned the following error {0}.",
  83. mockCmdlet.runTime().ErrorStream[0].Exception.Message
  84. )
  85. );
  86. }
  87. List<Collection> collections = MockObject.ConvertList<Collection>(mockCmdlet.runTime().OutputPipeline);
  88. Assert.NotNull(collections);
  89. Assert.True(collections.Count == countOfExpectedCollections,
  90. String.Format("The expected number of collections returned {0} does not match the actual {1}.",
  91. countOfExpectedCollections,
  92. collections.Count
  93. )
  94. );
  95. Assert.True(MockObject.HasExpectedResults<Collection>(collections, MockObject.ContainsExpectedCollection),
  96. "The actual result does not match the expected."
  97. );
  98. }
  99. [Fact]
  100. [Trait(Category.AcceptanceType, Category.CheckIn)]
  101. public void AddCollection()
  102. {
  103. List<TrackingResult> trackingIds = null;
  104. int countOfExpectedCollections = 0;
  105. NewAzureRemoteAppCollection mockCmdlet = SetUpTestCommon<NewAzureRemoteAppCollection>();
  106. // Required parameters for this test
  107. mockCmdlet.CollectionName = collectionName;
  108. mockCmdlet.Location = region;
  109. mockCmdlet.Plan = billingPlan;
  110. mockCmdlet.ImageName = templateName;
  111. mockCmdlet.Description = description;
  112. mockCmdlet.CustomRdpProperty = customRDPString;
  113. // Setup the environment for testing this cmdlet
  114. countOfExpectedCollections = MockObject.SetUpDefaultRemoteAppCollectionCreate(remoteAppManagementClientMock, mockCmdlet.CollectionName, mockCmdlet.Location, mockCmdlet.Plan, mockCmdlet.ImageName, mockCmdlet.Description, mockCmdlet.CustomRdpProperty, trackingId);
  115. mockCmdlet.ResetPipelines();
  116. mockCmdlet.ExecuteCmdlet();
  117. if (mockCmdlet.runTime().ErrorStream.Count != 0)
  118. {
  119. Assert.True(false,
  120. String.Format("New-AzureRemoteAppCollection returned the following error {0}",
  121. mockCmdlet.runTime().ErrorStream[0].Exception.Message
  122. )
  123. );
  124. }
  125. trackingIds = MockObject.ConvertList<TrackingResult>(mockCmdlet.runTime().OutputPipeline);
  126. Assert.NotNull(trackingIds);
  127. Assert.True(trackingIds.Count == countOfExpectedCollections,
  128. String.Format("The expected number of collections returned {0} does not match the actual {1}",
  129. countOfExpectedCollections,
  130. trackingIds.Count
  131. )
  132. );
  133. Assert.True(MockObject.HasExpectedResults<TrackingResult>(trackingIds, MockObject.ContainsExpectedTrackingId),
  134. "The actual result does not match the expected."
  135. );
  136. }
  137. [Fact]
  138. [Trait(Category.AcceptanceType, Category.CheckIn)]
  139. public void UpdateCollection()
  140. {
  141. List<TrackingResult> trackingIds = null;
  142. int countOfExpectedCollections = 0;
  143. UpdateAzureRemoteAppCollection mockCmdlet = SetUpTestCommon<UpdateAzureRemoteAppCollection>();
  144. // Required parameters for this test
  145. mockCmdlet.CollectionName = collectionName;
  146. mockCmdlet.ImageName = templateName;
  147. // Setup the environment for testing this cmdlet
  148. MockObject.SetUpDefaultRemoteAppCollectionByName(remoteAppManagementClientMock, mockCmdlet.CollectionName);
  149. countOfExpectedCollections = MockObject.SetUpDefaultRemoteAppCollectionSet(remoteAppManagementClientMock, mockCmdlet.CollectionName, subscriptionId, String.Empty, mockCmdlet.ImageName, null, String.Empty, trackingId);
  150. mockCmdlet.ResetPipelines();
  151. mockCmdlet.ExecuteCmdlet();
  152. if (mockCmdlet.runTime().ErrorStream.Count != 0)
  153. {
  154. Assert.True(false,
  155. String.Format("New-AzureRemoteAppCollection returned the following error {0}",
  156. mockCmdlet.runTime().ErrorStream[0].Exception.Message
  157. )
  158. );
  159. }
  160. trackingIds = MockObject.ConvertList<TrackingResult>(mockCmdlet.runTime().OutputPipeline);
  161. Assert.NotNull(trackingIds);
  162. Assert.True(trackingIds.Count == countOfExpectedCollections,
  163. String.Format("The expected number of collections returned {0} does not match the actual {1}",
  164. countOfExpectedCollections,
  165. trackingIds.Count
  166. )
  167. );
  168. Assert.True(MockObject.HasExpectedResults<TrackingResult>(trackingIds, MockObject.ContainsExpectedTrackingId),
  169. "The actual result does not match the expected."
  170. );
  171. }
  172. [Fact]
  173. public void PatchCollectionTest()
  174. {
  175. UpdateAzureRemoteAppCollection mockCmdlet = SetUpTestCommon<UpdateAzureRemoteAppCollection>();
  176. string collectionName = "mycol";
  177. string expectedTrackingId = "2432145";
  178. String imageName = "my template image";
  179. CollectionUpdateDetails requestData = null;
  180. Collection expectedCollection = null;
  181. // Required parameters for this test
  182. mockCmdlet.CollectionName = collectionName;
  183. mockCmdlet.ImageName = imageName;
  184. requestData = new CollectionUpdateDetails()
  185. {
  186. TemplateImageName = mockCmdlet.ImageName
  187. };
  188. expectedCollection = new Collection()
  189. {
  190. Name = collectionName,
  191. Status = "Active"
  192. };
  193. PerfomrCollectionTestHelper(mockCmdlet, collectionName, expectedCollection, expectedTrackingId, requestData, true);
  194. }
  195. [Fact]
  196. [Trait(Category.AcceptanceType, Category.CheckIn)]
  197. public void SetCollection()
  198. {
  199. SetAzureRemoteAppCollection mockCmdlet = SetUpTestCommon<SetAzureRemoteAppCollection>();
  200. string collectionName = "mycol";
  201. string expectedTrackingId = "2432145";
  202. CollectionUpdateDetails requestData = null;
  203. Collection expectedCollection = null;
  204. // Required parameters for this test
  205. mockCmdlet.CollectionName = collectionName;
  206. mockCmdlet.Plan = billingPlan;
  207. requestData = new CollectionUpdateDetails()
  208. {
  209. PlanName = mockCmdlet.Plan,
  210. AdInfo = null
  211. };
  212. expectedCollection = new Collection()
  213. {
  214. Name = collectionName,
  215. Status = "Active"
  216. };
  217. PerfomrCollectionTestHelper(mockCmdlet, collectionName, expectedCollection, expectedTrackingId, requestData, false);
  218. }
  219. [Fact]
  220. public void SetCollectionCustomRdpPropertyTest()
  221. {
  222. SetAzureRemoteAppCollection mockCmdlet = SetUpTestCommon<SetAzureRemoteAppCollection>();
  223. string collectionName = "mycol";
  224. string expectedTrackingId = "fdadffdas";
  225. CollectionUpdateDetails requestData = null;
  226. Collection expectedCollection = null;
  227. // Required parameters for this test
  228. mockCmdlet.CollectionName = collectionName;
  229. mockCmdlet.CustomRdpProperty = "some:value:*";
  230. requestData = new CollectionUpdateDetails()
  231. {
  232. CustomRdpProperty = mockCmdlet.CustomRdpProperty
  233. };
  234. expectedCollection = new Collection()
  235. {
  236. Name = collectionName,
  237. Status = "Active"
  238. };
  239. PerfomrCollectionTestHelper(mockCmdlet, collectionName, expectedCollection, expectedTrackingId, requestData, false);
  240. }
  241. [Fact]
  242. public void SetCollectionDescriptionTest()
  243. {
  244. SetAzureRemoteAppCollection mockCmdlet = SetUpTestCommon<SetAzureRemoteAppCollection>();
  245. string collectionName = "mycol";
  246. string expectedTrackingId = "213145";
  247. CollectionUpdateDetails requestData = null;
  248. Collection expectedCollection = null;
  249. // Required parameters for this test
  250. mockCmdlet.CollectionName = collectionName;
  251. mockCmdlet.Description = "This is my test collection";
  252. requestData = new CollectionUpdateDetails()
  253. {
  254. Description = mockCmdlet.Description
  255. };
  256. expectedCollection = new Collection()
  257. {
  258. Name = collectionName,
  259. Status = "Active"
  260. };
  261. PerfomrCollectionTestHelper(mockCmdlet, collectionName, expectedCollection, expectedTrackingId, requestData, false);
  262. }
  263. private void PerformCollectionTestWithAdInfoHelper(
  264. RdsCmdlet mockCmdlet,
  265. string collectionName,
  266. Collection expectedCollection,
  267. String trackingId,
  268. CollectionUpdateDetails reqestData,
  269. bool forceRedeploy)
  270. {
  271. ISetup<IRemoteAppManagementClient, Task<CollectionResult>> setupGet = null;
  272. ISetup<IRemoteAppManagementClient, Task<OperationResultWithTrackingId>> setupSetApi = null;
  273. MockCommandRuntime cmdRuntime = null;
  274. IEnumerable<TrackingResult> trackingIds = null;
  275. // Setup the environment for testing this cmdlet
  276. setupGet = remoteAppManagementClientMock.Setup(c => c.Collections.GetAsync(collectionName, It.IsAny<CancellationToken>()));
  277. setupGet.Returns(Task.Factory.StartNew(
  278. () =>
  279. new CollectionResult()
  280. {
  281. Collection = expectedCollection,
  282. StatusCode = System.Net.HttpStatusCode.OK
  283. }));
  284. setupSetApi = remoteAppManagementClientMock.Setup(
  285. c => c.Collections.SetAsync(
  286. collectionName,
  287. forceRedeploy,
  288. false,
  289. It.Is<CollectionUpdateDetails>(col =>
  290. col.CustomRdpProperty == reqestData.CustomRdpProperty &&
  291. col.Description == reqestData.Description &&
  292. col.PlanName == reqestData.PlanName &&
  293. col.TemplateImageName == reqestData.TemplateImageName &&
  294. (col.AdInfo == null ||
  295. (reqestData.AdInfo != null &&
  296. col.AdInfo.DomainName == reqestData.AdInfo.DomainName &&
  297. col.AdInfo.OrganizationalUnit == reqestData.AdInfo.OrganizationalUnit &&
  298. col.AdInfo.UserName == reqestData.AdInfo.UserName &&
  299. col.AdInfo.Password == reqestData.AdInfo.Password)
  300. )
  301. ),
  302. It.IsAny<CancellationToken>()));
  303. setupSetApi.Returns(Task.Factory.StartNew(() => new OperationResultWithTrackingId()
  304. {
  305. TrackingId = trackingId
  306. }));
  307. mockCmdlet.ResetPipelines();
  308. mockCmdlet.ExecuteCmdlet();
  309. cmdRuntime = mockCmdlet.runTime();
  310. if (cmdRuntime.ErrorStream.Count > 0)
  311. {
  312. Assert.True(cmdRuntime.ErrorStream.Count == 0,
  313. String.Format("Set-AzureRemoteAppCollection returned the following error {0}",
  314. mockCmdlet.runTime().ErrorStream[0].Exception.Message));
  315. }
  316. trackingIds = LanguagePrimitives.GetEnumerable(mockCmdlet.runTime().OutputPipeline).Cast<TrackingResult>();
  317. Assert.NotNull(trackingIds);
  318. Assert.Equal(1, trackingIds.Count());
  319. Assert.True(trackingIds.Any(t => t.TrackingId == trackingId), "The actual result does not match the expected.");
  320. }
  321. private void PerfomrCollectionTestHelper(
  322. RdsCmdlet mockCmdlet,
  323. string collectionName,
  324. Collection expectedCollection,
  325. String trackingId,
  326. CollectionUpdateDetails reqestData,
  327. bool forceRedeploy)
  328. {
  329. ISetup<IRemoteAppManagementClient, Task<CollectionResult>> setupGet = null;
  330. ISetup<IRemoteAppManagementClient, Task<OperationResultWithTrackingId>> setupSetApi = null;
  331. MockCommandRuntime cmdRuntime = null;
  332. IEnumerable<TrackingResult> trackingIds = null;
  333. // Setup the environment for testing this cmdlet
  334. setupGet = remoteAppManagementClientMock.Setup(c => c.Collections.GetAsync(collectionName, It.IsAny<CancellationToken>()));
  335. setupGet.Returns(Task.Factory.StartNew(
  336. () =>
  337. new CollectionResult()
  338. {
  339. Collection = expectedCollection,
  340. StatusCode = System.Net.HttpStatusCode.OK
  341. }));
  342. setupSetApi = remoteAppManagementClientMock.Setup(
  343. c => c.Collections.SetAsync(
  344. collectionName,
  345. forceRedeploy,
  346. false,
  347. It.Is<CollectionUpdateDetails>(col =>
  348. col.CustomRdpProperty == reqestData.CustomRdpProperty &&
  349. col.Description == reqestData.Description &&
  350. col.PlanName == reqestData.PlanName &&
  351. col.TemplateImageName == reqestData.TemplateImageName &&
  352. col.AdInfo == null),
  353. It.IsAny<CancellationToken>()));
  354. setupSetApi.Returns(Task.Factory.StartNew(() => new OperationResultWithTrackingId()
  355. {
  356. TrackingId = trackingId
  357. }));
  358. mockCmdlet.ResetPipelines();
  359. mockCmdlet.ExecuteCmdlet();
  360. cmdRuntime = mockCmdlet.runTime();
  361. if (cmdRuntime.ErrorStream.Count > 0)
  362. {
  363. Assert.True(cmdRuntime.ErrorStream.Count == 0,
  364. String.Format("Set-AzureRemoteAppCollection returned the following error {0}",
  365. mockCmdlet.runTime().ErrorStream[0].Exception.Message));
  366. }
  367. trackingIds = LanguagePrimitives.GetEnumerable(mockCmdlet.runTime().OutputPipeline).Cast<TrackingResult>();
  368. Assert.NotNull(trackingIds);
  369. Assert.Equal(1, trackingIds.Count());
  370. Assert.True(trackingIds.Any(t => t.TrackingId == trackingId), "The actual result does not match the expected.");
  371. }
  372. [Fact]
  373. public void SetCollectionAdConfigTest()
  374. {
  375. SetAzureRemoteAppCollection mockCmdlet = SetUpTestCommon<SetAzureRemoteAppCollection>();
  376. string collectionName = "mycol";
  377. System.Security.SecureString password = new System.Security.SecureString();
  378. string expectedTrackingId = "2432145";
  379. CollectionUpdateDetails requestData = null;
  380. string userName = @"Administrator@MyDomain";
  381. Collection expectedCollection = null;
  382. // Required parameters for this test
  383. mockCmdlet.CollectionName = collectionName;
  384. password.AppendChar('p');
  385. mockCmdlet.Credential = new PSCredential(userName, password);
  386. requestData = new CollectionUpdateDetails()
  387. {
  388. AdInfo = new ActiveDirectoryConfig()
  389. {
  390. UserName = userName,
  391. Password = "p"
  392. }
  393. };
  394. expectedCollection = new Collection()
  395. {
  396. Name = collectionName,
  397. Status = "Active",
  398. AdInfo = new ActiveDirectoryConfig()
  399. };
  400. PerformCollectionTestWithAdInfoHelper(mockCmdlet, collectionName, expectedCollection, expectedTrackingId, requestData, false);
  401. }
  402. [Fact]
  403. public void SetInactiveCollectionAdConfigTest()
  404. {
  405. SetAzureRemoteAppCollection mockCmdlet = SetUpTestCommon<SetAzureRemoteAppCollection>();
  406. string collectionName = "mycol";
  407. System.Security.SecureString password = new System.Security.SecureString();
  408. string expectedTrackingId = "fasdfsadfsdf";
  409. CollectionUpdateDetails requestData = null;
  410. string userName = @"MyDomain\Administrator";
  411. Collection expectedCollection = null;
  412. // Required parameters for this test
  413. mockCmdlet.CollectionName = collectionName;
  414. password.AppendChar('f');
  415. mockCmdlet.Credential = new PSCredential(userName, password);
  416. requestData = new CollectionUpdateDetails()
  417. {
  418. AdInfo = new ActiveDirectoryConfig()
  419. {
  420. UserName = userName,
  421. Password = "f"
  422. }
  423. };
  424. expectedCollection = new Collection()
  425. {
  426. Name = collectionName,
  427. Status = "Inactive",
  428. AdInfo = new ActiveDirectoryConfig()
  429. };
  430. PerformCollectionTestWithAdInfoHelper(mockCmdlet, collectionName, expectedCollection, expectedTrackingId, requestData, true);
  431. }
  432. [Fact]
  433. public void RemoveCollection()
  434. {
  435. List<TrackingResult> trackingIds = null;
  436. RemoveAzureRemoteAppCollection mockCmdlet = SetUpTestCommon<RemoveAzureRemoteAppCollection>();
  437. // Required parameters for this test
  438. mockCmdlet.CollectionName = collectionName;
  439. // Setup the environment for testing this cmdlet
  440. MockObject.SetUpDefaultRemoteAppCollectionByName(remoteAppManagementClientMock, collectionName);
  441. MockObject.SetUpDefaultRemoteAppCollectionDelete(remoteAppManagementClientMock, mockCmdlet.CollectionName, trackingId);
  442. mockCmdlet.ResetPipelines();
  443. Log("Calling Remove-AzureRemoteAppCollection");
  444. mockCmdlet.ExecuteCmdlet();
  445. if (mockCmdlet.runTime().ErrorStream.Count != 0)
  446. {
  447. Assert.True(false,
  448. String.Format("Remove-AzureRemoteAppCollection returned the following error {0}",
  449. mockCmdlet.runTime().ErrorStream[0].Exception.Message
  450. )
  451. );
  452. }
  453. trackingIds = MockObject.ConvertList<TrackingResult>(mockCmdlet.runTime().OutputPipeline);
  454. Assert.NotNull(trackingIds);
  455. Assert.True(MockObject.HasExpectedResults<TrackingResult>(trackingIds, MockObject.ContainsExpectedTrackingId),
  456. "The actual result does not match the expected."
  457. );
  458. Log("The test for Remove-AzureRemoteAppCollection completed successfully");
  459. }
  460. [Fact]
  461. [Trait(Category.AcceptanceType, Category.CheckIn)]
  462. public void GetRegionList()
  463. {
  464. List<Region> regionList = null;
  465. GetAzureRemoteAppLocation mockCmdlet = SetUpTestCommon<GetAzureRemoteAppLocation>();
  466. // Setup the environment for testing this cmdlet
  467. MockObject.SetUpDefaultRemoteAppRegionList(remoteAppManagementClientMock);
  468. mockCmdlet.ResetPipelines();
  469. Log("Calling Get-AzureRemoteAppRegionList");
  470. mockCmdlet.ExecuteCmdlet();
  471. if (mockCmdlet.runTime().ErrorStream.Count != 0)
  472. {
  473. Assert.True(false,
  474. String.Format("Get-AzureRemoteAppRegionList returned the following error {0}.",
  475. mockCmdlet.runTime().ErrorStream[0].Exception.Message
  476. )
  477. );
  478. }
  479. regionList = MockObject.ConvertList<Region>(mockCmdlet.runTime().OutputPipeline);
  480. Assert.NotNull(regionList);
  481. Assert.True(MockObject.HasExpectedResults<Region>(regionList, MockObject.ContainsExpectedRegion),
  482. "The actual result does not match the expected."
  483. );
  484. Log("The test for Get-AzureRemoteAppRegionList");
  485. }
  486. }
  487. }