package com.scopely.integration.leanplum;

import com.scopely.integration.leanplum.model.ActionParams;
import com.scopely.integration.leanplum.model.Track;
import org.junit.Test;
import rx.observers.TestSubscriber;

import static org.fest.assertions.api.Assertions.assertThat;

public class TrackTest extends ProductionApiClientTest {
    @Test
    public void testTrack_FailsWithEmptyPayload() throws Exception {
        TestSubscriber<LeanplumActionResponse> testSubscriber = track(new Track(null, null, null, null, null, null, true));
        assertThat(testSubscriber.getOnErrorEvents()).hasSize(1);
        assertThat(testSubscriber.getOnErrorEvents().get(0)).isInstanceOf(LeanplumException.class);
        //noinspection ThrowableResultOfMethodCallIgnored
        assertThat(((LeanplumException) testSubscriber.getOnErrorEvents().get(0)).getResponse().error).isEqualsToByComparingFields(LeanplumError.createLeanplumError("'event' must be provided"));
    }

    @Test
    public void testTrack_SucceedsWithEventName() throws Exception {
        TestSubscriber<LeanplumActionResponse> testSubscriber = track(new Track("level_up", null, null, null, null, null, true));
        testSubscriber.assertNoErrors();
    }

    /**
     * This should not be accepted
     */
    @Test
    public void testTrack_SucceedsWithOnlyMessageId() throws Exception {
        TestSubscriber<LeanplumActionResponse> testSubscriber = track(new Track(null, null, null, null, null, 12L, true));
        testSubscriber.assertNoErrors();
    }

    @Test
    public void testTrack_withParams() throws Exception {
        TestSubscriber<LeanplumActionResponse> testSubscriber = track(new Track(null, null, null, null, ActionParams.of("key", 12), 12L, true));
        testSubscriber.assertNoErrors();
    }


    @Test
    public void testTrack_withName_andParams() throws Exception {
        TestSubscriber<LeanplumActionResponse> testSubscriber = track(new Track("my_event", null, null, null, ActionParams.of("key", 12), null, true));
        testSubscriber.assertNoErrors();
    }

    private TestSubscriber<LeanplumActionResponse> track(Track track) {
        return awaitAndTransform(leanplumApi.track(track));
    }

}

@@ -166,20 +166,76 @@
 g.junit.
-Test
+Ignore;%0Aimport org.junit.Test;%0Aimport retrofit.RetrofitError
 ;%0Aimport
@@ -327,16 +327,106 @@
 tThat;%0A%0A
+@Ignore(%22These are currently returning reliable 500 errors; ticket opened with Leanplum%22)%0A
 public c
@@ -1368,54 +1368,63 @@
     
-/**%0A     * This should not be accepted%0A     */
+@SuppressWarnings(%22ThrowableResultOfMethodCallIgnored%22)
 %0A   
@@ -1638,38 +1638,134 @@
     
-testSubscriber.assertNoErrors(
+Throwable throwable = testSubscriber.getOnErrorEvents().get(0);%0A        assertThat(throwable).isInstanceOf(RetrofitError.class
 );%0A 

Disable track tests until Leanplum fixes persistent 500