private IBackupService GetMockBackupServiceForRestore(IBackupRepository backupRepository,
                                                              BackupOptions options)
        {
            Mock <IAppServiceProvider> mockAppProvider = new Mock <IAppServiceProvider>();

            mockAppProvider
            .Setup(a => a.GetAppsByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <App> >(null));

            Mock <IStandardServiceProvider> mockStandardProvider = new Mock <IStandardServiceProvider>();

            mockStandardProvider
            .Setup(a => a.GetStandardComponentsByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <StandardComponent> >(null));

            Mock <IChartServiceProvider> mockChartProvider = new Mock <IChartServiceProvider>();

            mockChartProvider
            .Setup(a => a.GetChartsByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <Chart> >(null));

            Mock <IDynamicListServiceProvider> mockDynamicListProvider = new Mock <IDynamicListServiceProvider>();

            mockDynamicListProvider
            .Setup(a => a.GetDynamicListsByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <DynamicList> >(null));

            Mock <IPageServiceProvider> mockPageProvider = new Mock <IPageServiceProvider>();

            mockPageProvider
            .Setup(a => a.GetPagesByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <Page> >(null));

#pragma warning disable CA2000 // Dispose objects before losing scope
            InternalDatabaseServiceProvider databaserProvider = new InternalDatabaseServiceProvider(
                new DatabaseService(null, null),
                new DatabaseMongoRepository(_context.GetMongoConnection()));
#pragma warning restore CA2000 // Dispose objects before losing scope

            Mock <IFileSeviceProvider> mockFileProvider = new Mock <IFileSeviceProvider>();
            mockFileProvider
            .Setup(a => a.UploadFileAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>()))
            .Returns(Task.FromResult(new ResponseUploadFile {
                DownloadableUrl = "http://localhost", FileId = DataUtil.GenerateUniqueId()
            }));

            mockFileProvider
            .Setup(a => a.ValidateFile(It.IsAny <IFormFile>()))
            .Returns(Task.FromResult(true));

            mockFileProvider
            .Setup(a => a.DownloadFileAsync(It.IsAny <string>()))
            .Returns(Task.FromResult(new ResponseDownloadFile
            {
                FileBytes = File.ReadAllBytes(@"Artifacts\" + zipFileName),
                FileName  = zipFileName,
                MIMEType  = "application/zip"
            }));

            IOptionsMonitor <BackupOptions> backupOptionsMock = Mock.Of <IOptionsMonitor <BackupOptions> >(_ => _.CurrentValue == options);

            Mock <ICompositeControlServiceProvider> mockCompositeControlProvider = new Mock <ICompositeControlServiceProvider>();
            mockCompositeControlProvider
            .Setup(a => a.GetByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <CompositeControl> >(null));

            BackupService backupService = new BackupService(
                mockAppProvider.Object,
                mockStandardProvider.Object,
                mockChartProvider.Object,
                mockDynamicListProvider.Object,
                databaserProvider,
                mockPageProvider.Object,
                mockFileProvider.Object,
                backupRepository,
                mockCompositeControlProvider.Object,
                backupOptionsMock,
                new FakeServiceLogger <BackupService>());

            databaserProvider.Dispose();

            return(backupService);
        }
