EventPublisher { static Message buildMessage(PublisherConfiguration publisherConfiguration, Object event) { Message message = new Message(publisherConfiguration.basicProperties) .exchange(publisherConfiguration.exchange) .routingKey(publisherConfiguration.routingKey); if (publisherConfiguration.persistent) { message.persistent(); } if (event instanceof ContainsData) { message.body(((ContainsData) event).getData()); } else if (event instanceof ContainsContent) { message.body(((ContainsContent) event).getContent()); } else if (event instanceof ContainsId) { message.body(((ContainsId) event).getId()); } return message; } @Inject  EventPublisher(ConnectionFactory connectionFactory); void addEvent(Class<T> eventType, PublisherConfiguration configuration); void publishEvent(@Observes Object event);  }
@Test public void buildMessageShouldReturnSameInteger() { final int input = 300; Message message = EventPublisher.buildMessage(TEST_CONFIGURATION, new ContainsId<Integer>() { @Override public void setId(Integer id) { } @Override public Integer getId() { return input; } } ); int output = message.getBodyAs(Integer.class); Assert.assertEquals(input, output); }
@Test public void buildMessageShouldReturnSameLong() { final long input = 3000115550607l; Message message = EventPublisher.buildMessage(TEST_CONFIGURATION, new ContainsId<Long>() { @Override public void setId(Long id) { } @Override public Long getId() { return input; } } ); long output = message.getBodyAs(Long.class); Assert.assertEquals(input, output); }
@Test public void buildMessageShouldReturnSameUUID() { final String input = UUID.randomUUID().toString(); Message message = EventPublisher.buildMessage(TEST_CONFIGURATION, new ContainsId<String>() { @Override public void setId(String id) {} @Override public String getId() { return input; } } ); String output = message.getBodyAs(String.class); Assert.assertEquals(input, output); }
@Test public void buildMessageShouldReturnSameText() { final String input = "testText"; Message message = EventPublisher.buildMessage(TEST_CONFIGURATION, new ContainsContent<String>() { @Override public void setContent(String content) { } @Override public String getContent() { return input; } } ); String output = message.getBodyAs(String.class); Assert.assertEquals(input, output); }
@Test public void buildMessageShouldReturnSameData() { final byte[] input = "testText".getBytes(Charset.forName("UTF-8")); Message message = EventPublisher.buildMessage(TEST_CONFIGURATION, new ContainsData() { @Override public void setData(byte[] data) {} @Override public byte[] getData() { return input; } } ); byte[] output = message.getBodyContent(); Assert.assertArrayEquals(input, output); }