public void getNotificationWhenAlertConditionIsMetTest() { DatabasePerSecondStat perSecondStat = new DatabasePerSecondStat() { CameraId = 1, DateTime = DateTime.Now, DateTimeReceived = DateTime.Now, HasSavedImage = false, NumDetectedObjects = 1, PerSecondStatId = 1, PerHourStatId = 1 }; Mock <IDatabaseQueryService> mockDBService = new Mock <IDatabaseQueryService>(MockBehavior.Loose); List <DatabaseAlert> singleDBAlerts = new List <DatabaseAlert>(); singleDBAlerts.Add(dbAlerts[0]); mockDBService.Setup(x => x.GetAllAlerts(0)).Returns(singleDBAlerts); mockDBService.Setup(x => x.GetEarliestPerSecondStatTriggeringAlert(singleDBAlerts[0], It.IsAny <DateTime>(), It.IsAny <DateTime>())).Returns(perSecondStat); mockDBService.Setup(x => x.PersistNewNotification(It.Is <DatabaseNotification>(p => p.AlertId == 1))).Returns(true); Thread alertMonitoringThread = new Thread(delegate() { int snoozeDurationMinutes = 5; AlertMonitoringService alertMonitoringService = new AlertMonitoringService(mockDBService.Object, new EmailService("test", "test"), snoozeDurationMinutes); alertMonitoringService.StartMonitoring(); }); alertMonitoringThread.Start(); Thread.Sleep(1000); mockDBService.Verify(m => m.PersistNewNotification(It.Is <DatabaseNotification>(p => p.AlertId == 1)), Times.Once); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //Configure logger ASAP so that if the app crashes latter we can have logs of the crash ConfigureLogger(); services.AddMvc().AddSessionStateTempDataProvider(); //Add session support services.AddSession(); // Allow the use of the MySQL Database as a service in this project. // Uses connection string from the project configuration // Passing it as a service ensures everything in the project will use this query service and use this connection string DatabaseQueryService dbQueryService = new DatabaseQueryService(Configuration.GetConnectionString("DefaultConnection")); EmailService emailService = new EmailService(Configuration.GetSection("EmailServiceConfiguration")["SourceEmailAddress"], Configuration.GetSection("EmailServiceConfiguration")["SourceEmailPassword"]); IConfigurationSection alertMonitoringConfig = Configuration.GetSection("AlertMonitoringServiceConfiguration"); if (alertMonitoringConfig != null && Convert.ToBoolean(alertMonitoringConfig["Enabled"])) { Thread alertMonitoringThread = new Thread(delegate() { int snoozeDurationMinutes = Convert.ToInt32(alertMonitoringConfig["SnoozeDurationMinutes"]); AlertMonitoringService alertMonitoringService = new AlertMonitoringService(dbQueryService, emailService, snoozeDurationMinutes); alertMonitoringService.StartMonitoring(); }); alertMonitoringThread.Start(); } // Other services are constructed using the database query service, meaning they all use the same connection string AbstractGraphStatisticService graphStatisticService = new GraphStatisticService(dbQueryService); AbstractLocationService locationService = new LocationService(dbQueryService); AbstractCameraService cameraService = new CameraService(dbQueryService, graphStatisticService, locationService); AbstractAuthenticationService authenticationService = new AuthenticationService(dbQueryService); AbstractDataMessageService dataMessageService = new DataMessageService(dbQueryService); AbstractNotificationService notificationService = new NotificationService(dbQueryService); AbstractAlertService alertService = new AlertService(dbQueryService, cameraService, notificationService); AbstractAPIKeyService apiKeyService = new APIKeyService(dbQueryService); AbstractUserService userService = new UserService(dbQueryService, notificationService, Configuration.GetSection("WebServiceConfiguration")["Hostname"], emailService, apiKeyService); IConfigurationSection alertSummaryConfig = Configuration.GetSection("AlertSummaryServiceConfiguration"); bool sendFramesAsJpg = alertMonitoringConfig != null && Convert.ToBoolean(alertSummaryConfig["SendFramesAsJpg"]); AlertSummaryService alertSummaryService = new AlertSummaryService(alertService, sendFramesAsJpg); services.Add(new ServiceDescriptor(typeof(AbstractAuthenticationService), authenticationService)); services.Add(new ServiceDescriptor(typeof(AbstractCameraService), cameraService)); services.Add(new ServiceDescriptor(typeof(AbstractDataMessageService), dataMessageService)); services.Add(new ServiceDescriptor(typeof(AbstractLocationService), locationService)); services.Add(new ServiceDescriptor(typeof(AbstractGraphStatisticService), graphStatisticService)); services.Add(new ServiceDescriptor(typeof(AbstractAlertService), alertService)); services.Add(new ServiceDescriptor(typeof(AbstractNotificationService), notificationService)); services.Add(new ServiceDescriptor(typeof(AbstractUserService), userService)); services.Add(new ServiceDescriptor(typeof(AbstractAPIKeyService), apiKeyService)); services.Add(new ServiceDescriptor(typeof(AlertSummaryService), alertSummaryService)); }