package com.mollie.api;

import static org.mockito.Mockito.*;

import java.math.BigDecimal;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class ApiTest {
	private static String API_KEY = "test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM";

	protected MollieClient api;

	@Rule
	public ExpectedException thrown = ExpectedException.none();

	@Before
	public void setUp() throws Exception {
		api = spy(new MollieClient());
		api.initResources(api); // make sure the resources also get the spied api

		api.setApiKey(API_KEY);
	}

	@Test
	public void testInvalidApiKeyFails() throws MollieException
	{
		api = new MollieClient();
		thrown.expect(MollieException.class);
		thrown.expectMessage("Invalid api key: \"invalid\". An API key must start with \"test_\" or \"live_\".");
		api.setApiKey("invalid");
	}

	@Test
	public void testCreatePaymentFailsEmptyHttpBody() throws MollieException
	{
		String msgBody = "{\"amount\":100,\"redirectUrl\":\"http://www.chucknorris.rhk/return.php\",\"description\":\"Order #1337 24 Roundhousekicks\"}";

		thrown.expect(MollieException.class);
		thrown.expectMessage("Unable to decode Mollie response: \"\"");

		doReturn("").when(api).performHttpCall(
			MollieClient.HTTP_POST, "payments", msgBody);

		try {
			api.payments().create(new BigDecimal(100),
				"Order #1337 24 Roundhousekicks",
				"http://www.chucknorris.rhk/return.php", null);
		} catch (MollieException e) {
			verify(api, times(1)).performHttpCall(MollieClient.HTTP_POST,
				"payments",
				msgBody);
			throw e;
		}
	}
}
@@ -1295,16 +1295,927 @@
 Body);%0A%0A
+%09%09try %7B%0A%09%09%09api.payments().create(new BigDecimal(100),%0A%09%09%09%09%22Order #1337 24 Roundhousekicks%22,%0A%09%09%09%09%22http://www.chucknorris.rhk/return.php%22, null);%0A%09%09%7D catch (MollieException e) %7B%0A%09%09%09verify(api, times(1)).performHttpCall(MollieClient.HTTP_POST,%0A%09%09%09%09%22payments%22,%0A%09%09%09%09msgBody);%0A%09%09%09throw e;%0A%09%09%7D%0A%09%7D%0A%0A%09@Test%0A%09public void testCreatePaymentFailsError() throws MollieException%0A%09%7B%0A%09%09String msgReturn = %22%7B %5C%22error%5C%22:%7B %5C%22type%5C%22:%5C%22request%5C%22, %5C%22message%5C%22:%5C%22Unauthorized request%5C%22, %5C%22links%5C%22:%7B %5C%22documentation%5C%22:%5C%22https://www.mollie.nl/api/docs/%5C%22 %7D %7D %7D%22;%0A%09%09String msgBody = %22%7B%5C%22amount%5C%22:100,%5C%22redirectUrl%5C%22:%5C%22http://www.chucknorris.rhk/return.php%5C%22,%5C%22description%5C%22:%5C%22Order #1337 24 Roundhousekicks%5C%22%7D%22;%0A%0A%09%09thrown.expect(MollieException.class);%0A%09%09thrown.expectMessage(%22Error executing API call (request): Unauthorized request.%22);%0A%0A%09%09doReturn(msgReturn).when(api).performHttpCall(%0A%09%09%09MollieClient.HTTP_POST, %22payments%22, msgBody);%0A%0A
 %09%09try %7B%0A

Add payment failure test