Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.45 KB | None | 0 0
  1. package co.legion.app.kiosk;
  2.  
  3. import org.junit.Assert;
  4. import org.junit.Test;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9. import java.util.GregorianCalendar;
  10. import java.util.List;
  11. import java.util.TimeZone;
  12. import java.util.concurrent.TimeUnit;
  13.  
  14. import co.legion.app.kiosk.client.models.Schedule;
  15. import co.legion.app.kiosk.client.models.TimeAttendanceConfig;
  16.  
  17. /**
  18.  * Created by aldo on 10/7/18.
  19.  */
  20. public class ClockInTest {
  21.  
  22.     @Test
  23.     public void testGetShiftForClockinBefore () {
  24.         ScheduleManager manager = new ScheduleManager(getShifts());
  25.         Calendar cal = getCal(2018, 10, 1, 9, 0);
  26.         Schedule sc = manager.getShiftForClockIn(cal.getTime());
  27.         Assert.assertEquals("1", sc.getShiftId());
  28.     }
  29.  
  30.     @Test
  31.     public void testGetShiftForClockinDuringFirst () {
  32.         ScheduleManager manager = new ScheduleManager(getShifts());
  33.         Calendar cal = getCal(2018, 10, 1, 11, 0);
  34.         Schedule sc = manager.getShiftForClockIn(cal.getTime());
  35.         Assert.assertEquals("1", sc.getShiftId());
  36.     }
  37.  
  38.     @Test
  39.     public void testGetShiftForClockinStartFirst () {
  40.         ScheduleManager manager = new ScheduleManager(getShifts());
  41.         Calendar cal = getCal(2018, 10, 1, 10, 0);
  42.         Schedule sc = manager.getShiftForClockIn(cal.getTime());
  43.         Assert.assertEquals("1", sc.getShiftId());
  44.     }
  45.  
  46.     @Test
  47.     public void testGetShiftForClockinEndFirst () {
  48.         ScheduleManager manager = new ScheduleManager(getShifts());
  49.         Calendar cal = getCal(2018, 10, 1, 14, 0);
  50.         Schedule sc = manager.getShiftForClockIn(cal.getTime());
  51.         Assert.assertEquals("1", sc.getShiftId());
  52.     }
  53.  
  54.     @Test
  55.     public void testGetShiftForClockinBetween () {
  56.         ScheduleManager manager = new ScheduleManager(getShifts());
  57.         Calendar cal = getCal(2018, 10, 1, 14, 30);
  58.         Schedule sc = manager.getShiftForClockIn(cal.getTime());
  59.         Assert.assertEquals("2", sc.getShiftId());
  60.     }
  61.  
  62.     @Test
  63.     public void testGetShiftForClockinAfterSecond () {
  64.         ScheduleManager manager = new ScheduleManager(getShifts());
  65.         Calendar cal = getCal(2018, 10, 1, 20, 0);
  66.         Schedule sc = manager.getShiftForClockIn(cal.getTime());
  67.         Assert.assertEquals("2", sc.getShiftId());
  68.     }
  69.  
  70.     @Test
  71.     public void testCanClockinNoShiftAllowed () {
  72.         PolicyManager policyManager = new PolicyManager(getConfig(true, true, 15, true, 15));
  73.         Calendar cal = getCal(2018, 10, 1, 20, 0);
  74.         ClockInRestrictionReason reason = policyManager.canClockIn(null, cal.getTime());
  75.         Assert.assertEquals(ClockInRestrictionReason.None, reason);
  76.     }
  77.  
  78.     @Test
  79.     public void testCanClockinNoShiftNotAllowed () {
  80.         PolicyManager policyManager = new PolicyManager(getConfig(false, true, 15, true, 15));
  81.         Calendar cal = getCal(2018, 10, 1, 20, 0);
  82.         ClockInRestrictionReason reason = policyManager.canClockIn(null, cal.getTime());
  83.         Assert.assertEquals(ClockInRestrictionReason.NoShift, reason);
  84.     }
  85.  
  86.     @Test
  87.     public void testCanClockinTooEearlyAllowed () {
  88.         PolicyManager policyManager = new PolicyManager(getConfig(true, true, 15, true, 15));
  89.         Calendar cal = getCal(2018, 10, 1, 9, 25);
  90.         ClockInRestrictionReason reason = policyManager.canClockIn(getShift("1", 2018, 10, 1, 10, 0, 60 * 4), cal.getTime());
  91.         Assert.assertEquals(ClockInRestrictionReason.None, reason);
  92.     }
  93.  
  94.     @Test
  95.     public void testCanClockinTooEearlyBoundryNotAllowed () {
  96.         PolicyManager policyManager = new PolicyManager(getConfig(true, false, 15, true, 15));
  97.         Calendar cal = getCal(2018, 10, 1, 9, 45);
  98.         ClockInRestrictionReason reason = policyManager.canClockIn(getShift("1", 2018, 10, 1, 10, 0, 60 * 4), cal.getTime());
  99.         Assert.assertEquals(ClockInRestrictionReason.None, reason);
  100.     }
  101.  
  102.     @Test
  103.     public void testCanClockinTooEearlyNotAllowed () {
  104.         PolicyManager policyManager = new PolicyManager(getConfig(true, false, 15, true, 15));
  105.         Calendar cal = getCal(2018, 10, 1, 9, 40);
  106.         ClockInRestrictionReason reason = policyManager.canClockIn(getShift("1", 2018, 10, 1, 10, 0, 60 * 4), cal.getTime());
  107.         Assert.assertEquals(ClockInRestrictionReason.TooEarly, reason);
  108.     }
  109.  
  110.     @Test
  111.     public void testCanClockinTooLateAllowed () {
  112.         PolicyManager policyManager = new PolicyManager(getConfig(true, true, 15, true, 15));
  113.         Calendar cal = getCal(2018, 10, 1, 10, 35);
  114.         ClockInRestrictionReason reason = policyManager.canClockIn(getShift("1", 2018, 10, 1, 10, 0, 60 * 4), cal.getTime());
  115.         Assert.assertEquals(ClockInRestrictionReason.None, reason);
  116.     }
  117.  
  118.     @Test
  119.     public void testCanClockinTooLateBoundryNotAllowed () {
  120.         PolicyManager policyManager = new PolicyManager(getConfig(true, true, 15, false, 15));
  121.         Calendar cal = getCal(2018, 10, 1, 10, 15);
  122.         ClockInRestrictionReason reason = policyManager.canClockIn(getShift("1", 2018, 10, 1, 10, 0, 60 * 4), cal.getTime());
  123.         Assert.assertEquals(ClockInRestrictionReason.None, reason);
  124.     }
  125.  
  126.     @Test
  127.     public void testCanClockinTooLateNotAllowed () {
  128.         PolicyManager policyManager = new PolicyManager(getConfig(true, true, 15, false, 15));
  129.         Calendar cal = getCal(2018, 10, 1, 10, 20);
  130.         ClockInRestrictionReason reason = policyManager.canClockIn(getShift("1", 2018, 10, 1, 10, 0, 60 * 4), cal.getTime());
  131.         Assert.assertEquals(ClockInRestrictionReason.TooLate, reason);
  132.     }
  133.  
  134.     private List<Schedule> getShifts() {
  135.         List<Schedule> shifts = new ArrayList<>();
  136.         shifts.add(getShift("1", 2018, 10, 1, 10, 0, 60 * 4));
  137.         shifts.add(getShift("2", 2018, 10, 1, 15, 0, 60 * 4));
  138.         return shifts;
  139.     }
  140.  
  141.     private Schedule getShift (String shiftId, int year, int month, int date, int hour, int minute, int duration) {
  142.         Calendar cal = getCal(2018, 10, 1, 10, 0);
  143.         Schedule sc = new Schedule();
  144.         sc.setShiftId(shiftId);
  145.         setStartDate(sc, cal);
  146.         cal.add(Calendar.MINUTE, duration);
  147.         setEndDate(sc, cal);
  148.         return sc;
  149.     }
  150.  
  151.     private Calendar getCal (int year, int month, int date, int hour, int minute) {
  152.         Calendar cal = new GregorianCalendar();
  153.         cal.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
  154.         cal.clear();
  155.         cal.set(year, month, date, hour, minute, 0);
  156.         return cal;
  157.     }
  158.  
  159.     private void setStartDate (Schedule sc, Calendar cal) {
  160.         sc.setDayOfTheYear(cal.get(Calendar.DAY_OF_YEAR));
  161.         sc.setYear(cal.get(Calendar.YEAR));
  162.         sc.setStartDate(cal.getTime());
  163.         sc.setStartMin(cal.get(Calendar.MINUTE) * cal.get(Calendar.HOUR));
  164.     }
  165.  
  166.     private void setEndDate (Schedule sc, Calendar cal) {
  167.         sc.setEndDate(cal.getTime());
  168.         sc.setEndMin(cal.get(Calendar.MINUTE) * cal.get(Calendar.HOUR));
  169.     }
  170.  
  171.     private TimeAttendanceConfig getConfig () {
  172.         return getConfig(true, true, 15, true, 15);
  173.     }
  174.  
  175.     private TimeAttendanceConfig getConfig (boolean allowClockInWithoutShift,
  176.                                             boolean allowEarlyClockIn,
  177.                                             int earlyClockInTolerance,
  178.                                             boolean allowLateClockIn,
  179.                                             int lateClockInTolerance) {
  180.         TimeAttendanceConfig config = new TimeAttendanceConfig();
  181.         config.setAllowClockInWithoutShift(allowClockInWithoutShift);
  182.         config.setAllowEarlyClockIn(allowEarlyClockIn);
  183.         config.setEarlyClockInTolerance(earlyClockInTolerance);
  184.         config.setAllowLateClockIn(allowLateClockIn);
  185.         config.setLateClockInTolerance(lateClockInTolerance);
  186.         return config;
  187.     }
  188.  
  189.     class ScheduleManager {
  190.  
  191.         private List<Schedule> shifts;
  192.  
  193.         public ScheduleManager() {
  194.         }
  195.  
  196.         public ScheduleManager(List<Schedule> shifts) {
  197.             this.shifts = shifts;
  198.         }
  199.  
  200.         public List<Schedule> getShifts() {
  201.             return shifts;
  202.         }
  203.  
  204.         public void setShifts(List<Schedule> shifts) {
  205.             this.shifts = shifts;
  206.         }
  207.  
  208.         public Schedule getShiftForClockIn () {
  209.             return getShiftForClockIn(new Date());
  210.         }
  211.  
  212.         public Schedule getShiftForClockIn (Date now) {
  213.  
  214.             Schedule sc = null;
  215.             for (Schedule schedule: shifts) {
  216.                 sc = schedule;
  217.                 if (schedule.getStartDate().after(now)) {
  218.                     break;
  219.                 }
  220.                 if ((schedule.getStartDate().equals(now) || schedule.getStartDate().before(now)) &&
  221.                         schedule.getEndDate().equals(now) || schedule.getEndDate().after(now)) {
  222.                     break;
  223.                 }
  224.             }
  225.  
  226.             return sc;
  227.         }
  228.     }
  229.  
  230.     class PolicyManager {
  231.  
  232.         private TimeAttendanceConfig config;
  233.  
  234.         public PolicyManager() {
  235.         }
  236.  
  237.         public PolicyManager(TimeAttendanceConfig config) {
  238.             this.config = config;
  239.         }
  240.  
  241.         public TimeAttendanceConfig getConfig() {
  242.             return config;
  243.         }
  244.  
  245.         public void setConfig(TimeAttendanceConfig config) {
  246.             this.config = config;
  247.         }
  248.  
  249.         public ClockInRestrictionReason canClockIn (Schedule sc) {
  250.             return canClockIn(sc, new Date());
  251.         }
  252.  
  253.         public ClockInRestrictionReason canClockIn (Schedule sc, Date now) {
  254.             ClockInRestrictionReason reason = ClockInRestrictionReason.None;
  255.             if (sc == null && !config.getAllowClockInWithoutShift()) {
  256.                 reason = ClockInRestrictionReason.NoShift;
  257.             } else if (sc != null) {
  258.                 long diff = now.getTime() - sc.getStartDate().getTime();
  259.                 long diffMinutes = TimeUnit.MILLISECONDS.toMinutes(diff);
  260.                 if (diff < 0 && -diffMinutes > config.getEarlyClockInTolerance() &&
  261.                         !config.isAllowEarlyClockIn()) {
  262.                     reason = ClockInRestrictionReason.TooEarly;
  263.                 } else if (diff > 0 && diffMinutes > config.getLateClockInTolerance() &&
  264.                         !config.isAllowLateClockIn()) {
  265.                     reason = ClockInRestrictionReason.TooLate;
  266.                 }
  267.             }
  268.             return reason;
  269.         }
  270.     }
  271.  
  272.     enum ClockInRestrictionReason {
  273.         None, TooEarly, TooLate, NoShift
  274.     }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement