/** * nextWindow. * * @param currTime current timestamp */ public AbsenceWindow nextWindow(long currTime) { AbsenceWindow window = new AbsenceWindow(); if (rule instanceof AbsenceDailyRule) { AbsenceDailyRule r = (AbsenceDailyRule) rule; long adjustment = 0; // if today's window already expires, then adjust to tomorrow's window if (currTime % AbsenceDailyRule.DAY_MILLI_SECONDS > r.startOffset) { adjustment = AbsenceDailyRule.DAY_MILLI_SECONDS; } // use current timestamp to round down to day long day = currTime - currTime % AbsenceDailyRule.DAY_MILLI_SECONDS; day += adjustment; window.startTime = day + r.startOffset; window.endTime = day + r.endOffset; return window; } else { throw new UnsupportedOperationException("not supported rule " + rule); } } }
@Test public void testDataMissing() { List<Object> expectedHosts = Arrays.asList("host1"); AbsenceWindow window = new AbsenceWindow(); window.startTime = 100L; window.endTime = 200L; AbsenceWindowProcessor processor = new AbsenceWindowProcessor(expectedHosts, window); processor.process(Arrays.asList("host2"), 90); Assert.assertEquals(processor.checkStatus(), AbsenceWindowProcessor.OccurStatus.not_sure); processor.process(Arrays.asList("host3"), 101); Assert.assertEquals(processor.checkStatus(), AbsenceWindowProcessor.OccurStatus.not_sure); processor.process(Arrays.asList("host3"), 138); Assert.assertEquals(processor.checkStatus(), AbsenceWindowProcessor.OccurStatus.not_sure); processor.process(Arrays.asList("host2"), 189); Assert.assertEquals(processor.checkStatus(), AbsenceWindowProcessor.OccurStatus.not_sure); processor.process(Arrays.asList("host2"), 201); Assert.assertEquals(processor.checkStatus(), AbsenceWindowProcessor.OccurStatus.absent); }
@Test(expected = IllegalStateException.class) public void testDataExists() { List<Object> expectedHosts = Arrays.asList("host1"); AbsenceWindow window = new AbsenceWindow(); window.startTime = 100L; window.endTime = 200L; AbsenceWindowProcessor processor = new AbsenceWindowProcessor(expectedHosts, window); processor.process(Arrays.asList("host2"), 90); Assert.assertEquals(processor.checkStatus(), AbsenceWindowProcessor.OccurStatus.not_sure); processor.process(Arrays.asList("host3"), 101); Assert.assertEquals(processor.checkStatus(), AbsenceWindowProcessor.OccurStatus.not_sure); processor.process(Arrays.asList("host1"), 138); Assert.assertEquals(processor.checkStatus(), AbsenceWindowProcessor.OccurStatus.occured); processor.process(Arrays.asList("host2"), 189); Assert.assertEquals(processor.checkStatus(), AbsenceWindowProcessor.OccurStatus.occured); processor.process(Arrays.asList("host2"), 201); Assert.assertEquals(processor.checkStatus(), AbsenceWindowProcessor.OccurStatus.occured); Assert.assertEquals(processor.checkExpired(), true); processor.process(Arrays.asList("host2"), 225); } }