public void WillLogFileProgress()
        {
            var cancellationToken = new CancellationTokenSource();

            _mockClientHelper.Setup(x => x.UploadFileAsync(
                                        It.IsAny <long>(),
                                        It.IsAny <string>(),
                                        It.IsAny <FileStream>(),
                                        It.IsAny <Guid>(),
                                        It.IsAny <CancellationToken>(),
                                        It.IsAny <int>()
                                        ))
            .Callback <long, string, Stream, Guid, CancellationToken, int>((a, b, c, d, e, f) =>
            {
                _mockClientHelper.Raise(x => x.UploadFileReportProgress += null, new ReportProgressEventArgs(b, 50, 100));
                _mockClientHelper.Raise(x => x.UploadFileReportTrace    += null, new ReportTraceEventArgs(b, "completed"));
            })
            .ReturnsAsync(() => {
                return(new HttpResponseMessage(HttpStatusCode.Created));
            });

            var service = new FileContainerService(_mockClientHelper.Object, _context);

            service.CopyToContainerAsync(new Tuple <string, string>(_uploadDirectory, _containerPath), cancellationToken.Token).Wait();

            Assert.IsTrue(_logger.Log.Contains("Uploading 4 files."));
            Assert.IsTrue(_logger.Log.Contains(string.Format(@"File: '{0}\file1' took", _uploadDirectory)));
        }
        public void WillRetryAndPassIfFilesUploadDuringRetry()
        {
            var files    = new List <string>();
            var response = new HttpResponseMessage(HttpStatusCode.Conflict);
            var calls    = 0;

            _mockClientHelper.Setup(x => x.UploadFileAsync(
                                        It.IsAny <long>(),
                                        It.IsAny <string>(),
                                        It.IsAny <FileStream>(),
                                        It.IsAny <Guid>(),
                                        It.IsAny <CancellationToken>(),
                                        It.IsAny <int>()
                                        ))
            .Returns(() => Task.FromResult(response))
            .Callback <long, string, FileStream, Guid, CancellationToken, int>((a, b, c, d, e, f) => {
                calls++;
                if (calls == 4)
                {
                    response = new HttpResponseMessage(HttpStatusCode.Created);
                }
            });

            var service = new FileContainerService(_mockClientHelper.Object, _context);

            service.CopyToContainerAsync(new Tuple <string, string>(_uploadDirectory, _containerPath), new CancellationToken(), false).Wait();

            _mockClientHelper.Verify(y => y.UploadFileAsync(
                                         It.Is <long>(x => x == _context.ContainerId),
                                         It.Is <string>(x => x == "path/file0" || x == "path/file1" || x == "path/summary/file2" || x == "path/summary/file3"),
                                         It.IsAny <FileStream>(),
                                         It.Is <Guid>(x => x.Equals(_context.ProjectId)),
                                         It.IsAny <CancellationToken>(),
                                         It.Is <int>(x => x == 4 * 1024 * 1024)), Times.Exactly(8));
        }
        public void WillRetryAndThrowIfFilesFailToUpload()
        {
            _mockClientHelper.Setup(x => x.UploadFileAsync(
                                        It.IsAny <long>(),
                                        It.IsAny <string>(),
                                        It.IsAny <FileStream>(),
                                        It.IsAny <Guid>(),
                                        It.IsAny <CancellationToken>(),
                                        It.IsAny <int>()
                                        ))
            .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.Conflict)));

            var service = new FileContainerService(_mockClientHelper.Object, _context);

            var ex = Assert.ThrowsException <AggregateException>(() => service.CopyToContainerAsync(new Tuple <string, string>(_uploadDirectory, _containerPath), new CancellationToken(), false).Wait());

            Assert.IsTrue(string.Equals(ex.InnerExceptions[0].Message, Resources.FileUploadFailedAfterRetry));

            _mockClientHelper.Verify(y => y.UploadFileAsync(
                                         It.Is <long>(x => x == _context.ContainerId),
                                         It.Is <string>(x => x == "path/file0" || x == "path/file1" || x == "path/summary/file2" || x == "path/summary/file3"),
                                         It.IsAny <FileStream>(),
                                         It.Is <Guid>(x => x.Equals(_context.ProjectId)),
                                         It.IsAny <CancellationToken>(),
                                         It.Is <int>(x => x == 4 * 1024 * 1024)), Times.Exactly(8));
        }
        public void WillThrowIfCancelRequested()
        {
            var files = new List <string>();

            _mockClientHelper.Setup(x => x.UploadFileAsync(
                                        It.IsAny <long>(),
                                        It.IsAny <string>(),
                                        It.IsAny <FileStream>(),
                                        It.IsAny <Guid>(),
                                        It.IsAny <CancellationToken>(),
                                        It.IsAny <int>()
                                        )).Throws(new OperationCanceledException());

            var service = new FileContainerService(_mockClientHelper.Object, _context);


            var cancellationToken = new CancellationTokenSource();

            cancellationToken.Cancel();

            var ex = Assert.ThrowsException <AggregateException>(() => service.CopyToContainerAsync(new Tuple <string, string>(_uploadDirectory, _containerPath), cancellationToken.Token).Wait());
        }
        public void WillLogIfExceptionOccuredWhileUpload()
        {
            var cancellationToken = new CancellationTokenSource();

            _mockClientHelper.Setup(x => x.UploadFileAsync(
                                        It.IsAny <long>(),
                                        It.IsAny <string>(),
                                        It.IsAny <FileStream>(),
                                        It.IsAny <Guid>(),
                                        It.IsAny <CancellationToken>(),
                                        It.IsAny <int>()
                                        )).Returns(() =>
            {
                cancellationToken.Cancel();
                throw new Exception();
            });

            var service = new FileContainerService(_mockClientHelper.Object, _context);

            Assert.ThrowsException <AggregateException>(() => service.CopyToContainerAsync(new Tuple <string, string>(_uploadDirectory, _containerPath), cancellationToken.Token).Wait());

            Assert.IsTrue(_logger.Log.Contains("Fail to upload"));
        }
        public void UploadFilesTest()
        {
            _mockClientHelper.Setup(x => x.UploadFileAsync(
                                        It.IsAny <long>(),
                                        It.IsAny <string>(),
                                        It.IsAny <FileStream>(),
                                        It.IsAny <Guid>(),
                                        It.IsAny <CancellationToken>(),
                                        It.IsAny <int>()
                                        ))
            .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.Created)));

            var service = new FileContainerService(_mockClientHelper.Object, _context);

            service.CopyToContainerAsync(new Tuple <string, string>(_uploadDirectory, _containerPath), new CancellationToken()).Wait();

            _mockClientHelper.Verify(y => y.UploadFileAsync(
                                         It.Is <long>(x => x == _context.ContainerId),
                                         It.Is <string>(x => x == "path/file0" || x == "path/file1" || x == "path/summary/file2" || x == "path/summary/file3"),
                                         It.IsAny <FileStream>(),
                                         It.Is <Guid>(x => x.Equals(_context.ProjectId)),
                                         It.IsAny <CancellationToken>(),
                                         It.Is <int>(x => x == 4 * 1024 * 1024)), Times.Exactly(4));
        }