Exemplo n.º 2
0
        public async Task <IEnumerable <string> > Proceed(
            string folderPath,
            DatabaseOptions databaseOptions)
        {
            // Ensure this folder is exist
            if (!Directory.Exists(folderPath))
            {
                return(Enumerable.Empty <string>());
            }
            var allZipFiles     = Directory.GetFiles(folderPath, "*.zip");
            var allRunningFiles = new List <string>();

            if (allZipFiles != null && allZipFiles.Length > 0)
            {
                if (Directory.Exists("Temp"))
                {
                    Directory.Delete("Temp", true);
                }

                Directory.CreateDirectory("Temp");

                foreach (var file in allZipFiles)
                {
                    var fileNameWithoutExt = Path.GetFileNameWithoutExtension(file);
                    var extractFolder      = Path.Combine("Temp", fileNameWithoutExt);
                    ZipFile.ExtractToDirectory(file, extractFolder);

                    var backupFlatternModel = ConvertUtil.DeserializeObject <BackupFlatternFileModel>(File.ReadAllText(Path.Combine(extractFolder, fileNameWithoutExt + ".json")));

                    foreach (var chainingFile in backupFlatternModel.ChainingFiles)
                    {
                        switch (chainingFile)
                        {
                        case BackupService.APP_FILE:
                            var            appFilePath    = Path.Combine(extractFolder, BackupService.APP_FILE);
                            var            appsListString = File.ReadAllText(appFilePath);
                            var            appsList       = ConvertUtil.DeserializeObject <IEnumerable <App> >(appsListString);
                            IAppRepository appRepository;
                            if (databaseOptions.ConnectionType == ConnectionType.MongoDB)
                            {
#pragma warning disable CA2000 // Dispose objects before losing scope
                                appRepository = new AppMongoRepository(new MongoConnection(databaseOptions));
#pragma warning restore CA2000 // Dispose objects before losing scope
                            }
                            else
                            {
#pragma warning disable CA2000 // Dispose objects before losing scope
                                appRepository = new AppEFRepository(new Portal.Repositories.PortalDbContext(databaseOptions));
#pragma warning restore CA2000 // Dispose objects before losing scope
                            }

                            var appServiceProvider = new InternalAppServiceProvider(appRepository);
                            await appServiceProvider.ForceUpdateApps(appsList);

                            appServiceProvider.Dispose();
                            break;

                        case BackupService.STANDARD_FILE:
                            var standardFilePath = Path.Combine(extractFolder, BackupService.STANDARD_FILE);
                            var standardsString  = File.ReadAllText(standardFilePath);
                            var standardsList    = ConvertUtil.DeserializeObject <IEnumerable <StandardComponent> >(standardsString);
                            IStandardRepository standardRepository;
                            if (databaseOptions.ConnectionType == ConnectionType.MongoDB)
                            {
#pragma warning disable CA2000 // Dispose objects before losing scope
                                standardRepository = new StandardMongoRepository(new MongoConnection(databaseOptions));
#pragma warning restore CA2000 // Dispose objects before losing scope
                            }
                            else
                            {
#pragma warning disable CA2000 // Dispose objects before losing scope
                                standardRepository = new StandardEFRepository(new Portal.Repositories.PortalDbContext(databaseOptions));
#pragma warning restore CA2000 // Dispose objects before losing scope
                            }
                            var standardServiceProvider = new InternalStandardServiceProvider(standardRepository);
                            await standardServiceProvider.ForceUpdateStandards(standardsList);

                            standardServiceProvider.Dispose();
                            break;

                        case BackupService.CHART_FILE:
                            var chartFilePath = Path.Combine(extractFolder, BackupService.CHART_FILE);
                            var chartsString  = File.ReadAllText(chartFilePath);
                            var chartsList    = ConvertUtil.DeserializeObject <IEnumerable <Chart> >(chartsString);
                            IChartRepository chartRepository;
                            if (databaseOptions.ConnectionType == ConnectionType.MongoDB)
                            {
#pragma warning disable CA2000 // Dispose objects before losing scope
                                chartRepository = new ChartMongoRepository(new MongoConnection(databaseOptions));
#pragma warning restore CA2000 // Dispose objects before losing scope
                            }
                            else
                            {
#pragma warning disable CA2000 // Dispose objects before losing scope
                                chartRepository = new ChartEFRepository(new Portal.Repositories.PortalDbContext(databaseOptions));
#pragma warning restore CA2000 // Dispose objects before losing scope
                            }
                            var chartServiceProvider = new InternalChartServiceProvider(chartRepository);
                            await chartServiceProvider.ForceUpdateCharts(chartsList);

                            chartServiceProvider.Dispose();
                            break;

                        case BackupService.DATABASE_FILE:
                            var databaseFilePath = Path.Combine(extractFolder, BackupService.DATABASE_FILE);
                            var databasesString  = File.ReadAllText(databaseFilePath);
                            var databasesList    = ConvertUtil.DeserializeObject <IEnumerable <DatabaseConnection> >(databasesString);
                            IDatabaseRepository databaseRepository;
                            if (databaseOptions.ConnectionType == ConnectionType.MongoDB)
                            {
#pragma warning disable CA2000 // Dispose objects before losing scope
                                databaseRepository = new DatabaseMongoRepository(new MongoConnection(databaseOptions));
#pragma warning restore CA2000 // Dispose objects before losing scope
                            }
                            else
                            {
#pragma warning disable CA2000 // Dispose objects before losing scope
                                databaseRepository = new DatabaseEFRepository(new PortalDbContext(databaseOptions));
#pragma warning restore CA2000 // Dispose objects before losing scope
                            }
                            var databaseServiceProvider = new InternalDatabaseServiceProvider(new DatabaseService(null, null), databaseRepository);
                            await databaseServiceProvider.ForceUpdateDatabases(databasesList);

                            databaseServiceProvider.Dispose();
                            break;

                        case BackupService.PAGE_FILE:
                            var             pageFilePath = Path.Combine(extractFolder, BackupService.PAGE_FILE);
                            var             pagesString  = File.ReadAllText(pageFilePath);
                            var             pagesList    = ConvertUtil.DeserializeObject <IEnumerable <Page> >(pagesString);
                            IPageRepository pageRepository;
                            if (databaseOptions.ConnectionType == ConnectionType.MongoDB)
                            {
#pragma warning disable CA2000 // Dispose objects before losing scope
                                pageRepository = new PageMongoRepository(new MongoConnection(databaseOptions));
#pragma warning restore CA2000 // Dispose objects before losing scope
                            }
                            else
                            {
#pragma warning disable CA2000 // Dispose objects before losing scope
                                pageRepository = new PageEFRepository(new PortalDbContext(databaseOptions));
#pragma warning restore CA2000 // Dispose objects before losing scope
                            }
                            var pageServiceProvider = new InternalPageServiceProvider(pageRepository, null, null, null);
                            await pageServiceProvider.ForceUpdatePages(pagesList);

                            pageServiceProvider.Dispose();
                            break;

                        case BackupService.DYNAMICLIST_FILE:
                            var dynamicListFilePath = Path.Combine(extractFolder, BackupService.DYNAMICLIST_FILE);
                            var dynamicListString   = File.ReadAllText(dynamicListFilePath);
                            var dynamicListsList    = ConvertUtil.DeserializeObject <IEnumerable <DynamicList> >(dynamicListString);
                            IDynamicListRepository dynamicListRepository;
                            if (databaseOptions.ConnectionType == ConnectionType.MongoDB)
                            {
#pragma warning disable CA2000 // Dispose objects before losing scope
                                dynamicListRepository = new DynamicListMongoRepository(new MongoConnection(databaseOptions));
#pragma warning restore CA2000 // Dispose objects before losing scope
                            }
                            else
                            {
#pragma warning disable CA2000 // Dispose objects before losing scope
                                dynamicListRepository = new DynamicListEFRepository(new PortalDbContext(databaseOptions));
#pragma warning restore CA2000 // Dispose objects before losing scope
                            }
                            var dynamicListServiceProvider = new InternalDynamicListServiceProvider(dynamicListRepository);
                            await dynamicListServiceProvider.ForceUpdateDynamicLists(dynamicListsList);

                            dynamicListServiceProvider.Dispose();
                            break;

                        default:
                            break;
                        }
                    }

                    allRunningFiles.Add(fileNameWithoutExt + ".zip");
                }
            }

            return(allRunningFiles);
        }