public void EnsurePackageDownloadsUsesInMemoryCachedInstanceOnceDownloaded() { // Arrange var zipPackage = PackageUtility.CreatePackage("A", "1.2"); var uri = new Uri("http://nuget.org"); var mockRepository = new MockPackageCacheRepository(); var packageDownloader = new Mock<PackageDownloader>(); packageDownloader.Setup(d => d.DownloadPackage(uri, It.IsAny<IPackageMetadata>(), It.IsAny<Stream>())) .Callback(() => mockRepository.AddPackage(zipPackage)) .Verifiable(); var hashProvider = new Mock<IHashProvider>(MockBehavior.Strict); var context = new Mock<IDataServiceContext>(); context.Setup(c => c.GetReadStreamUri(It.IsAny<object>())).Returns(uri).Verifiable(); var servicePackage = new DataServicePackage { Id = "A", Version = "1.2", PackageHash = "NEWHASH", Downloader = packageDownloader.Object, HashProvider = hashProvider.Object, Context = context.Object }; // Act servicePackage.EnsurePackage(mockRepository); servicePackage.EnsurePackage(mockRepository); servicePackage.EnsurePackage(mockRepository); // Assert Assert.Equal(zipPackage, servicePackage._package); context.Verify(s => s.GetReadStreamUri(It.IsAny<object>()), Times.Once()); packageDownloader.Verify(d => d.DownloadPackage(uri, It.IsAny<IPackageMetadata>(), It.IsAny<Stream>()), Times.Once()); Assert.True(mockRepository.Exists(zipPackage)); }
public void EnsurePackageDownloadsUsesInMemoryCachedInstanceOnceDownloaded() { // Arrange var zipPackage = PackageUtility.CreatePackage("A", "1.2"); var uri = new Uri("http://nuget.org"); var mockRepository = new MockPackageCacheRepository(true); var packageDownloader = new Mock<PackageDownloader>(); packageDownloader.Setup(d => d.DownloadPackage(uri, It.IsAny<IPackageMetadata>(), It.IsAny<Stream>())) .Callback(() => mockRepository.AddPackage(zipPackage)) .Verifiable(); var hashProvider = new Mock<IHashProvider>(MockBehavior.Strict); hashProvider.Setup(h => h.CalculateHash(It.IsAny<Stream>())).Returns<Stream>((stream) => new byte[] { 1, 2, 3, 4 }); var servicePackage = new DataServicePackage { Id = "A", Version = "1.2", PackageHash = Convert.ToBase64String(new byte[] { 1, 2, 3, 4 }), Downloader = packageDownloader.Object, HashProvider = hashProvider.Object, DownloadUrl = uri }; // Act servicePackage.EnsurePackage(mockRepository); servicePackage.EnsurePackage(mockRepository); servicePackage.EnsurePackage(mockRepository); // Assert Assert.Equal(zipPackage, servicePackage._package); packageDownloader.Verify(d => d.DownloadPackage(uri, It.IsAny<IPackageMetadata>(), It.IsAny<Stream>()), Times.Once()); Assert.True(mockRepository.Exists(zipPackage)); }