package com.simplenote.android;

import org.json.JSONArray;
import org.json.JSONObject;

import com.simplenote.android.NotesDbAdapter;
import com.simplenote.android.APIBase.Response;

import junit.framework.Assert;
import android.test.AndroidTestCase;
import android.util.Log;

public class APITest extends AndroidTestCase {
	String logInToken;
	private NotesDbAdapter mDbHelper;
	public static final String emailAddress = "simplenote@solidstategroup.com";
	public static final String password = "simplenote1234";

	public void testLogin() throws Throwable {
		String authBody = APIBase.encode( "email=" + emailAddress + "&password=wegfewrg", true );
		Response authResponse = APIBase.HTTPPost( Constants.API_LOGIN_URL, authBody );
		Assert.assertTrue(authResponse.statusCode == 401);
		
		authBody = APIBase.encode( "email=" + emailAddress + "&password=" + password, true );
		authResponse = APIBase.HTTPPost( Constants.API_LOGIN_URL, authBody );
		Assert.assertTrue(authResponse.statusCode == 200);
		logInToken = authResponse.resp.replaceAll("(\\r|\\n)", "");
		
		authResponse = APIBase.HTTPGet(Constants.API_NOTES_URL + "?auth=" + logInToken + "&email=" + emailAddress);
		Log.i(Constants.TAG, "Log In Token: " + logInToken);
		Log.i(Constants.TAG, "Index response: " + authResponse.resp);
		
		JSONArray jsonNotes = new JSONArray(authResponse.resp);
		
		for (int i = 0; i < jsonNotes.length(); ++i) {
		    JSONObject jsonNote = jsonNotes.getJSONObject(i);
		    String key = jsonNote.getString("key");
		    authResponse = APIBase.HTTPGet(Constants.API_NOTE_URL + "?key=" + key + "&auth=" + logInToken + "&email=" + emailAddress);
			
			NotesDbAdapter mDbHelper = new NotesDbAdapter(getContext());
	        mDbHelper.open();
	        mDbHelper.deleteAllNotes();
	        mDbHelper.createNote(jsonNote.getString("key"), authResponse.resp, authResponse.resp, jsonNote.getString("modify"));
		}	
    }
}
@@ -322,63 +322,8 @@
 e %7B%0A
-%09String logInToken;%0A%09private NotesDbAdapter mDbHelper;%0A
 %09pub
@@ -456,223 +456,228 @@
 4%22;%0A
-%0A%09public void testLogin() throws Throwable %7B%0A%09%09String authBody = APIBase.encode( %22email=%22 + emailAddress + %22&password=wegfewrg%22, true );%0A%09%09Response authResponse = APIBase.HTTPPost( Constants.API_LOGIN_URL, authBody 
+%09public static final String LOGGING_TAG = Constants.TAG + %22APITest%22;%0A%0A%09private String logInToken;%0A%0A%09public void testLogin() throws Throwable %7B%0A%09%09Response authResponse = APIHelper.getLoginResponse(emailAddress, %22wegfewrg%22
 );%0A%09
@@ -732,51 +732,53 @@
 1);%0A
-%09%09
 %0A%09%09auth
-Body = APIBase.encode( %22email=%22 + 
+Response = APIHelper.getLoginResponse(
 emai
@@ -789,21 +789,18 @@
 ress
+,
  
-+ %22&
 password
 =%22 +
@@ -799,100 +799,8 @@
 word
-=%22 + password, true );%0A%09%09authResponse = APIBase.HTTPPost( Constants.API_LOGIN_URL, authBody 
 );%0A%09
@@ -913,18 +913,16 @@
 %22, %22%22);%0A
-%09%09
 %0A%09%09authR
@@ -1028,34 +1028,32 @@
 s);%0A%09%09Log.i(
-Constants.
+LOGGING_
 TAG, %22Log In
@@ -1085,26 +1085,24 @@
 %09%09Log.i(
-Constants.
+LOGGING_
 TAG, %22In
@@ -1203,16 +1203,99 @@
 sp);%0A%09%09%0A
+%09%09NotesDbAdapter mDbHelper = new NotesDbAdapter(getContext());%0A%09%09mDbHelper.open();%0A
 %09%09for (i
@@ -1337,20 +1337,17 @@
 +i) %7B%0A%09%09
-    
+%09
 JSONObje
@@ -1390,20 +1390,17 @@
 t(i);%0A%09%09
-    
+%09
 String k
@@ -1433,20 +1433,17 @@
 ey%22);%0A%09%09
-    
+%09
 authResp
@@ -1561,112 +1561,12 @@
 s);%0A
-%09%09%09
 %0A%09%09%09
-NotesDbAdapter mDbHelper = new NotesDbAdapter(getContext());%0A%09        mDbHelper.open();%0A%09        
 mDbH
@@ -1594,16 +1594,10 @@
 );%0A%09
-        
+%09%09
 mDbH
@@ -1716,13 +1716,30 @@
 %0A%09%09%7D
-%09%0A    
+%0A%09%09mDbHelper.close();%0A%09
 %7D%0A%7D

Use login method on API helper