@Test
  public void immediateFlushIsSane() {
    encoder.setImmediateFlush(true);
    assertThat(encoder.isImmediateFlush(), is(true));

    encoder.setImmediateFlush(false);
    assertThat(encoder.isImmediateFlush(), is(false));
  }
  @Test
  public void closePutsSeparatorAtTheEnd() throws Exception {
    IAccessEvent event = mockBasicILoggingEvent();

    encoder.doEncode(event);
    encoder.close();
    closeQuietly(outputStream);

    assertThat(outputStream.toString(), Matchers.endsWith(LINE_SEPARATOR));
  }
  @Test
  public void propertiesInContextAreIncluded() throws Exception {
    Map<String, String> propertyMap = new HashMap<String, String>();
    propertyMap.put("thing_one", "One");
    propertyMap.put("thing_two", "Three");

    final Context context = mock(Context.class);
    when(context.getCopyOfPropertyMap()).thenReturn(propertyMap);

    IAccessEvent event = mockBasicILoggingEvent();

    encoder.setContext(context);
    encoder.doEncode(event);
    closeQuietly(outputStream);

    JsonNode node = MAPPER.readTree(outputStream.toByteArray());

    assertThat(node.get("thing_one").textValue(), is("One"));
    assertThat(node.get("thing_two").textValue(), is("Three"));
  }
  @Test
  public void basicsAreIncluded() throws Exception {
    final long timestamp = System.currentTimeMillis();

    IAccessEvent event = mockBasicILoggingEvent();
    when(event.getTimeStamp()).thenReturn(timestamp);

    encoder.doEncode(event);
    closeQuietly(outputStream);

    JsonNode node = MAPPER.readTree(outputStream.toByteArray());

    assertThat(
        node.get("@timestamp").textValue(),
        is(FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZZ").format(timestamp)));
    assertThat(node.get("@version").intValue(), is(1));
    assertThat(
        node.get("@message").textValue(),
        is(
            String.format(
                "%s - %s [%s] \"%s\" %s %s",
                event.getRemoteHost(),
                event.getRemoteUser(),
                FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
                    .format(event.getTimeStamp()),
                event.getRequestURL(),
                event.getStatusCode(),
                event.getContentLength())));

    assertThat(node.get("@fields.method").textValue(), is(event.getMethod()));
    assertThat(node.get("@fields.protocol").textValue(), is(event.getProtocol()));
    assertThat(node.get("@fields.status_code").asInt(), is(event.getStatusCode()));
    assertThat(node.get("@fields.requested_url").textValue(), is(event.getRequestURL()));
    assertThat(node.get("@fields.requested_uri").textValue(), is(event.getRequestURI()));
    assertThat(node.get("@fields.remote_host").textValue(), is(event.getRemoteHost()));
    assertThat(node.get("@fields.HOSTNAME").textValue(), is(event.getRemoteHost()));
    assertThat(node.get("@fields.remote_user").textValue(), is(event.getRemoteUser()));
    assertThat(node.get("@fields.content_length").asLong(), is(event.getContentLength()));
  }
 @Before
 public void before() throws Exception {
   outputStream = new ByteArrayOutputStream();
   encoder = new LogstashAccessEncoder();
   encoder.init(outputStream);
 }