public void EnsurePackageDownloadUpdatesIdAndVersion()
        {
            // Arrange
            var zipPackage     = PackageUtility.CreatePackage("Abc", "1.0");
            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           = "abc",
                Version      = "1.0.0",
                PackageHash  = Convert.ToBase64String(new byte[] { 1, 2, 3, 4 }),
                Downloader   = packageDownloader.Object,
                HashProvider = hashProvider.Object,
                DownloadUrl  = uri
            };

            // Act
            servicePackage.EnsurePackage(mockRepository);

            // Assert
            // servicePackage should have been updated to match zipPackage
            Assert.Equal("Abc", servicePackage.Id);
            Assert.Equal("1.0", servicePackage.Version);
        }
        public void EnsurePackageDownloadsThePackageIfItIsNotCachedInMemoryOnInMachineCache()
        {
            // 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);

            // Assert
            context.Verify();
            packageDownloader.Verify();
            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));
        }