Example #1
0
        public void BuilRemoteFilename_forAFile_shouldBuildTheCorrectFilename()
        {
            var lastModificationDate = new DateTime(2016, 1, 1, 0, 0, 0);
            var filename             = "filename.jpg";

            var result = NameHandler.BuildRemoteFileName(filename, lastModificationDate);

            Assert.AreEqual("filename_[[2016-1-1-0-0-0]].jpg", result);
        }
 public void Execute(IMegaApiClient megaApiClient,
                     IMegaNzItemCollection megaNzItemCollection,
                     IFileManager fileManager,
                     IProgress <double> progressNotifier)
 {
     using (var filestream = fileManager.GetStreamToReadFile(SourcePath))
     {
         var sourceFileName   = System.IO.Path.GetFileName(SourcePath);
         var remoteFileName   = NameHandler.BuildRemoteFileName(sourceFileName, LastModifiedDate);
         var parentMegaNzNode = megaNzItemCollection.GetByPath(DestinationPath);
         var newMegaNzNode    = megaApiClient.UploadAsync(filestream, remoteFileName, parentMegaNzNode, progressNotifier).Result;
         megaNzItemCollection.Add(newMegaNzNode);
     }
 }
        public void Execute_withAnUploadCommand_shouldUploadTheMegaNzItemCollection()
        {
            // Arrange
            const string filename         = "File1.jpeg";
            var          sourcePath       = @"c:\testing\" + filename;
            var          lastModifiedDate = new DateTime(2016, 1, 1, 0, 0, 0);

            // The command list to be executed
            var commandList = new ICommand[] {
                new UploadFileCommand()
                {
                    SourcePath       = sourcePath,
                    DestinationPath  = @"\",
                    LastModifiedDate = lastModifiedDate
                }
            };

            // the initial MegaNZ node list
            const string rootName     = @"\";
            const string rootPath     = @"\";
            const string rootMegaNzId = "0";
            var          mockMegaNzNodeForRemoteRoot = new Mock <INode>(MockBehavior.Strict);

            mockMegaNzNodeForRemoteRoot.SetupGet(m => m.Id).Returns(rootMegaNzId);
            mockMegaNzNodeForRemoteRoot.SetupGet(m => m.Type).Returns(NodeType.Root);
            var remoteItems = new List <MegaNzItem> {
                new MegaNzItem(mockMegaNzNodeForRemoteRoot.Object, rootName, ItemType.Folder, rootPath, 0)
            };
            var megaNzItemCollection = new MegaNzItemCollection(remoteItems);

            const string newFileMegaNzId  = "1";
            var          uploadResultNode = new MegaNzNodeMock
            {
                Id                   = newFileMegaNzId,
                Name                 = NameHandler.BuildRemoteFileName(filename, lastModifiedDate),
                ParentId             = rootMegaNzId,
                Size                 = 1024,
                Type                 = NodeType.File,
                LastModificationDate = new DateTime(2016, 1, 1, 0, 0, 0)
            };

            // The name of the uploaded file
            var uploadedFileName = NameHandler.BuildRemoteFileName(filename, lastModifiedDate);

            var mockMegaApiClient    = new Mock <IMegaApiClient>(MockBehavior.Strict);
            var mockProgressNotifier = new Mock <IProgress <double> >(MockBehavior.Strict);

            mockMegaApiClient.Setup(m => m.UploadAsync(It.IsAny <Stream>(),
                                                       uploadedFileName,
                                                       It.Is <INode>(node => node.Id == rootMegaNzId),
                                                       mockProgressNotifier.Object))
            .ReturnsAsync(uploadResultNode);

            var mockFileManager = new Mock <IFileManager>(MockBehavior.Strict);

            mockFileManager.Setup(m => m.GetStreamToReadFile(sourcePath)).Returns((FileStream)null);

            // Act
            var executor = new CommandExecutor(mockMegaApiClient.Object, _mockLogger.Object);

            executor.Execute(commandList, megaNzItemCollection, mockFileManager.Object, mockProgressNotifier.Object);

            // Assert
            megaNzItemCollection.GetList().Should().Contain(item => item.MegaNzNode.Id == newFileMegaNzId);
        }