package com.ae.apps.common.managers;

import android.content.ContentResolver;
import android.content.res.Resources;
import android.database.Cursor;
import android.provider.ContactsContract;

import com.ae.apps.common.vo.ContactVo;

import org.junit.Test;
import org.mockito.Mockito;

import java.util.ArrayList;

import static org.mockito.Mockito.*;

import static org.junit.Assert.*;

/**
 * Created by midhunhk on 6/22/2017.
 */

public class ContactManagerTest {

    Cursor cursor = Mockito.mock(Cursor.class);
    Resources resources = Mockito.mock(Resources.class);
    ContentResolver contentResolver = Mockito.mock(ContentResolver.class);

    @Test
    public void testGetAllContacts(){
        ContactManager.Config config = new ContactManager.Config();
        config.contentResolver = contentResolver;
        config.resources = resources;

        mockMethods();

        ContactManager contactManager = new ContactManager(config);
        assertEquals(contactManager.contactManagerStatus, AbstractContactManager.STATUS.READY);
        assertEquals(contactManager.getAllContacts(), new ArrayList<ContactVo>());
    }

    @Test
    public void testGetAllContactsAsync(){
        ContactManager.Config config = new ContactManager.Config();
        config.contentResolver = contentResolver;
        config.resources = resources;
        config.readContactsAsync = true;
        config.consumer = new AbstractContactManager.ContactsDataConsumer() {
            @Override
            public void onContactsRead() {
                System.out.println("onContactsRead() callback");
            }
        };

        mockMethods();

        System.out.println("Creating ContactManager instance");
        ContactManager contactManager = new ContactManager(config);
        assertEquals(contactManager.contactManagerStatus, AbstractContactManager.STATUS.UNINITIALIZED);
        try {
            System.out.println("Force wait start for async method to complete");
            Thread.sleep(300);
            System.out.println("Force wait end for async method to complete");
        } catch (InterruptedException ex){

        }
        assertEquals(contactManager.contactManagerStatus, AbstractContactManager.STATUS.READY);
        assertEquals(contactManager.getAllContacts(), new ArrayList<ContactVo>());
    }

    @Test
    public void testContactManagerBuilder(){
        mockMethods();

        ContactManager.Builder builder = new ContactManager.Builder(contentResolver, resources);
        ContactManager contactManager = builder
                .addContactsWithPhoneNumbers(false)
                .readContactsAsync(false)
                .build();

        assertNotNull(contactManager);
    }

    private void mockMethods() {
        when(contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null))
                .thenReturn(cursor);
        when(cursor.getCount())
                .thenReturn(0);
    }
}

@@ -328,27 +328,34 @@
 org.
-mockito.Mockito.*;%0A
+junit.Assert.assertEquals;
 %0Aimp
@@ -386,58 +386,105 @@
 ert.
-*;%0A%0A/**%0A * Created by midhunhk on 6/22/2017.
+assertNotNull;%0Aimport static org.mockito.Mockito.when;%0A%0A/**%0A * Unit tests for ContactManager
 %0A */%0A
-%0A
 publ
@@ -518,16 +518,24 @@
  %7B%0A%0A    
+private 
 Cursor c
@@ -574,16 +574,24 @@
 s);%0A    
+private 
 Resource
@@ -635,24 +635,32 @@
 class);%0A    
+private 
 ContentResol
@@ -765,16 +765,17 @@
 ntacts()
+ 
 %7B%0A      
@@ -1254,16 +1254,17 @@
 sAsync()
+ 
 %7B%0A      
@@ -2194,18 +2194,67 @@
 tion ex)
+ 
 %7B%0A
+            System.err.println(ex.getMessage());
 %0A       
@@ -2495,16 +2495,17 @@
 uilder()
+ 
 %7B%0A      
@@ -2548,22 +2548,21 @@
 ager
-.Builder build
+ contactManag
 er =
@@ -2620,57 +2620,8 @@
 ces)
-;%0A        ContactManager contactManager = builder
 %0A   

update test case