package com.jakewharton.trakt.services;

import com.jakewharton.apibuilder.ApiException;
import com.jakewharton.trakt.BaseTestCase;
import com.jakewharton.trakt.ServiceManager;
import com.jakewharton.trakt.entities.Response;

public class AccountServiceTest extends BaseTestCase {
	/*
	//XXX: This requires a developer API key.
	public void test_create() {
		try {
			getManager().accountService().create("JakeWharton", "zomg secret password", "jakewharton@gmail.com").fire();
			fail("Account creation of duplicate user succeeded.");
		} catch (ApiException e) {
			assertEquals("Exception text does not match.", "{\"status\":\"failure\",\"error\":\"JakeWharton is already a registered username\"}", e.getMessage().trim());
			
			//TODO: change this to a custom error that includes a response we
			//can actually test against to differentiate between a problem with
			//the API or simply a failed creation.
		}
	}
	*/
	
	public void test_testSuccess() {
		Response response = getManager().accountService().test().fire();
		assertNotNull("Result was null.", response);
		assertEquals("Authentication failed.", "success", response.getStatus());
		assertEquals("Authentication failed.", "all good!", response.getMessage());
	}
	
	public void test_testFailure() {
		//We have to create our own uninitialized service for this
		AccountService service = ServiceManager.createAccountService();
		service.setApiKey(BaseTestCase.API_KEY);
		service.setAuthentication("JakeWharton", "this is not my password hash!");
		
		try {
			service.test().fire();
			fail("Authentication test succeeded with invalid credentials.");
		} catch (ApiException e) {
			assertEquals("Response error does not match.", "{\"status\":\"failure\",\"error\":\"failed authentication\"}", e.getMessage().trim());
		}
	}
}

@@ -38,56 +38,8 @@
 s;%0A%0A
-import com.jakewharton.apibuilder.ApiException;%0A
 impo
@@ -122,16 +122,61 @@
 anager;%0A
+import com.jakewharton.trakt.TraktException;%0A
 import c
@@ -715,19 +715,16 @@
 rim());%0A
-%09%09%09
 %0A%09%09%09//TO
@@ -909,17 +909,16 @@
 %0A%09%7D%0A%09*/%0A
-%09
 %0A%09public
@@ -1214,17 +1214,16 @@
 ());%0A%09%7D%0A
-%09
 %0A%09public
@@ -1496,18 +1496,16 @@
 ash!%22);%0A
-%09%09
 %0A%09%09try %7B
@@ -1602,35 +1602,37 @@
 .%22);%0A%09%09%7D catch (
-Api
+Trakt
 Exception e) %7B%0A%09
@@ -1622,32 +1622,89 @@
 tException e) %7B%0A
+%09%09%09assertNotNull(%22Response was null.%22, e.getResponse());%0A
 %09%09%09assertEquals(
@@ -1737,38 +1737,24 @@
 atch.%22, 
-%22%7B%5C%22status%5C%22:%5C
 %22failure
 %5C%22,%5C%22err
@@ -1745,30 +1745,99 @@
 %22failure
-%5C
 %22,
-%5C%22error%5C%22:%5C
+ e.getResponse().getStatus());%0A%09%09%09assertEquals(%22Response error does not match.%22, 
 %22failed 
@@ -1850,27 +1850,24 @@
 tication
-%5C%22%7D
 %22, e.get
 Message(
@@ -1854,38 +1854,43 @@
 tion%22, e.get
-Message().trim
+Response().getError
 ());%0A%09%09%7D%0A%09%7D%0A

Update test case to use TraktException.