Пример #1
0
        public void GetMetricsByInterval_ReturnsCorrectMetrics()
        {
            //Arrange
            var request = new HddMetricGetByIntervalRequest()
            {
                FromTime = DateTimeOffset.MinValue,
                ToTime   = DateTimeOffset.Now
            };

            //фейковые метрики возвращаемые репозиторием
            var mockMetrics = new List <HddMetric>()
            {
                { new HddMetric()
                  {
                      Time = DateTimeOffset.MinValue, Value = 100
                  } },
                { new HddMetric()
                  {
                      Time = DateTimeOffset.UtcNow, Value = 121
                  } }
            };

            mockRepository.
            Setup(repository => repository.GetByTimeInterval(It.IsAny <DateTimeOffset>(), It.IsAny <DateTimeOffset>())).
            Returns(mockMetrics);

            //Act
            var result = controller.GetMetrics(request);

            var response = ((result as OkObjectResult).Value as AllHddMetricsResponse).Metrics;

            //сравнение полученных значений с ожидаемыми значениями
            bool check = true;

            if (mockMetrics.Count == response.Count)
            {
                for (int i = 0; i < mockMetrics.Count; i++)
                {
                    if ((mockMetrics[i].Value != response[i].Value) ||
                        (mockMetrics[i].Time != response[i].Time))
                    {
                        check = false;
                    }
                }
            }
            else
            {
                check = false;
            }

            // Assert
            Assert.True(check);
        }
        public IActionResult GetMetrics([FromRoute] HddMetricGetByIntervalRequest request)
        {
            _logger.LogDebug("Вызов метода. Параметры:" +
                             $" {nameof(request.FromTime)} = {request.FromTime}" +
                             $" {nameof(request.ToTime)} = {request.ToTime}");

            var metrics = _repository.GetByTimeInterval(request.FromTime, request.ToTime);

            var response = new AllHddMetricsResponse()
            {
                Metrics = new List <HddMetricDto>()
            };

            foreach (var metric in metrics)
            {
                response.Metrics.Add(_mapper.Map <HddMetricDto>(metric));
            }

            return(Ok(response));
        }