Exemple #1
0
        public void ExportController_ZipNotFound()
        {
            var storage         = new StorageSubPathFilesystem(_appSettings, new FakeIWebLogger());
            var selectorStorage = new FakeSelectorStorage(storage);
            var export          = new ExportService(_query, _appSettings, selectorStorage, new FakeIWebLogger());
            var controller      = new ExportController(_bgTaskQueue, selectorStorage, export);

            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            var actionResult = controller.Status("____fail", true) as NotFoundObjectResult;

            Assert.AreEqual(404, actionResult.StatusCode);
        }
Exemple #2
0
        public async Task ExportController_TestZipping()
        {
            // to avoid skip of adding zip
            var zipFilesList = Directory.GetFiles(_createAnImage.BasePath,
                                                  "*.*", SearchOption.AllDirectories)
                               .Where(p => ".zip" == Path.GetExtension(p));

            foreach (var toDelPath in zipFilesList)
            {
                new StorageHostFullPathFilesystem().FileDelete(toDelPath);
            }

            IServiceCollection services = new ServiceCollection();

            services.AddHostedService <UpdateBackgroundQueuedHostedService>();
            services.AddSingleton <IUpdateBackgroundTaskQueue, UpdateBackgroundTaskQueue>();
            services.AddSingleton <IWebLogger, FakeIWebLogger>();
            services.AddSingleton <ITelemetryService, FakeTelemetryService>();
            var serviceProvider = services.BuildServiceProvider();

            var service = serviceProvider.GetService <IHostedService>() as UpdateBackgroundQueuedHostedService;

            var backgroundQueue = serviceProvider.GetService <IUpdateBackgroundTaskQueue>();

            if (service == null)
            {
                throw new Exception("service should not be null");
            }
            await service.StartAsync(CancellationToken.None);

            // the test
            _appSettings.DatabaseType = AppSettings.DatabaseTypeList.InMemoryDatabase;

            var fakeStorage = new FakeIStorage(new List <string> {
                "/"
            },
                                               new List <string> {
                _createAnImage.DbPath
            }, new List <byte[]> {
                CreateAnImage.Bytes
            });

            var storageSelector = new FakeSelectorStorage(fakeStorage);

            var fakeQuery = new FakeIQuery(new List <FileIndexItem> {
                new FileIndexItem
                {
                    FileName        = _createAnImage.DbPath,
                    ParentDirectory = "/",
                    FileHash        = "file-hash",
                    ColorClass      = ColorClassParser.Color.Winner,        // 1
                }
            });

            var appSettings = new AppSettings {
                TempFolder = _createAnImage.BasePath, Verbose = true
            };

            var export     = new ExportService(fakeQuery, appSettings, storageSelector, new FakeIWebLogger());
            var controller = new ExportController(
                backgroundQueue, storageSelector, export)
            {
                ControllerContext = { HttpContext = new DefaultHttpContext() }
            };


            var actionResult = controller.CreateZip(_createAnImage.DbPath,
                                                    true, false) as JsonResult;

            Assert.AreNotEqual(actionResult, null);
            var zipHash = actionResult.Value as string;

            Assert.AreEqual(true, zipHash.Contains("SR"));

            await Task.Delay(150);

            // Get from real fs in to fake memory
            var sourceFullPath = Path.Join(appSettings.TempFolder, zipHash) + ".zip";
            await fakeStorage.WriteStreamAsync(new StorageHostFullPathFilesystem().ReadStream(sourceFullPath), sourceFullPath);

            var actionResult2zip = controller.Status(zipHash, true) as JsonResult;

            Assert.AreNotEqual(actionResult2zip, null);

            var resultValue = ( string )actionResult2zip.Value;

            if (resultValue != "OK" && resultValue != "Not Ready")
            {
                throw new Exception(actionResult2zip.StatusCode.ToString());
            }

            // Don't check if file exist due async
            await service.StopAsync(CancellationToken.None);
        }