EventConsumer extends MessageConsumer { @SuppressWarnings("unchecked") Object buildEvent(Message message) { Object event = eventPool.get(); if (event instanceof ContainsData) { ((ContainsData) event).setData(message.getBodyContent()); } else if (event instanceof ContainsContent) { Class<?> parameterType = getParameterType(event, ContainsContent.class); ((ContainsContent) event).setContent(message.getBodyAs(parameterType)); } else if (event instanceof ContainsId) { Class<?> parameterType = getParameterType(event, ContainsId.class); ((ContainsId) event).setId(message.getBodyAs(parameterType)); } return event; } EventConsumer(Event<Object> eventControl, Instance<Object> eventPool); @Override void handleMessage(Message message);  }
@Test public void testBuildEventShouldReturnEvent() { Message message = new Message(); EventConsumer eventConsumer = buildEventConsumer(TestEventOne.class); TestEventOne event = (TestEventOne)eventConsumer.buildEvent(message); Assert.assertNotNull(event); }
@Test public void testBuildEventShouldReturnSameData() { byte[] input = "abc".getBytes(Charset.forName("UTF-8")); Message message = new Message().body(input); EventConsumer eventConsumer = buildEventConsumer(TestEventContainsData.class); TestEventContainsData event = (TestEventContainsData)eventConsumer.buildEvent(message); byte[] output = event.getData(); Assert.assertNotNull(output); Assert.assertArrayEquals(input, output); }
@Test public void testBuildEventShouldReturnSameId() { Integer input = 12345; Message message = new Message().body(input); EventConsumer eventConsumer = buildEventConsumer(TestEventContainsId.class); TestEventContainsId event = (TestEventContainsId)eventConsumer.buildEvent(message); Integer output = event.getId(); Assert.assertNotNull(output); Assert.assertEquals(input, output); }
@Test public void testBuildEventShouldReturnSameContent() { String input = "test"; Message message = new Message().body(input); EventConsumer eventConsumer = buildEventConsumer(TestEventContainsContent.class); TestEventContainsContent event = (TestEventContainsContent)eventConsumer.buildEvent(message); String output = event.getContent(); Assert.assertNotNull(output); Assert.assertEquals(input, output); }