Example #1
0
        public async Task SyncBasicTests()
        {
            var mockTracer = new Mock<ITracer>();
            mockTracer
                .Setup(m => m.Trace(It.IsAny<string>(), It.IsAny<IDictionary<string, string>>()));

            var repository = Mock.Of<IRepository>();
            var fileSystem = new Mock<IFileSystem>();
            var fileBase = new Mock<FileBase>();
            var fileInfoFactory = new Mock<IFileInfoFactory>();
            var fileInfo = new Mock<FileInfoBase>();
            var dirBase = new Mock<DirectoryBase>();
            var dirInfoFactory = new Mock<IDirectoryInfoFactory>(); // mock dirInfo to make FileSystemHelpers.DeleteDirectorySafe not throw exception
            var dirInfoBase = new Mock<DirectoryInfoBase>();
            fileSystem.Setup(f => f.File).Returns(fileBase.Object);
            fileSystem.Setup(f => f.FileInfo).Returns(fileInfoFactory.Object);
            fileInfoFactory.Setup(f => f.FromFileName(It.IsAny<string>()))
                           .Returns(() => fileInfo.Object);
            fileSystem.Setup(f => f.Directory).Returns(dirBase.Object);
            fileSystem.Setup(f => f.DirectoryInfo).Returns(dirInfoFactory.Object);
            dirInfoFactory.Setup(d => d.FromDirectoryName(It.IsAny<string>())).Returns(dirInfoBase.Object);
            fileBase.Setup(fb => fb.Exists(It.IsAny<string>())).Returns((string path) =>
            {
                return (path != null && (path.EndsWith("f-delete") || path.EndsWith("bar.txt")));
            });
            fileBase.Setup(fb => fb.SetLastWriteTimeUtc(It.IsAny<string>(), It.IsAny<DateTime>()));

            dirBase.Setup(d => d.Exists(It.IsAny<string>())).Returns((string path) =>
            {
                return (path != null && (path.EndsWith("f-delete-dir") || path.EndsWith("f2")));
            });
            FileSystemHelpers.Instance = fileSystem.Object;

            // prepare change from OneDrive
            OneDriveModel.OneDriveChange change = new OneDriveModel.OneDriveChange();
            change.IsDeleted = false;

            // prepare OneDriveInfo
            var info = new OneDriveInfo();
            info.AccessToken = "fake-token";
            info.RepositoryUrl = "https://api.onedrive.com/v1.0/drive/special/approot:/fake-folder";
            info.TargetChangeset = new ChangeSet("id", "authorName", "authorEmail", "message", DateTime.UtcNow);

            // prepare http handler
            var handler = new TestMessageHandler((HttpRequestMessage message) =>
            {
                StringContent content = null;
                if (message != null && message.RequestUri != null)
                {
                    if (message.RequestUri.AbsoluteUri.Equals(info.RepositoryUrl))
                    {
                        content = new StringContent(@"{ 'id': 'fake-id'}", Encoding.UTF8, "application/json");
                        return new HttpResponseMessage { Content = content };
                    }
                    else if (message.RequestUri.AbsoluteUri.Equals("https://api.onedrive.com/v1.0/drive/items/fake-id/view.changes"))
                    {
                        content = new StringContent(ViewChangePayload, Encoding.UTF8, "application/json");
                        return new HttpResponseMessage { Content = content };
                    }
                    else if (message.RequestUri.AbsoluteUri.EndsWith("items/A6034FFBC93398FD!331")
                        || message.RequestUri.AbsoluteUri.EndsWith("items/A6034FFBC93398FD!330"))
                    {
                        content = new StringContent(@"{ '@content.downloadUrl': 'http://site-does-not-exist.microsoft.com'}", Encoding.UTF8, "application/json");
                        return new HttpResponseMessage { Content = content };
                    }
                }

                content = new StringContent("test file content", Encoding.UTF8, "application/json");
                return new HttpResponseMessage { Content = content };
            });

            // perform action
            OneDriveHelper helper = CreateMockOneDriveHelper(handler: handler, tracer: mockTracer.Object);
            await helper.Sync(info, repository);

            // verification
            /*
             Sycing f2 to wwwroot:
             
                 There are 6 changes
                    2 deletion
                      f2\f-delete       (existed as file)
                      f2\f-delete-dir   (existed as folder)
  
                    2 file changes
                      f2\foo.txt        (not existed)
                      f2\f22\bar.txt    (existed)

                    2 folder chagnes
                      f2                (existed)
                      f2\f22            (not existed)
             */

            // deletion
            mockTracer.Verify(t => t.Trace(@"Deleted file D:\home\site\wwwroot\f-delete", It.Is<IDictionary<string, string>>(d => d.Count == 0)));
            mockTracer.Verify(t => t.Trace(@"Deleted directory D:\home\site\wwwroot\f-delete-dir", It.Is<IDictionary<string, string>>(d => d.Count == 0)));

            // file changes
            mockTracer.Verify(t => t.Trace(@"Creating file D:\home\site\wwwroot\foo.txt ...", It.Is<IDictionary<string, string>>(d => d.Count == 0)));
            mockTracer.Verify(t => t.Trace(@"Updating file D:\home\site\wwwroot\f22\bar.txt ...", It.Is<IDictionary<string, string>>(d => d.Count == 0)));

            mockTracer.Verify(t => t.Trace(@"Deleted file D:\home\site\wwwroot\f-delete", It.Is<IDictionary<string, string>>(d => d.Count == 0)));
            mockTracer.Verify(t => t.Trace(@"Deleted directory D:\home\site\wwwroot\f-delete-dir", It.Is<IDictionary<string, string>>(d => d.Count == 0)));

            // directory changes
            mockTracer.Verify(t => t.Trace(@"Ignore folder f2", It.Is<IDictionary<string, string>>(d => d.Count == 0)));
            mockTracer.Verify(t => t.Trace(@"Creating directory D:\home\site\wwwroot\f22 ...", It.Is<IDictionary<string, string>>(d => d.Count == 0)));
        }
