public Folder GetExistingItems(string folder)
 {
     CatalogItem[] items = _sourceCommunicator.GetExistingReports(folder);
     Folder rootFolder = new Folder();
     rootFolder.Name = folder;
     var itemsList = items.ToList();
     List<Report> reports = new List<Report>();
     List<Folder> folders = new List<Folder>();
     folders.Add(rootFolder);
     foreach (var item in itemsList)
     {
         if (item.Type == ItemTypeEnum.Report)
         {
             var r = new Report(item);
             reports.Add(r);
         }
         else if (item.Type == ItemTypeEnum.Folder)
         {
             var f = new Folder(item);
             folders.Add(f);
         }
     }
     foreach (var currentFolder in folders.Where(f=>f.Depth > 1).OrderBy(f=>f.Depth))
     {
         FindParentFolder(currentFolder, rootFolder);
     }
     foreach (var report in reports)
     {
         FindParentFolder(report, rootFolder);
     }
     return rootFolder;
 }
 private void FindParentFolder(Report report, Folder rootFolder)
 {
     string[] path = report.Path.Split(new []{"/"},StringSplitOptions.RemoveEmptyEntries);
     Folder currentFolder = rootFolder;
     for (int i = 1; i < path.Length-1; i++)
     {
         string currentPath = path[i];
         var tempFolder = currentFolder.SubFolders.FirstOrDefault(f => f.Name.Equals(currentPath, StringComparison.OrdinalIgnoreCase));
         if (tempFolder != null)
         {
             currentFolder = tempFolder;
         }
     }
     report.ParentFolder = currentFolder;
     currentFolder.Reports.Add(report);
 }
        public void UploadReports_SubfolderWithReport_IgnoresUnselected()
        {
            Folder rootFolder = new Folder() {Name = "RootFolder", Path = "/RootFolder"};
            Report report = new Report(){Name="Report", ParentFolder = rootFolder, Path = "/RootFolder/Report"};
            rootFolder.Reports.Add(report);
            Folder subFolder = new Folder(){Name="SubFolder", Path="/RootFolder/SubFolder", Selected =false};
            Report subReport = new Report() { Name = "SubReport", ParentFolder = subFolder, Path = "/RootFolder/SubFolder/SubReport", Selected = false};
            subFolder.Reports.Add(subReport);
            rootFolder.SubFolders.Add(subFolder);

            Mock<IRSCommunicator> sourceComm = new Mock<IRSCommunicator>();
            sourceComm.Setup(s => s.GetReportDefinition("/RootFolder/Report")).Returns(new byte[0]);

            Mock<IRSCommunicator> destComm = new Mock<IRSCommunicator>();

            RSRepository repo = new RSRepository(sourceComm.Object, destComm.Object);

            repo.UploadReports(rootFolder, "NewRootFolder", "dsPath");

            sourceComm.Verify(s=>s.GetReportDefinition("/RootFolder/Report"), Times.Once());
            sourceComm.Verify(s=>s.GetReportDefinition("/RootFolder/SubFolder/SubReport"), Times.Never());

            destComm.Verify(d=>d.CreateFolder("NewRootFolder", "/"), Times.Once());
            destComm.Verify(d => d.CreateReport("Report", "/NewRootFolder", It.IsAny<byte[]>(), "dsPath"), Times.Once());
            destComm.Verify(d => d.CreateFolder("SubFolder", "/NewRootFolder"), Times.Never());
            destComm.Verify(d => d.CreateReport("SubReport", "/NewRootFolder/SubFolder", It.IsAny<byte[]>(), "dsPath"), Times.Never());
        }
        public void UploadReports_SingleReport()
        {
            Folder rootFolder = new Folder() {Name = "RootFolder", Path = "/RootFolder"};
            Report report = new Report(){Name="Report", ParentFolder = rootFolder, Path = "/RootFolder/Report"};
            rootFolder.Reports.Add(report);

            Mock<IRSCommunicator> sourceComm = new Mock<IRSCommunicator>();
            sourceComm.Setup(s => s.GetReportDefinition("/RootFolder/Report")).Returns(new byte[0]);

            Mock<IRSCommunicator> destComm = new Mock<IRSCommunicator>();

            RSRepository repo = new RSRepository(sourceComm.Object, destComm.Object);

            repo.UploadReports(rootFolder, "NewRootFolder", "dsPath");

            sourceComm.Verify(s=>s.GetReportDefinition("/RootFolder/Report"), Times.Once());

            destComm.Verify(d=>d.CreateFolder("NewRootFolder", "/"), Times.Once());
            destComm.Verify(d => d.CreateReport("Report", "/NewRootFolder", It.IsAny<byte[]>(), "dsPath"), Times.Once());
        }