package com.gimranov.zandy.app;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.test.AndroidTestCase;
import android.test.IsolatedContext;

import com.gimranov.zandy.app.ServerCredentials;
import com.gimranov.zandy.app.data.Database;
import com.gimranov.zandy.app.task.APIException;
import com.gimranov.zandy.app.task.APIRequest;
import com.gimranov.zandy.app.test.*;
import com.gimranov.zandy.app.test.BuildConfig;


public class ApiTest extends AndroidTestCase {

	private IsolatedContext mContext;
	private Database mDb;
	private ServerCredentials mCred;
	
	/**
	 * Access information for the Zandy test user on Zotero.org
	 */
	private static final String TEST_UID = BuildConfig.TEST_USER_ID;
	private static final String TEST_KEY = BuildConfig.TEST_USER_KEY_READONLY;
	private static final String TEST_COLLECTION = "U8GNSSF3";
	
	// unlikely to exist
	private static final String TEST_MISSING_ITEM = "ZZZZZZZZ";
	
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		mContext = new IsolatedContext(null, getContext());
		mDb = new Database(mContext);
		
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
		SharedPreferences.Editor editor = settings.edit();
		// For Zotero, the key and secret are identical, it seems
		editor.putString("user_key", TEST_KEY);
		editor.putString("user_secret", TEST_KEY);
		editor.putString("user_id", TEST_UID);
		editor.commit();
		
		mCred = new ServerCredentials(mContext);
	}
	
	public void testPreConditions() {
		// Make sure we do indeed have the key set up
		assertTrue(ServerCredentials.check(mContext));
	}
	
	public void testItemsRequest() throws APIException {
		APIRequest items = APIRequest.fetchItems(false, mCred);
		items.issue(mDb, mCred);
	}
	
	public void testCollectionsRequest() throws APIException {
		APIRequest collections = APIRequest.fetchCollections(mCred);
		collections.issue(mDb, mCred);
	}
	
	public void testItemsForCollection() throws APIException {
		APIRequest collection = APIRequest.fetchItems(TEST_COLLECTION, false, mCred);
		collection.issue(mDb, mCred);
	}
	
	// verify that we fail on this item, which should be missing
	public void testMissingItem() throws APIException {
		APIRequest missingItem = APIRequest.fetchItem(TEST_MISSING_ITEM, mCred);
		try {
			missingItem.issue(mDb, mCred);
			// We shouldn't get here
			fail();
		} catch (APIException e) {
			// We expect only one specific exception message
			if (!"Item does not exist".equals(e.getCause().getMessage()))
				throw e;
		}
	}
}

@@ -26,16 +26,48 @@
 y.app;%0A%0A
+import android.content.Context;%0A
 import a
@@ -164,116 +164,90 @@
 oid.
-test.AndroidTestCase;%0Aimport android.test.IsolatedContext;%0A%0Aimport com.gimranov.zandy.app.ServerCredentials;
+support.test.filters.SmallTest;%0Aimport android.support.test.runner.AndroidJUnit4;%0A
 %0Aimp
@@ -388,46 +388,8 @@
 st;%0A
-import com.gimranov.zandy.app.test.*;%0A
 impo
@@ -437,53 +437,321 @@
 g;%0A%0A
-%0Apublic class ApiTest extends AndroidTestCase
+import org.junit.Before;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0A%0Aimport static android.support.test.InstrumentationRegistry.getTargetContext;%0Aimport static junit.framework.Assert.assertTrue;%0Aimport static junit.framework.Assert.fail;%0A%0A@RunWith(AndroidJUnit4.class)%0A@SmallTest%0Apublic class ApiTest
  %7B%0A%0A
@@ -759,24 +759,16 @@
 private 
-Isolated
 Context 
@@ -1197,33 +1197,25 @@
 Z%22;%0A
-%09%0A%09%0A%09@Override%0A%09protected
+%0A%09@Before%0A%09public
  voi
@@ -1249,62 +1249,25 @@
 %7B%0A%09%09
-super.setUp();%0A%09%09mContext = new IsolatedContext(null, 
+mContext = getTar
 getC
@@ -1274,17 +1274,16 @@
 ontext()
-)
 ;%0A%09%09mDb 
@@ -1704,25 +1704,31 @@
 ontext);%0A%09%7D%0A
-%09
+%0A%09@Test
 %0A%09public voi
@@ -1847,25 +1847,31 @@
 ntext));%0A%09%7D%0A
-%09
+%0A%09@Test
 %0A%09public voi
@@ -1993,33 +1993,39 @@
 mDb, mCred);%0A%09%7D%0A
-%09
+%0A%09@Test
 %0A%09public void te
@@ -2168,17 +2168,23 @@
 ed);%0A%09%7D%0A
-%09
+%0A%09@Test
 %0A%09public

Move away from deprecated instrumentation test