public void test_that_when_config_is_enabled__the_detector_subscribes_into_the_message_stream()
        {
            _mockLogger        = new Mock <ILogger <PIRDetectionSink> >();
            _mockConfig        = new Mock <IOptions <PIRDetectionSinkConfig> >();
            _mockStationConfig = new Mock <IOptions <StationConfig> >();
            _mockTimeProvider  = new Mock <ITimeProvider>();
            _mockInfluxClient  = new Mock <IInfluxClient>();

            var testScheduler = new TestScheduler();

            var kmStream = testScheduler.CreateHotObservable <KafkaMessage>(new Recorded <Notification <KafkaMessage> >(0, Notification.CreateOnNext(new KafkaMessage()
            {
                Id = "id1"
            })));

            //setup
            _mockConfig.Setup(x => x.Value).Returns(new PIRDetectionSinkConfig {
                Enabled = true
            });

            var filter = new PIRDetectionSink(_mockLogger.Object, _mockConfig.Object, _mockStationConfig.Object, _mockTimeProvider.Object, _mockInfluxClient.Object);

            filter.Observe(kmStream);

            _mockLogger.VerifyLogInfo("Detection filter  Enabled. Subscribing to Kafka message stream.");

            Assert.IsTrue(kmStream.Subscriptions.Count > 0);
        }
        public void test_that_when_config_is_disabled__the_detector_does_not_subscribe_into_the_message_stream()
        {
            _mockLogger        = new Mock <ILogger <PIRDetectionSink> >();
            _mockConfig        = new Mock <IOptions <PIRDetectionSinkConfig> >();
            _mockStationConfig = new Mock <IOptions <StationConfig> >();
            _mockTimeProvider  = new Mock <ITimeProvider>();
            _mockInfluxClient  = new Mock <IInfluxClient>();

            //setup
            _mockConfig.Setup(x => x.Value).Returns(new PIRDetectionSinkConfig {
                Enabled = false
            });

            Subject <KafkaMessage> kmStream = new Subject <KafkaMessage>();

            var filter = new PIRDetectionSink(_mockLogger.Object, _mockConfig.Object, _mockStationConfig.Object, _mockTimeProvider.Object, _mockInfluxClient.Object);

            filter.Observe(kmStream);

            _mockLogger.VerifyLogInfo("Detection filter  disabled. Bypassing subscription to Kafka message stream");

            kmStream.OnNext(new KafkaMessage()
            {
            });

            Assert.IsFalse(kmStream.HasObservers);
        }
        public void test_that_when_threshold_is_exceeded_during_the_buffer_period__video_record_request_is_generated()
        {
            const string testStationId   = "5677";
            const string testStationArea = "Area1";
            const string testKMPayLoad   = "AAAADDD5677ZZZZ";

            //set the date and time for the test
            DateTime testDateTime = new DateTime(2020, 1, 1, 1, 0, 0);

            _mockLogger        = new Mock <ILogger <PIRDetectionSink> >();
            _mockConfig        = new Mock <IOptions <PIRDetectionSinkConfig> >();
            _mockStationConfig = new Mock <IOptions <StationConfig> >();
            _mockTimeProvider  = new Mock <ITimeProvider>();
            _mockInfluxClient  = new Mock <IInfluxClient>();

            _mockStationConfig.Setup(x => x.Value).Returns(new StationConfig()
            {
                Stations = new[] { new Station()
                                   {
                                       Id = testStationId, Description = testStationArea, Enabled = true
                                   } }
            });

            _mockTimeProvider.Setup(x => x.GetCurrentTimeUtc()).Returns(testDateTime);

            var testScheduler = new TestScheduler();

            var kmStream = testScheduler.CreateColdObservable <KafkaMessage>(
                testScheduler.OnNextAt(2000, GenerateTestKM(testKMPayLoad, testDateTime.AddMilliseconds(2000))),
                testScheduler.OnNextAt(3000, GenerateTestKM(testKMPayLoad, testDateTime.AddMilliseconds(3000))),
                testScheduler.OnNextAt(4000, GenerateTestKM(testKMPayLoad, testDateTime.AddMilliseconds(4000))),
                testScheduler.OnNextAt(7000, GenerateTestKM(testKMPayLoad, testDateTime.AddMilliseconds(7000)))
                );

            //setup
            _mockConfig.Setup(x => x.Value).Returns(new PIRDetectionSinkConfig {
                Enabled = true
            });

            var filter = new PIRDetectionSink(_mockLogger.Object, _mockConfig.Object, _mockStationConfig.Object, _mockTimeProvider.Object, _mockInfluxClient.Object, testScheduler);

            filter.Observe(kmStream);

            testScheduler.AdvanceByMs(10500);
            testScheduler.AdvanceByMs(1000);
            _mockLogger.VerifyLogInfo($"Activity detected in station {testStationId}");

            _mockInfluxClient.Verify(x => x.WritePirDetectEvent(It.IsAny <string>(), testStationId, testStationArea, It.IsAny <DateTime>()), Times.AtLeastOnce);
        }