package marketing.heyday.hartmann.fotodocumentation.rest;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

/**
 * 
 * <p>Copyright: Copyright (c) 2024</p>
 * <p>Company: heyday Marketing GmbH</p>
 * @author <a href="mailto:p.verboom@heyday.marketing">Patrick Verboom</a>
 * @version 1.0
 * 
 * created: 14 Nov 2024
 */
@TestMethodOrder(OrderAnnotation.class)
public class CustomerResourceTest extends AbstractRestTest {
	private static final Log LOG = LogFactory.getLog(CustomerResourceTest.class);
	private static final String PATH = "api/customer";
	private static final String BASE_DOWNLOAD = "json/CustomerResourceTest-";

	@BeforeAll
	public static void init() {
		initDB();
	}

	@Test
	@Order(1)
	public void doGetAll() throws IOException {
		LOG.info("doGetAll");

		String authorization = getAuthorization();
		LOG.info("authorization: " + authorization);
		String path = deploymentURL + PATH;
		Request request = Request.Get(path).addHeader("Accept", "application/json; charset=utf-8")
				.addHeader("Authorization", authorization);

		HttpResponse httpResponse = executeRequest(request);
		int code = httpResponse.getStatusLine().getStatusCode();
		assertEquals(200, code);

		String text = getResponseText(httpResponse, "doGetAll");
		String expected = fileToString(BASE_DOWNLOAD + "doGetAll.json");
		jsonAssert(expected, text);
	}

	@Test
	@Order(1)
	public void doGetCustomer() throws IOException {
		LOG.info("doGetCustomer");

		String authorization = getAuthorization();
		LOG.info("authorization: " + authorization);
		String path = deploymentURL + PATH + "/1";
		Request request = Request.Get(path).addHeader("Accept", "application/json; charset=utf-8")
				.addHeader("Authorization", authorization);

		HttpResponse httpResponse = executeRequest(request);
		int code = httpResponse.getStatusLine().getStatusCode();
		assertEquals(200, code);

		String text = getResponseText(httpResponse, "doGetCustomer");
		String expected = fileToString(BASE_DOWNLOAD + "doGetCustomer.json");
		jsonAssert(expected, text);
	}
}
