Beispiel #1
0
        public void Delete_GivenIdShouldDeleteInDb()
        {
            var ctx            = new Mock <RawMetricsContext>(false);
            var mockCollection = new Mock <IMongoCollection <RawMetric> >();

            ctx.Setup(c => c.RawMetrics).Returns(mockCollection.Object);

            var dao = new RawMetricsDAO(ctx.Object);

            dao.Delete("Tot");

            mockCollection.Verify(c => c.DeleteOne(It.IsAny <FilterDefinition <RawMetric> >(), It.IsAny <CancellationToken>()), Times.Once());
        }
Beispiel #2
0
        public void GetMetricsInPeriod_GivenValidPeriodShouldReturnDevices()
        {
            var mockCollection = new Mock <IMongoCollection <RawMetric> >();

            var ctx = new Mock <RawMetricsContext>(false);

            ctx.Setup(c => c.RawMetrics).Returns(mockCollection.Object);

            var dao = new RawMetricsDAO(ctx.Object);

            dao.GetMetricsInPeriodASC(1, 2);
            mockCollection.Verify(c => c.FindSync(It.IsAny <FilterDefinition <RawMetric> >(), It.IsAny <FindOptions <RawMetric, RawMetric> >(), It.IsAny <CancellationToken>()), Times.Once());
        }
Beispiel #3
0
        public void Create_GivenValidNewEntityShouldCreateInDb()
        {
            var ctx            = new Mock <RawMetricsContext>(false);
            var mockCollection = new Mock <IMongoCollection <RawMetric> >();

            ctx.Setup(c => c.RawMetrics).Returns(mockCollection.Object);

            var dao    = new RawMetricsDAO(ctx.Object);
            var result = dao.Create(new RawMetric()
            {
                DeviceId = 2, Date = DateTime.Now.Ticks, Value = "12"
            });

            mockCollection.Verify(c => c.InsertOne(It.IsAny <RawMetric>(), It.IsAny <InsertOneOptions>(), It.IsAny <CancellationToken>()), Times.Once());
        }
Beispiel #4
0
        public void Create_GivenInvalidEntityShouldThrowException()
        {
            var ctx            = new Mock <RawMetricsContext>(false);
            var mockCollection = new Mock <IMongoCollection <RawMetric> >();

            ctx.Setup(c => c.RawMetrics).Returns(mockCollection.Object);

            var dao = new RawMetricsDAO(ctx.Object);

            var ex = Assert.Throws <Exception>(() => dao.Create(new RawMetric()
            {
                Date = DateTime.Now.Ticks, Value = "12"
            }));

            Assert.AreEqual("RawMetric entity integrity not respected. Please verify all your required fields.", ex.Message);
        }
        protected override void OnStart(string[] args)
        {
            IDictionary <string, Object> paramMap = new Dictionary <string, Object>();

            paramMap[Constants.Context.PROVIDER_URL] =
                "t3://" + topicHost + ":" + topicPort;

            IContext context = ContextFactory.CreateContext(paramMap);


            IConnectionFactory cf = context.LookupConnectionFactory(connectionFactoryName);

            IQueue queue = (IQueue)context.LookupDestination(topicName);

            IConnection connection = cf.CreateConnection();

            connection.ClientID = "MetricService";

            connection.Start();
            ISession consumerSession = connection.CreateSession(
                Constants.SessionMode.AUTO_ACKNOWLEDGE);

            IMessageConsumer consumer = consumerSession.CreateConsumer(queue);


            consumer.Message += new MessageEventHandler(OnMessage);


            log.WriteEntry("Service started");
            var _context = new RawMetricsContext(true);

            dao = new RawMetricsDAO(_context);


            _thread              = new Thread(KeepAlive);
            _thread.Name         = "My Worker Thread";
            _thread.IsBackground = true;
            _thread.Start();
        }
Beispiel #6
0
        public async Task <List <CalculatedMetricsModel> > generateCalculatedMetrics()
        {
            RawMetricsContext context       = new RawMetricsContext(true);
            RawMetricsDAO     rawMetricsDAO = new RawMetricsDAO(context);

            // If we are on monday, we look for the rawdata of the week
            List <RawMetric> rawMetrics = new List <RawMetric>();
            // Otherwise, we look for  the rawdata of the previous 24h
            DateTime dateOfTheDay = new DateTime();

            if (dateOfTheDay.DayOfWeek == DayOfWeek.Monday)
            {
                dateOfTheDay = DateTime.Now;
                rawMetrics   =
                    await rawMetricsDAO.GetMetricsInPeriodASC(DateTimeToUnixLong(dateOfTheDay.AddDays(-7)), DateTimeToUnixLong(dateOfTheDay));
            }
            else
            {
                rawMetrics =
                    await rawMetricsDAO.GetMetricsInPeriodASC(DateTimeToUnixLong(dateOfTheDay.AddDays(-1)), DateTimeToUnixLong(dateOfTheDay));
            }
            return(Calculation(rawMetrics));
        }
Beispiel #7
0
        public void Update_GivenValidNewEntityShouldUpdateExisting()
        {
            var ctx            = new Mock <RawMetricsContext>(false);
            var mockCollection = new Mock <IMongoCollection <RawMetric> >();
            int deviceId       = 1;

            ctx.Setup(c => c.RawMetrics).Returns(mockCollection.Object);

            var dao = new RawMetricsDAO(ctx.Object);

            var initialValue = dao.Create(new RawMetric()
            {
                DeviceId = deviceId, Date = DateTime.Now.Ticks, Value = "12"
            });

            var updatedValue = dao.Update(new RawMetric()
            {
                DeviceId = deviceId, Date = DateTime.Now.Ticks, Value = "16"
            });

            mockCollection.Verify(c => c.ReplaceOne(It.IsAny <FilterDefinition <RawMetric> >(), It.IsAny <RawMetric>(), It.IsAny <UpdateOptions>(), It.IsAny <CancellationToken>()), Times.Once());
            Assert.AreNotEqual(initialValue.Value, updatedValue.Value);
        }
Beispiel #8
0
        public void Get_GivenIdShouldReturnObject()
        {
            var ctx            = new Mock <RawMetricsContext>(false);
            var mockCollection = new Mock <IMongoCollection <RawMetric> >();

            var cursorMock = new Mock <IAsyncCursor <RawMetric> >();

            cursorMock.Setup(c => c.MoveNext(It.IsAny <CancellationToken>())).Returns(true);
            cursorMock.Setup(c => c.Current).Returns(new[] { new RawMetric()
                                                             {
                                                                 DeviceId = 1
                                                             } });

            mockCollection.Setup(c => c.FindSync(It.IsAny <FilterDefinition <RawMetric> >(), It.IsAny <FindOptions <RawMetric, RawMetric> >(), It.IsAny <CancellationToken>())).Returns(cursorMock.Object);
            ctx.Setup(c => c.RawMetrics).Returns(mockCollection.Object);

            var dao = new RawMetricsDAO(ctx.Object);

            var result = dao.Get("Toto");

            mockCollection.Verify(c => c.FindSync(It.IsAny <FilterDefinition <RawMetric> >(), It.IsAny <FindOptions <RawMetric, RawMetric> >(), It.IsAny <CancellationToken>()), Times.Once());
            Assert.AreEqual(1, result.DeviceId);
        }
 public RawMetricsService(RawMetricsDAO dao)
 {
     _dao = dao;
 }
        public RawMetricsService()
        {
            var _context = new RawMetricsContext(true);

            _dao = new RawMetricsDAO(_context);
        }