/*
 * Copyright (c) 2006-2015 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 com.dmdirc.util.ClientInfo;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Optional;

import org.junit.Before;
import org.junit.BeforeClass;
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.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class SentryErrorReporterTest {

    private static RavenFactory ravenFactory;
    @Mock private Raven raven;
    @Mock private ClientInfo clientInfo;
    @Captor private ArgumentCaptor<Event> eventCaptor;
    private Optional<Throwable> throwable;
    private SentryErrorReporter sentryErrorReporter;

    @BeforeClass
    public static void setUpClass() {
        // Static calls are horrible.
        ravenFactory = mock(RavenFactory.class);
        RavenFactory.registerFactory(ravenFactory);
    }

    @Before
    public void setUp() {
        when(ravenFactory.createRavenInstance(Matchers.<Dsn>anyObject())).thenReturn(raven);
        throwable = Optional.of(new IllegalArgumentException());
        sentryErrorReporter = new SentryErrorReporter(clientInfo);
    }

    @Test
    public void testSendsMessage() {
        sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, LocalDateTime.now(),
                throwable);
        verify(raven).sendEvent(eventCaptor.capture());
        assertEquals("message 123", eventCaptor.getValue().getMessage());
    }

    @Test
    public void testSendsTimestamp() {
        final LocalDateTime date = LocalDateTime.ofEpochSecond(56185920L, 0, ZoneOffset.UTC);
        sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, date, throwable);
        verify(raven).sendEvent(eventCaptor.capture());
        assertEquals(56185920000L, eventCaptor.getValue().getTimestamp().getTime());
    }

    @Test
    public void testSendsLowLevelAsInfo() {
        sentryErrorReporter.sendException("message 123", ErrorLevel.LOW, LocalDateTime.now(),
                throwable);
        verify(raven).sendEvent(eventCaptor.capture());
        assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
    }

    @Test
    public void testSendsMediumLevelAsWarning() {
        sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, LocalDateTime.now(),
                throwable);
        verify(raven).sendEvent(eventCaptor.capture());
        assertEquals(Event.Level.WARNING, eventCaptor.getValue().getLevel());
    }

    @Test
    public void testSendsHighLevelAsError() {
        sentryErrorReporter.sendException("message 123", ErrorLevel.HIGH, LocalDateTime.now(),
                throwable);
        verify(raven).sendEvent(eventCaptor.capture());
        assertEquals(Event.Level.ERROR, eventCaptor.getValue().getLevel());
    }

    @Test
    public void testSendsFatalLevelAsFatal() {
        sentryErrorReporter.sendException("message 123", ErrorLevel.FATAL, LocalDateTime.now(),
                throwable);
        verify(raven).sendEvent(eventCaptor.capture());
        assertEquals(Event.Level.FATAL, eventCaptor.getValue().getLevel());
    }

    @Test
    public void testSendsUnknownLevelAsInfo() {
        sentryErrorReporter.sendException("message 123", ErrorLevel.UNKNOWN, LocalDateTime.now(),
                throwable);
        verify(raven).sendEvent(eventCaptor.capture());
        assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
    }

    @Test
    public void testAddsExceptionInterface() {
        final Exception exception = new IndexOutOfBoundsException("Message blah");
        sentryErrorReporter.sendException("message 123", ErrorLevel.UNKNOWN, LocalDateTime.now(),
                Optional.of(exception));
        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());
    }

}
@@ -1552,39 +1552,37 @@
 er;%0A%0Aimport 
-net.kencochrane
+com.getsentry
 .raven.Raven
@@ -1586,39 +1586,37 @@
 ven;%0Aimport 
-net.kencochrane
+com.getsentry
 .raven.Raven
@@ -1627,39 +1627,37 @@
 ory;%0Aimport 
-net.kencochrane
+com.getsentry
 .raven.dsn.D
@@ -1663,39 +1663,37 @@
 Dsn;%0Aimport 
-net.kencochrane
+com.getsentry
 .raven.event
@@ -1703,39 +1703,37 @@
 ent;%0Aimport 
-net.kencochrane
+com.getsentry
 .raven.event
@@ -1775,23 +1775,21 @@
 ort 
-net.kencochrane
+com.getsentry
 .rav
@@ -5909,8 +5909,9 @@
     %7D%0A%0A%7D
+%0A

Fix Sentry tests.