EventFactory { public static <T extends ImageEvent> T imageEvent(Class<T> eventClass, Image image) { try { T event = eventClass.newInstance(); ZonedDateTime now = now(); event.setId(uuid()); event.setTimestamp(now); event.setImage(image); return event; } catch (Exception e) { throw new RuntimeException(e); } }  static T instanceEvent(Class<T> eventClass, Instance instance); static InstanceEvent instanceToEvent(Instance instance); static InstanceState instanceEventToState(InstanceEvent event); static T imageEvent(Class<T> eventClass, Image image); static ImageEvent imageToEvent(Image image); static ImageState imageEventToState(ImageEvent event); static T projectEvent(Class<T> eventClass, Project project); static String uuid(); static ZonedDateTime now();  }
@Test public void testImageEvent() throws Exception { Image image = new Image(); ZonedDateTime timestamp = now(); image.setTimestamp(timestamp); ImageEvent event1 = imageEvent(ImageSavedEvent.class, image); Thread.sleep(50); ImageEvent event2 = imageEvent(ImageSavedEvent.class, image); assertThat(event1.getId(), not(equalTo(event2.getId()))); assertThat(event1.getTimestamp(), not(equalTo(event2.getTimestamp()))); assertThat(event1.getImage(), equalTo(event2.getImage())); assertThat(event1.getImage().getTimestamp(), equalTo(timestamp)); }