package net.kencochrane.raven.appengine;

import mockit.*;
import net.kencochrane.raven.RavenFactory;
import net.kencochrane.raven.appengine.connection.AppEngineAsyncConnection;
import net.kencochrane.raven.connection.Connection;
import net.kencochrane.raven.dsn.Dsn;
import org.hamcrest.Matchers;
import org.testng.annotations.Test;

import java.util.Collections;
import java.util.ServiceLoader;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;

public class AppEngineRavenFactoryTest {
    @Tested
    private AppEngineRavenFactory appEngineRavenFactory;
    @Injectable
    private Connection mockConnection;
    @Injectable
    private Dsn mockDsn;

    @Test
    public void checkServiceLoaderProvidesFactory() {
        ServiceLoader<RavenFactory> ravenFactories = ServiceLoader.load(RavenFactory.class);

        assertThat(ravenFactories, Matchers.<RavenFactory>hasItem(instanceOf(AppEngineRavenFactory.class)));
    }

    @Test
    public void asyncConnectionCreatedByAppEngineRavenFactoryIsForAppEngine() {
        Connection connection = appEngineRavenFactory.createAsyncConnection(mockDsn, mockConnection);

        assertThat(connection, is(instanceOf(AppEngineAsyncConnection.class)));
    }

    @Test
    public void asyncConnectionWithoutQueueNameKeepsDefaultQueue(
            @Mocked final AppEngineAsyncConnection mockAppEngineAsyncConnection) {
        appEngineRavenFactory.createAsyncConnection(mockDsn, mockConnection);

        new Verifications(){{
            new AppEngineAsyncConnection(mockConnection);
            mockAppEngineAsyncConnection.setQueue(anyString);
            times = 0;
        }};
    }

    @Test
    public void asyncConnectionWithQueueNameSetsQueue(
            @Mocked final AppEngineAsyncConnection mockAppEngineAsyncConnection,
            @Injectable("queueName") final String mockQueueName) {
        new NonStrictExpectations(){{
            mockDsn.getOptions();
            result = Collections.singletonMap(AppEngineRavenFactory.QUEUE_NAME, mockQueueName);
        }};

        appEngineRavenFactory.createAsyncConnection(mockDsn, mockConnection);

        new Verifications(){{
            new AppEngineAsyncConnection(mockConnection);
            mockAppEngineAsyncConnection.setQueue(mockQueueName);
        }};
    }
}

@@ -1286,32 +1286,1150 @@
 lass)));%0A    %7D%0A%0A
+    @Test%0A    public void asyncConnectionWithoutConnectionIdGeneratesDefaultId() %7B%0A        final String dnsString = %22a1fe25d3-bc41-4040-8aa2-484e5aae87c5%22;%0A        new NonStrictExpectations() %7B%7B%0A            mockDsn.toString();%0A            result = dnsString;%0A        %7D%7D;%0A%0A        appEngineRavenFactory.createAsyncConnection(mockDsn, mockConnection);%0A%0A        new Verifications() %7B%7B%0A            String connectionId = AppEngineRavenFactory.class.getCanonicalName() + dnsString;%0A            new AppEngineAsyncConnection(connectionId, mockConnection);%0A        %7D%7D;%0A    %7D%0A%0A    @Test%0A    public void asyncConnectionWithConnectionIdUsesId(%0A            @Injectable(%22543afd41-379d-41cb-8c99-8ce73e83a0cc%22) final String connectionId) %7B%0A        new NonStrictExpectations() %7B%7B%0A            mockDsn.getOptions();%0A            result = Collections.singletonMap(AppEngineRavenFactory.CONNECTION_IDENTIFIER, connectionId);%0A        %7D%7D;%0A%0A        appEngineRavenFactory.createAsyncConnection(mockDsn, mockConnection);%0A%0A        new Verifications() %7B%7B%0A            new AppEngineAsyncConnection(connectionId, mockConnection);%0A        %7D%7D;%0A    %7D%0A%0A
     @Test%0A    pu
@@ -2484,24 +2484,24 @@
 faultQueue(%0A
-
             
@@ -2681,69 +2681,12 @@
 ns()
+ 
 %7B%7B%0A
-            new AppEngineAsyncConnection(mockConnection);%0A
     
@@ -3025,32 +3025,33 @@
 ctExpectations()
+ 
 %7B%7B%0A            m
@@ -3290,69 +3290,12 @@
 ns()
+ 
 %7B%7B%0A
-            new AppEngineAsyncConnection(mockConnection);%0A
     

Add tests to ensure AppEngineRavenFactory connectionId system works