Ejemplo n.º 1
0
        public void Execute_FileWithHashAlreadyExists_ThrowsException()
        {
            int        projectId  = new Random().Next(1, 1000);
            string     filePath   = Path.Combine(AppContext.BaseDirectory, "test.log");
            FileDetail fileDetail = new FileDetail();

            fileDetail.Hash = Guid.NewGuid().ToString();

            _fileUtils.GetFileHash(filePath).Returns(fileDetail);

            LogFileModel model = DataHelper.CreateLogFileModel();

            _logFileRepo.GetByHash(projectId, fileDetail.Hash).Returns(model);

            // execute
            TestDelegate del = () => _createLogFileCommand.Execute(projectId, filePath);

            // assert
            Assert.Throws <ValidationException>(del);

            _fileUtils.Received(1).GetFileHash(filePath);
            _logFileRepo.Received(1).GetByHash(projectId, fileDetail.Hash);

            // we shouldn't have even tried to validate or do the insert
            _logFileValidator.DidNotReceive().Validate(Arg.Any <LogFileModel>());
            _dbContext.DidNotReceive().ExecuteNonQuery(Arg.Any <string>(), Arg.Any <object>());
        }
Ejemplo n.º 2
0
        public dynamic Save()
        {
            SaveLogFileViewModel model = this.Bind <SaveLogFileViewModel>();

            // make sure the processing directory exists
            _dirWrap.CreateDirectory(_appSettings.LogFileProcessingDirectory);

            foreach (HttpFile f in Request.Files)
            {
                // save the file to disk
                string filePath = Path.Combine(_appSettings.LogFileProcessingDirectory, f.Name);
                using (var fileStream = File.Create(filePath))
                {
                    f.Value.Seek(0, SeekOrigin.Begin);
                    f.Value.CopyTo(fileStream);
                }

                // create the log file record - this will kick off the job to actually process the file
                try
                {
                    _dbContext.BeginTransaction();
                    _createLogFileCommand.Execute(model.ProjectId, filePath);
                    _dbContext.Commit();
                }
                catch (Exception ex)
                {
                    _dbContext.Rollback();
                    return(this.Response.AsJson <string>(ex.Message, HttpStatusCode.BadRequest));
                }
            }
            return(HttpStatusCode.OK);
        }