Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package co.legion.app.kiosk;
- import org.junit.Assert;
- import org.junit.Test;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- import java.util.List;
- import java.util.TimeZone;
- import java.util.concurrent.TimeUnit;
- import co.legion.app.kiosk.client.models.Schedule;
- import co.legion.app.kiosk.client.models.TimeAttendanceConfig;
- /**
- * Created by aldo on 10/7/18.
- */
- public class ClockInTest {
- @Test
- public void testGetShiftForClockinBefore () {
- ScheduleManager manager = new ScheduleManager(getShifts());
- Calendar cal = getCal(2018, 10, 1, 9, 0);
- Schedule sc = manager.getShiftForClockIn(cal.getTime());
- Assert.assertEquals("1", sc.getShiftId());
- }
- @Test
- public void testGetShiftForClockinDuringFirst () {
- ScheduleManager manager = new ScheduleManager(getShifts());
- Calendar cal = getCal(2018, 10, 1, 11, 0);
- Schedule sc = manager.getShiftForClockIn(cal.getTime());
- Assert.assertEquals("1", sc.getShiftId());
- }
- @Test
- public void testGetShiftForClockinStartFirst () {
- ScheduleManager manager = new ScheduleManager(getShifts());
- Calendar cal = getCal(2018, 10, 1, 10, 0);
- Schedule sc = manager.getShiftForClockIn(cal.getTime());
- Assert.assertEquals("1", sc.getShiftId());
- }
- @Test
- public void testGetShiftForClockinEndFirst () {
- ScheduleManager manager = new ScheduleManager(getShifts());
- Calendar cal = getCal(2018, 10, 1, 14, 0);
- Schedule sc = manager.getShiftForClockIn(cal.getTime());
- Assert.assertEquals("1", sc.getShiftId());
- }
- @Test
- public void testGetShiftForClockinBetween () {
- ScheduleManager manager = new ScheduleManager(getShifts());
- Calendar cal = getCal(2018, 10, 1, 14, 30);
- Schedule sc = manager.getShiftForClockIn(cal.getTime());
- Assert.assertEquals("2", sc.getShiftId());
- }
- @Test
- public void testGetShiftForClockinAfterSecond () {
- ScheduleManager manager = new ScheduleManager(getShifts());
- Calendar cal = getCal(2018, 10, 1, 20, 0);
- Schedule sc = manager.getShiftForClockIn(cal.getTime());
- Assert.assertEquals("2", sc.getShiftId());
- }
- @Test
- public void testCanClockinNoShiftAllowed () {
- PolicyManager policyManager = new PolicyManager(getConfig(true, true, 15, true, 15));
- Calendar cal = getCal(2018, 10, 1, 20, 0);
- ClockInRestrictionReason reason = policyManager.canClockIn(null, cal.getTime());
- Assert.assertEquals(ClockInRestrictionReason.None, reason);
- }
- @Test
- public void testCanClockinNoShiftNotAllowed () {
- PolicyManager policyManager = new PolicyManager(getConfig(false, true, 15, true, 15));
- Calendar cal = getCal(2018, 10, 1, 20, 0);
- ClockInRestrictionReason reason = policyManager.canClockIn(null, cal.getTime());
- Assert.assertEquals(ClockInRestrictionReason.NoShift, reason);
- }
- @Test
- public void testCanClockinTooEearlyAllowed () {
- PolicyManager policyManager = new PolicyManager(getConfig(true, true, 15, true, 15));
- Calendar cal = getCal(2018, 10, 1, 9, 25);
- ClockInRestrictionReason reason = policyManager.canClockIn(getShift("1", 2018, 10, 1, 10, 0, 60 * 4), cal.getTime());
- Assert.assertEquals(ClockInRestrictionReason.None, reason);
- }
- @Test
- public void testCanClockinTooEearlyBoundryNotAllowed () {
- PolicyManager policyManager = new PolicyManager(getConfig(true, false, 15, true, 15));
- Calendar cal = getCal(2018, 10, 1, 9, 45);
- ClockInRestrictionReason reason = policyManager.canClockIn(getShift("1", 2018, 10, 1, 10, 0, 60 * 4), cal.getTime());
- Assert.assertEquals(ClockInRestrictionReason.None, reason);
- }
- @Test
- public void testCanClockinTooEearlyNotAllowed () {
- PolicyManager policyManager = new PolicyManager(getConfig(true, false, 15, true, 15));
- Calendar cal = getCal(2018, 10, 1, 9, 40);
- ClockInRestrictionReason reason = policyManager.canClockIn(getShift("1", 2018, 10, 1, 10, 0, 60 * 4), cal.getTime());
- Assert.assertEquals(ClockInRestrictionReason.TooEarly, reason);
- }
- @Test
- public void testCanClockinTooLateAllowed () {
- PolicyManager policyManager = new PolicyManager(getConfig(true, true, 15, true, 15));
- Calendar cal = getCal(2018, 10, 1, 10, 35);
- ClockInRestrictionReason reason = policyManager.canClockIn(getShift("1", 2018, 10, 1, 10, 0, 60 * 4), cal.getTime());
- Assert.assertEquals(ClockInRestrictionReason.None, reason);
- }
- @Test
- public void testCanClockinTooLateBoundryNotAllowed () {
- PolicyManager policyManager = new PolicyManager(getConfig(true, true, 15, false, 15));
- Calendar cal = getCal(2018, 10, 1, 10, 15);
- ClockInRestrictionReason reason = policyManager.canClockIn(getShift("1", 2018, 10, 1, 10, 0, 60 * 4), cal.getTime());
- Assert.assertEquals(ClockInRestrictionReason.None, reason);
- }
- @Test
- public void testCanClockinTooLateNotAllowed () {
- PolicyManager policyManager = new PolicyManager(getConfig(true, true, 15, false, 15));
- Calendar cal = getCal(2018, 10, 1, 10, 20);
- ClockInRestrictionReason reason = policyManager.canClockIn(getShift("1", 2018, 10, 1, 10, 0, 60 * 4), cal.getTime());
- Assert.assertEquals(ClockInRestrictionReason.TooLate, reason);
- }
- private List<Schedule> getShifts() {
- List<Schedule> shifts = new ArrayList<>();
- shifts.add(getShift("1", 2018, 10, 1, 10, 0, 60 * 4));
- shifts.add(getShift("2", 2018, 10, 1, 15, 0, 60 * 4));
- return shifts;
- }
- private Schedule getShift (String shiftId, int year, int month, int date, int hour, int minute, int duration) {
- Calendar cal = getCal(2018, 10, 1, 10, 0);
- Schedule sc = new Schedule();
- sc.setShiftId(shiftId);
- setStartDate(sc, cal);
- cal.add(Calendar.MINUTE, duration);
- setEndDate(sc, cal);
- return sc;
- }
- private Calendar getCal (int year, int month, int date, int hour, int minute) {
- Calendar cal = new GregorianCalendar();
- cal.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
- cal.clear();
- cal.set(year, month, date, hour, minute, 0);
- return cal;
- }
- private void setStartDate (Schedule sc, Calendar cal) {
- sc.setDayOfTheYear(cal.get(Calendar.DAY_OF_YEAR));
- sc.setYear(cal.get(Calendar.YEAR));
- sc.setStartDate(cal.getTime());
- sc.setStartMin(cal.get(Calendar.MINUTE) * cal.get(Calendar.HOUR));
- }
- private void setEndDate (Schedule sc, Calendar cal) {
- sc.setEndDate(cal.getTime());
- sc.setEndMin(cal.get(Calendar.MINUTE) * cal.get(Calendar.HOUR));
- }
- private TimeAttendanceConfig getConfig () {
- return getConfig(true, true, 15, true, 15);
- }
- private TimeAttendanceConfig getConfig (boolean allowClockInWithoutShift,
- boolean allowEarlyClockIn,
- int earlyClockInTolerance,
- boolean allowLateClockIn,
- int lateClockInTolerance) {
- TimeAttendanceConfig config = new TimeAttendanceConfig();
- config.setAllowClockInWithoutShift(allowClockInWithoutShift);
- config.setAllowEarlyClockIn(allowEarlyClockIn);
- config.setEarlyClockInTolerance(earlyClockInTolerance);
- config.setAllowLateClockIn(allowLateClockIn);
- config.setLateClockInTolerance(lateClockInTolerance);
- return config;
- }
- class ScheduleManager {
- private List<Schedule> shifts;
- public ScheduleManager() {
- }
- public ScheduleManager(List<Schedule> shifts) {
- this.shifts = shifts;
- }
- public List<Schedule> getShifts() {
- return shifts;
- }
- public void setShifts(List<Schedule> shifts) {
- this.shifts = shifts;
- }
- public Schedule getShiftForClockIn () {
- return getShiftForClockIn(new Date());
- }
- public Schedule getShiftForClockIn (Date now) {
- Schedule sc = null;
- for (Schedule schedule: shifts) {
- sc = schedule;
- if (schedule.getStartDate().after(now)) {
- break;
- }
- if ((schedule.getStartDate().equals(now) || schedule.getStartDate().before(now)) &&
- schedule.getEndDate().equals(now) || schedule.getEndDate().after(now)) {
- break;
- }
- }
- return sc;
- }
- }
- class PolicyManager {
- private TimeAttendanceConfig config;
- public PolicyManager() {
- }
- public PolicyManager(TimeAttendanceConfig config) {
- this.config = config;
- }
- public TimeAttendanceConfig getConfig() {
- return config;
- }
- public void setConfig(TimeAttendanceConfig config) {
- this.config = config;
- }
- public ClockInRestrictionReason canClockIn (Schedule sc) {
- return canClockIn(sc, new Date());
- }
- public ClockInRestrictionReason canClockIn (Schedule sc, Date now) {
- ClockInRestrictionReason reason = ClockInRestrictionReason.None;
- if (sc == null && !config.getAllowClockInWithoutShift()) {
- reason = ClockInRestrictionReason.NoShift;
- } else if (sc != null) {
- long diff = now.getTime() - sc.getStartDate().getTime();
- long diffMinutes = TimeUnit.MILLISECONDS.toMinutes(diff);
- if (diff < 0 && -diffMinutes > config.getEarlyClockInTolerance() &&
- !config.isAllowEarlyClockIn()) {
- reason = ClockInRestrictionReason.TooEarly;
- } else if (diff > 0 && diffMinutes > config.getLateClockInTolerance() &&
- !config.isAllowLateClockIn()) {
- reason = ClockInRestrictionReason.TooLate;
- }
- }
- return reason;
- }
- }
- enum ClockInRestrictionReason {
- None, TooEarly, TooLate, NoShift
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement