public void LoadsCsv()
        {
            // Arrange
            Guid profileId = Guid.NewGuid();
            var  profile   = new AwsProfile
            {
                Id = profileId,
                DetailedBillingS3Bucket = "my-bucket",
                IsBillingHistoryLoaded  = true,
                Account = "12345",
            };

            AwsProfileRepositoryMock.Setup(x => x.Find(profileId)).Returns(profile);
            AwsClientFactoryMock.Setup(x => x.GetClient(profile)).Returns(AwsClientMock.Object);

            var storage = new TestStorageService();

            AwsClientMock.Setup(x => x.StorageService).Returns(storage);
            storage.Contents = new MemoryStream(Resources.LineItemsZip);

            var now = new DateTime(2014, 6, 14, 16, 15, 14, DateTimeKind.Utc);

            ClockMock.Setup(x => x.UtcNow).Returns(now);

            var command = new UpdateBillingData(AwsClientFactory, AwsProfileRepository, BillingManager, Clock, new S3PathParser());

            string   loadedPeriod = null;
            LineItem lineItem     = null;

            BillingManagerMock.Setup(x => x.LoadLineItems(It.IsAny <IEnumerable <LineItem> >(), It.IsAny <string>()))
            .Callback((IEnumerable <LineItem> lineItems, string period) =>
            {
                lineItem     = lineItems.FirstOrDefault();
                loadedPeriod = period;
            });

            // Act
            command.LoadDeltas(profileId);

            // Assert
            loadedPeriod.Should().Be("2014-06");
            lineItem.RecordId.Should().Be("31861622192480759163092020", "should match first line item from line-items.csv embedded resource");
        }
        public void QueriesCorrectS3Object()
        {
            // Arrange
            Guid profileId    = Guid.NewGuid();
            var  lastModified = new DateTime(2014, 6, 14, 13, 12, 11, DateTimeKind.Utc);
            var  profile      = new AwsProfile
            {
                Id = profileId,
                DetailedBillingS3Bucket = "my-bucket",
                Account = "12345",
                IsBillingHistoryLoaded = true,
                BillingMetadata        = { { "2014-06", new BillingMetadata {
                                                 LastModified = lastModified
                                             } } }
            };

            AwsProfileRepositoryMock.Setup(x => x.Find(profileId)).Returns(profile);
            AwsClientFactoryMock.Setup(x => x.GetClient(profile)).Returns(AwsClientMock.Object);

            var storage = new TestStorageService();

            AwsClientMock.Setup(x => x.StorageService).Returns(storage);

            var now = new DateTime(2014, 6, 14, 16, 15, 14, DateTimeKind.Utc);

            ClockMock.Setup(x => x.UtcNow).Returns(now);

            var command = new UpdateBillingData(AwsClientFactory, AwsProfileRepository, BillingManager, Clock, new S3PathParser());

            // Act
            command.LoadDeltas(profileId);

            // Assert
            string expectedUrl = UpdateBillingData.MonthlyCsvUrl.Create("my-bucket", "12345", "2014-06");

            storage.S3Url.Should().Be(expectedUrl);
            storage.LastModified.Should().Be(lastModified);
            storage.CallCount.Should().Be(1);
        }
        public void SetsLastModified()
        {
            // Arrange
            Guid profileId = Guid.NewGuid();
            var  profile   = new AwsProfile
            {
                Id = profileId,
                DetailedBillingS3Bucket = "my-bucket",
                IsBillingHistoryLoaded  = true,
                Account = "12345",
            };

            AwsProfileRepositoryMock.Setup(x => x.Find(profileId)).Returns(profile);
            AwsClientFactoryMock.Setup(x => x.GetClient(profile)).Returns(AwsClientMock.Object);

            var now = new DateTime(2014, 6, 14, 16, 15, 14, DateTimeKind.Utc);

            ClockMock.Setup(x => x.UtcNow).Returns(now);

            var storage = new TestStorageService();

            AwsClientMock.Setup(x => x.StorageService).Returns(storage);
            storage.NewLastModified = new DateTime(2020, 1, 1, 1, 1, 1, DateTimeKind.Utc);
            storage.Contents        = new MemoryStream(Resources.LineItemsZip);

            var command = new UpdateBillingData(AwsClientFactory, AwsProfileRepository, BillingManager, Clock, new S3PathParser());

            // Act
            command.LoadDeltas(profileId);

            // Assert
            AwsProfileRepositoryMock.Verify(
                x => x.Update(It.Is((AwsProfile p) =>
                                    p.Id == profileId &&
                                    p.BillingMetadata["2014-06"].LastModified == storage.NewLastModified)
                              ));
        }