Example #2
0
        public async Task SyncBasicTests()
        {
            var mockTracer = new Mock <ITracer>();

            mockTracer
            .Setup(m => m.Trace(It.IsAny <string>(), It.IsAny <IDictionary <string, string> >()));

            var repository      = Mock.Of <IRepository>();
            var fileSystem      = new Mock <IFileSystem>();
            var fileBase        = new Mock <FileBase>();
            var fileInfoFactory = new Mock <IFileInfoFactory>();
            var fileInfo        = new Mock <FileInfoBase>();
            var dirBase         = new Mock <DirectoryBase>();
            var dirInfoFactory  = new Mock <IDirectoryInfoFactory>(); // mock dirInfo to make FileSystemHelpers.DeleteDirectorySafe not throw exception
            var dirInfoBase     = new Mock <DirectoryInfoBase>();

            fileSystem.Setup(f => f.File).Returns(fileBase.Object);
            fileSystem.Setup(f => f.FileInfo).Returns(fileInfoFactory.Object);
            fileInfoFactory.Setup(f => f.FromFileName(It.IsAny <string>()))
            .Returns(() => fileInfo.Object);
            fileSystem.Setup(f => f.Directory).Returns(dirBase.Object);
            fileSystem.Setup(f => f.DirectoryInfo).Returns(dirInfoFactory.Object);
            dirInfoFactory.Setup(d => d.FromDirectoryName(It.IsAny <string>())).Returns(dirInfoBase.Object);
            fileBase.Setup(fb => fb.Exists(It.IsAny <string>())).Returns((string path) =>
            {
                return(path != null && (path.EndsWith("f-delete") || path.EndsWith("bar.txt")));
            });
            fileBase.Setup(fb => fb.SetLastWriteTimeUtc(It.IsAny <string>(), It.IsAny <DateTime>()));

            dirBase.Setup(d => d.Exists(It.IsAny <string>())).Returns((string path) =>
            {
                return(path != null && (path.EndsWith("f-delete-dir") || path.EndsWith("f2")));
            });
            FileSystemHelpers.Instance = fileSystem.Object;

            // prepare change from OneDrive
            OneDriveModel.OneDriveChange change = new OneDriveModel.OneDriveChange();
            change.IsDeleted = false;

            // prepare OneDriveInfo
            var info = new OneDriveInfo();

            info.AccessToken     = "fake-token";
            info.RepositoryUrl   = "https://api.onedrive.com/v1.0/drive/special/approot:/fake-folder";
            info.TargetChangeset = new ChangeSet("id", "authorName", "authorEmail", "message", DateTime.UtcNow);

            // prepare http handler
            var handler = new TestMessageHandler((HttpRequestMessage message) =>
            {
                StringContent content = null;
                if (message != null && message.RequestUri != null)
                {
                    if (message.RequestUri.AbsoluteUri.Equals(info.RepositoryUrl))
                    {
                        content = new StringContent(@"{ 'id': 'fake-id'}", Encoding.UTF8, "application/json");
                        return(new HttpResponseMessage {
                            Content = content
                        });
                    }
                    else if (message.RequestUri.AbsoluteUri.Equals("https://api.onedrive.com/v1.0/drive/items/fake-id/view.changes"))
                    {
                        content = new StringContent(ViewChangePayload, Encoding.UTF8, "application/json");
                        return(new HttpResponseMessage {
                            Content = content
                        });
                    }
                    else if (message.RequestUri.AbsoluteUri.EndsWith("items/A6034FFBC93398FD!331") ||
                             message.RequestUri.AbsoluteUri.EndsWith("items/A6034FFBC93398FD!330"))
                    {
                        content = new StringContent(@"{ '@content.downloadUrl': 'http://site-does-not-exist.microsoft.com'}", Encoding.UTF8, "application/json");
                        return(new HttpResponseMessage {
                            Content = content
                        });
                    }
                }

                content = new StringContent("test file content", Encoding.UTF8, "application/json");
                return(new HttpResponseMessage {
                    Content = content
                });
            });

            // perform action
            OneDriveHelper helper = CreateMockOneDriveHelper(handler: handler, tracer: mockTracer.Object);
            await helper.Sync(info, repository);

            // verification

            /*
             * Sycing f2 to wwwroot:
             *
             *   There are 6 changes
             *      2 deletion
             *        f2\f-delete       (existed as file)
             *        f2\f-delete-dir   (existed as folder)
             *
             *      2 file changes
             *        f2\foo.txt        (not existed)
             *        f2\f22\bar.txt    (existed)
             *
             *      2 folder chagnes
             *        f2                (existed)
             *        f2\f22            (not existed)
             */

            // deletion
            mockTracer.Verify(t => t.Trace(@"Deleted file D:\home\site\wwwroot\f-delete", It.Is <IDictionary <string, string> >(d => d.Count == 0)));
            mockTracer.Verify(t => t.Trace(@"Deleted directory D:\home\site\wwwroot\f-delete-dir", It.Is <IDictionary <string, string> >(d => d.Count == 0)));

            // file changes
            mockTracer.Verify(t => t.Trace(@"Creating file D:\home\site\wwwroot\foo.txt ...", It.Is <IDictionary <string, string> >(d => d.Count == 0)));
            mockTracer.Verify(t => t.Trace(@"Updating file D:\home\site\wwwroot\f22\bar.txt ...", It.Is <IDictionary <string, string> >(d => d.Count == 0)));

            mockTracer.Verify(t => t.Trace(@"Deleted file D:\home\site\wwwroot\f-delete", It.Is <IDictionary <string, string> >(d => d.Count == 0)));
            mockTracer.Verify(t => t.Trace(@"Deleted directory D:\home\site\wwwroot\f-delete-dir", It.Is <IDictionary <string, string> >(d => d.Count == 0)));

            // directory changes
            mockTracer.Verify(t => t.Trace(@"Ignore folder f2", It.Is <IDictionary <string, string> >(d => d.Count == 0)));
            mockTracer.Verify(t => t.Trace(@"Creating directory D:\home\site\wwwroot\f22 ...", It.Is <IDictionary <string, string> >(d => d.Count == 0)));
        }