/src/Storage/Commands.Storage.Test/Service/MockStorageTableManagement.cs

https://gitlab.com/jslee1/azure-powershell · C# · 223 lines · 109 code · 20 blank · 94 comment · 10 complexity · 08cb59c1e6715e19870e128ca94edd96 MD5 · raw file

  1. // ----------------------------------------------------------------------------------
  2. //
  3. // Copyright 2012 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 System;
  15. using System.Collections.Generic;
  16. using System.Threading.Tasks;
  17. using Microsoft.WindowsAzure.Commands.Common.Storage;
  18. using Microsoft.WindowsAzure.Commands.Storage.Model.Contract;
  19. using Microsoft.WindowsAzure.Storage;
  20. using Microsoft.WindowsAzure.Storage.Table;
  21. namespace Microsoft.WindowsAzure.Commands.Storage.Test.Service
  22. {
  23. /// <summary>
  24. /// Mocked table management
  25. /// </summary>
  26. public class MockStorageTableManagement : IStorageTableManagement
  27. {
  28. /// <summary>
  29. /// Exists table lists
  30. /// </summary>
  31. public List<CloudTable> tableList = new List<CloudTable>();
  32. /// <summary>
  33. /// Exists permissions
  34. /// </summary>
  35. public TablePermissions tablePermissions = new TablePermissions();
  36. /// <summary>
  37. /// Table end point
  38. /// </summary>
  39. private string TableEndPoint = "http://127.0.0.1/account/";
  40. /// <summary>
  41. /// List azure storage tables
  42. /// </summary>
  43. /// <param name="prefix">Table name prefix</param>
  44. /// <param name="requestOptions">Table request options</param>
  45. /// <param name="operationContext">Operation context</param>
  46. /// <returns>An enumerable collection of tables that begin with the specified prefix</returns>
  47. public IEnumerable<CloudTable> ListTables(string prefix, TableRequestOptions requestOptions, OperationContext operationContext)
  48. {
  49. if (String.IsNullOrEmpty(prefix))
  50. {
  51. return tableList;
  52. }
  53. else
  54. {
  55. List<CloudTable> prefixTables = new List<CloudTable>();
  56. foreach (CloudTable table in tableList)
  57. {
  58. if (table.Name.ToLower().StartsWith(prefix.ToLower()))
  59. {
  60. prefixTables.Add(table);
  61. }
  62. }
  63. return prefixTables;
  64. }
  65. }
  66. /// <summary>
  67. /// Get table reference from azure server
  68. /// </summary>
  69. /// <param name="name">Table name</param>
  70. /// <param name="requestOptions">Table request options</param>
  71. /// <param name="operationContext">Operation context</param>
  72. /// <returns>A CloudTable object if the specified table exists, otherwise null.</returns>
  73. public CloudTable GetTableReferenceFromServer(string name, TableRequestOptions requestOptions, OperationContext operationContext)
  74. {
  75. foreach (CloudTable table in tableList)
  76. {
  77. if (table.Name == name)
  78. {
  79. return table;
  80. }
  81. }
  82. return null;
  83. }
  84. /// <summary>
  85. /// Get a table reference
  86. /// </summary>
  87. /// <param name="name">Table name</param>
  88. /// <returns>Cloud table object</returns>
  89. public CloudTable GetTableReference(string name)
  90. {
  91. Uri tableUri = new Uri(String.Format("{0}{1}", TableEndPoint, name));
  92. CloudTableClient tableClient = new CloudTableClient(new Uri(TableEndPoint), null);
  93. return new CloudTable(tableUri);
  94. }
  95. /// <summary>
  96. /// Cloud a azure storage table if not exists.
  97. /// </summary>
  98. /// <param name="table">Cloud table object</param>
  99. /// <param name="requestOptions">Table request options</param>
  100. /// <param name="operationContext">Operation context</param>
  101. /// <returns>True if table was created; otherwise, false.</returns>
  102. public bool CreateTableIfNotExists(CloudTable table, TableRequestOptions requestOptions, OperationContext operationContext)
  103. {
  104. CloudTable tableRef = GetTableReferenceFromServer(table.Name, requestOptions, operationContext);
  105. if (tableRef != null)
  106. {
  107. return false;
  108. }
  109. else
  110. {
  111. tableRef = GetTableReference(table.Name);
  112. tableList.Add(tableRef);
  113. return true;
  114. }
  115. }
  116. /// <summary>
  117. /// Delete the specified azure storage table
  118. /// </summary>
  119. /// <param name="table">Cloud table object</param>
  120. /// <param name="requestOptions">Table request options</param>
  121. /// <param name="operationContext">Operation context</param>
  122. public void Delete(CloudTable table, TableRequestOptions requestOptions = null, OperationContext operationContext = null)
  123. {
  124. foreach (CloudTable tableRef in tableList)
  125. {
  126. if (table.Name == tableRef.Name)
  127. {
  128. tableList.Remove(tableRef);
  129. return;
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// Checks whether the table exists.
  135. /// </summary>
  136. /// <param name="table">Cloud table object</param>
  137. /// <param name="requestOptions">Table request options</param>
  138. /// <param name="operationContext">Operation context</param>
  139. /// <returns>True if table exists; otherwise, false.</returns>
  140. public bool DoesTableExist(CloudTable table, TableRequestOptions requestOptions, OperationContext operationContext)
  141. {
  142. foreach (CloudTable tableRef in tableList)
  143. {
  144. if (table.Name == tableRef.Name)
  145. {
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. /// <summary>
  152. /// Get table permission
  153. /// </summary>
  154. /// <param name="table">CloudTable object</param>
  155. /// <param name="requestOptions">Table request options</param>
  156. /// <param name="operationContext">Operation context</param>
  157. /// <returns>Table permission</returns>
  158. public TablePermissions GetTablePermissions(CloudTable table, TableRequestOptions requestOptions, OperationContext operationContext)
  159. {
  160. return this.tablePermissions;
  161. }
  162. /// <summary>
  163. /// Set table permission
  164. /// </summary>
  165. /// <param name="table">Cloud table object</param>
  166. /// <param name="tablePermissions">table permissions</param>
  167. /// <param name="requestOptions">Table request options</param>
  168. /// <param name="operationContext">Operation context</param>
  169. /// <returns></returns>
  170. public void SetTablePermissions(CloudTable table, TablePermissions tablePermissions, TableRequestOptions requestOptions, OperationContext operationContext)
  171. {
  172. this.tablePermissions = tablePermissions;
  173. }
  174. /// <summary>
  175. /// Return a task that asynchronously set table permissions
  176. /// </summary>
  177. /// <param name="table">target table</param>
  178. /// <param name="tablePermissions">permissions to set</param>
  179. /// <param name="requestOptions">request options</param>
  180. /// <param name="operationContext">context</param>
  181. /// <returns></returns>
  182. public Task SetTablePermissionsAsync(CloudTable table, TablePermissions tablePermissions, TableRequestOptions requestOptions, OperationContext operationContext)
  183. {
  184. return Task.Factory.StartNew(() => this.SetTablePermissions(table, tablePermissions, requestOptions, operationContext));
  185. }
  186. /// <summary>
  187. /// Return a task that asynchronously fetch table permissions
  188. /// </summary>
  189. /// <param name="table">target table</param>
  190. /// <param name="requestOptions">request options</param>
  191. /// <param name="operationContext">context</param>
  192. /// <returns></returns>
  193. public Task<TablePermissions> GetTablePermissionsAsync(CloudTable table, TableRequestOptions requestOptions, OperationContext operationContext)
  194. {
  195. return Task.Factory.StartNew(() => this.GetTablePermissions(table,
  196. requestOptions, operationContext));
  197. }
  198. public AzureStorageContext StorageContext
  199. {
  200. get { throw new NotImplementedException(); }
  201. }
  202. }
  203. }