/*
 * Copyright 2011 The greplin-exception-catcher Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.greplin.gec;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.StringWriter;

/**
 * Tests for the GecAppender.
 */
public class GecAppenderTest {
  private static final String ENVIRONMENT = "prod";
  private static final String PROJECT = "secretTown";
  private static final String SERVER_NAME = "secretTown FE 1";
  private static final String LOG_MESSAGE = "Error while rendering request";
  private static final String MESSAGE = "Illegal in 50 states";
  private static final String TYPE = IllegalArgumentException.class.getCanonicalName();

  GecAppender appender;


  @Before
  public void setUp() {
    appender = new GecAppender();
    appender.setEnvironment(ENVIRONMENT);
    appender.setProject(PROJECT);
    appender.setServerName(SERVER_NAME);
  }

  @Test
  public void testGecAppender() throws IOException {
    try {
      throw new IllegalArgumentException(MESSAGE);
    } catch (IllegalArgumentException e) {
      StringWriter writer = new StringWriter();
      appender.writeFormattedException(LOG_MESSAGE, e, writer);

      JsonNode root = parseJson(writer.toString());
      assertField(root, "project", PROJECT);
      assertField(root, "environment", ENVIRONMENT);
      assertField(root, "serverName", SERVER_NAME);
      assertField(root, "backtrace", ExceptionUtils.getStackTrace(e));
      assertField(root, "message", MESSAGE);
      assertField(root, "logMessage", LOG_MESSAGE);
      assertField(root, "type", TYPE);
    }
  }

  @Test
  public void testNonExceptionAppender() throws IOException {
    StringWriter writer = new StringWriter();
    appender.writeFormattedException("Error while rendering request", writer);

    JsonNode root = parseJson(writer.toString());
    assertField(root, "project", PROJECT);
    assertField(root, "environment", ENVIRONMENT);
    assertField(root, "serverName", SERVER_NAME);
    assertField(root, "message", LOG_MESSAGE);
    assertField(root, "logMessage", LOG_MESSAGE);
    assertField(root, "type", "N/A");
    String backtrace = root.path("backtrace").getTextValue();
    Assert.assertTrue(backtrace.contains("testNonExceptionAppender"));
    Assert.assertFalse(backtrace.contains("writeFormattedException"));
  }


  /**
   * Parse a JSON string in to a JsonNode.
   * @param json the string
   * @return the node
   * @throws IOException should never happen
   */
  private JsonNode parseJson(String json) throws IOException {
    return new ObjectMapper().readTree(json);
  }


  /**
   * Assert that the given field has the given value.
   * @param root the root of the JSON tree
   * @param field the field name
   * @param value the expected value
   */
  private void assertField(JsonNode root, String field, String value) {
    Assert.assertEquals(value, root.path(field).getTextValue());
  }
}

@@ -699,16 +699,47 @@
 nUtils;%0A
+import org.apache.log4j.Level;%0A
 import o
@@ -1897,16 +1897,29 @@
 SAGE, e,
+ Level.ERROR,
  writer)
@@ -2526,16 +2526,29 @@
 equest%22,
+ Level.ERROR,
  writer)

Fix tests