TimeOperation implements EventOperation { @Override public InternalEvent perform(InternalEvent ievent) { String field; try { field = ievent.getEventObj().getFieldAsString(timeField); } catch (FieldNotFoundException e) { throw new OperationException( "time field " + timeField + " does not exist in " + ievent.getEventString()); } ievent.setEventTime(getTimestamp(field, timeFieldType)); return ievent; } TimeOperation(String timeField, TimeFieldType timeFieldType); @Override InternalEvent perform(InternalEvent ievent);  }
@Test public void testValidTime() throws FieldNotFoundException { InternalEvent ievent = new InternalEvent("foo", null, 1); DummyStringEvent devent = spy(new DummyStringEvent("")); ievent.setEventObj(devent); doReturn("1504728473").when(devent).getFieldAsString("foo"); TimeOperation op = new TimeOperation("foo", TimeFieldType.SECONDS); op.perform(ievent); assertEquals(1504728473000l, ievent.getEventTime()); }
@Test public void testValidISO8601Ms() throws FieldNotFoundException { InternalEvent ievent = new InternalEvent("foo", null, 1); DummyStringEvent devent = spy(new DummyStringEvent("")); ievent.setEventObj(devent); doReturn("2020-05-04T10:45:01.480Z").when(devent).getFieldAsString("foo"); TimeOperation op = new TimeOperation("foo", TimeFieldType.ISO8601); op.perform(ievent); assertEquals(1588589101480l, ievent.getEventTime()); }
@Test public void testValidISO8601Ds() throws FieldNotFoundException { InternalEvent ievent = new InternalEvent("foo", null, 1); DummyStringEvent devent = spy(new DummyStringEvent("")); ievent.setEventObj(devent); doReturn("2020-05-04T10:45:01Z").when(devent).getFieldAsString("foo"); TimeOperation op = new TimeOperation("foo", TimeFieldType.ISO8601); op.perform(ievent); assertEquals(1588589101000l, ievent.getEventTime()); }
@Test(expected = OperationException.class) public void testInvalidISO8601() throws FieldNotFoundException { InternalEvent ievent = new InternalEvent("foo", null, 1); DummyStringEvent devent = spy(new DummyStringEvent("")); ievent.setEventObj(devent); doReturn("note a date string").when(devent).getFieldAsString("foo"); TimeOperation op = new TimeOperation("foo", TimeFieldType.ISO8601); op.perform(ievent); }
@Test(expected = OperationException.class) public void testInvalidTime() throws FieldNotFoundException { InternalEvent ievent = new InternalEvent("foo", null, 1); DummyStringEvent devent = spy(new DummyStringEvent("")); ievent.setEventObj(devent); doReturn("-1").when(devent).getFieldAsString("foo"); TimeOperation op = new TimeOperation("foo", TimeFieldType.SECONDS); op.perform(ievent); }
@Test(expected = OperationException.class) public void testNullField() throws FieldNotFoundException { InternalEvent ievent = new InternalEvent("foo", null, 1); DummyStringEvent devent = spy(new DummyStringEvent("")); ievent.setEventObj(devent); doThrow(FieldNotFoundException.class).when(devent).getFieldAsString("foo"); TimeOperation op = new TimeOperation("foo", TimeFieldType.SECONDS); op.perform(ievent); }