package com.amplify.hiccup.client;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.robolectric.RobolectricTestRunner;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;

@RunWith(RobolectricTestRunner.class)
public class HiccupClientTest {

    private static final String METHOD = "method";
    private static final String BODY = "body";

    private HiccupClient hiccupClient;

    @Mock
    private Context context;
    @Mock
    private ContentResolver contentResolver;
    @Mock
    private Uri uri;

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

        given(context.getContentResolver()).willReturn(contentResolver);

        hiccupClient = new HiccupClient(context);
    }

    @Test
    public void shouldSendUriRequestViaContentResolver() {
        Cursor expectedCursor = mock(Cursor.class);
        when(contentResolver.query(uri, null, null, null, null)).thenReturn(expectedCursor);

        Cursor actualCursor = hiccupClient.get(uri);

        assertThat(actualCursor, is(expectedCursor));
    }

    @Test
    public void shouldInsertAsPostToContentProviderWithPostMethod() {
        hiccupClient.post(uri, null);

        ArgumentCaptor<ContentValues> captor = ArgumentCaptor.forClass(ContentValues.class);
        verify(contentResolver).insert(eq(uri), captor.capture());
        ContentValues contentValues = captor.getValue();
        assertThat(contentValues.getAsString(METHOD), is("POST"));
    }

    @Test
    public void shouldInsertAsPostToContentProviderWithJsonModel() {
        String expectedJson = "Lois, this is not my Batman glass.";

        hiccupClient.post(uri, expectedJson);

        ArgumentCaptor<ContentValues> captor = ArgumentCaptor.forClass(ContentValues.class);
        verify(contentResolver).insert(eq(uri), captor.capture());
        ContentValues contentValues = captor.getValue();
        assertThat(contentValues.getAsString(BODY), is(expectedJson));
    }

    @Test
    public void shouldReturnUriFromInsertAsPostToContentProvider() {
        Uri expectedUri = mock(Uri.class);
        when(contentResolver.insert(eq(uri), any(ContentValues.class))).thenReturn(expectedUri);

        Uri actualUri = hiccupClient.post(uri, null);

        assertThat(actualUri, is(expectedUri));
    }
}

@@ -1202,40 +1202,22 @@
 ould
-SendUriRequestViaContentResolver
+MakeGetRequest
 () %7B
@@ -1514,37 +1514,23 @@
 ould
-InsertAsPostToContentProvider
+MakePostRequest
 With
@@ -1912,48 +1912,30 @@
 ldIn
-sertAsPostToContentProviderWithJsonModel
+cludeBodyInPostRequest
 () %7B
@@ -1950,28 +1950,20 @@
  String 
-expectedJson
+body
  = %22Lois
@@ -2027,28 +2027,20 @@
 st(uri, 
-expectedJson
+body
 );%0A%0A    
@@ -2311,20 +2311,12 @@
  is(
-expectedJson
+body
 ));%0A
@@ -2371,37 +2371,19 @@
 From
-InsertAsPostToContentProvider
+PostRequest
 () %7B

Update test names in HiccupClientTest