package com.ibm.mil.cafejava;

import com.worklight.wlclient.api.WLResponse;

import org.codehaus.jackson.type.TypeReference;
import org.json.JSONObject;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import rx.Observable;
import rx.functions.Func1;
import rx.observers.TestSubscriber;

import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class JsonConverterTest {
    private Observable<WLResponse> observable;
    private TestSubscriber<Person> subscriber;

    @Test public void testSimpleObject() {
        Person testPerson = new Person("John", 25, true);

        createMocks(testPerson);

        observable.lift(new JsonConverter<>(Person.class)).subscribe(subscriber);

        subscriber.assertNoErrors();
        subscriber.assertTerminalEvent();

        List<Person> persons = subscriber.getOnNextEvents();
        assertEquals(1, persons.size());
        assertEquals(testPerson, persons.get(0));
    }

    @Test public void testSimpleArray() {
        Person firstPerson = new Person("John", 25, true);
        Person secondPerson = new Person("Megan", 21, false);
        List<Person> persons = Arrays.asList(firstPerson, secondPerson);

        createMocks(persons);

        observable.lift(new JsonConverter<>(new TypeReference<List<Person>>() {}))
                .flatMap(new Func1<List<Person>, Observable<Person>>() {
                    @Override
                    public Observable<Person> call(List<Person> persons) {
                        return Observable.from(persons);
                    }
                }).subscribe(subscriber);

        subscriber.assertValues(firstPerson, secondPerson);
    }

    private void createMocks(Object object) {
        JSONObject jsonObject = mock(JSONObject.class);
        when(jsonObject.toString()).thenReturn(object.toString());

        WLResponse wlResponse = mock(WLResponse.class);
        when(wlResponse.getResponseJSON()).thenReturn(jsonObject);

        observable = Observable.just(wlResponse);
        subscriber = new TestSubscriber<>();
    }
}

@@ -624,21 +624,17 @@
  Person 
-testP
+p
 erson = 
@@ -684,21 +684,17 @@
 teMocks(
-testP
+p
 erson);%0A
@@ -984,13 +984,9 @@
 als(
-testP
+p
 erso
@@ -1726,24 +1726,1449 @@
 on);%0A    %7D%0A%0A
+    @SuppressWarnings(%22ThrowableResultOfMethodCallIgnored%22)%0A    @Test public void testBadObject() %7B%0A        createMocks(%22Not a Person%22);%0A%0A        observable.lift(new JsonConverter%3C%3E(Person.class)).subscribe(subscriber);%0A%0A        subscriber.assertNoValues();%0A        subscriber.assertTerminalEvent();%0A%0A        List%3CThrowable%3E errors = subscriber.getOnErrorEvents();%0A        assertEquals(1, errors.size());%0A%0A        Throwable t = errors.get(0);%0A        assertEquals(%22Could not convert JSON payload: Not a Person%22, t.getMessage());%0A    %7D%0A%0A    @SuppressWarnings(%22ThrowableResultOfMethodCallIgnored%22)%0A    @Test public void testBadArray() %7B%0A        Person person = new Person(%22John%22, 25, true);%0A        createMocks(person);%0A%0A        observable.lift(new JsonConverter%3C%3E(new TypeReference%3CList%3CPerson%3E%3E() %7B%7D))%0A                .flatMap(new Func1%3CList%3CPerson%3E, Observable%3CPerson%3E%3E() %7B%0A                    @Override%0A                    public Observable%3CPerson%3E call(List%3CPerson%3E persons) %7B%0A                        return Observable.from(persons);%0A                    %7D%0A                %7D).subscribe(subscriber);%0A%0A        subscriber.assertNoValues();%0A        subscriber.assertTerminalEvent();%0A%0A        List%3CThrowable%3E errors = subscriber.getOnErrorEvents();%0A        assertEquals(1, errors.size());%0A%0A        Throwable t = errors.get(0);%0A        assertEquals(%22Could not convert JSON payload: %22 + person.toString(), t.getMessage());%0A    %7D%0A%0A
     private 

Write error tests for JsonConverter