33
44using System ;
55using System . IO ;
6+ using System . Text ;
67using System . Threading ;
78using System . Threading . Tasks ;
89using Microsoft . Extensions . Logging ;
@@ -18,11 +19,16 @@ public class ValidationPackageFileServiceFacts
1819 private readonly PackageValidationSet _validationSet ;
1920 private readonly Package _package ;
2021 private readonly string _validationContainerName ;
22+ private readonly string _backupContainerName ;
2123 private readonly string _packagesContainerName ;
2224 private readonly string _packageFileName ;
2325 private readonly string _validationSetPackageFileName ;
26+ private readonly string _backupFileName ;
2427 private readonly Uri _testUri ;
2528 private readonly string _etag ;
29+ private readonly string _packageContent ;
30+ private readonly MemoryStream _packageStream ;
31+ private readonly DateTimeOffset _endOfAccess ;
2632 private readonly Mock < ICoreFileStorageService > _fileStorageService ;
2733 private readonly Mock < IPackageDownloader > _packageDownloader ;
2834 private readonly Mock < ILogger < ValidationPackageFileService > > _logger ;
@@ -37,6 +43,7 @@ public ValidationPackageFileServiceFacts()
3743 Id = "NuGet.Versioning" ,
3844 } ,
3945 NormalizedVersion = "4.5.0-ALPHA" ,
46+ Hash = "NzMzMS1QNENLNEczSDQ1SA==" ,
4047 } ;
4148 _validationSet = new PackageValidationSet
4249 {
@@ -47,10 +54,15 @@ public ValidationPackageFileServiceFacts()
4754
4855 _packagesContainerName = "packages" ;
4956 _validationContainerName = "validation" ;
57+ _backupContainerName = "package-backups" ;
5058 _packageFileName = "nuget.versioning.4.5.0-alpha.nupkg" ;
5159 _validationSetPackageFileName = "validation-sets/0b44d53f-0689-4f82-9530-f25f26b321aa/nuget.versioning.4.5.0-alpha.nupkg" ;
60+ _backupFileName = "nuget.versioning/4.5.0-alpha/rQw3wx1psxXzqB8TyM3nAQlK2RcluhsNwxmcqXE2YbgoDW735o8TPmIR4uWpoxUERddvFwjgRSGw7gNPCwuvJg2..nupkg" ;
5261 _testUri = new Uri ( "http://example.com/nupkg.nupkg" ) ;
5362 _etag = "\" some-etag\" " ;
63+ _packageContent = "Hello, world." ;
64+ _packageStream = new MemoryStream ( Encoding . ASCII . GetBytes ( _packageContent ) ) ;
65+ _endOfAccess = new DateTimeOffset ( 2018 , 1 , 3 , 8 , 30 , 0 , TimeSpan . Zero ) ;
5466
5567 _fileStorageService = new Mock < ICoreFileStorageService > ( MockBehavior . Strict ) ;
5668 _packageDownloader = new Mock < IPackageDownloader > ( MockBehavior . Strict ) ;
@@ -62,6 +74,45 @@ public ValidationPackageFileServiceFacts()
6274 _logger . Object ) ;
6375 }
6476
77+ [ Fact ]
78+ public async Task BackupPackageFileFromValidationSetPackageAsync ( )
79+ {
80+ DateTimeOffset ? endOfAccess = null ;
81+ _fileStorageService
82+ . Setup ( x => x . GetFileReadUriAsync (
83+ _validationContainerName ,
84+ _validationSetPackageFileName ,
85+ It . IsAny < DateTimeOffset ? > ( ) ) )
86+ . ReturnsAsync ( _testUri )
87+ . Callback < string , string , DateTimeOffset ? > ( ( _ , __ , a ) => endOfAccess = a )
88+ . Verifiable ( ) ;
89+
90+ _packageDownloader
91+ . Setup ( x => x . DownloadAsync ( _testUri , CancellationToken . None ) )
92+ . ReturnsAsync ( _packageStream )
93+ . Verifiable ( ) ;
94+
95+ _fileStorageService
96+ . Setup ( x => x . FileExistsAsync ( _backupContainerName , _backupFileName ) )
97+ . ReturnsAsync ( false )
98+ . Verifiable ( ) ;
99+
100+ _fileStorageService
101+ . Setup ( x => x . SaveFileAsync ( _backupContainerName , _backupFileName , _packageStream , true ) )
102+ . Returns ( Task . CompletedTask )
103+ . Verifiable ( ) ;
104+
105+ var before = DateTimeOffset . UtcNow ;
106+ await _target . BackupPackageFileFromValidationSetPackageAsync ( _package , _validationSet ) ;
107+ var after = DateTimeOffset . UtcNow ;
108+
109+ _fileStorageService . Verify ( ) ;
110+ _packageDownloader . Verify ( ) ;
111+ Assert . NotNull ( endOfAccess ) ;
112+ Assert . InRange ( endOfAccess . Value , before . AddMinutes ( 10 ) , after . AddMinutes ( 10 ) ) ;
113+ Assert . Throws < ObjectDisposedException > ( ( ) => _packageStream . Length ) ;
114+ }
115+
65116 [ Fact ]
66117 public async Task DownloadPackageFileToDiskAsync ( )
67118 {
@@ -72,18 +123,17 @@ public async Task DownloadPackageFileToDiskAsync()
72123 null ) )
73124 . ReturnsAsync ( _testUri )
74125 . Verifiable ( ) ;
75-
76- var expected = new MemoryStream ( ) ;
126+
77127 _packageDownloader
78- . Setup ( x => x . DownloadAsync (
79- _testUri ,
80- CancellationToken . None ) )
81- . ReturnsAsync ( expected ) ;
128+ . Setup ( x => x . DownloadAsync ( _testUri , CancellationToken . None ) )
129+ . ReturnsAsync ( _packageStream )
130+ . Verifiable ( ) ;
82131
83132 var actual = await _target . DownloadPackageFileToDiskAsync ( _package ) ;
84133
85- Assert . Same ( expected , actual ) ;
134+ Assert . Same ( _packageStream , actual ) ;
86135 _fileStorageService . Verify ( ) ;
136+ _packageDownloader . Verify ( ) ;
87137 }
88138
89139 [ Fact ]
@@ -96,7 +146,7 @@ public async Task CopyValidationPackageForValidationSetAsync()
96146 _validationContainerName ,
97147 _validationSetPackageFileName ,
98148 It . Is < IAccessCondition > ( y => y . IfMatchETag == null && y . IfNoneMatchETag == null ) ) )
99- . ReturnsAsync ( string . Empty )
149+ . ReturnsAsync ( _etag )
100150 . Verifiable ( ) ;
101151
102152 await _target . CopyValidationPackageForValidationSetAsync ( _validationSet ) ;
@@ -133,7 +183,7 @@ public async Task CopyValidationPackageToPackageFileAsync()
133183 _packagesContainerName ,
134184 _packageFileName ,
135185 It . Is < IAccessCondition > ( y => y . IfNoneMatchETag == "*" ) ) )
136- . ReturnsAsync ( string . Empty )
186+ . ReturnsAsync ( _etag )
137187 . Verifiable ( ) ;
138188
139189 await _target . CopyValidationPackageToPackageFileAsync ( _validationSet . PackageId , _validationSet . PackageNormalizedVersion ) ;
@@ -168,7 +218,7 @@ public async Task CopyValidationSetPackageToPackageFileAsync()
168218 _packagesContainerName ,
169219 _packageFileName ,
170220 accessCondition ) )
171- . ReturnsAsync ( string . Empty )
221+ . ReturnsAsync ( _etag )
172222 . Verifiable ( ) ;
173223
174224 await _target . CopyValidationSetPackageToPackageFileAsync ( _validationSet , accessCondition ) ;
@@ -211,16 +261,15 @@ public async Task DeletePackageForValidationSetAsync()
211261 [ Fact ]
212262 public async Task GetPackageForValidationSetReadUriAsync ( )
213263 {
214- var endOfAccess = new DateTimeOffset ( 2018 , 1 , 3 , 8 , 30 , 0 , TimeSpan . Zero ) ;
215264 _fileStorageService
216265 . Setup ( x => x . GetFileReadUriAsync (
217266 _validationContainerName ,
218267 _validationSetPackageFileName ,
219- endOfAccess ) )
268+ _endOfAccess ) )
220269 . ReturnsAsync ( _testUri )
221270 . Verifiable ( ) ;
222271
223- var actual = await _target . GetPackageForValidationSetReadUriAsync ( _validationSet , endOfAccess ) ;
272+ var actual = await _target . GetPackageForValidationSetReadUriAsync ( _validationSet , _endOfAccess ) ;
224273
225274 Assert . Equal ( _testUri , actual ) ;
226275 _fileStorageService . Verify ( ) ;
0 commit comments