/*
 * Copyright (c) 2006-2014 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.logger;

import java.util.Date;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import net.kencochrane.raven.Raven;
import net.kencochrane.raven.RavenFactory;
import net.kencochrane.raven.dsn.Dsn;
import net.kencochrane.raven.event.Event;
import net.kencochrane.raven.event.interfaces.ExceptionInterface;
import net.kencochrane.raven.event.interfaces.SentryInterface;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ErrorReporterTest {

    @Mock private RavenFactory ravenFactory;
    @Mock private Raven raven;
    @Captor private ArgumentCaptor<Event> eventCaptor;
    private ErrorReporter errorReporter;

    @Before
    public void setUp() throws Exception {
        RavenFactory.registerFactory(ravenFactory);
        when(ravenFactory.createRavenInstance(Matchers.<Dsn>anyObject())).thenReturn(raven);
        errorReporter = new ErrorReporter();
    }

    @Test
    public void testSendsMessage() {
        errorReporter.sendException("message 123", ErrorLevel.MEDIUM, new Date(), null, null);
        verify(raven).sendEvent(eventCaptor.capture());
        assertEquals("message 123", eventCaptor.getValue().getMessage());
    }

    @Test
    public void testSendsTimestamp() {
        final Date date = new Date(561859200000l);
        errorReporter.sendException("message 123", ErrorLevel.MEDIUM, date, null, null);
        verify(raven).sendEvent(eventCaptor.capture());
        assertEquals(date, eventCaptor.getValue().getTimestamp());
    }

    @Test
    public void testSendsLowLevelAsInfo() {
        errorReporter.sendException("message 123", ErrorLevel.LOW, new Date(), null, null);
        verify(raven).sendEvent(eventCaptor.capture());
        assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
    }

    @Test
    public void testSendsMediumLevelAsWarning() {
        errorReporter.sendException("message 123", ErrorLevel.MEDIUM, new Date(), null, null);
        verify(raven).sendEvent(eventCaptor.capture());
        assertEquals(Event.Level.WARNING, eventCaptor.getValue().getLevel());
    }

    @Test
    public void testSendsHighLevelAsError() {
        errorReporter.sendException("message 123", ErrorLevel.HIGH, new Date(), null, null);
        verify(raven).sendEvent(eventCaptor.capture());
        assertEquals(Event.Level.ERROR, eventCaptor.getValue().getLevel());
    }

    @Test
    public void testSendsFatalLevelAsFatal() {
        errorReporter.sendException("message 123", ErrorLevel.FATAL, new Date(), null, null);
        verify(raven).sendEvent(eventCaptor.capture());
        assertEquals(Event.Level.FATAL, eventCaptor.getValue().getLevel());
    }

    @Test
    public void testSendsUnknownLevelAsInfo() {
        errorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), null, null);
        verify(raven).sendEvent(eventCaptor.capture());
        assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
    }

    @Test
    public void testAddsDetailsAsExtra() {
        errorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), null,
                "details 456");
        verify(raven).sendEvent(eventCaptor.capture());
        assertEquals("details 456", eventCaptor.getValue().getExtra().get("details"));
    }

    @Test
    public void testAddsExceptionInterface() {
        final Exception exception = new IndexOutOfBoundsException("Message blah");
        errorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), exception, null);
        verify(raven).sendEvent(eventCaptor.capture());
        final SentryInterface iface = eventCaptor.getValue().getSentryInterfaces()
                .get(ExceptionInterface.EXCEPTION_INTERFACE);
        assertNotNull(iface);
        assertTrue(iface instanceof ExceptionInterface);
        assertEquals(exception.getMessage(),
                ((ExceptionInterface) iface).getExceptions().getFirst().getExceptionMessage());
    }

}
@@ -1198,16 +1198,46 @@
 Before;%0A
+import org.junit.BeforeClass;%0A
 import o
@@ -1871,16 +1871,56 @@
 rtTrue;%0A
+import static org.mockito.Mockito.mock;%0A
 import s
@@ -2063,38 +2063,32 @@
 Test %7B%0A%0A    
-@Mock 
 private 
 RavenFactory
@@ -2071,24 +2071,31 @@
     private 
+static 
 RavenFactory
@@ -2248,16 +2248,21 @@
  @Before
+Class
 %0A    pub
@@ -2261,24 +2261,31 @@
 %0A    public 
+static 
 void setUp()
@@ -2286,29 +2286,104 @@
 etUp
-() throws Exception %7B
+Class() %7B%0A        // Static calls are horrible.%0A        ravenFactory = mock(RavenFactory.class);
 %0A   
@@ -2431,16 +2431,61 @@
 ctory);%0A
+    %7D%0A%0A    @Before%0A    public void setUp() %7B%0A
         

Fix failures caused by static factory.