예제 #1
0
        public void VerifyFirstStamp_NoFirstInStamp_AddBeginOfDayAsFirstInStamp()
        {
            // Arrange
            // Target day is 01.03.2016
            DateTime targetDay              = new DateTime(2016, 3, 1);
            int      targetEmployerID       = 1;
            List <EmployerTimeStamp> stamps = new List <EmployerTimeStamp>()
            {
                new EmployerTimeStamp()
                {
                    EmployerID = targetEmployerID, Type = StampType.Out, Time = targetDay
                }
            };
            int originalStampsCount           = stamps.Count;
            DailyReportsManager dailyReporter = this.СreateDailyReporter(stamps);
            EmployerTimeStamp   expectedStamp = new EmployerTimeStamp()
            {
                EmployerID = targetEmployerID, Type = StampType.In, Time = new DateTime(targetDay.Year, targetDay.Month, targetDay.Day, 0, 0, 0)
            };
            List <Notification> notifications = new List <Notification>();

            // Act
            dailyReporter.VerifyFirstStamp(stamps, targetEmployerID, targetDay, notifications);

            // Assertions
            // Check
            Assert.AreEqual(stamps.Count, originalStampsCount + 1);
            Assert.That(stamps[0], Is.EqualTo(expectedStamp).Using(new EmployerTimeStampComparer()));
        }
        /// <summary>
        /// Check out first element of collection of stamps
        /// to add if require non existent first In-stamp.
        /// </summary>
        /// <param name="employerStamps">Collection that should be checked.</param>
        /// <param name="employerID">Unique identifier of target employer.</param>
        /// <param name="day">Date of day of reporting.</param>
        /// <param name="notifications">Collection of notifications that will be filled during processing of collecting.</param>
        internal void VerifyFirstStamp(List <EmployerTimeStamp> employerStamps, int employerID, DateTime day, List <Notification> notifications)
        {
            if (employerStamps == null)
            {
                throw new ArgumentNullException(ReporterMessages.StampsCollectionReferenceIsNull);
            }

            if (notifications == null)
            {
                throw new ArgumentNullException(ReporterMessages.NotificationsRefrenceIsNull);
            }

            // if first record is not in-stamp then insert begging of target day instead of it.
            if (employerStamps.Count == 0 || employerStamps[0].Type != StampType.In)
            {
                notifications.Add(new Notification(ReporterMessages.FirstInStampNotFound, NotificationType.Warning));
                notifications.Add(new Notification(ReporterMessages.BeginDayAsInStampAdded, NotificationType.Message));

                EmployerTimeStamp firstInStamp = new EmployerTimeStamp();
                firstInStamp.EmployerID = employerID;
                firstInStamp.Type       = StampType.In;

                // set beginning of target day.
                firstInStamp.Time = new DateTime(day.Year, day.Month, day.Day, 0, 0, 0);

                employerStamps.Insert(0, firstInStamp);
            }
        }
        /// <summary>
        /// Check out sequence of stamps in collection
        /// that each In-stamp is followed by Out-stamp.
        /// </summary>
        /// <param name="employerStamps">Collection that should be checked.</param>
        /// <param name="employerID">Unique identifier of target employer.</param>
        /// <param name="day">Date of day of reporting.</param>
        /// <param name="notifications">Collection of notifications that will be filled during processing of collecting.</param>
        internal void VerifyStampsSequence(List <EmployerTimeStamp> employerStamps, int employerID, DateTime day, List <Notification> notifications)
        {
            if (employerStamps == null)
            {
                throw new ArgumentNullException(ReporterMessages.StampsCollectionReferenceIsNull);
            }

            if (notifications == null)
            {
                throw new ArgumentNullException(ReporterMessages.NotificationsRefrenceIsNull);
            }

            int i = 0;

            while (i < employerStamps.Count - 1)
            {
                if (employerStamps[i].Type == employerStamps[i + 1].Type)
                {
                    if (employerStamps[i].Time == employerStamps[i + 1].Time)
                    {
                        notifications.Add(new Notification(ReporterMessages.OneOfEqualStampsRemoved, NotificationType.Warning));
                        employerStamps.RemoveAt(i + 1);

                        // Indexer should not be changed because
                        // new (i + 1) element can be same type and time again.
                    }
                    else
                    {
                        notifications.Add(new Notification(ReporterMessages.NewStampInMiddleBetweenSameType, NotificationType.Warning));

                        // Compute difference in milliseconds.
                        double diffInMilliseconds = (employerStamps[i + 1].Time - employerStamps[i].Time).TotalMilliseconds;

                        EmployerTimeStamp middleStamp = new EmployerTimeStamp();
                        middleStamp.EmployerID = employerID;
                        middleStamp.Type       = employerStamps[i].Type == StampType.In ? StampType.Out : StampType.In;

                        // Add half of difference to earlier date.
                        middleStamp.Time = employerStamps[i].Time + TimeSpan.FromMilliseconds(diffInMilliseconds / 2);

                        employerStamps.Insert(i + 1, middleStamp);

                        // Go to element that was (i + 1) before insert.
                        i += 2;
                    }
                }
                else
                {
                    i++;
                }
            }
        }
        /// <summary>
        /// Check out last element of collection of stamps
        /// to add if require non existent last out-stamp.
        /// </summary>
        /// <param name="employerStamps">Collection that should be checked.</param>
        /// <param name="employerID">Unique identifier of target employer.</param>
        /// <param name="day">Date of day of reporting.</param>
        /// <param name="notifications">Collection of notifications that will be filled during processing of collecting.</param>
        internal void VerifyLastStamp(List <EmployerTimeStamp> employerStamps, int employerID, DateTime day, List <Notification> notifications)
        {
            if (employerStamps == null)
            {
                throw new ArgumentNullException(ReporterMessages.StampsCollectionReferenceIsNull);
            }

            if (notifications == null)
            {
                throw new ArgumentNullException(ReporterMessages.NotificationsRefrenceIsNull);
            }

            // if last record is not out-stamp.
            if (employerStamps.Count == 0 || employerStamps[employerStamps.Count - 1].Type != StampType.Out)
            {
                notifications.Add(new Notification(ReporterMessages.LastOutStampNotFound, NotificationType.Warning));

                // Date of next day.
                DateTime nextDay = day.AddDays(1);

                // Find records for next day.
                List <EmployerTimeStamp> employerStampsForNextDay = this.employerStampsSource.GetByEmployerIDForDay(employerID, nextDay);
                DateTime nextDayFindingDate = this.NextDayEarliestFindingTime(day);

                // First stamp of next day is Out-stamp and satisfies restriction time.
                if (employerStampsForNextDay != null && employerStampsForNextDay.Count > 0 &&
                    employerStampsForNextDay[0].Type == StampType.Out && employerStampsForNextDay[0].Time <= nextDayFindingDate)
                {
                    notifications.Add(new Notification(ReporterMessages.FirstOutNextDayAsLast, NotificationType.Message));

                    employerStamps.Add(employerStampsForNextDay[0]);
                }
                else
                {
                    notifications.Add(new Notification(ReporterMessages.EndDayAsLastOutStamp, NotificationType.Message));

                    // Added stamp.
                    EmployerTimeStamp lastOutStamp = new EmployerTimeStamp();
                    lastOutStamp.EmployerID = employerID;
                    lastOutStamp.Type       = StampType.Out;

                    // End of target day.
                    lastOutStamp.Time = new DateTime(day.Year, day.Month, day.Day, 23, 59, 59);

                    employerStamps.Add(lastOutStamp);
                }
            }
        }
