package com.clescot.webappender.formatter;

import com.clescot.webappender.Row;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.when;

@RunWith(Enclosed.class)
public class ChromeLoggerFormatterTest {



    public static class TestFormat extends AbstractFormatterTest {

        public static final String NO_EVENTS_IN_BASE64 = "eyJjb2x1bW5zIjpbImxvZyIsImJhY2t0cmFjZSIsInR5cGUiXSwicm93cyI6W10sInZlcnNpb24iOiIxLjAifQ==";

        @Test
        public void testFormat_with_no_events() throws Exception {
            //given
            ArrayList<Row> iLoggingEvents = Lists.newArrayList();
            ChromeLoggerFormatter chromeLoggerFormatter = new ChromeLoggerFormatter();
            //when
            String formattedLogs = chromeLoggerFormatter.format(iLoggingEvents);
            //then
            assertThat(formattedLogs).isEqualTo(NO_EVENTS_IN_BASE64);
        }


    }

    public static class TestGetJSON extends AbstractFormatterTest {
        @InjectMocks
        private ChromeLoggerFormatter chromeLoggerFormatterMocked;

        @Mock
        private ObjectMapper objectMapper;

        @Before
        public void setUp() throws Exception {
            MockitoAnnotations.initMocks(this);
        }

        @Test
        public void test_get_json_nominal_case() throws Exception {
            ChromeLoggerFormatter chromeLoggerFormatter = new ChromeLoggerFormatter();
            String json = chromeLoggerFormatter.getJSON(getILoggingEvents());
            assertThat(json).isEqualTo("{\"version\": \"1.0\",\"columns\": [\"log\", \"backtrace\", \"type\"], \"rows\":[[[{\"___class_name\": \"com.clescot.webappender.formatter.AbstractFormatterTest\"}],\"/path/to/file.py : 25\",\"\"]]}");
        }

        @Test(expected = JsonGenerationException.class)
        public void test_get_json_when_object_mapper_throw_a_json_processing_exception() throws Exception {

            //given
            when(objectMapper.writeValueAsString(anyObject())).thenThrow(new JsonGenerationException(""));


            //when
            chromeLoggerFormatterMocked.getJSON(getILoggingEvents());

            //then

        }
    }

    public static class TestIsActive{
        @Test
        public void test_chromium_browser() throws Exception {
            //given
            ChromeLoggerFormatter chromeLoggerFormatter = new ChromeLoggerFormatter();
            String chromiumUserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36";
            Map<String,List<String>> headers = Maps.newHashMap();
            headers.put("user-agent", Arrays.asList(chromiumUserAgent));
            //when
            boolean active = chromeLoggerFormatter.isActive(headers);
            //then
            assertThat(active).isTrue();


        }

        @Test
        public void test_firefox_browser() throws Exception {
            ChromeLoggerFormatter chromeLoggerFormatter = new ChromeLoggerFormatter();
            String firefoxUserAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0";
            Map<String,List<String>> headers = Maps.newHashMap();
            headers.put("user-agent", Arrays.asList(firefoxUserAgent));
            //when
            boolean active = chromeLoggerFormatter.isActive(headers);
            //then
            assertThat(active).isFalse();


        }

    }


}

@@ -77,67 +77,8 @@
 ow;%0A
-import com.fasterxml.jackson.core.JsonGenerationException;%0A
 impo
@@ -589,94 +589,8 @@
 hat;
-%0Aimport static org.mockito.Matchers.anyObject;%0Aimport static org.mockito.Mockito.when;
 %0A%0A@R
@@ -789,93 +789,97 @@
 %22eyJ
-jb2x1bW5zIjpbImxvZyIsImJhY2t0cmFjZSIsInR5cGUiXSwicm93cyI6W10sInZlcnNpb24iOiIxLjAifQ==
+2ZXJzaW9uIjogIjEuMCIsImNvbHVtbnMiOiBbImxvZyIsICJiYWNrdHJhY2UiLCAidHlwZSJdLCJyb3dzIjogW119
 %22;%0A%0A
@@ -2034,17 +2034,16 @@
 type%5C%22%5D,
- 
 %5C%22rows%5C%22
@@ -2047,446 +2047,95 @@
 s%5C%22:
+ 
 %5B%5B%5B
-%7B
 %5C%22
-___class_name%5C%22: %5C%22com.clescot.webappender.formatter.AbstractFormatterTest
+dummy message
 %5C%22
-%7D
 %5D,%5C%22
-/path/to/file.py : 25%5C%22,%5C%22%5C%22%5D%5D%7D%22);%0A        %7D%0A%0A        @Test(expected = JsonGenerationException.class)%0A        public void test_get_json_when_object_mapper_throw_a_json_processing_exception() throws Exception %7B%0A%0A            //given%0A            when(objectMapper.writeValueAsString(anyObject())).thenThrow(new JsonGenerationException(%22
+:,%5C%22ERROR%5C%22%5D,%5B%5B%5C%22dummy message%5C%22%5D,%5C%22:,%5C%22ERROR%5C%22%5D%5D%7D
 %22)
-)
 ;%0A
-%0A%0A   
         
  //w
@@ -2134,116 +2134,11 @@
     
- //when%0A            chromeLoggerFormatterMocked.getJSON(getILoggingEvents());%0A%0A            //then%0A%0A        %7D
+%7D%0A%0A
 %0A   

fix tests