public void Generate_shouldFilterOutTheInboxFolder()
        {
            var root = new MegaNzNodeMock
            {
                Id       = "1",
                Name     = @"\",
                ParentId = string.Empty,
                Type     = NodeType.Root
            };

            var inboxFolder = new MegaNzNodeMock
            {
                Id       = "2",
                Name     = null,
                ParentId = string.Empty,
                Type     = NodeType.Inbox
            };

            var megaNzCollection = new List <INode> {
                root,
                inboxFolder
            };

            // Act
            var generator = new MegaNzItemListGenerator();

            MegaNzItem[] result = generator.Generate(megaNzCollection).ToArray();

            // Assert
            result.Length.Should().Be(1);
            result[0].MegaNzNode.Type.Should().Be(NodeType.Root);
        }
Пример #2
0
        public void Constructor_usingINodeOfTypeFile_shouldParseTheCorrectLastModificationDatetime()
        {
            // We need a structure like this:
            // root
            //    +--> folder1
            //               +--> folder2
            //                          +--> file1
            var folder1 = new MegaNzNodeMock
            {
                Id       = "2",
                Name     = "folder1",
                Owner    = "User",
                ParentId = "1",
                Size     = 0,
                Type     = NodeType.Directory
            };

            var folder2 = new MegaNzNodeMock
            {
                Id       = "3",
                Name     = "folder2",
                Owner    = "User",
                ParentId = "2",
                Size     = 0,
                Type     = NodeType.Directory
            };

            var file1 = new MegaNzNodeMock
            {
                Id       = "4",
                Name     = "file1_[[2016-1-2-3-4-5]].jpeg",
                Owner    = "User",
                ParentId = "3",
                Size     = 1024,
                Type     = NodeType.File
            };

            var nodeDictionary = new Dictionary <string, INode> {
                { _root.Id, _root },
                { folder1.Id, folder1 },
                { folder2.Id, folder2 },
                { file1.Id, file1 }
            };

            // Act
            var file1Item = new MegaNzItem(file1, nodeDictionary);

            // Assert
            file1Item.LastModified.Should().HaveValue();
            file1Item.LastModified.Value.Year.Should().Be(2016);
            file1Item.LastModified.Value.Month.Should().Be(1);
            file1Item.LastModified.Value.Day.Should().Be(2);
            file1Item.LastModified.Value.Hour.Should().Be(3);
            file1Item.LastModified.Value.Minute.Should().Be(4);
            file1Item.LastModified.Value.Second.Should().Be(5);
        }
        public void Generate_shouldFilterOutTheInboxFolderHierarchyNodes()
        {
            var root = new MegaNzNodeMock
            {
                Id       = "1",
                Name     = @"\",
                ParentId = string.Empty,
                Type     = NodeType.Root
            };

            var inboxFolder = new MegaNzNodeMock
            {
                Id       = "2",
                Name     = null,
                ParentId = string.Empty,
                Type     = NodeType.Trash
            };

            var folderChildOfInbox = new MegaNzNodeMock
            {
                Id       = "3",
                Name     = "folder1",
                ParentId = inboxFolder.Id,
                Type     = NodeType.Directory
            };

            var fileChildOfInbox = new MegaNzNodeMock
            {
                Id                   = "4",
                Name                 = "file1",
                ParentId             = folderChildOfInbox.Id,
                Type                 = NodeType.File,
                Size                 = 1024,
                LastModificationDate = new DateTime(2016, 1, 1, 0, 0, 0)
            };

            var megaNzCollection = new List <INode> {
                root,
                inboxFolder,
                folderChildOfInbox,
                fileChildOfInbox
            };

            // Act
            var generator = new MegaNzItemListGenerator();

            MegaNzItem[] result = generator.Generate(megaNzCollection).ToArray();

            // Assert
            result.Length.Should().Be(1);
            result[0].MegaNzNode.Type.Should().Be(NodeType.Root);
        }
