package com.bitmoving.util;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

import java.io.IOException;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class MessageBuilderTest {

    private final LoggerContext context = new LoggerContext();
    private final Logger logger = context.getLogger(MessageBuilderTest.class);
    private final LayoutWrappingEncoder encoder = setupEncoder();

    @Test
    public void basic_fields_are_set_on_message() throws IOException {
        String token = "token";
        String author = "author";
        MessageBuilder builder = new MessageBuilder(token, author, encoder);

        LoggingEvent event = new LoggingEvent("", logger, Level.DEBUG, "a log message", null, null);
        JsonObject message = builder.buildMessage(event);

        assertThat(message.get("flow_token").asString(), equalTo(token));
        assertThat(message.get("author").asObject().get("name").asString(), equalTo(author));
    }

    @Test
    public void put_marker_values_into_fields() throws IOException {
        Marker marker = MarkerFactory.getMarker("COUNTRY");
        LoggingEvent event = new LoggingEvent("", logger, Level.ALL, "a log message", null, null);
        event.setMarker(marker);

        MessageBuilder builder = new MessageBuilder("token", "author", encoder);

        JsonObject message = builder.buildMessage(event);
        System.out.println("message = " + message);
        JsonValue fields = message.get("thread").asObject().get("fields");

        assertThat(fields.asArray().size(), equalTo(1));

        JsonObject field = fields.asArray().get(0).asObject();

        assertThat(field.get("label").asString(), equalTo("Marker"));
        assertThat(field.get("value").asString(), equalTo(marker.getName()));
    }

    private LayoutWrappingEncoder setupEncoder() {
        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        encoder.setContext(context);
        encoder.setPattern("[%thread] %-5level %logger{35} - %msg %n");
        encoder.start();
        return encoder;
    }
}

@@ -395,33 +395,8 @@
 ue;%0A
-import org.junit.Ignore;%0A
 impo
@@ -554,52 +554,8 @@
 To;%0A
-import static org.hamcrest.CoreMatchers.is;%0A
 impo
@@ -1814,60 +1814,8 @@
 t);%0A
-        System.out.println(%22message = %22 + message);%0A
     
@@ -1885,16 +1885,16 @@
 elds%22);%0A
+
 %0A       
@@ -2074,24 +2074,24 @@
 %22Marker%22));%0A
-
         asse
@@ -2159,24 +2159,562 @@
 )));%0A    %7D%0A%0A
+    @Test%0A    public void convert_log_level_to_status() throws IOException %7B%0A        LoggingEvent event = new LoggingEvent(%22%22, logger, Level.ERROR, %22%22, null, null);%0A        MessageBuilder builder = new MessageBuilder(%22token%22, %22author%22, encoder);%0A%0A        JsonObject message = builder.buildMessage(event);%0A        JsonObject status = message.get(%22thread%22).asObject().get(%22status%22).asObject();%0A%0A        assertThat(status.get(%22value%22).asString(), equalTo(%22error%22));%0A        assertThat(status.get(%22color%22).asString(), equalTo(%22red%22));%0A    %7D%0A%0A
     private 

Test for insertion of status colors in Flowdock message