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>()); }
public LogFileModel Execute(int projectId, string filePath) { FileDetail fileDetail = _fileUtils.GetFileHash(filePath); LogFileModel logFile = _logFileRepo.GetByHash(projectId, fileDetail.Hash); if (logFile != null) { throw new ValidationException("Log file already loaded for this project"); } // save details of the file itself logFile = new LogFileModel(); logFile.ProjectId = projectId; logFile.FileHash = fileDetail.Hash; logFile.FileLength = fileDetail.Length; logFile.FileName = fileDetail.Name; logFile.RecordCount = -1; logFile.Status = LogFileStatus.Processing; // validate ValidationResult result = _logFileValidator.Validate(logFile); if (!result.Success) { throw new ValidationException(result.Messages); } // insert new record string sql = @"INSERT INTO LogFiles (ProjectId, FileName, FileHash, CreateDate, FileLength, RecordCount, Status) VALUES (@ProjectId, @FileName, @FileHash, @CreateDate, @FileLength, @RecordCount, @Status)"; _dbContext.ExecuteNonQuery(sql, logFile); sql = @"select last_insert_rowid()"; logFile.Id = _dbContext.ExecuteScalar <int>(sql); // register the job to process the log file _jobRegistrationService.RegisterProcessLogFileJob(logFile.Id, filePath); return(logFile); }