Пример #4
0
        public void Constructor_usingINodeOfTypeDirectory_shouldBuildTheCorrectPath()
        {
            // We need a structure like this:
            // root
            //    +--> folder1
            //               +--> folder2
            //                          +--> folder3
            var folder1 = new MegaNzNodeMock
            {
                Id       = "2",
                Name     = "folder1",
                Owner    = "User",
                ParentId = "1",
                Size     = 0,
                Type     = NodeType.Directory
            };

            var folder2 = new MegaNzNodeMock
            {
                Id       = "3",
                Name     = "folder2",
                Owner    = "User",
                ParentId = "2",
                Size     = 0,
                Type     = NodeType.Directory
            };

            var folder3 = new MegaNzNodeMock
            {
                Id       = "4",
                Name     = "folder3",
                Owner    = "User",
                ParentId = "3",
                Size     = 0,
                Type     = NodeType.Directory
            };

            var nodeDictionary = new Dictionary <string, INode> {
                { _root.Id, _root },
                { folder1.Id, folder1 },
                { folder2.Id, folder2 },
                { folder3.Id, folder3 }
            };

            // Act
            var folder3Item = new MegaNzItem(folder3, nodeDictionary);

            // Assert
            folder3Item.Path.Should().Be(@"\folder1\folder2\folder3\"); // We want to have the backslash at the end of the path for the directory item
        }
Пример #5
0
        public void Constructor_usingINodeOfTypeFile_shouldBuildTheCorrectPath()
        {
            // We need a structure like this:
            // root
            //    +--> folder1
            //               +--> folder2
            //                          +--> file1
            var folder1 = new MegaNzNodeMock
            {
                Id       = "2",
                Name     = "folder1",
                Owner    = "User",
                ParentId = "1",
                Size     = 0,
                Type     = NodeType.Directory
            };

            var folder2 = new MegaNzNodeMock
            {
                Id       = "3",
                Name     = "folder2",
                Owner    = "User",
                ParentId = "2",
                Size     = 0,
                Type     = NodeType.Directory
            };

            var file1 = new MegaNzNodeMock
            {
                Id       = "4",
                Name     = "file1_[[2016-1-1-0-0-1]].jpeg",
                Owner    = "User",
                ParentId = "3",
                Size     = 1024,
                Type     = NodeType.File
            };

            var nodeDictionary = new Dictionary <string, INode> {
                { _root.Id, _root },
                { folder1.Id, folder1 },
                { folder2.Id, folder2 },
                { file1.Id, file1 }
            };

            // Act
            var file1Item = new MegaNzItem(file1, nodeDictionary);

            // Assert
            file1Item.Path.Should().Be(@"\folder1\folder2\file1.jpeg");
        }
Пример #6
0
        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);
        }