예제 #5
0
        public void VerifyLastStamp_LastOutStampNextDayAfter4am_AddEndOfTargetDayAsLastOfOutStamp()
        {
            // Arrange
            int      targetEmployerID       = 1;
            DateTime targetDay              = new DateTime(2016, 3, 1);
            List <EmployerTimeStamp> stamps = new List <EmployerTimeStamp>()
            {
                // In-stamp at 6.00 pm of target day
                new EmployerTimeStamp()
                {
                    EmployerID = targetEmployerID, Type = StampType.In, Time = targetDay.AddHours(18)
                },

                // In-stamp at 6.00 am of next day
                new EmployerTimeStamp()
                {
                    EmployerID = targetEmployerID, Type = StampType.In, Time = targetDay.AddDays(1).AddHours(4).AddMinutes(1)
                }
            };
            var dailyReporter = this.СreateDailyReporter(stamps);
            List <Notification> notifications = new List <Notification>();

            // expected added stamp has Out type and end of target day as time.
            var expectedStamp = new EmployerTimeStamp()
            {
                EmployerID = targetEmployerID, Type = StampType.Out, Time = targetDay.AddHours(23).AddMinutes(59).AddSeconds(59)
            };

            // Remove last record to pass to method.
            stamps.RemoveAt(stamps.Count - 1);

            // Act
            dailyReporter.VerifyLastStamp(stamps, targetEmployerID, targetDay, notifications);

            // Assert
            // Check that item was added to collection.
            Assert.That(stamps[stamps.Count - 1], Is.EqualTo(expectedStamp).Using(new EmployerTimeStampComparer()));
        }