public void WhenShortDayWillSetThePeriodDateTimeToReflect()
        {
            var date = new DateTime(2016, 3, 27);
            var position = new PowerPosition(date);

            Assert.AreEqual(23, position.Periods.Length);
            Assert.AreEqual(new DateTime(2016, 3, 26, 23, 0, 0), position.Periods[0].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 0, 0, 0), position.Periods[1].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 2, 0, 0), position.Periods[2].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 3, 0, 0), position.Periods[3].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 4, 0, 0), position.Periods[4].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 5, 0, 0), position.Periods[5].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 6, 0, 0), position.Periods[6].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 7, 0, 0), position.Periods[7].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 8, 0, 0), position.Periods[8].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 9, 0, 0), position.Periods[9].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 10, 0, 0), position.Periods[10].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 11, 0, 0), position.Periods[11].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 12, 0, 0), position.Periods[12].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 13, 0, 0), position.Periods[13].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 14, 0, 0), position.Periods[14].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 15, 0, 0), position.Periods[15].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 16, 0, 0), position.Periods[16].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 17, 0, 0), position.Periods[17].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 18, 0, 0), position.Periods[18].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 19, 0, 0), position.Periods[19].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 20, 0, 0), position.Periods[20].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 21, 0, 0), position.Periods[21].PeriodDateTime);
            Assert.AreEqual(new DateTime(2016, 3, 27, 22, 0, 0), position.Periods[22].PeriodDateTime);
        }
        public void Init()
        {
            _log = Substitute.For<ILog>();
            _dateCalculator = Substitute.For<IDateCalculator>();
            _powerService = Substitute.For<IPowerService>();
            _positionAggregator = Substitute.For<IPositionAggregator>();
            _fileNameGenerator = Substitute.For<IFileNameGenerator>();
            _reportContentWriter = Substitute.For<IReportContentWriter>();
            _file = Substitute.For<IFile>();
            _reportGenerator = new ReportGenerator(_log, _dateCalculator, _powerService, _positionAggregator, _fileNameGenerator, _reportContentWriter, _file);

            _reportFolder = @"C:\Temp\";
            _dates = new DateResult { ExtractDateTime = new DateTime(2015, 10, 5, 23, 34, 0), RequestDate = new DateTime(2015, 10, 6) };

            _powerTradeOne = new PowerTrade();
            _powerTradeTwo = new PowerTrade();
            _powerTrades = new[] { _powerTradeOne, _powerTradeTwo };
            _powerPosition = new PowerPosition();
            _fileName = "PowerPositions.csv";
            _content = "Local time, Volume etc";

            _dateCalculator.Calculate().Returns(_dates);
            _powerService.GetTrades(_dates.RequestDate).Returns(_powerTrades);
            _positionAggregator.Aggregate(_dates.RequestDate, Arg.Is<List<PowerTrade>>(x => x[0] == _powerTradeOne && x[1] == _powerTradeTwo)).Returns(_powerPosition);
            _fileNameGenerator.Generate(_dates.ExtractDateTime).Returns(_fileName);
            _reportContentWriter.Write(_powerPosition).Returns(_content);
        }
        public PowerPosition Aggregate(DateTime positionDate, IEnumerable<PowerTrade> powerTrades)
        {
            var powerTradesList = powerTrades.ToList();

            var position = new PowerPosition(positionDate);
            if (!powerTradesList.Any())
            {
                return position;
            }

            var noOfIntervals = position.Periods.Count();

            // as we do not have control over the number of intervals on the power trade
            // the decision has been made that it is safer to throw an exception than to carry on and calc a position based on data that isnt in the expected shape
            if (powerTradesList.Any(x => x.Periods.Length != noOfIntervals))
            {
                throw new Exception($"All trades expected to have {noOfIntervals} periods.");
            }

            for (int i = 0; i < noOfIntervals; i++)
            {
                position.Periods[i].Volume = powerTradesList.Sum(x => x.Periods[i].Volume);
            }

            return position;
        }
        public void WhenShortDayWillCreatePositionForTheRequiredNumberOfPeriods()
        {
            var date = new DateTime(2016, 3, 27);
            var position = new PowerPosition(date);

            Assert.AreEqual(date, position.Date);
            Assert.AreEqual(23, position.Periods.Length);
        }
        public void WillCreatePositionForTheRequiredNumberOfPeriods()
        {
            var date = new DateTime(2015, 6, 3);
            var position = new PowerPosition(date, 24);

            Assert.AreEqual(date, position.Date);
            Assert.AreEqual(24, position.Periods.Length);
        }
        public string Write(PowerPosition position)
        {
            StringBuilder content = new StringBuilder("Local Time,Volume");
            foreach (var period in position.Periods)
            {
                content.AppendLine();
                content.AppendFormat("{0:HH:mm},{1}", period.PeriodDateTime, period.Volume);
            }

            return content.ToString();
        }
        public void WillWriteAStringRepresentingThePosition()
        {
            var extractTime = new DateTime(2015, 5, 12);
            var position = new PowerPosition(extractTime, 5);
            for (int i = 0; i < position.Periods.Length; i++)
            {
                position.Periods[i].Volume = 10 * i;
            }

            var content = _writer.Write(position);

            Assert.AreEqual(Properties.Resources.ExpectedContent, content);
        }