Пример #7
0
        public void Execute_withACreateFolderCommand_shouldUploadTheMegaNzItemCollection()
        {
            // Scenario
            // Local file structure
            // root
            //    |
            //    +--> folder1
            //               |
            //               +--> folder2
            //
            // Remote file structure
            // root
            //    |
            //    +--> folder1

            // Arrange
            var createFolderCommand = new CreateFolderCommand
            {
                ParentPath = "\\folder1",
                Name       = "folder2"
            };
            var commandList = new[] { createFolderCommand };

            // Remote items
            // Root in the remote file structure
            var mockMegaNzNodeForRemoteRoot = new Mock <INode>(MockBehavior.Strict);

            mockMegaNzNodeForRemoteRoot.SetupGet(m => m.Id).Returns("0");
            mockMegaNzNodeForRemoteRoot.SetupGet(m => m.Type).Returns(NodeType.Root);
            mockMegaNzNodeForRemoteRoot.SetupGet(m => m.Name).Returns("\\");
            var megaNzItemRemoteRoot = new MegaNzItem(mockMegaNzNodeForRemoteRoot.Object, "\\", ItemType.Folder, "\\", 0);

            // Folder1 in the remote file structure
            var mockMegaNzNodeForRemoteFolder1 = new Mock <INode>(MockBehavior.Strict);

            mockMegaNzNodeForRemoteFolder1.SetupGet(m => m.Id).Returns("1");
            mockMegaNzNodeForRemoteFolder1.SetupGet(m => m.ParentId).Returns("0");
            mockMegaNzNodeForRemoteFolder1.SetupGet(m => m.Type).Returns(NodeType.Directory);
            mockMegaNzNodeForRemoteFolder1.SetupGet(m => m.Name).Returns("folder1");
            var megaNzItemForRemoteFolder1 = new MegaNzItem(mockMegaNzNodeForRemoteFolder1.Object, "\\folder1", ItemType.Folder, "\\folder1", 0);

            var remoteItems = new[] {
                megaNzItemRemoteRoot,
                megaNzItemForRemoteFolder1
            };
            var megaNzItemCollection = new MegaNzItemCollection(remoteItems);

            // The MegaNzNode for the folder created
            var mockMegaNzNodeForRemoteFolder2 = new MegaNzNodeMock {
                Id       = "2",
                Name     = "folder2",
                Type     = NodeType.Directory,
                Size     = 0,
                ParentId = "1"
            };

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

            mockMegaApiClient
            .Setup(m => m.CreateFolder(createFolderCommand.Name, mockMegaNzNodeForRemoteFolder1.Object))
            .Returns(mockMegaNzNodeForRemoteFolder2);

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

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

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

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

            // Assert
            megaNzItemCollection.GetList().Should().Contain(item => item.MegaNzNode.Id == mockMegaNzNodeForRemoteFolder2.Id);
        }
        public void Generate_shouldReturnTheCorrectCollectionOfItems()
        {
            // Given the following file tree in MegaNZ
            // root
            //    |
            //    +--> Folder1
            //    |          |
            //    |          +--> File1_[[2016-1-1-0-0-0]].jpeg
            //    |
            //    +--> File2_[[2017-2-2-1-1-1]].jpeg
            //
            // The result collection should contain:
            // - / (root)
            // - /Folder1/
            // - /Folder1/File1.jpeg (with last modification date = 1st Gen 2016)
            // - /File2.jpeg (with last modification date = 2st Feb 2016 01:01:01)
            var root = new MegaNzNodeMock {
                Id       = "1",
                Name     = @"\",
                ParentId = string.Empty,
                Type     = NodeType.Root
            };

            var folder1 = new MegaNzNodeMock
            {
                Id       = "2",
                Name     = "Folder1",
                ParentId = root.Id,
                Type     = NodeType.Directory
            };

            var file1 = new MegaNzNodeMock
            {
                Id       = "3",
                Name     = "File1_[[2016-1-1-0-0-0]].jpeg",
                ParentId = folder1.Id,
                Type     = NodeType.File,
                Size     = 1024
            };

            var file2 = new MegaNzNodeMock
            {
                Id       = "4",
                Name     = "File2_[[2017-2-2-1-1-1]].jpeg",
                ParentId = root.Id,
                Type     = NodeType.File,
                Size     = 2048
            };

            var megaNzCollection = new List <INode> {
                root,
                folder1,
                file1,
                file2
            };

            // Act
            var generator = new MegaNzItemListGenerator();

            MegaNzItem[] result = generator.Generate(megaNzCollection).ToArray();

            // Assert
            result.Length.Should().Be(4);

            result[0].Name.Should().Be(@"\");
            result[0].Path.Should().Be(@"\");
            result[0].Type.Should().Be(ItemType.Folder);

            result[1].Name.Should().Be("Folder1");
            result[1].Path.Should().Be(@"\Folder1\");  // Forlder's path MUST end with a backslash
            result[1].Type.Should().Be(ItemType.Folder);

            result[2].Name.Should().Be("File1.jpeg");
            result[2].Path.Should().Be(@"\Folder1\File1.jpeg");
            result[2].LastModified.Should().HaveValue();
            result[2].LastModified.Value.Year.Should().Be(2016);
            result[2].LastModified.Value.Month.Should().Be(1);
            result[2].LastModified.Value.Day.Should().Be(1);
            result[2].LastModified.Value.Hour.Should().Be(0);
            result[2].LastModified.Value.Minute.Should().Be(0);
            result[2].LastModified.Value.Second.Should().Be(0);
            result[2].Type.Should().Be(ItemType.File);

            result[3].Name.Should().Be("File2.jpeg");
            result[3].Path.Should().Be(@"\File2.jpeg");
            result[3].LastModified.Should().HaveValue();
            result[3].LastModified.Value.Year.Should().Be(2017);
            result[3].LastModified.Value.Month.Should().Be(2);
            result[3].LastModified.Value.Day.Should().Be(2);
            result[3].LastModified.Value.Hour.Should().Be(1);
            result[3].LastModified.Value.Minute.Should().Be(1);
            result[3].LastModified.Value.Second.Should().Be(1);
            result[3].Type.Should().Be(ItemType.File);
        }