package com.simplenote.android.net;

import junit.framework.TestCase;

import com.simplenote.android.Constants;
import com.simplenote.android.model.Note;
import com.simplenote.android.net.Api.Response;

/**
 * Exercise the SimpleNoteApi class
 * @author bryanjswift
 */
public class SimpleNoteApiTest extends TestCase {
	public static final String emailAddress = "simplenote@solidstategroup.com";
	public static final String password = "simplenote1234";
	public static final String LOGGING_TAG = Constants.TAG + "SimpleNoteApiTest";

	String noteBody;

	private abstract class FailingCallback extends HttpCallback {
		@Override
		public void on200(Response response) {
			fail("Should not have returned a 200 response code");
		}
		@Override
		public void on400(Response response) {
			fail("Should not have returned a 400 response code");
		}
		@Override
		public void on401(Response response) {
			fail("Should not have returned a 401 response code");
		}
		@Override
		public void on403(Response response) {
			fail("Should not have returned a 403 response code");
		}
		@Override
		public void on404(Response response) {
			fail("Should not have returned a 404 response code");
		}
		@Override
		public void on500(Response response) {
			fail("Should not have returned a 500 response code");
		}
		@Override
		public void onException(String url, String data, Throwable t) {
			fail("Should not have thrown an exception: " + t.getMessage());
		}
	}
	/**
	 * Test we are able to log in successfully
	 */
	public void testSuccessfulLogin() {
		SimpleNoteApi.login(emailAddress, password, new FailingCallback() {
			@Override
			public void on200(Response response) {
				assertTrue(response.body.length() > 0);
			}
		});
	}
	/**
	 * Test failed login gets a 401 response
	 */
	public void testFailingLogin() {
		SimpleNoteApi.login(emailAddress, "password", new FailingCallback() {
			@Override
			public void on401(Response response) {
				// if we are here we should pass
			}
		});
	}
	/**
	 * Test able to retrieve notes after successful login
	 */
	public void testIndexAndNoteRetrieval() {
		String validToken = SimpleNoteApi.login(emailAddress, password, new FailingCallback() {
			@Override
			public void on200(Response response) {
				assertTrue(response.body.length() > 0);
			}
		});
		Note[] notes = SimpleNoteApi.index(validToken, emailAddress, new FailingCallback() {
			@Override
			public void on200(Response response) {
				assertTrue(response.body.length() > 0);
			}
		});
		assertTrue(notes.length > 0);
		Note note = SimpleNoteApi.retrieve(notes[0], validToken, emailAddress, new FailingCallback() {
			@Override
			public void on200(Response response) {
				// If we are here we passed
				noteBody = response.body;
			}
		});
		assertEquals(note.getTitleAndBody(), noteBody);
	}
}

@@ -2798,10 +2798,1062 @@
 dy);%0A%09%7D%0A
+%09/**%0A%09 * Test we are able to update notes%0A%09 */%0A%09public void testNoteUpdate() %7B%0A%09%09String validToken = SimpleNoteApi.login(emailAddress, password, new FailingCallback() %7B%0A%09%09%09@Override%0A%09%09%09public void on200(Response response) %7B%0A%09%09%09%09assertTrue(response.body.length() %3E 0);%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09Note%5B%5D notes = SimpleNoteApi.index(validToken, emailAddress, new FailingCallback() %7B%0A%09%09%09@Override%0A%09%09%09public void on200(Response response) %7B%0A%09%09%09%09assertTrue(response.body.length() %3E 0);%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09assertTrue(notes.length %3E 0);%0A%09%09final Note serverNote = SimpleNoteApi.retrieve(notes%5B0%5D, validToken, emailAddress, new FailingCallback() %7B%0A%09%09%09@Override%0A%09%09%09public void on200(Response response) %7B%0A%09%09%09%09// If we are here we passed%0A%09%09%09%09noteBody = response.body;%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09assertEquals(serverNote.getTitleAndBody(), noteBody);%0A%09%09final Note newNote = serverNote.setBody(serverNote.getBody() + %22%5Cn Test appended%22);%0A%09%09SimpleNoteApi.update(newNote, validToken, emailAddress, new FailingCallback() %7B%0A%09%09%09@Override%0A%09%09%09public void on200(Response response) %7B%0A%09%09%09%09// passed%0A%09%09%09%7D%0A%09%09%7D);%0A%09%7D%0A
 %7D%0A

Add (failing) test to try and update a note on the server