public void ConstructorWorks(bool hasCategory)
        {
            var category      = new CategoryEntityImp();
            var standingOrder = new StandingOrderEntityImp(ApplicationContext)
            {
                Category        = hasCategory ? category : null,
                Description     = "Test",
                FirstBookDate   = new DateTime(2015, 4, 1),
                LastBookDate    = new DateTime(2016, 4, 1),
                LastBookedDate  = new DateTime(2015, 5, 1),
                MonthPeriodStep = 1,
                ReferenceDay    = 1,
                ReferenceMonth  = 4,
                Value           = 45.22
            };
            var standingOrderPersistence = new StandingOrderEntityPersistence(standingOrder);

            Assert.That(standingOrderPersistence.PersistentId, Is.EqualTo(standingOrder.PersistentId));
            Assert.That(standingOrderPersistence.Description, Is.EqualTo(standingOrder.Description));
            Assert.That(standingOrderPersistence.FirstBookDate, Is.EqualTo(standingOrder.FirstBookDate));
            Assert.That(standingOrderPersistence.LastBookDate, Is.EqualTo(standingOrder.LastBookDate));
            Assert.That(standingOrderPersistence.LastBookedDate, Is.EqualTo(standingOrder.LastBookedDate));
            Assert.That(standingOrderPersistence.MonthPeriodStep, Is.EqualTo(standingOrder.MonthPeriodStep));
            Assert.That(standingOrderPersistence.ReferenceDay, Is.EqualTo(standingOrder.ReferenceDay));
            Assert.That(standingOrderPersistence.ReferenceMonth, Is.EqualTo(standingOrder.ReferenceMonth));
            Assert.That(standingOrderPersistence.Value, Is.EqualTo(standingOrder.Value));
            Assert.That(standingOrderPersistence.Category, Is.InstanceOf <CategoryEntityStub>());
            Assert.That(standingOrderPersistence.Category.PersistentId, Is.EqualTo(hasCategory ? category.PersistentId : ""));
        }
        public void Serialize(bool hasCategory)
        {
            var category      = new CategoryEntityImp();
            var standingOrder = new StandingOrderEntityImp(ApplicationContext)
            {
                Category        = hasCategory ? category : null,
                Description     = "Test",
                FirstBookDate   = new DateTime(2015, 4, 1),
                LastBookDate    = new DateTime(2016, 4, 1),
                LastBookedDate  = new DateTime(2015, 5, 1),
                MonthPeriodStep = 1,
                ReferenceDay    = 1,
                ReferenceMonth  = 4,
                Value           = 45.22
            };
            var standingOrderPersistence = new StandingOrderEntityPersistence(standingOrder);

            var data = standingOrderPersistence.Serialize();

            Assert.That(data.Attribute("CategoryId").Value, Is.EqualTo(hasCategory ? category.PersistentId : string.Empty));
            Assert.That(data.Attribute("Description").Value, Is.EqualTo("Test"));
            Assert.That(data.Attribute("FirstBookDate").Value, Is.EqualTo(standingOrder.FirstBookDate.ToString(CultureInfo.InvariantCulture)));
            Assert.That(data.Attribute("LastBookDate").Value, Is.EqualTo(standingOrder.LastBookDate.ToString(CultureInfo.InvariantCulture)));
            Assert.That(data.Attribute("LastBookedDate").Value, Is.EqualTo(standingOrder.LastBookedDate.ToString(CultureInfo.InvariantCulture)));
            Assert.That(data.Attribute("MonthPeriodStep").Value, Is.EqualTo(standingOrder.MonthPeriodStep.ToString(CultureInfo.InvariantCulture)));
            Assert.That(data.Attribute("ReferenceDay").Value, Is.EqualTo(standingOrder.ReferenceDay.ToString(CultureInfo.InvariantCulture)));
            Assert.That(data.Attribute("ReferenceMonth").Value, Is.EqualTo(standingOrder.ReferenceMonth.ToString(CultureInfo.InvariantCulture)));
            Assert.That(data.Attribute("Value").Value, Is.EqualTo(standingOrder.Value.ToString(CultureInfo.InvariantCulture)));
        }
        public void IsMonthOfPeriod(int period, int referenceMonth, int month, bool expectedIsPeriodMonth)
        {
            var entity = new StandingOrderEntityImp(ApplicationContext)
            {
                MonthPeriodStep = period,
                ReferenceDay    = 1,
                ReferenceMonth  = referenceMonth
            };

            Assert.That(entity.IsMonthOfPeriod(month), Is.EqualTo(expectedIsPeriodMonth));
        }
        public string CreateStandingOrder(StandingOrderEntityData requestData)
        {
            EnsureRepositoryOpen("CreateStandingOrder");

            var standingOrder = new StandingOrderEntityImp(_applicationContext);

            SetRequestEntityImpData(standingOrder, requestData);
            _allStandingOrders.Add(standingOrder);
            _persistenceHandler.SaveChanges(new SavingTask(FilePath, standingOrder.Clone()));

            return(standingOrder.PersistentId);
        }
        public void GetNextPeriodForLastBookDateReturnsNull([Values(1, 3, 6, 12)] int period)
        {
            var entity = new StandingOrderEntityImp(ApplicationContext)
            {
                MonthPeriodStep = period,
                ReferenceDay    = 1,
                ReferenceMonth  = 4,
                FirstBookDate   = new DateTime(2014, 2, 1),
                LastBookedDate  = new DateTime(2014, 4, 1),
                LastBookDate    = new DateTime(2014, 4, 1)
            };

            Assert.That(entity.GetNextPaymentDateTime(), Is.Null);
        }
        public void GetNextPeriodDateTimeForFirstCall([Values(1, 3, 6, 12)] int period)
        {
            var entity = new StandingOrderEntityImp(ApplicationContext)
            {
                MonthPeriodStep = period,
                ReferenceDay    = 1,
                ReferenceMonth  = 4,
                FirstBookDate   = new DateTime(2014, 4, 1)
            };

            var expectedDateTime = new DateTime(2014, 4, 1);
            var nextDateTime     = entity.GetNextPaymentDateTime();

            Assert.That(nextDateTime, Is.EqualTo(expectedDateTime));
        }
        public void StateIsCalculatedCorrectly(DateTime firstBookDate, DateTime lastBookDate, DateTime lastBookedDate, StandingOrderState expectedState)
        {
            ApplicationContext.Now.Returns(lastBookedDate);

            var entity = new StandingOrderEntityImp(ApplicationContext)
            {
                MonthPeriodStep = 1,
                ReferenceDay    = 1,
                ReferenceMonth  = 4,
                FirstBookDate   = firstBookDate,
                LastBookDate    = lastBookDate
            };

            Assert.That(entity.State, Is.EqualTo(expectedState));
        }
        public void ChangePropertyUpdatesHasChanged(Action <StandingOrderEntityImp> changeObject)
        {
            var categories = Enumerable.Range(1, 3).Select(i => new CategoryEntityImp()).ToArray();
            var element    = new XElement("Request",
                                          new XAttribute("Id", "TestId"),
                                          new XAttribute("FirstBookDate", new DateTime(2014, 7, 4).ToString(CultureInfo.InvariantCulture)),
                                          new XAttribute("ReferenceMonth", 1),
                                          new XAttribute("ReferenceDay", 2),
                                          new XAttribute("MonthPeriodStep", 3),
                                          new XAttribute("Description", "TestDescription"),
                                          new XAttribute("LastBookDate", new DateTime(2014, 7, 4)),
                                          new XAttribute("LastBookedDate", new DateTime(2014, 7, 4)),
                                          new XAttribute("Value", 12.77),
                                          new XAttribute("CategoryId", categories.First().PersistentId));

            var standingOrder = new StandingOrderEntityImp(ApplicationContext, element, categories);

            Assert.That(standingOrder.HasChanged, Is.False);

            changeObject(standingOrder);
            Assert.That(standingOrder.HasChanged, Is.True);
        }
        public void Deserialize(bool withCategory)
        {
            const string persistentId = "TestId";
            var          dateTime     = new DateTime(2014, 7, 4);
            const double value        = 12.77;
            const string description  = "TestDescription";

            var categories = Enumerable.Range(1, 3).Select(i => new CategoryEntityImp()).ToArray();
            var categoryId = withCategory ? categories.First().PersistentId : "";

            var element = new XElement("Request",
                                       new XAttribute("Id", persistentId),
                                       new XAttribute("FirstBookDate", dateTime.ToString(CultureInfo.InvariantCulture)),
                                       new XAttribute("ReferenceMonth", 1),
                                       new XAttribute("ReferenceDay", 2),
                                       new XAttribute("MonthPeriodStep", 3),
                                       new XAttribute("Description", description),
                                       new XAttribute("LastBookDate", dateTime),
                                       new XAttribute("LastBookedDate", dateTime),
                                       new XAttribute("Value", value),
                                       new XAttribute("CategoryId", categoryId));

            var standingOrder = new StandingOrderEntityImp(ApplicationContext, element, categories);

            Assert.That(standingOrder.PersistentId, Is.EqualTo(persistentId));
            Assert.That(standingOrder.FirstBookDate, Is.EqualTo(dateTime));
            Assert.That(standingOrder.ReferenceMonth, Is.EqualTo(1));
            Assert.That(standingOrder.ReferenceDay, Is.EqualTo(2));
            Assert.That(standingOrder.MonthPeriodStep, Is.EqualTo(3));
            Assert.That(standingOrder.Description, Is.EqualTo(description));
            Assert.That(standingOrder.LastBookDate, Is.EqualTo(dateTime));
            Assert.That(standingOrder.LastBookedDate, Is.EqualTo(dateTime));
            Assert.That(standingOrder.Value, Is.EqualTo(value));
            Assert.That(standingOrder.Category, Is.EqualTo(withCategory ? categories.First() : null));
            Assert.That(standingOrder.HasChanged, Is.False);
        }
        private void SetRequestEntityImpData(StandingOrderEntityImp standingOrder, StandingOrderEntityData data)
        {
            standingOrder.Description     = data.Description;
            standingOrder.FirstBookDate   = data.FirstBookDate;
            standingOrder.MonthPeriodStep = data.MonthPeriodStep;
            standingOrder.ReferenceDay    = data.ReferenceDay;
            standingOrder.ReferenceMonth  = data.ReferenceMonth;
            standingOrder.Value           = data.Value;

            if (data.PaymentCount.HasValue)
            {
                var monthsAddToFirstBookDate = data.MonthPeriodStep * data.PaymentCount.Value;
                standingOrder.LastBookDate = standingOrder.FirstBookDate.AddMonths(monthsAddToFirstBookDate);
            }
            else
            {
                standingOrder.LastBookDate = DateTime.MaxValue;
            }

            if (!string.IsNullOrEmpty(data.CategoryEntityId))
            {
                standingOrder.Category = _allCategories.Single(c => c.PersistentId == data.CategoryEntityId);
            }
        }
        public void InitialState()
        {
            var standingOrder = new StandingOrderEntityImp(ApplicationContext);

            Assert.That(standingOrder.HasChanged, Is.True);
        }
 internal void AddStandingOrder(StandingOrderEntityImp standingOrderEntity)
 {
     _allStandingOrders.Add(standingOrderEntity);
 }