package org.sagebionetworks.markdown;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import org.apache.http.client.HttpResponseException;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.test.util.ReflectionTestUtils;

public class MarkdownDaoImplTest {
	@Mock
	MarkdownClient mockMarkdownClient;

	private MarkdownDaoImpl dao;

	@Before
	public void before() {
		MockitoAnnotations.initMocks(this);
		dao = new MarkdownDaoImpl();
		ReflectionTestUtils.setField(dao, "markdownClient", mockMarkdownClient);
	}

	@Test (expected = IllegalArgumentException.class)
	public void testConvertMarkdownWithNullMarkdown() throws Exception {
		dao.convertMarkdown(null, null);
	}

	@Test (expected = HttpResponseException.class)
	public void testConvertMarkdownWithNullResponse() throws Exception {
		String rawMarkdown = "## a heading";
		String request = "{\"markdown\":\"## a heading\"}";
		when(mockMarkdownClient.requestMarkdownConversion(request)).thenThrow(new HttpResponseException(500,""));
		dao.convertMarkdown(rawMarkdown, null);
	}

	@Test
	public void testConvertMarkdown() throws Exception {
		String rawMarkdown = "## a heading";
		String outputType = "html";
		String request = "{\"markdown\":\"## a heading\",\"output\":\"html\"}";
		String result = "<h2 toc=\"true\">a heading</h2>\n";
		String response = "{\"result\":\"<h2 toc=\\\"true\\\">a heading</h2>\\n\"}";
		when(mockMarkdownClient.requestMarkdownConversion(request)).thenReturn(response);
		assertEquals(result, dao.convertMarkdown(rawMarkdown, outputType));
	}
}

@@ -122,61 +122,8 @@
 n;%0A%0A
-import org.apache.http.client.HttpResponseException;%0A
 impo
@@ -759,28 +759,30 @@
 ected = 
-HttpResponse
+MarkdownClient
 Exceptio
@@ -1033,20 +1033,22 @@
 new 
-HttpResponse
+MarkdownClient
 Exce

update test