public void SetUp()
        {
            var idCounter = 0;

            repoList         = new List <DriveReport>();
            _emplMock        = Substitute.For <IGenericRepository <Employment> >();
            _calculatorMock  = Substitute.For <IReimbursementCalculator>();
            _orgUnitMock     = Substitute.For <IGenericRepository <OrgUnit> >();
            _rateTypeMock    = Substitute.For <IGenericRepository <RateType> >();
            _routeMock       = Substitute.For <IRoute <RouteInformation> >();
            _coordinatesMock = Substitute.For <IAddressCoordinates>();
            _subMock         = Substitute.For <IGenericRepository <Core.DomainModel.Substitute> >();
            _mailServiceMock = Substitute.For <IMailService>();
            _subServiceMock  = Substitute.For <ISubstituteService>();
            _reportRepoMock  = NSubstitute.Substitute.For <IGenericRepository <DriveReport> >();
            _personMock      = Substitute.For <IGenericRepository <Person> >();
            _logger          = new Core.ApplicationServices.Logger.Logger();
            _customSettings  = new CustomSettings();

            _reportRepoMock.Insert(new DriveReport()).ReturnsForAnyArgs(x => x.Arg <DriveReport>()).AndDoes(x => repoList.Add(x.Arg <DriveReport>())).AndDoes(x => x.Arg <DriveReport>().Id = idCounter).AndDoes(x => idCounter++);
            _reportRepoMock.AsQueryable().ReturnsForAnyArgs(repoList.AsQueryable());

            _calculatorMock.Calculate(new RouteInformation(), new DriveReport()).ReturnsForAnyArgs(x => x.Arg <DriveReport>());
            // The mocked reports share the exact same timestamp if they are driven on same day, so for test purposes we simplify the method and check if they are identical, so we are able to mock the method.
            _calculatorMock.AreReportsDrivenOnSameDay(1, 1).ReturnsForAnyArgs(x => (long)x[0] == (long)x[1]);

            _rateTypeMock.AsQueryable().ReturnsForAnyArgs(new List <RateType>
            {
                new RateType()
                {
                    TFCode = "1234",
                    IsBike = false,
                    RequiresLicensePlate = true,
                    Id          = 1,
                    Description = "TestRate"
                }
            }.AsQueryable());

            _coordinatesMock.GetAddressCoordinates(new Address()).ReturnsForAnyArgs(new DriveReportPoint()
            {
                Latitude  = "1",
                Longitude = "2",
            });

            _routeMock.GetRoute(DriveReportTransportType.Car, new List <Address>()).ReturnsForAnyArgs(new RouteInformation()
            {
                Length = 2000
            });

            _uut = new DriveReportService(_mailServiceMock, _reportRepoMock, _calculatorMock, _orgUnitMock, _emplMock, _subMock, _coordinatesMock, _routeMock, _rateTypeMock, _personMock, _logger, _customSettings);
        }
Example #2
0
        /// <summary>
        /// Recalculates the deduction of 4 km from the persons reports driven on the date of the given unix time stamp. Does not recalculate rejected or invoiced reports.
        /// </summary>
        /// <param name="DriveDateTimestamp"></param>
        /// <param name="PersonId"></param>
        public void CalculateFourKmRuleForOtherReports(DriveReport report)
        {
            if (report.Status.Equals(ReportStatus.Rejected))
            {
                report.FourKmRuleDeducted = 0;
            }

            var reportsFromSameDayWithFourKmRule = _driveReportRepository.AsQueryable().Where(x => x.PersonId == report.PersonId &&
                                                                                              x.Status != ReportStatus.Rejected &&
                                                                                              x.Status != ReportStatus.Invoiced &&
                                                                                              x.FourKmRule)
                                                   .OrderBy(x => x.DriveDateTimestamp).ToList();

            foreach (var r in reportsFromSameDayWithFourKmRule)
            {
                if (_calculator.AreReportsDrivenOnSameDay(report.DriveDateTimestamp, r.DriveDateTimestamp))
                {
                    _calculator.CalculateFourKmRuleForReport(r);
                }
            }

            _driveReportRepository.Save();
        }