public void Execute_IntegrationTest_SQLite() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); List <W3CEvent> logEvents = null; using (StreamReader logStream = new StreamReader(TestAsset.ReadTextStream(TestAsset.LogFile))) { logEvents = W3CEnumerable.FromStream(logStream).ToList(); } using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); // create the project first so we have one ProjectModel project = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project); // create the log file LogFileModel logFile = DataHelper.CreateLogFileModel(project.Id); DataHelper.InsertLogFileModel(dbContext, logFile); // create the request batch ICreateRequestBatchCommand createRequestBatchCommand = new CreateRequestBatchCommand(dbContext, new RequestValidator()); createRequestBatchCommand.Execute(logFile.Id, logEvents); int rowCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM Requests"); Assert.AreEqual(logEvents.Count, rowCount); } }
public void GetByUriStemAggregate_Integration_ReturnsData() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); List <W3CEvent> logEvents = null; string uriStemAggregate = Guid.NewGuid().ToString(); int expectedCount = new Random().Next(3, 7); using (StreamReader logStream = new StreamReader(TestAsset.ReadTextStream(TestAsset.LogFile))) { logEvents = W3CEnumerable.FromStream(logStream).ToList(); } for (int i = 0; i < expectedCount; i++) { logEvents[i].cs_uri_stem = uriStemAggregate; } using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); IRequestRepository requestRepo = new RequestRepository(dbContext); ICreateRequestBatchCommand createRequestBatchCommand = new CreateRequestBatchCommand(dbContext, new RequestValidator()); // create the project ProjectModel project = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project); ProjectModel project2 = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project2); // create log file and request records for each LogFileModel logFile = DataHelper.CreateLogFileModel(project.Id); DataHelper.InsertLogFileModel(dbContext, logFile); createRequestBatchCommand.Execute(logFile.Id, logEvents); LogFileModel logFile2 = DataHelper.CreateLogFileModel(project2.Id); DataHelper.InsertLogFileModel(dbContext, logFile2); createRequestBatchCommand.Execute(logFile2.Id, logEvents); IEnumerable <RequestModel> result = requestRepo.GetByUriStemAggregate(project.Id, uriStemAggregate); Assert.IsNotNull(result); Assert.AreEqual(expectedCount, result.Count()); foreach (RequestModel rm in result) { Assert.AreEqual(logFile.Id, rm.LogFileId); } } }
public void Execute_IntegrationTest_SQLite() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); List <W3CEvent> logEvents = null; using (StreamReader logStream = new StreamReader(TestAsset.ReadTextStream(TestAsset.LogFile))) { logEvents = W3CEnumerable.FromStream(logStream).ToList(); } using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); ICreateProjectCommand createProjectCommand = new CreateProjectCommand(dbContext, new ProjectValidator()); ICreateRequestBatchCommand createRequestBatchCommand = new CreateRequestBatchCommand(dbContext, new RequestValidator()); IDeleteLogFileCommand deleteLogFileCommand = new DeleteLogFileCommand(dbContext); // create the project first so we have one ProjectModel project = DataHelper.CreateProjectModel(); // create 2 the log files LogFileModel logFile1 = DataHelper.CreateLogFileModel(project.Id); DataHelper.InsertLogFileModel(dbContext, logFile1); LogFileModel logFile2 = DataHelper.CreateLogFileModel(project.Id); DataHelper.InsertLogFileModel(dbContext, logFile2); // create the request batch createRequestBatchCommand.Execute(logFile1.Id, logEvents); createRequestBatchCommand.Execute(logFile2.Id, logEvents); int rowCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM Requests WHERE LogFileId = @LogFileId", new { LogFileId = logFile1.Id }); Assert.AreEqual(logEvents.Count, rowCount); // run the delete command deleteLogFileCommand.Execute(logFile1.Id); // there should be no requests for logFile1, but requests for logFile2 should still exist rowCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM Requests WHERE LogFileId = @LogFileId", new { LogFileId = logFile1.Id }); Assert.AreEqual(0, rowCount); rowCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM Requests WHERE LogFileId = @LogFileId", new { LogFileId = logFile2.Id }); Assert.AreEqual(logEvents.Count, rowCount); rowCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM LogFiles"); Assert.AreEqual(1, rowCount); } }
public void GetByLogFile_Integration_ReturnsData() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); List <W3CEvent> logEvents = null; int logFileId = 0; using (StreamReader logStream = new StreamReader(TestAsset.ReadTextStream(TestAsset.LogFile))) { logEvents = W3CEnumerable.FromStream(logStream).ToList().GetRange(0, 10); } using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); IRequestRepository requestRepo = new RequestRepository(dbContext); ICreateRequestBatchCommand createRequestBatchCommand = new CreateRequestBatchCommand(dbContext, new RequestValidator()); // create the project ProjectModel project = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project); // create multiple log file records for (var i = 0; i < 3; i++) { LogFileModel logFile = DataHelper.CreateLogFileModel(project.Id); DataHelper.InsertLogFileModel(dbContext, logFile); createRequestBatchCommand.Execute(logFile.Id, logEvents); if (logFileId == 0) { logFileId = logFile.Id; } } IEnumerable <RequestModel> result = requestRepo.GetByLogFile(logFileId); Assert.IsNotNull(result); Assert.AreEqual(logEvents.Count, result.Count()); } }
public static IEnumerable <ISiteLog> GetLogs(string logFolderPath) { Stream contentStream = LoadFileContent(logFolderPath); if (contentStream == null) { return(null); } StreamReader reader = new StreamReader(contentStream); IEnumerable <W3CEvent> events = W3CEnumerable.FromStream(reader); ExcludeMediaItems(ref events); // Exclude js,css,image,font items from logs IEnumerable <ISiteLog> logs = events.Select(x => new SiteLog { LogDate = x.dateTime, ServerSiteName = x.s_sitename, ServerComputerName = x.s_computername, ServerIp = x.s_ip, ServerPort = x.s_port, ServerReceivedBytes = x.cs_bytes, ServerResponseCode = x.sc_status, ServerWin32Code = x.sc_win32_status, ServerResponseSubStatus = x.sc_substatus, ServerResponseTimeMiliseconds = x.time_taken, ServerSentBytes = x.sc_bytes, ClientRequestedMethod = x.cs_method, ClientRequestedUri = x.cs_uri_stem, ClientRequestedUriQuery = x.cs_uri_query, ClientUserName = x.cs_username, ClientIp = x.c_ip, ClientBrowserVersion = x.cs_version, ClientUserAgent = x.cs_User_Agent, ClientRequestedCookie = x.cs_Cookie, ClientRequestedHost = x.cs_host, ClientRequestedReferer = x.cs_Referer }); return(logs); }
public void Execute_IntegrationTest_SQLite() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); List <W3CEvent> logEvents1 = null; List <W3CEvent> logEvents2 = new List <W3CEvent>(); using (StreamReader logStream = new StreamReader(TestAsset.ReadTextStream(TestAsset.LogFile))) { logEvents1 = W3CEnumerable.FromStream(logStream).ToList(); logEvents2.AddRange(logEvents1.GetRange(0, 10)); } using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); ISetLogFileUnprocessedCommand setLogFileUnprocessedCommand = new SetLogFileUnprocessedCommand(dbContext, _jobRegistrationService); // create the project first so we have one ProjectModel project = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project); // create the log files LogFileModel logFile1 = DataHelper.CreateLogFileModel(project.Id); logFile1.Status = LogFileStatus.Complete; DataHelper.InsertLogFileModel(dbContext, logFile1); LogFileModel logFile2 = DataHelper.CreateLogFileModel(project.Id); logFile2.Status = LogFileStatus.Complete; DataHelper.InsertLogFileModel(dbContext, logFile2); // check that the log file is processed int processedCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM LogFiles WHERE ProjectId = @ProjectId AND Status = @Status", new { ProjectId = project.Id, Status = LogFileStatus.Complete }); Assert.AreEqual(2, processedCount); // execute for a single log file setLogFileUnprocessedCommand.Execute(logFile1.Id); processedCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM LogFiles WHERE ProjectId = @ProjectId AND Status = @Status", new { ProjectId = project.Id, Status = LogFileStatus.Processing }); Assert.AreEqual(1, processedCount); } }
public void Execute_ValidationSucceeds_BatchInserted() { int logFileId = new Random().Next(1, 1000); _requestValidator.Validate(Arg.Any <RequestModel>()).Returns(new ValidationResult()); using (StreamReader logStream = new StreamReader(TestAsset.ReadTextStream(TestAsset.LogFile))) { var logEvents = W3CEnumerable.FromStream(logStream).ToList(); int eventCount = logEvents.Count; Assert.Greater(eventCount, 1); // execute _createRequestBatchCommand.Execute(logFileId, logEvents); // assert _requestValidator.Received(eventCount).Validate(Arg.Any <RequestModel>()); // should receive eventCount + 1 -> delete also done _dbContext.Received(eventCount + 1).ExecuteNonQuery(Arg.Any <string>(), Arg.Any <object>()); } }