/ToMigrate/Raven.Tests.FileSystem/Bundles/Versioning/VersioningAndSynchronization.cs

https://github.com/fitzchak/ravendb · C# · 343 lines · 252 code · 86 blank · 5 comment · 0 complexity · 4fc7db8a8f5721acbce756053104b7f6 MD5 · raw file

  1. // -----------------------------------------------------------------------
  2. // <copyright file="VersioningAndSynchronization.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Raven.Abstractions.FileSystem;
  9. using Raven.Client.FileSystem.Bundles.Versioning;
  10. using Raven.Client.FileSystem.Extensions;
  11. using Raven.Database.Bundles.Versioning.Data;
  12. using Raven.Database.Extensions;
  13. using Raven.Database.FileSystem.Bundles.Versioning;
  14. using Raven.Json.Linq;
  15. using Raven.Tests.Helpers;
  16. using Xunit;
  17. using Xunit.Extensions;
  18. namespace Raven.Tests.FileSystem.Bundles.Versioning
  19. {
  20. public class VersioningAndSynchronization : RavenFilesTestBase
  21. {
  22. [Theory]
  23. [PropertyData("Storages")]
  24. public async Task ContentUpdateSynchronizationShouldCreateRevisions(string requestedStorage)
  25. {
  26. using (var source = NewStore())
  27. using (var destination = NewStore(1, requestedStorage: requestedStorage, activeBundles: "Versioning"))
  28. {
  29. const int maxRevisions = 2;
  30. await destination.AsyncFilesCommands.Configuration.SetKeyAsync(VersioningUtil.DefaultConfigurationName, new FileVersioningConfiguration
  31. {
  32. Id = VersioningUtil.DefaultConfigurationName,
  33. MaxRevisions = maxRevisions
  34. });
  35. await source.AsyncFilesCommands.UploadAsync("file.txt", CreateUniformFileStream(1024));
  36. var synchronizationReport = await source.AsyncFilesCommands.Synchronization.StartAsync("file.txt", destination.AsyncFilesCommands);
  37. using (var dstSession = destination.OpenAsyncSession())
  38. {
  39. Assert.NotNull(await dstSession.LoadFileAsync("file.txt"));
  40. var revisions = await dstSession.GetRevisionNamesForAsync("file.txt", 0, 128);
  41. Assert.Equal(1, revisions.Length);
  42. Assert.Equal((await dstSession.DownloadAsync("file.txt")).GetMD5Hash(), (await dstSession.DownloadAsync(revisions[0])).GetMD5Hash());
  43. }
  44. await source.AsyncFilesCommands.UploadAsync("file.txt", CreateUniformFileStream(2048));
  45. await source.AsyncFilesCommands.Synchronization.StartAsync("file.txt", destination.AsyncFilesCommands);
  46. using (var dstSession = destination.OpenAsyncSession())
  47. {
  48. Assert.NotNull(await dstSession.LoadFileAsync("file.txt"));
  49. var revisions = await dstSession.GetRevisionNamesForAsync("file.txt", 0, 128);
  50. Assert.Equal(2, revisions.Length);
  51. Assert.Equal((await dstSession.DownloadAsync("file.txt")).GetMD5Hash(), (await dstSession.DownloadAsync(revisions[1])).GetMD5Hash());
  52. }
  53. await source.AsyncFilesCommands.UploadAsync("file.txt", StringToStream("123456789"));
  54. await source.AsyncFilesCommands.Synchronization.StartAsync("file.txt", destination.AsyncFilesCommands);
  55. using (var dstSession = destination.OpenAsyncSession())
  56. {
  57. Assert.NotNull(await dstSession.LoadFileAsync("file.txt"));
  58. var revisions = await dstSession.GetRevisionNamesForAsync("file.txt", 0, 128);
  59. Assert.Equal(maxRevisions, revisions.Length); // cannot be more revisions than defined in configuration
  60. Assert.Equal((await dstSession.DownloadAsync("file.txt")).GetMD5Hash(), (await dstSession.DownloadAsync(revisions[1])).GetMD5Hash());
  61. }
  62. using (var dstSession = destination.OpenAsyncSession())
  63. {
  64. dstSession.RegisterUpload("file.txt", StringToStream("abc")); // make sure that you can upload a new version
  65. await dstSession.SaveChangesAsync();
  66. var revisions = await dstSession.GetRevisionNamesForAsync("file.txt", 0, 128);
  67. Assert.Equal(StreamToString(await dstSession.DownloadAsync(revisions[0])), "123456789");
  68. Assert.Equal(StreamToString(await dstSession.DownloadAsync(revisions[1])), "abc");
  69. }
  70. }
  71. }
  72. [Theory]
  73. [PropertyData("Storages")]
  74. public async Task MetadataUpdateSynchronizationShouldCreateRevisions(string requestedStorage)
  75. {
  76. using (var source = NewStore())
  77. using (var destination = NewStore(1, requestedStorage: requestedStorage, activeBundles: "Versioning"))
  78. {
  79. const int maxRevisions = 2;
  80. await destination.AsyncFilesCommands.Configuration.SetKeyAsync(VersioningUtil.DefaultConfigurationName, new FileVersioningConfiguration
  81. {
  82. Id = VersioningUtil.DefaultConfigurationName,
  83. MaxRevisions = maxRevisions
  84. });
  85. await source.AsyncFilesCommands.UploadAsync("file.txt", CreateUniformFileStream(1024));
  86. await source.AsyncFilesCommands.Synchronization.StartAsync("file.txt", destination.AsyncFilesCommands);
  87. using (var dstSession = destination.OpenAsyncSession())
  88. {
  89. Assert.NotNull(await dstSession.LoadFileAsync("file.txt"));
  90. var revisions = await dstSession.GetRevisionNamesForAsync("file.txt", 0, 128);
  91. Assert.Equal(1, revisions.Length);
  92. Assert.Equal((await dstSession.DownloadAsync("file.txt")).GetMD5Hash(), (await dstSession.DownloadAsync(revisions[0])).GetMD5Hash());
  93. }
  94. await source.AsyncFilesCommands.UpdateMetadataAsync("file.txt", new RavenJObject { { "New", "Metadata" } });
  95. var report = await source.AsyncFilesCommands.Synchronization.StartAsync("file.txt", destination.AsyncFilesCommands);
  96. Assert.Equal(SynchronizationType.MetadataUpdate, report.Type);
  97. using (var dstSession = destination.OpenAsyncSession())
  98. {
  99. Assert.NotNull(await dstSession.LoadFileAsync("file.txt"));
  100. var revisions = await dstSession.GetRevisionNamesForAsync("file.txt", 0, 128);
  101. Assert.Equal(2, revisions.Length);
  102. var metadata = (await dstSession.LoadFileAsync("file.txt")).Metadata;
  103. Assert.Equal("Metadata", metadata["New"]);
  104. }
  105. await source.AsyncFilesCommands.UpdateMetadataAsync("file.txt", new RavenJObject { { "Another", "Metadata" } });
  106. await source.AsyncFilesCommands.Synchronization.StartAsync("file.txt", destination.AsyncFilesCommands);
  107. using (var dstSession = destination.OpenAsyncSession())
  108. {
  109. Assert.NotNull(await dstSession.LoadFileAsync("file.txt"));
  110. var revisions = await dstSession.GetRevisionNamesForAsync("file.txt", 0, 128);
  111. Assert.Equal(maxRevisions, revisions.Length); // cannot be more revisions than defined in configuration
  112. var metadata = (await dstSession.LoadFileAsync("file.txt")).Metadata;
  113. Assert.Equal("Metadata", metadata["Another"]);
  114. }
  115. using (var dstSession = destination.OpenAsyncSession())
  116. {
  117. var file = await dstSession.LoadFileAsync("file.txt");
  118. file.Metadata.Add("Test", "Test");
  119. await dstSession.SaveChangesAsync();
  120. var revisions = await dstSession.GetRevisionNamesForAsync("file.txt", 0, 128);
  121. Assert.Equal("Metadata", (await dstSession.LoadFileAsync(revisions[0])).Metadata["Another"]);
  122. Assert.Equal("Test", (await dstSession.LoadFileAsync(revisions[1])).Metadata["Test"]);
  123. }
  124. }
  125. }
  126. [Theory]
  127. [PropertyData("Storages")]
  128. public async Task RenameSynchronizationShouldRemoveRevisionsByDefault(string requestedStorage)
  129. {
  130. using (var source = NewStore())
  131. using (var destination = NewStore(1, requestedStorage: requestedStorage, activeBundles: "Versioning"))
  132. {
  133. await destination.AsyncFilesCommands.Configuration.SetKeyAsync(VersioningUtil.DefaultConfigurationName, new FileVersioningConfiguration
  134. {
  135. Id = VersioningUtil.DefaultConfigurationName,
  136. });
  137. await source.AsyncFilesCommands.UploadAsync("file.txt", CreateUniformFileStream(1024));
  138. await source.AsyncFilesCommands.Synchronization.StartAsync("file.txt", destination.AsyncFilesCommands);
  139. using (var dstSession = destination.OpenAsyncSession())
  140. {
  141. var revisions = await dstSession.GetRevisionNamesForAsync("file.txt", 0, 128);
  142. Assert.Equal(1, revisions.Length);
  143. }
  144. await source.AsyncFilesCommands.UploadAsync("file.txt", CreateUniformFileStream(1024, 'c'));
  145. await source.AsyncFilesCommands.Synchronization.StartAsync("file.txt", destination.AsyncFilesCommands);
  146. using (var dstSession = destination.OpenAsyncSession())
  147. {
  148. var revisions = await dstSession.GetRevisionNamesForAsync("file.txt", 0, 128);
  149. Assert.Equal(2, revisions.Length);
  150. }
  151. await source.AsyncFilesCommands.RenameAsync("file.txt", "renamed.txt");
  152. await source.AsyncFilesCommands.Synchronization.StartAsync("file.txt", destination.AsyncFilesCommands);
  153. using (var dstSession = destination.OpenAsyncSession())
  154. {
  155. var revisions = await dstSession.GetRevisionNamesForAsync("renamed.txt", 0, 128);
  156. Assert.Equal(1, revisions.Length);
  157. }
  158. }
  159. }
  160. [Theory]
  161. [PropertyData("Storages")]
  162. public async Task RenameSynchronizationShouldKeepRevisionsIfDefined(string requestedStorage)
  163. {
  164. using (var source = NewStore())
  165. using (var destination = NewStore(1, requestedStorage: requestedStorage, activeBundles: "Versioning"))
  166. {
  167. await destination.AsyncFilesCommands.Configuration.SetKeyAsync(VersioningUtil.DefaultConfigurationName, new FileVersioningConfiguration
  168. {
  169. Id = VersioningUtil.DefaultConfigurationName,
  170. ResetOnRename = false
  171. });
  172. await source.AsyncFilesCommands.UploadAsync("file.txt", CreateUniformFileStream(1024));
  173. await source.AsyncFilesCommands.Synchronization.StartAsync("file.txt", destination.AsyncFilesCommands);
  174. using (var dstSession = destination.OpenAsyncSession())
  175. {
  176. var revisions = await dstSession.GetRevisionNamesForAsync("file.txt", 0, 128);
  177. Assert.Equal(1, revisions.Length);
  178. }
  179. await source.AsyncFilesCommands.UploadAsync("file.txt", CreateUniformFileStream(1024, 'c'));
  180. await source.AsyncFilesCommands.Synchronization.StartAsync("file.txt", destination.AsyncFilesCommands);
  181. using (var dstSession = destination.OpenAsyncSession())
  182. {
  183. var revisions = await dstSession.GetRevisionNamesForAsync("file.txt", 0, 128);
  184. Assert.Equal(2, revisions.Length);
  185. }
  186. await source.AsyncFilesCommands.RenameAsync("file.txt", "renamed.txt");
  187. await source.AsyncFilesCommands.Synchronization.StartAsync("file.txt", destination.AsyncFilesCommands);
  188. using (var dstSession = destination.OpenAsyncSession())
  189. {
  190. var revisions = await dstSession.GetRevisionNamesForAsync("renamed.txt", 0, 128);
  191. Assert.Equal(2, revisions.Length);
  192. }
  193. }
  194. }
  195. [Theory]
  196. [PropertyData("Storages")]
  197. public async Task DeleteSynchronizationShouldDeleteRevisions(string requestedStorage)
  198. {
  199. using (var source = NewStore())
  200. using (var destination = NewStore(1, requestedStorage: requestedStorage, activeBundles: "Versioning"))
  201. {
  202. await destination.AsyncFilesCommands.Configuration.SetKeyAsync(VersioningUtil.DefaultConfigurationName, new FileVersioningConfiguration
  203. {
  204. Id = VersioningUtil.DefaultConfigurationName,
  205. ResetOnRename = false
  206. });
  207. await source.AsyncFilesCommands.UploadAsync("file.txt", CreateUniformFileStream(1024));
  208. await source.AsyncFilesCommands.Synchronization.StartAsync("file.txt", destination.AsyncFilesCommands);
  209. using (var dstSession = destination.OpenAsyncSession())
  210. {
  211. var revisions = await dstSession.GetRevisionNamesForAsync("file.txt", 0, 128);
  212. Assert.Equal(1, revisions.Length);
  213. }
  214. await source.AsyncFilesCommands.UploadAsync("file.txt", CreateUniformFileStream(1024, 'c'));
  215. await source.AsyncFilesCommands.Synchronization.StartAsync("file.txt", destination.AsyncFilesCommands);
  216. using (var dstSession = destination.OpenAsyncSession())
  217. {
  218. var revisions = await dstSession.GetRevisionNamesForAsync("file.txt", 0, 128);
  219. Assert.Equal(2, revisions.Length);
  220. }
  221. await source.AsyncFilesCommands.DeleteAsync("file.txt");
  222. await source.AsyncFilesCommands.Synchronization.StartAsync("file.txt", destination.AsyncFilesCommands);
  223. using (var dstSession = destination.OpenAsyncSession())
  224. {
  225. var revisions = await dstSession.GetRevisionNamesForAsync("renamed.txt", 0, 128);
  226. Assert.Equal(0, revisions.Length);
  227. }
  228. }
  229. }
  230. [Fact]
  231. public async Task RevisionsShouldNotBeSynchronized()
  232. {
  233. using (var source = NewStore(activeBundles: "Versioning"))
  234. using (var destination = NewStore(1))
  235. {
  236. const int maxRevisions = 2;
  237. await source.AsyncFilesCommands.Configuration.SetKeyAsync(VersioningUtil.DefaultConfigurationName, new FileVersioningConfiguration
  238. {
  239. Id = VersioningUtil.DefaultConfigurationName,
  240. MaxRevisions = maxRevisions
  241. });
  242. await source.AsyncFilesCommands.UploadAsync("test.txt", StringToStream("abc"));
  243. await source.AsyncFilesCommands.UploadAsync("test.txt", StringToStream("def"));
  244. await source.AsyncFilesCommands.Synchronization.SetDestinationsAsync(destination.AsyncFilesCommands.ToSynchronizationDestination());
  245. var results = await source.AsyncFilesCommands.Synchronization.StartAsync(true);
  246. Assert.Equal(1, results.Length);
  247. Assert.Equal(1, results[0].Reports.Count());
  248. using (var dstSession = destination.OpenAsyncSession())
  249. {
  250. Assert.NotNull(await dstSession.LoadFileAsync("test.txt"));
  251. var revisions = await dstSession.GetRevisionNamesForAsync("test.txt", 0, 128);
  252. Assert.Equal(0, revisions.Length);
  253. }
  254. }
  255. }
  256. }
  257. }