package com.amplify.hiccup.service;

import android.database.Cursor;
import android.provider.BaseColumns;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.robolectric.RobolectricTestRunner;

import java.util.Arrays;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

@RunWith(RobolectricTestRunner.class)
public class HttpCursorFactoryTest {

    private HttpCursorFactory factory;

    @Mock
    private JsonConverter jsonConverter;

    @Before
    public void setUp() {
        initMocks(this);

        factory = new HttpCursorFactory(jsonConverter);
    }

    @Test
    public void shouldReturnEmptyCursorForNullModel() {
        Cursor cursor = factory.createCursor(null);

        assertThat(cursor.getCount(), is(0));
    }

    @Test
    public void shouldConvertSingleModelIntoCursorWithSingleRow() {
        Cursor cursor = factory.createCursor(new Object());

        assertThat(cursor.getCount(), is(1));
    }

    @Test
    public void shouldConvertMultipleModelsIntoCursorWithMultipleRows() {
        Iterable<?> someList = Arrays.asList(new Object(), new Object(), new Object());

        Cursor cursor = factory.createCursor(someList);

        assertThat(cursor.getCount(), is(3));
    }

    @Test
    public void shouldConvertSingleModelIntoCursorWithBaseColumnId() {
        Cursor cursor = factory.createCursor(new Object());

        int idColumn = cursor.getColumnIndex(BaseColumns._ID);
        cursor.moveToFirst();
        assertThat(cursor.getLong(idColumn), is(1L));
    }

    @Test
    public void shouldConvertSingleModelIntoCursorWithJsonBody() {
        String expectedJsonBody = "{\"aKey\" : \"some value\"}";
        Object dbModel = new Object();
        when(jsonConverter.toJson(dbModel)).thenReturn(expectedJsonBody);

        Cursor cursor = factory.createCursor(dbModel);

        int bodyColumn = cursor.getColumnIndex("body");
        cursor.moveToFirst();
        String actualBody = cursor.getString(bodyColumn);
        assertThat(actualBody, is(expectedJsonBody));
    }

    @Test
    public void shouldConvertMultipleModelsIntoCursorWithJsonBody() {
        String expectedJsonBody = "{\"someKey\" : \"a value\"}";
        Object dbModel = new Object();
        Iterable<?> modelItems = Arrays.asList(dbModel);
        when(jsonConverter.toJson(dbModel)).thenReturn(expectedJsonBody);

        Cursor cursor = factory.createCursor(modelItems);

        int bodyColumn = cursor.getColumnIndex("body");
        cursor.moveToFirst();
        String actualBody = cursor.getString(bodyColumn);
        assertThat(actualBody, is(expectedJsonBody));
    }

}

@@ -370,16 +370,62 @@
 .Is.is;%0A
+import static org.mockito.Matchers.anyObject;%0A
 import s
@@ -2327,32 +2327,40 @@
 lsIntoCursorWith
+Multiple
 JsonBody() %7B%0A   
@@ -2379,28 +2379,27 @@
 expectedJson
-Body
+One
  = %22%7B%5C%22someK
@@ -2432,94 +2432,149 @@
     
-Object dbModel = new Object();%0A        Iterable%3C?%3E modelItems = Arrays.asList(dbModel)
+String expectedJsonTwo = %22%7B%5C%22anotherKey%5C%22 : %5C%22another value!%5C%22%7D%22;%0A        String expectedJsonThree = %22%7B%5C%22toBeSure%5C%22 : %5C%22yet another value%5C%22%7D%22
 ;%0A  
@@ -2597,39 +2597,43 @@
 onverter.toJson(
-dbModel
+anyObject()
 )).thenReturn(ex
@@ -2634,36 +2634,71 @@
 urn(expectedJson
-Body
+One, expectedJsonTwo, expectedJsonThree
 );%0A%0A        Curs
@@ -2734,18 +2734,63 @@
 sor(
-modelItems
+Arrays.asList(new Object(), new Object(), new Object())
 );%0A%0A
@@ -2887,87 +2887,247 @@
     
-String actualBody = cursor.getString(bodyColumn);%0A        assertThat(actualBody
+assertThat(cursor.getString(bodyColumn), is(expectedJsonOne));%0A        cursor.moveToNext();%0A        assertThat(cursor.getString(bodyColumn), is(expectedJsonTwo));%0A        cursor.moveToNext();%0A        assertThat(cursor.getString(bodyColumn)
 , is
@@ -3131,33 +3131,34 @@
  is(expectedJson
-Body
+Three
 ));%0A    %7D%0A%0A%7D%0A

Fix test for multiple json body in HttpCursor