/src/test/java/org/graylog2/log/GelfJsonAppenderTest.java

http://github.com/t0xa/gelfj · Java · 89 lines · 69 code · 20 blank · 0 comment · 0 complexity · 413cfaa47a32e83d763d0815617f9083 MD5 · raw file

  1. package org.graylog2.log;
  2. import junit.framework.TestCase;
  3. import org.apache.log4j.Category;
  4. import org.apache.log4j.Level;
  5. import org.apache.log4j.spi.LoggingEvent;
  6. import org.graylog2.GelfMessage;
  7. import org.graylog2.GelfSender;
  8. import org.graylog2.GelfSenderResult;
  9. import org.graylog2.GelfUDPSender;
  10. import org.junit.Before;
  11. import org.junit.Test;
  12. import java.io.IOException;
  13. import static org.hamcrest.CoreMatchers.is;
  14. import static org.hamcrest.CoreMatchers.notNullValue;
  15. import static org.junit.Assert.assertThat;
  16. public class GelfJsonAppenderTest {
  17. private static final String CLASS_NAME = GelfJsonAppenderTest.class.getCanonicalName();
  18. private TestGelfSender gelfSender;
  19. private GelfAppender gelfAppender;
  20. @Before
  21. public void setUp() throws IOException {
  22. gelfSender = new TestGelfSender("localhost");
  23. gelfAppender = new GelfJsonAppender() {
  24. @Override
  25. public GelfSender getGelfSender() {
  26. return gelfSender;
  27. }
  28. @Override
  29. public void append(LoggingEvent event) {
  30. super.append(event);
  31. }
  32. @Override
  33. public Object transformExtendedField(String field, Object object) {
  34. return super.transformExtendedField(field, object);
  35. }
  36. };
  37. }
  38. @Test
  39. public void testAppend() throws Exception {
  40. String message = "{\"simpleProperty\":\"hello gelf\", \"message\":\"test\"}";
  41. LoggingEvent event = new LoggingEvent(CLASS_NAME, Category.getInstance(this.getClass()), 123L, Level.INFO, message, new RuntimeException("LOL"));
  42. gelfAppender.append(event);
  43. assertThat("simpleProperty property exists in additional fields", (String) gelfSender.getLastMessage().getAdditonalFields().get("simpleProperty"), is("hello gelf"));
  44. assertThat("message property exists in additional fields", (String) gelfSender.getLastMessage().getAdditonalFields().get("message"), is("test"));
  45. assertThat("Full message is still JSON", (String) gelfSender.getLastMessage().getFullMessage(), is(message));
  46. }
  47. @Test
  48. public void testBrokenJasom() throws Exception {
  49. String message = "{\"simpleProperty\":\"hello gelf, \"message\":\"test}";
  50. LoggingEvent event = new LoggingEvent(CLASS_NAME, Category.getInstance(this.getClass()), 123L, Level.INFO, message, new RuntimeException("LOL"));
  51. gelfAppender.append(event);
  52. assertThat("No additional fields are created", gelfSender.getLastMessage().getAdditonalFields().size(), is(0));
  53. assertThat("Full message is the same", (String) gelfSender.getLastMessage().getFullMessage(), is(message));
  54. }
  55. private class TestGelfSender extends GelfUDPSender {
  56. private GelfMessage lastMessage;
  57. public TestGelfSender(String host) throws IOException {
  58. super(host);
  59. }
  60. @Override
  61. public GelfSenderResult sendMessage(GelfMessage message) {
  62. this.lastMessage = message;
  63. return GelfSenderResult.OK;
  64. }
  65. public GelfMessage getLastMessage() {
  66. return lastMessage;
  67. }
  68. }
  69. }